你应该参考一下JTable的实例
你这里的问题是动态初始化吗?
这个问题不是很大的 你可以用TablbModel初始化
TableModel负责对数据的控制 而JTable属于View
下面这个例子是从你给的数据生成一个界面的
package components;
/*
* SimpleTableDemo.java requires no other files.
*/
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.File;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class SimpleTableDemo extends JPanel {
private final String RESOUCE_PATH = "resource\\";
public SimpleTableDemo() {
super(new GridLayout(1, 0));
Vector columnNamesVec = new Vector();
columnNamesVec.add("Utility Name");
columnNamesVec.add("Amount");
columnNamesVec.add("Price");
Vector rowsData = new Vector();
for (int i = 0; i < 10; i++) {
rowsData.add(new Vector());
for (int j = i; j < i + 3; j++) {
((Vector) rowsData.get(i)).add(String.valueOf(j));
}
}
String[] columnNames = { "Utility Name", "Amount", "Price" };
Object[][] data = {};
// final JTable table = new JTable(data, columnNames);
final JTable table = new JTable(rowsData, columnNamesVec);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
// table.setFillsViewportHeight(true);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
}
/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
SimpleTableDemo newContentPane = new SimpleTableDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public void talbeInit(File file) {
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
参考资料:http://java.sun.com/docs/books/tutorial/uiswing/components/table.html