更新JTable中的数据 [英] Updating Data in a JTable
本文介绍了更新JTable中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有一张桌子.其中一个单元格保存一个JLabel
.如果更改JLabel
的文本,如何获取JTable
来显示更改?
看下面的代码,我应该怎么做才能显示对JLabel
的更改?
Lets say I have a table. One of the cells holds a JLabel
. If I change the text of the JLabel
how do I get the JTable
to show the change?
Look at the following code, what should I change to make it show the changes to the JLabel
?
public class ActivTimerFrame extends JFrame implements ActionListener{
//Data for table and Combo Box
String timePlay = "1 Hour";
String timeDev = "2 Hours";
String[] comboChoices = {"Play Time", "Dev Time"};
String[] columnNames = {"Activity", "Time Allowed", "Time Left"};
Object[][] data = {{"Play Time", "1 Hour", timePlay }, {"Dev Time", "2 Hours", timeDev }};
//This is where the UI stuff is...
JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
JPanel mainPanel = new JPanel();
JComboBox comboBox = new JComboBox(comboChoices);
JButton start = new JButton("Start");
JButton stop = new JButton("Stop");
public ActivTimerFrame() {
super("Activity Timer");
setSize(655, 255);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
GridLayout layout = new GridLayout(2,1);
setLayout(layout);
add(scrollPane);
stop.setEnabled(false);
start.addActionListener(this);
mainPanel.add(comboBox);
mainPanel.add(start);
mainPanel.add(stop);
add(mainPanel);
}
@Override
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if(source == start) {
timePlay ="It Works";
}
}
}
推荐答案
您可以
table.getModel().setValueAt(cellValueObject, rowIndex, colIndex);
设置特定的单元格.
在您尝试的情况下,您可以做
in you case for what you are trying, you can do
timePlay ="It Works";
table.getModel().setValueAt(timePlay, 0, 1);
这篇关于更新JTable中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文