可以說我有一張桌子。其中一個單元格包含JLabel
。如果我更改JLabel
的文本,我如何讓JTable
顯示更改? 看看下面的代碼,我應該改變什麼才能使其顯示JLabel
的更改?更新JTable中的數據
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";
}
}
}
你說的是什麼標籤?我沒有在任何地方看到標籤。如果你真的在使用標籤,那麼不要。您應該只在模型中存儲文本,然後讓渲染器渲染文本。 – camickr 2011-05-07 02:49:05