如何将自定义Jtable添加到GUI Builder netbeans表单
JPanel panel = new JPanel();
String data[][] = {{"Vinod","MCA","Computer"},
{"Deepak","PGDCA","History"},
{"Ranjan","M.SC.","Biology"},
{"Radha","BCA","Computer"}};
String col[] = {"Name","Course","Subject"};
DefaultTableModel model = new DefaultTableModel(data, col);
table = new JTable(model);
JTableHeader header = table.getTableHeader();
header.setBackground(Color.yellow);
JScrollPane pane = new JScrollPane(table);
Object obj1 = GetData(table, 2, 2);
System.out.println("Cell value of 3 column and 3 row :" + obj1);
Object obj2 = GetData(table, 2, 1);
System.out.println("Cell value of 2 column and 3 row :" + obj2);
panel.add(pane);
frame.add(panel);
frame.setSize(500,150);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public Object GetData(JTable table, int row_index, int col_index){
return table.getModel().getValueAt(row_index, col_index);
}
}
答案 0 :(得分:2)
好的,既然您已经提供了一些细节,那么这就是您的答案。 有多种方法可以做到这一点。
一:使用GUI
将以下代码放在文本框中:
new javax.swing.table.DefaultTableModel( new Object [][] {
{"Vinod","MCA","Computer"},
{"Deepak","PGDCA","History"},
{"Ranjan","M.SC.","Biology"},
{"Radha","BCA","Computer"}},
new String [] {"Name","Course","Subject"})
保存更改。 (现在请注意,jTable可能无法在您的设计器上正确显示,但这没关系)
二:使用代码片段
这将允许您在表格全部设置后编辑一些代码。将以下代码放入该编辑器中(假设您的表名为jTable1)
jTable1.setModel(new javax.swing.table.DefaultTableModel(new Object [][] {
{"Vinod","MCA","Computer"},
{"Deepak","PGDCA","History"},
{"Ranjan","M.SC.","Biology"},
{"Radha","BCA","Computer"}
},
new String [] {
"Name","Course","Subject"
}));
jTable1.getTableHeader().setBackground(java.awt.Color.yellow);
注意:这也涵盖了颜色变化,因此可以更好地控制。