无法显示 JTable
我在显示 JTable 面板时遇到了一个小问题(我猜)。 我的类包含对象数组:
public class Item
{
String itemDesc = "";
float price = 0;
private itemType enmItemType;
Object[][] data = {{itemDesc, enmItemType , new Float(price)}};
.
.
.
.
}
这里是包含 JTable 的 Table 类:
class Table extends JFrame
{
// Instance attributes used in this example
private JPanel topPanel;
private JTable table;
private JScrollPane scrollPane;
private JButton update_Button;
// Constructor of main frame
public Table()
{
// Set the frame characteristics
setTitle("Add new item" );
setSize(300, 200);
setBackground( Color.gray );
// Create a panel to hold all other components
topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
// Create columns names
String columnNames[] = {"Item Description", "Item Type", "Item Price"};
// Create some data
Object dataValues[][] ;
Item itm = new Item();
dataValues = itm.data;
// Create a new table instance
table = new JTable( dataValues, columnNames );
////////////////////////////
JComboBox itemTypeCombobox = new JComboBox();
TableColumn column1 = table.getColumnModel().getColumn(1);
column1.setCellEditor(new DefaultCellEditor(itemTypeCombobox));
////////////////////////////
// Add the table to a scrolling pane
scrollPane = new JScrollPane( table );
topPanel.add( scrollPane, BorderLayout.CENTER );
JButton button = new JButton("Add Item");
topPanel.add( button, BorderLayout.SOUTH );
}
}
主程序是:
public static void main(String[] args)
{
Menu m = new Menu();
m.chooseMenu();
// Create an instance of the test application
Table mainFrame = new Table();
mainFrame.setVisible( true );
}
我没有收到任何错误/警告,但我仍然没有看到任何表。 有人可以告诉我是什么原因导致了这个问题吗?
谢谢。
I'm having small problem (I guess) of showing the JTable panel.
I have class contains Object array with:
public class Item
{
String itemDesc = "";
float price = 0;
private itemType enmItemType;
Object[][] data = {{itemDesc, enmItemType , new Float(price)}};
.
.
.
.
}
here is the Table class contains the JTable:
class Table extends JFrame
{
// Instance attributes used in this example
private JPanel topPanel;
private JTable table;
private JScrollPane scrollPane;
private JButton update_Button;
// Constructor of main frame
public Table()
{
// Set the frame characteristics
setTitle("Add new item" );
setSize(300, 200);
setBackground( Color.gray );
// Create a panel to hold all other components
topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
// Create columns names
String columnNames[] = {"Item Description", "Item Type", "Item Price"};
// Create some data
Object dataValues[][] ;
Item itm = new Item();
dataValues = itm.data;
// Create a new table instance
table = new JTable( dataValues, columnNames );
////////////////////////////
JComboBox itemTypeCombobox = new JComboBox();
TableColumn column1 = table.getColumnModel().getColumn(1);
column1.setCellEditor(new DefaultCellEditor(itemTypeCombobox));
////////////////////////////
// Add the table to a scrolling pane
scrollPane = new JScrollPane( table );
topPanel.add( scrollPane, BorderLayout.CENTER );
JButton button = new JButton("Add Item");
topPanel.add( button, BorderLayout.SOUTH );
}
}
The main program is:
public static void main(String[] args)
{
Menu m = new Menu();
m.chooseMenu();
// Create an instance of the test application
Table mainFrame = new Table();
mainFrame.setVisible( true );
}
I did not receive any error/warning, but still, I don't see any table.
Can someone direct me what causing the problem?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道出了什么问题。但我稍微改变了你的代码(因为它有编译时错误)
它对我来说工作得很好。以下是屏幕截图
你的主表类
I cant tell what has gone wrong. But I changed your code a bit (since it had compile time errors)
It works fine for me. Following is the screenshot
Your main table class
要添加新行数据,您需要使用 DefaultTableModel 的 addRow(...) 方法。
所有更新都应该针对模型进行,而不是针对用于创建模型的数组进行。
To add a new row of data you need to use the addRow(...) method of the DefaultTableModel.
All updates should be done to the model, not the Array used to create the model.