1

有没有办法读取 JScrollPane 的内容?

我已经实现的是使用该方法添加DefaultTableModel的。我将其用作声明和实现 a 的参数。这稍后用作 a 的声明和实现的参数。在代码的整个执行过程中,我在using方法中添加了 nows。我手头的目标是阅读添加的行的内容。three columnsaddColumn()DefaultTableModelJTableJTableJScrollPaneDefaultTableModeladdRow()

有没有人有什么建议?一切将不胜感激!

JTabbedPane tabbedPane = new JTabbedPane();
DefaultTableModel model = new DefaultTableModel();

model.addColumn("Column1");
model.addColumn("Column2");
model.addColumn("Column3");

JTable testResults = new JTable(model) {
    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }
};

JScrollPane resultTab = new JScrollPane(testResults);
resultTab.setName("Tab1");
resultTab.setVisible(true);

Thread thread1 = new Thread() {
    public void run() {
        if(tabbedPane.getSelectedComponent().getName().compareTo("Tab1") == 0) {
            model.addRow(new Object[]{"content1", "content2", "content3"});
            model.addRow(new Object[]{"content4", "content5", "content6"});
            ...
            ...
            ...
            model.addRow(new Object[]{"this", "will", "continue"});
        }
        else {
            model.addRow(new Object[]{"content1", "content2", "content3"});
            model.addRow(new Object[]{"content4", "content5", "content6"});
        }
    };
thread1.start();

I want to read the data "content1", "content2", "content3", "content4", "content5", "content6", etc. The ellipses represent that there is a undefined number of rows being added throughout the source code.

4

3 回答 3

3

You want to read the data from DefaultTableModel and not the JScrollPane - it's just a Swing container.

getDataVector() will help you here. It returns vector of vectors representing data in your table model.

于 2012-07-20T15:13:54.670 回答
1
table.getModel().getValueAt(0, 0);

This will return "contents1" from the table model.

table.getModel().getValueAt(0, 1);

This will return contents2.

Check out TabelModel for more details.

于 2012-07-20T21:27:58.250 回答
0

Robin has suggested a good point. You should add rows on the EDT thread. If you have a long running process that is fetching rows, then I suggest using a SwingWorker to grab those rows and add them to your TableModel. You should use the concrete implementation (DefaultTableModel in your case) to do so.

于 2012-07-20T16:05:53.503 回答