本文介绍了单击按钮时,Java将数据传递到JTable的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在做一个Java应用程序,该应用程序必须传递一个初始化的字符串数组以显示为JTable的行.
I'm doing a java app which has to pass an initialized array of string to display as a row to a JTable.
这是我尝试过的:
Object[] _row1;
public PrintMeNow() {
table = new JTable();
table.setBounds(16, 203, 362, 16);
table.setShowHorizontalLines(true);
table.setShowVerticalLines(true);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setModel(new DefaultTableModel(
new Object[][] {
_row1, {2,2,2}, {3,3,3}, {4,4,4}
},
new String[] {
"QTY", "Item Code", "Amount"
}
));
}
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
_row1 = new Object[]{"DN Korina", "DN Madrid", "DN Romania"};
}
});
但是当我按下开始按钮时,它没有填充行.我在这里做错了什么?非常感谢您的帮助.谢谢.
But when I pressed the start button, it's not filling the row. What am I doing wrong in here? Help is much appreciated. Thanks.
推荐答案
您需要做的是在Model
中添加一行.像这样
What you need to do is add a row to the Model
. Something like this
public void actionPerformed(ActionEvent arg0) {
DefaultTableModel model = (DefaultTableModel)table.getModel();
model.addRow(new Object[]{"DN Korina", "DN Madrid", "DN Romania"});
}
最简单的方法是在手之前设置模型以使其没有行
The easiest way is to set up you model before hand to have no rows
String[] colNames = {
"QTY", "Item Code", "Amount"
};
DefaultTableModel model = new DefaultTableModel(colNames, 0); <== 0 rows
JTable table = new JTable(model);
然后,无论您要添加什么行,只需将其添加到模型中
Then whatever rows you want to add, just add to the model
public void actionPerformed(ActionEvent e){
model.addRow(new Object[]{"DN Korina", "DN Madrid", "DN Romania"});
}
更新示例
import java.awt.*;
import java.util.Stack;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class TestTable {
private String[] colNames = { "Col 1", "Col2", "Col3", "Col4", "Col5" };
private DefaultTableModel model = new DefaultTableModel(colNames, 0);
private JTable table = new JTable(model);
private MyStack myStack = new MyStack();
private Stack<Integer[]> stack;
private JButton button = new JButton("Add Row");
public TestTable() {
stack = myStack.getStack();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!stack.isEmpty()) {
Integer[] array = stack.pop();
model.addRow(array);
}
}
});
JFrame frame = new JFrame();
frame.add(new JScrollPane(table), BorderLayout.CENTER);
frame.add(button, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TestTable();
}
});
}
}
class MyStack {
public Stack<Integer[]> stack = new Stack<Integer[]>();
public MyStack() {
int k = 1;
for (int i = 0; i < 20; i++) {
Integer[] array = new Integer[5];
for (int j = 0; j < 5; j++) {
array[j] = i * k * (j + 1);
}
k++;
stack.push(array);
}
}
public Stack<Integer[]> getStack() {
return stack;
}
}
说明
- 我有一个具有
Stack
的MyStack类.它甚至可以是任何数据结构,例如数组,但是我选择使用Stack
,因为我可以使用.pop()
.然后获取Integer[]
. - 我在GUI类中实例化该类
- 然后我提取该
Stack
.请记住,Stack
持有Integer[]
的 - 正如我所解释的,您想向
model
添加一个维度数组. - 因此,每次单击按钮时,都会从
Stack
中拉出一个Integer[]
,然后将其作为一行添加到model
中.
- I have a MyStack class that has a
Stack
. It could be any data structure like an array even, but I chose to use aStack
because I can just.pop()
it. And the get theInteger[]
. - I instantiate that class in the GUI class
- I then extract that
Stack
. Keep in mind theStack
holdsInteger[]
's - As i explained, you want to add a single dimension array to the
model
. - So every time I click the button, an
Integer[]
is pulled out of theStack
, then I just add that array to themodel
as a row.
这篇关于单击按钮时,Java将数据传递到JTable的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!