将JTables与Netbeans 6.1 aka Matisse结合使用
在我们回答之前:是的,我已经在Sun阅读了jtable教程。不,它没有帮助我。是的,我很傻。请不要以对该文档的引用作为答案。我特别感兴趣的是如何通过Netbeans IDE向我的Jtable动态添加行和列。我已经有一个包含包含我的数据的哈希图的对象。我不知道该将对象传递到哪个对象或者对象。谢谢你的时间!
我有一个向量,其中包含一系列对象(长度为l)(每个对象对应一行)。如何使该矢量对象显示在JTable上?
解决方案
JTable使用TableModel来保存其数据。数据散列/向量将需要进行调整才能使用;我们可以使用散列/向量作为后备数据编写" TableModel"实现,或者,如果我们不会动态更新散列/向量并需要自动显示,则可以将所有内容复制到DefaultTableModel的实例中,并使用它。
如果确实使用适配器,并且动态更新哈希/矢量,请记住,所有更新必须在事件分发线程中完成。 :-)
为了增加我的上一个答案的价值,我实际上已经编写了一个表模型,该表模型使用(本质上)使用" ArrayList <Row>"作为后备数据,其中" Row"是" HashMap <String,Object>"。 ,将列名称映射到值。
整个过程大约有1500行代码,尽管出于目的我的代码可能有些过分,而且我们可能不必编写几乎相同数量的代码。一切顺利!
只是为了说明,下面是如何使用DefaultTableModel显示来自HashMap和Vector的数据的示例。
以下是将数据从" HashMap"转储到" DefaultTableModel"上的示例,该表用作" JTable"的" TableModel"。
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class JTableExample extends JFrame
{
private void makeGUI()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// HashMap with some data.
HashMap<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
// Create a DefaultTableModel, which will be used as the
// model for the JTable.
DefaultTableModel model = new DefaultTableModel();
// Populate the model with data from HashMap.
model.setColumnIdentifiers(new String[] {"key", "value"});
for (String key : map.keySet())
model.addRow(new Object[] {key, map.get(key)});
// Make a JTable, using the DefaultTableModel we just made
// as its model.
JTable table = new JTable(model);
this.getContentPane().add(table);
this.setSize(200,200);
this.setLocation(200,200);
this.validate();
this.setVisible(true);
}
public static void main(String[] args)
{
new JTableExample().makeGUI();
}
}
为了使用"向量"将数据列包含到" JTable"中:
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class JTableExample extends JFrame
{
private void makeGUI()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Vector with data.
Vector<String> v = new Vector<String>();
v.add("first");
v.add("second");
// Create a DefaultTableModel, which will be used as the
// model for the JTable.
DefaultTableModel model = new DefaultTableModel();
// Add a column of data from Vector into the model.
model.addColumn("data", v);
// Make a JTable, using the DefaultTableModel we just made
// as its model.
JTable table = new JTable(model);
this.getContentPane().add(table);
this.setSize(200,200);
this.setLocation(200,200);
this.validate();
this.setVisible(true);
}
public static void main(String[] args)
{
new JTableExample().makeGUI();
}
}
我必须承认在使用上述示例时,列名不会出现(我通常使用DefaultTableModel的setDataVector方法),因此,如果有人对如何显示列名有任何建议,请执行:)
只是对coobird职位的补充;为了显示标题,我这样做:
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class JTableExample extends JFrame
{
private void makeGUI()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// HashMap with some data.
HashMap<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
// Create a DefaultTableModel, which will be used as the
// model for the JTable.
DefaultTableModel model = new DefaultTableModel();
// Populate the model with data from HashMap.
model.setColumnIdentifiers(new String[] {"key", "value"});
for (String key : map.keySet())
model.addRow(new Object[] {key, map.get(key)});
// Make a JTable, using the DefaultTableModel we just made
// as its model.
JTable table = new JTable(model);
this.getContentPane().add(new JScrollPane(table));
this.setSize(200,200);
this.setLocation(200,200);
this.validate();
this.setVisible(true);
}
public static void main(String[] args)
{
new JTableExample().makeGUI();
}
}
顺便说一句,帖子对我酷鸟很有帮助,我们不知道我有多感恩!

