ProgrammingFreak
ProgrammingFreak

Reputation: 558

Tried to add data to the JTable Dynamically using DefaultTableModel but it is not working

I tried to add data to the JTable Dynamically using DefaultTableModel, but it isn't working.

I had created two classes: One class (Input.java) represents a popup which allows you to write the data (name, option and constraint) and save it. Another class represents a popup which includes a table (InputMain.java). InputMain will then transform those info (if received) to a row with in the table that list out those all the information in three columns. In Input, I created an instance of InputMain, and set the model of the table to a new DefaultTableModel() (if I don't do this, it will throw an error) (a table is already created in InputMain, so that if I create an instance of InputMain, I am probably calling the table within InputMain). Then, according to actionPerformed(ActionEvent e), after the 'enter' button is clicked within the JPanel, it will create a new array of data with nameText, optionText and constraintText. And finally, the data will be added to the model.

I don't know why I can't add all those data into the table, except that method above sounds logical to me.

Here's the InputMain.java class:

    package com.company;

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

/**
 * Created by paull on 14/5/2016.
 */
public class InputMain {
    JButton add;
    JButton change;
    JButton delete;
    JTable table;
    String[] columns;
    String[][] data;
    JButton create;
    JPanel labelPanel;
    JPanel bigPanel;
    JPanel centerPanel;

    public InputMain() {
        int fontSize = 50;
        add = new JButton("add");
        change = new JButton("change");
        delete = new JButton("delete");
        create = new JButton("create");
        columns = new String[]{"name","option","constraint"};
        data = new String[][]{{"","",""}};

        table = new JTable(data,columns) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false; //to avoid it from being changable by double clicking any data
            }
        };
        table.setPreferredScrollableViewportSize(new Dimension(450,63));
        table.setFillsViewportHeight(true);

        JScrollPane jsp = new JScrollPane(table); // not always can the table be fully shown --> add a scrollbar


        add.setFont(new Font(add.getName(),Font.PLAIN,fontSize));
        change.setFont(new Font(add.getName(),Font.PLAIN,fontSize));
        delete.setFont(new Font(add.getName(),Font.PLAIN,fontSize));
        create.setFont(new Font(add.getName(),Font.PLAIN,fontSize));
        table.setFont(new Font(table.getName(),Font.PLAIN,fontSize));
        //table.getEditorComponent().setFont(new Font(add.getName(),Font.PLAIN,fontSize));
        table.getTableHeader().setFont(new Font(table.getName(),Font.PLAIN,fontSize));

        labelPanel = new JPanel();
        labelPanel.add(add);
        labelPanel.add(change);
        labelPanel.add(delete);
        labelPanel.setLayout(new FlowLayout(10,30,10));

        centerPanel = new JPanel();
        centerPanel.add(labelPanel);
        centerPanel.setLayout(new GridBagLayout());

        bigPanel = new JPanel();
        bigPanel.setLayout(new GridLayout(3,0));
        bigPanel.add(centerPanel);
        bigPanel.add(jsp);
        bigPanel.add(create);

        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                InputFrame s = new InputFrame();
                Input i = new Input();
                s.add(i.labelPanel);
                s.setVisible(true);
            }
        }); // pressing add button pops up the Input Frame

    }

}

And here's the Input.java class:

package com.company;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by paull on 14/5/2016.
 */
public class Input extends JPanel{

    JLabel nameLabel;
    JLabel optionLabel;
    JLabel constraintLabel;
    JButton enter;
    JPanel labelPanel;
    JPanel jp2;
    JPanel jp3;
    JPanel jp4;
    JTextField nameText;
    String[] optionStrings;
    JComboBox<String> optionText;
    JCheckBox wk1;
    JCheckBox wk2;
    JCheckBox wk3;
    JCheckBox wk4;
    JCheckBox wk5;


    public Input() {
        nameLabel = new JLabel("name: ");
        optionLabel = new JLabel("option: ");
        constraintLabel = new JLabel("constraint:");
        enter = new JButton("add");
        nameText = new JTextField(10);
        Options c = new Options();
        optionStrings = new String[]{c.satNight,c.sunEight,c.sunNineThirty,c.sunEleven,c.sunNight};
        optionText = new JComboBox<String>(optionStrings);
        wk1 = new JCheckBox("1");
        wk2 = new JCheckBox("2");
        wk3 = new JCheckBox("3");
        wk4 = new JCheckBox("4");
        wk5 = new JCheckBox("5");


        int fontSize = 50;
        nameLabel.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        optionLabel.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        constraintLabel.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        enter.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        nameText.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        optionText.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        wk1.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        wk2.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        wk3.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        wk4.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        wk5.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));

        labelPanel = new JPanel();
        labelPanel.add(nameLabel);
        labelPanel.add(nameText);
        labelPanel.add(optionLabel);
        labelPanel.add(optionText);
        labelPanel.add(constraintLabel);
        labelPanel.add(wk1);
        labelPanel.add(wk2);
        labelPanel.add(wk3);
        labelPanel.add(wk4);
        labelPanel.add(wk5);
        labelPanel.add(enter);
        labelPanel.setLayout(new FlowLayout(10,30,10));


        InputMain i = new InputMain();
        i.table.setModel(new DefaultTableModel());
        DefaultTableModel model = (DefaultTableModel) i.table.getModel();

        enter.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String constraintText = "";
                String[] data = new String[]{nameText.getText(),optionText.getSelectedItem().toString(),constraintText};
                model.addRow(data);

                nameText.setText("");

            }
        });

    }


}

Thanks in advance.

Upvotes: 1

Views: 88

Answers (1)

Frank
Frank

Reputation: 2066

I think the problem lies in the actionPerformed of InputMain.

       add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                InputFrame s = new InputFrame();
                Input i = new Input();
                s.add(i.labelPanel);
                s.setVisible(true);
            }
        });

You construct a new instance of Input. That means that you create a new empty DefaultTableModel and attach it to your table each time you click on the add button. Try move the construction of the Input outside of your actionPerformed like this:

         Input i = new Input();
         add.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    InputFrame s = new InputFrame();                    
                    s.add(i.labelPanel);
                    s.setVisible(true);
                }

UPDATE Of course the instantiation of InputMain inside of Input should be removed, too. Instead have a member of Type InputMain in Input:

private InputMain inputMain;

and change the constructor of Input to set the value:

public Input (InputMain inputMain) {
    this.inputMain = inputMain;
    ...

And change the instantiation of Input in InputMain to

Input i = new Input(this);

Upvotes: 1

Related Questions