当前位置: 首页 > 知识库问答 >
问题:

JavaFX2-ObservableList到TableView

成和悌
2023-03-14

希望我能把问题弄得尽可能清楚。我正在使用JavaFX库为GUI开发一个小型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")
        );

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

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

有人知道解决方案吗?我如何获得上面提到的列中显示的InternetAdress的字符串值?

提前致谢

共有1个答案

左丘昊天
2023-03-14

我认为您需要定义一个自定义单元格值工厂,以获得所需格式的地址信息,而不是使用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;
  }
}
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
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
 类似资料:
  • 我有一个TableView(javafx.scene.control.TableView),我用数据填充了它。该数据作为ArrayList从数据库中检索,因此我使用了folowing流: 我用预算数据填充ArrayList 我更新数组中的值 初始数据显示正确。我原本期望通过更改ArrayList(最后一步),ObservableList会看到更改并将其传递给我的TableView。为了使数组中的更

  • 我注意到在JavaFX1.x中,脚本语言允许非常简单的字符串国际化。JavaFX2中有类似的特性吗? 基本上:国际化JavaFX2应用程序的最佳实践是什么?

  • 我的问题涉及到JavaFX(JavaFX 8,虽然我猜它在2. x中是类似的)中观察列表和列表更改列表的正确用法。因为我是一个JavaFX初学者,我想知道我是否正确理解了它们应该如何使用。 假设我有一个自定义对象列表(我们称之为Slot),我想在GridPane中呈现这些对象:每个Slot都知道它应该在GridPane“grid”中的哪个位置(=行和列数据)。 将存在插槽的初始(数组)列表,但由于

  • 我试图找到一种简单的方法来链接下载类型的树视图到相同类型的可观察列表。 主控制器。JAVA Download.java 如何实现按对象删除(下载)机制,是否有更简单的方法将observablelist的项绑定到treeview?

  • 我想将所有囚犯数据显示到一个TableView中。囚犯看起来像这样(所有代码都是一个示例): 然后我有了我的TableView:数据是一个可观察列表 这工作正常,但我的问题是,如何在TableView中添加大小写?我试过了 但它不起作用。当然,我没有忘记在最后将所有列添加到TableView。

  • 我正在使用JavaFX2.2。5使用Media和MediaPlayer类在我的应用程序中播放本地音频文件。我找到了JavaFX1.3支持的文件类型的文档,但找不到版本2或更高版本的文档。是否有人知道版本2的此类文档,或确认此信息也适用于版本2?