如何将自定义 JTable 代码添加到 GUI Builder Nebeans?
如何将自定义 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);
}
}
how can add custom Jtable to GUI Builder netbeans form
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,现在您已经提供了一些详细信息,这就是您的答案。
有多种方法可以做到这一点。
一:使用 GUI
将以下代码放入文本框中:
保存更改。
(现在请注意,jTable 可能无法在设计器上正确显示,但这没关系)
二:使用代码片段
这将允许您在表格全部设置后编辑一些代码。将以下代码放入该编辑器中(假设您的表名称为 jTable1)
注意:这也涵盖了颜色更改,因此此处可以更好地控制。
Ok, Now that you have provided some details, Here is the answer for you.
There are multiple ways to do this.
one: using GUI
Place the following code to the text box:
Save your changes.
(Now notice that the jTable may not display properly on your designer, but that is OK)
two: using code fragments
This will allow you to edit some code after table is all set. Place the following code in to that editor (assuming your table's name is jTable1)
note: this covers the color change as well, so better control here.