Updating data in JTable

Say I have a table. One of the cells contains JLabel. If I changed the text JLabel, how do I get JTableto show the change? Look at the following code, what should I change so that it displays the changes in 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";


        }

    }



}
+3
source share
2 answers

You can do

table.getModel().setValueAt(cellValueObject, rowIndex, colIndex);

to set a specific cell.

in your case for what you are trying, you can do

        timePlay ="It Works";
        table.getModel().setValueAt(timePlay, 0, 1);
+8
source

, JTable TableModel, AbstractTableModel DefaultTableModel, , , . , JTable, ( , DefaultTableModel). Swing JTables , , .

+4

All Articles