2012-11-01 101 views
1

希望我会尽可能清楚我的问题。我正在使用gui的JavaFX库开发一个小型的Java应用程序。我正在做一个POP连接并将消息存储为ObservableList。为此,我使用javax.mail。我通过这个observablelist到的tableview与下面我传递所需的值到TableColumn来:JavaFX 2 - ObservableList <Message> to TableView

 fromColumn.setCellValueFactory(
      new PropertyValueFactory<Message,String>("from") 
     ); 
     subjectColumn.setCellValueFactory(
      new PropertyValueFactory<Message,String>("subject") 
     ); 
     dateColumn.setCellValueFactory(
      new PropertyValueFactory<Message,String>("sentDate") 
     ); 

主题和sentDate是beeing读入完美。但不幸的是,“from”是将对象引用添加到TableColumn,因为Message-Class中的From-Attribute是InternetAdress-Object,它的toString() - 方法不返回字符串,但可能是引用。其结果是被显示在fromColumn的follwoing:

[Ljavax.mail.internet.InternetAddress;@3596cd38 

任何人知道的解决方案,我怎么能得到InternetAdress的字串值在一栏中被显示?

由于提前

+1

低于你的答案是不应答。因此,通过编辑您的问题,将这些代码示例附加到您的问题中。你可以在“迄今为止我所尝试过的”主题下做到这一点。并且我建议调试你的代码或者在'返回新的ReadOnlyObjectWrapper (Arrays.toString(m.getValue()。getFrom())'之前编写'System.out.println(m.getValue()。getFrom()); );'。阅读InternetAddress的API并尝试使用'toUnicodeString()'或'getAddress()'方法。 –

+0

同样尝试'返回新的ReadOnlyObjectWrapper (m.getValue()。getFrom()。toUnicodeString());'。 –

回答

1

我认为你需要定义一个自定义单元格值工厂获得在格式的地址信息,您需要,而不是使用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())); 
    } 
}); 

以下是可执行样品(加样的数据文件),其展示使用自定义的单元格值的工厂。将示例数据文件放在与应用程序java程序相同的目录中,并确保构建系统将示例文件复制到构建输出目录,该目录包含应用程序的已编译类文件。您需要路径上的javamail 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; 
    } 
} 

msg_1.txt

From [email protected] 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: <[email protected]> 
Delivered-To: [email protected] 
Received: from shodan.irccrew.org (shodan.irccrew.org [80.83.4.2]) 
    by danu.procontrol.fi (Postfix) with ESMTP id 434B423848 
    for <[email protected]>; 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 <[email protected]> 
To: [email protected] 
Subject: [dovecot] first test mail 
Message-ID: <[email protected]> 
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: doveco[email protected] 
Errors-to: [email protected] 
X-original-sender: [email protected] 
Precedence: bulk 
X-list: dovecot 
X-IMAPbase: 1096038620 0000010517 
X-UID: 1             
Status: O 

lets see if it works 

msg_2.txt

From [email protected] 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: <[email protected]> 
Delivered-To: [email protected] 
Received: from shodan.irccrew.org (shodan.irccrew.org [80.83.4.2]) 
    by danu.procontrol.fi (Postfix) with ESMTP id 8D21723848 
    for <[email protected]>; 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 <[email protected]> 
To: [email protected] 
Subject: [dovecot] Dovecot 0.93 released 
Message-ID: <[email protected]> 
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: [email protected] 
Errors-to: [email protected] 
X-original-sender: [email protected] 
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. 

msg_3.txt

From [email protected] 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: <[email protected]> 
Delivered-To: [email protected] 
Received: from shodan.irccrew.org (shodan.irccrew.org [80.83.4.2]) 
    by danu.procontrol.fi (Postfix) with ESMTP id F141123829 
    for <[email protected]>; 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 <[email protected]> 
To: [email protected] 
Subject: [dovecot] v0.95 released 
Message-ID: <[email protected]> 
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: [email protected] 
Errors-to: [email protected] 
X-original-sender: [email protected] 
Precedence: bulk 
X-list: dovecot 
X-UID: 3             
Status: O 

v0.95 2002-07-31 Timo Sirainen <[email protected]> 

    + 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 

样本程序输出: Sample TableView with Mail Message Info

+0

不幸的是,这不工作,我不断得到一个空白的桌面。 – Ilir

+0

我添加了一个可执行示例程序,演示该解决方案的工作原理。 – jewelsea

+0

谢谢大家的帮助,我将不得不检查差异并进行修改。 – Ilir

0

使用上述方案中,阉我用它,因为它是和我接受了Eclipse的建议我加一个try/catch和进一步的返回值,它看起来像这样结尾:

 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; 
     } 
    }); 

结果是一样的,我得到一个(视觉)空的tableview。这意味着即使是使用setCellFactury()解决方案的表列也是空的。那么,作为解决方法,我可以定义一个类,我可以将三个值存储为一个字符串,然后使用PropertyValueFactory将其传递给setCellValueFactory(),但我希望能够正确完成它。

其他建议?

问候

0

这是一个Java版本8 jewelsea的解决方案,但在真棒lambda表达式:

fromColumn.setCellValueFactory(m -> { 
     // m.getValue() returns the Message instance for a particular TableView row 
     return new ReadOnlyObjectWrapper<String>(Arrays.toString(m.getValue().getFrom())); 
    } 
}); 

大声笑,得爱lambda表达式