如何向JTable中插入数据?
我编写此代码用于在表中显示字符串。
但它没有显示并且没有任何效果。
什么是问题?
public pamnel() {
initComponents();
String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
Object[][] data = {
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)}
};
jTable1 = new JTable(data, columnNames);
}
编辑: 我在面板上添加 Jtable。
在主面板中将其添加到 jframe 中。
JFrame frame = new JFrame();
frame.add(new pamnel());
frame.setVisible(true);
frame.setSize(600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
显示表格但未显示数据。 表格的行和列都是空的!
i write this code for showing strings in a table.
but it doesnt shown and has no effect.
what is problrem?
public pamnel() {
initComponents();
String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
Object[][] data = {
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)}
};
jTable1 = new JTable(data, columnNames);
}
Edit:
I add Jtable on a panel.
in the main add panel to a jframe.
JFrame frame = new JFrame();
frame.add(new pamnel());
frame.setVisible(true);
frame.setSize(600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
The table is shown but data isn't show.
the row and column of table is empty!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须将 JTable 添加到其父组件并 setVisible()。
You have to add the JTable to its parent-component and setVisible().
尝试这样的事情,只是为了让它运行。请注意,您删除了 pammel 方法中的
void
,并添加了 main。Try something like this, just to make it run. Notice you drop the
void
in the pammel method and a main was added.代码真的是你自己写的吗?我记得这是在 Oracle Swing 教程中看到的。不管怎样,您还没有将
JTable
添加到组件中。观察下面的源JTable添加到
SimpleTableDemo
构造函数中的 JPanel。然后将 JPanel 设置为主 JFrame 中的内容窗格,设置为frame.setVisible(true)。这发生在createAndShowGUI
方法中。您的代码不显示 JTable 的原因是 JTable 是一个抽象小部件。您需要将抽象小部件添加到组件中,例如 JFrame(在上述情况下)才能显示它。Did you really write the code yourself? I remember this from an oracle swing tutorial. Anyway, you've not added the
JTable
to a component. Observe below sourceThe JTable is added to the JPanel in the
SimpleTableDemo
constructor. Then the JPanel is set as the content pane in the for the main JFrame, which is made as frame.setVisible(true). This happens in thecreateAndShowGUI
method. The reason your code doesn't display the JTable is because a JTable is an abstract widget. You need to add the abstract widget to a component, such as a JFrame (in the above case) for it to be displayed.看起来新的 JTable 没有添加到
pamnel
中。可能是在
initComponents()
中添加了另一个未使用数据初始化的实例。add
方法不会向组件添加对变量的引用,它只是 添加变量的值。因此,如果变量发生更改,组件仍包含以前的(旧)值。更改create-add的顺序:
It seams like the new JTable is not being added to
pamnel
.Probably another instance, not initialized with data, is being added in
initComponents()
The
add
method does not add a reference to the variable to the component, it only adds the value of the variable. So, if the variable is changed, the component still contains the previous (old) value.Change the order of create-add: