Tableview JavaFX2-可观察列表<;信息>;到桌面视图

Tableview JavaFX2-可观察列表<;信息>;到桌面视图,tableview,javafx,Tableview,Javafx,希望我能把问题弄清楚。我正在使用用于gui的JavaFX库开发一个小型java应用程序。我正在进行POP连接,并将消息存储为ObservableList。为此,我使用javax.mail。我将此observablelist传递给tableview,并通过以下操作将所需的值传递给TableColumns: fromColumn.setCellValueFactory( new PropertyValueFactory<Message,String>

希望我能把问题弄清楚。我正在使用用于gui的JavaFX库开发一个小型java应用程序。我正在进行POP连接,并将消息存储为ObservableList。为此,我使用javax.mail。我将此observablelist传递给tableview,并通过以下操作将所需的值传递给TableColumns:

        fromColumn.setCellValueFactory(
            new PropertyValueFactory<Message,String>("from")
        );
        subjectColumn.setCellValueFactory(
            new PropertyValueFactory<Message,String>("subject")
        );
        dateColumn.setCellValueFactory(
            new PropertyValueFactory<Message,String>("sentDate")
        );
有人知道我如何获得在上述列中显示的InternetAddress的字符串值的解决方案吗


提前感谢

我认为您需要定义一个自定义单元格值工厂,以获得所需格式的地址信息,而不是使用PropertyValueFactory

以下示例适用于只读表-如果表中的消息数据需要可编辑,则解决方案将非常复杂

fromColumn.setCellValueFactory(new Callback<CellDataFeatures<Message, String>, ObservableValue<String>>() {
    @Override public ObservableValue<String> call(CellDataFeatures<Message, String> m) {
        // m.getValue() returns the Message instance for a particular TableView row
        return new ReadOnlyObjectWrapper<String>(Arrays.toString(m.getValue().getFrom()));
    }
});
fromColumn.setCellValueFactory(新回调(){
@覆盖公共observeValue调用(celldatam){
//m.getValue()返回特定TableView行的消息实例
返回新的ReadOnlyObjectWrapper(Arrays.toString(m.getValue().getFrom());
}
});
下面是一个可执行示例(加上示例数据文件),演示了自定义单元格值工厂的使用。将示例数据文件放在与应用程序java程序相同的目录中,并确保生成系统将示例文件复制到包含应用程序编译类文件的生成输出目录。您将需要路径上的jar文件来编译和运行应用程序

import java.io.*;
import java.util.Arrays;
import java.util.logging.*;
import javafx.application.Application;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.value.ObservableValue;
import javafx.collections.*;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Callback;
import javax.mail.*;
import javax.mail.internet.MimeMessage;

public class MailTableSample extends Application {
  private TableView<Message> table = new TableView<Message>();
  public static void main(String[] args) { launch(args);}

  @Override public void start(Stage stage) {
    stage.setTitle("Table View Sample");

    final Label label = new Label("Mail");
    label.setFont(new Font("Arial", 20));

    table.setEditable(false);

    TableColumn subjectColumn = new TableColumn("Subject");
    subjectColumn.setMinWidth(100);
    subjectColumn.setCellValueFactory(
      new PropertyValueFactory<Message, String>("subject")
    );

    TableColumn sentDate = new TableColumn("Sent");
    sentDate.setMinWidth(100);
    sentDate.setCellValueFactory(
      new PropertyValueFactory<Message, String>("sentDate")
    );

    TableColumn fromColumn = new TableColumn("From");
    fromColumn.setMinWidth(200);
    fromColumn.setCellValueFactory(new Callback<CellDataFeatures<Message, String>, ObservableValue<String>>() {
        @Override public ObservableValue<String> call(CellDataFeatures<Message, String> m) {
          try {
            // m.getValue() returns the Message instance for a particular TableView row
            return new ReadOnlyObjectWrapper<String>(Arrays.toString(m.getValue().getFrom()));
          } catch (MessagingException ex) {
            Logger.getLogger(MailTableSample.class.getName()).log(Level.SEVERE, null, ex);
            return null;
          }
        }
    });    

    table.setItems(fetchMessages());
    table.getColumns().addAll(fromColumn, subjectColumn, sentDate);
    table.setPrefSize(600, 200);

    final VBox vbox = new VBox();
    vbox.setSpacing(5);
    vbox.setPadding(new Insets(10));
    vbox.getChildren().addAll(label, table);

    stage.setScene(new Scene(vbox));
    stage.show();
  }

  private ObservableList<Message> fetchMessages() {
    ObservableList<Message> messages = FXCollections.observableArrayList();
    try {
      Session session = Session.getDefaultInstance(System.getProperties());
      for (int i = 0; i < 3; i++) {
        InputStream mboxStream = new BufferedInputStream(
          getClass().getResourceAsStream("msg_" + (i+1) + ".txt")
        );
        Message message = new MimeMessage(session, mboxStream);
        messages.add(message);
      }
    } catch (MessagingException ex) {
      Logger.getLogger(MailTableSample.class.getName()).log(Level.SEVERE, null, ex);
    }

    return messages;
  }
}
import java.io.*;
导入java.util.array;
导入java.util.logging.*;
导入javafx.application.application;
导入javafx.beans.property.ReadOnlyObjectWrapper;
导入javafx.beans.value.observeValue;
导入javafx.collections.*;
导入javafx.geometry.Insets;
导入javafx.scene.scene;
导入javafx.scene.control.*;
导入javafx.scene.control.TableColumn.CellDataFeatures;
导入javafx.scene.control.cell.PropertyValueFactory;
导入javafx.scene.layout.VBox;
导入javafx.scene.text.Font;
导入javafx.stage.stage;
导入javafx.util.Callback;
导入javax.mail.*;
导入javax.mail.internet.mimessage;
公共类MailTableSample扩展应用程序{
private TableView table=new TableView();
公共静态void main(字符串[]args){launch(args);}
@覆盖公共无效开始(阶段){
stage.setTitle(“表格视图示例”);
最终标签=新标签(“邮件”);
label.setFont(新字体(“Arial”,20));
table.setEditable(假);
TableColumn subjectColumn=新的TableColumn(“主题”);
subjectColumn.setMinWidth(100);
subjectColumn.setCellValueFactory(
新财产价值工厂(“主体”)
);
TableColumn sentDate=新的TableColumn(“已发送”);
sentDate.setMinWidth(100);
sentDate.setCellValueFactory(
新PropertyValue工厂(“sentDate”)
);
TableColumn fromColumn=新的TableColumn(“From”);
fromColumn.setMinWidth(200);
fromColumn.setCellValueFactory(新回调(){
@覆盖公共observeValue调用(celldatam){
试一试{
//m.getValue()返回特定TableView行的消息实例
返回新的ReadOnlyObjectWrapper(Arrays.toString(m.getValue().getFrom());
}捕获(消息例外){
Logger.getLogger(MailTableSample.class.getName()).log(Level.SEVERE,null,ex);
返回null;
}
}
});    
table.setItems(fetchMessages());
table.getColumns().addAll(fromColumn、subjectColumn、sentDate);
表1.setPrefSize(600200);
最终VBox VBox=新的VBox();
vbox.setspace(5);
vbox.setPadding(新插图(10));
vbox.getChildren().addAll(标签、表格);
舞台场景(新场景(vbox));
stage.show();
}
私有ObservableList fetchMessages(){
ObservableList messages=FXCollections.observableArrayList();
试一试{
Session Session=Session.getDefaultInstance(System.getProperties());
对于(int i=0;i<3;i++){
InputStream mboxStream=新的BufferedInputStream(
getClass().getResourceAsStream(“msg_216;”+(i+1)+“.txt”)
);
Message Message=new mimessage(会话,mboxStream);
消息。添加(消息);
}
}捕获(消息例外){
Logger.getLogger(MailTableSample.class.getName()).log(Level.SEVERE,null,ex);
}
返回消息;
}
}
msg_1.txt

From cras@irccrew.org  Tue Jul 23 19:39:23 2002
Received: with ECARTIS (v1.0.0; list dovecot); Tue, 23 Jul 2002 19:39:23 +0300 (EEST)
Return-Path: <cras@irccrew.org>
Delivered-To: dovecot@procontrol.fi
Received: from shodan.irccrew.org (shodan.irccrew.org [80.83.4.2])
    by danu.procontrol.fi (Postfix) with ESMTP id 434B423848
    for <dovecot@procontrol.fi>; Tue, 23 Jul 2002 19:39:23 +0300 (EEST)
Received: by shodan.irccrew.org (Postfix, from userid 6976)
    id 175FA4C0A0; Tue, 23 Jul 2002 19:39:23 +0300 (EEST)
Date: Tue, 23 Jul 2002 19:39:23 +0300
From: Timo Sirainen <tss@iki.fi>
To: dovecot@procontrol.fi
Subject: [dovecot] first test mail
Message-ID: <20020723193923.J22431@irccrew.org>
Mime-Version: 1.0
Content-Disposition: inline
User-Agent: Mutt/1.2.5i
Content-Type: text/plain; charset=us-ascii
X-archive-position: 1
X-ecartis-version: Ecartis v1.0.0
Sender: dovecot-bounce@procontrol.fi
Errors-to: dovecot-bounce@procontrol.fi
X-original-sender: tss@iki.fi
Precedence: bulk
X-list: dovecot
X-IMAPbase: 1096038620 0000010517
X-UID: 1                                                  
Status: O

lets see if it works
来自cras@irccrew.org2002年7月23日星期二19:39:23
接收:使用ECARTIS(v1.0.0;列表dovecot);2002年7月23日星期二19:39:23+0300(欧洲标准时间)
返回路径:
交付给:dovecot@procontrol.fi
收到:来自shodan.ircrew.org(shodan.ircrew.org[80.83.4.2])
由具有ESMTP id 434B423848的danu.procontrol.fi(后缀)编写
对于2002年7月23日星期二19:39:23+0300(欧洲标准时间)
接收:由shodan.irccew.org(后缀,来自用户ID 6976)
id 175FA4C0A0;2002年7月23日星期二19:39:23+0300(欧洲标准时间)
日期:2002年7月23日星期二19:39:23+0300
发件人:Timo Sirainen
致:dovecot@procontrol.fi
主题:[dovecot]第一封测试邮件
消息ID:
Mime版本:1.0
内容配置:内联
用户代理:Mutt/1.2.5i
内容类型:文本/纯文本;字符集=美国ascii码
X位置:1
X-ecartis-version:ecartis v1.0.0
发件人:多佛科特-bounce@procontrol.fi
错误发送至:dovecot-bounce@procontrol.fi
X原始发件人:tss@iki.fi
优先:批量
X名单:多佛科特
X-IMAPbase:1096038620 0000010517
X-UID:1
状态:O
让我们看看它是否有效
msg_2.txt

From cras@irccrew.org  Mon Jul 29 02:17:12 2002
Received: with ECARTIS (v1.0.0; list dovecot); Mon, 29 Jul 2002 02:17:12 +0300 (EEST)
Return-Path: <cras@irccrew.org>
Delivered-To: dovecot@procontrol.fi
Received: from shodan.irccrew.org (shodan.irccrew.org [80.83.4.2])
    by danu.procontrol.fi (Postfix) with ESMTP id 8D21723848
    for <dovecot@procontrol.fi>; Mon, 29 Jul 2002 02:17:12 +0300 (EEST)
Received: by shodan.irccrew.org (Postfix, from userid 6976)
    id 8BAD24C0A0; Mon, 29 Jul 2002 02:17:11 +0300 (EEST)
Date: Mon, 29 Jul 2002 02:17:11 +0300
From: John Smith <jsmithspam@yahoo.com>
To: dovecot@procontrol.fi
Subject: [dovecot] Dovecot 0.93 released
Message-ID: <20020729021711.W22431@irccrew.org>
Mime-Version: 1.0
Content-Disposition: inline
User-Agent: Mutt/1.2.5i
Content-Type: text/plain; charset=us-ascii
X-archive-position: 2
X-ecartis-version: Ecartis v1.0.0
Sender: dovecot-bounce@procontrol.fi
Errors-to: dovecot-bounce@procontrol.fi
X-original-sender: tss@iki.fi
Precedence: bulk
X-list: dovecot
X-UID: 2                                                  
Status: O

First alpha quality release, everything critical is now implemented. From
now on it's mostly stabilization and optimization. Features that can't break
existing code could still be added, especially SSL and authentication stuff.
来自cras@irccrew.org2002年7月29日星期一02:17:12
接收:使用ECARTIS(v1.0.0;列表dovecot);2002年7月29日星期一02:17:12+0300(欧洲标准时间)
返回路径:
交付给:dovecot@procontrol.fi
收到:来自shodan.ircrew.org(shodan.ircrew.org[80.83.4.2])
由具有ESMTP id 8D21723848的danu.procontrol.fi(后缀)编写
对于2002年7月29日星期一02:17:12+0300(欧洲标准时间)
收到:shodan.ircc
From cras@irccrew.org  Mon Jul 29 02:17:12 2002
Received: with ECARTIS (v1.0.0; list dovecot); Mon, 29 Jul 2002 02:17:12 +0300 (EEST)
Return-Path: <cras@irccrew.org>
Delivered-To: dovecot@procontrol.fi
Received: from shodan.irccrew.org (shodan.irccrew.org [80.83.4.2])
    by danu.procontrol.fi (Postfix) with ESMTP id 8D21723848
    for <dovecot@procontrol.fi>; Mon, 29 Jul 2002 02:17:12 +0300 (EEST)
Received: by shodan.irccrew.org (Postfix, from userid 6976)
    id 8BAD24C0A0; Mon, 29 Jul 2002 02:17:11 +0300 (EEST)
Date: Mon, 29 Jul 2002 02:17:11 +0300
From: John Smith <jsmithspam@yahoo.com>
To: dovecot@procontrol.fi
Subject: [dovecot] Dovecot 0.93 released
Message-ID: <20020729021711.W22431@irccrew.org>
Mime-Version: 1.0
Content-Disposition: inline
User-Agent: Mutt/1.2.5i
Content-Type: text/plain; charset=us-ascii
X-archive-position: 2
X-ecartis-version: Ecartis v1.0.0
Sender: dovecot-bounce@procontrol.fi
Errors-to: dovecot-bounce@procontrol.fi
X-original-sender: tss@iki.fi
Precedence: bulk
X-list: dovecot
X-UID: 2                                                  
Status: O

First alpha quality release, everything critical is now implemented. From
now on it's mostly stabilization and optimization. Features that can't break
existing code could still be added, especially SSL and authentication stuff.
From cras@irccrew.org  Wed Jul 31 22:48:41 2002
Received: with ECARTIS (v1.0.0; list dovecot); Wed, 31 Jul 2002 22:48:41 +0300 (EEST)
Return-Path: <cras@irccrew.org>
Delivered-To: dovecot@procontrol.fi
Received: from shodan.irccrew.org (shodan.irccrew.org [80.83.4.2])
    by danu.procontrol.fi (Postfix) with ESMTP id F141123829
    for <dovecot@procontrol.fi>; Wed, 31 Jul 2002 22:48:40 +0300 (EEST)
Received: by shodan.irccrew.org (Postfix, from userid 6976)
    id 42ED44C0A0; Wed, 31 Jul 2002 22:48:40 +0300 (EEST)
Date: Wed, 31 Jul 2002 22:48:39 +0300
From: Timo Sirainen <tss@iki.fi>
To: dovecot@procontrol.fi
Subject: [dovecot] v0.95 released
Message-ID: <20020731224839.H22431@irccrew.org>
Mime-Version: 1.0
Content-Disposition: inline
User-Agent: Mutt/1.2.5i
Content-Type: text/plain; charset=us-ascii
X-archive-position: 3
X-ecartis-version: Ecartis v1.0.0
Sender: dovecot-bounce@procontrol.fi
Errors-to: dovecot-bounce@procontrol.fi
X-original-sender: tss@iki.fi
Precedence: bulk
X-list: dovecot
X-UID: 3                                                  
Status: O

v0.95 2002-07-31  Timo Sirainen <tss@iki.fi>

    + Initial SSL support using GNU TLS, tested with v0.5.1.
      TLS support is still missing.
    + Digest-MD5 authentication method
    + passwd-file authentication backend
    + Code cleanups
    - Found several bugs from mempool and ioloop code, now we should
      be stable? :)
    - A few corrections for long header field handling
        fromColumn.setCellValueFactory(new Callback<CellDataFeatures<Message, String>, ObservableValue<String>>() {
        public ObservableValue<String> call(CellDataFeatures<Message, String> m) {
            // m.getValue() returns the Message instance for a particular TableView row
            try {
                return new ReadOnlyObjectWrapper<String>(Arrays.toString(m.getValue().getFrom()));
            } catch (MessagingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
    });
fromColumn.setCellValueFactory(m -> {
        // m.getValue() returns the Message instance for a particular TableView row
        return new ReadOnlyObjectWrapper<String>(Arrays.toString(m.getValue().getFrom()));
    }
});