Aqeel Haider
Aqeel Haider

Reputation: 633

ArrayList in Jtable Column

I'm creating a Jtable, with only one column (month Column) and adding an arraylist in the column with the help of button.

the Arraylist contains name of months depending upon checked months.

my Problem is when i add array list the previous row also change.

I have no idea what is going on.

Here is my code for the problem

package test;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Jtable extends JFrame {

    JTable table;
    JButton button;
    JPanel mp;

    DefaultTableModel model;

    JCheckBox chkJan;
    JCheckBox chkFeb;
    JCheckBox chkMarch;
    JCheckBox chkApril;
    JCheckBox chkMay;
    JCheckBox chkJun;
    JCheckBox chkJuly;
    JCheckBox chkAug;
    JCheckBox chkSep;
    JCheckBox chkOct;
    JCheckBox chkNov;
    JCheckBox chkDec;

    List<String> Months = null;

    public Jtable() {

        table = new JTable();
        button = new JButton("Add to Table");
        mp = new JPanel();
        Months = new ArrayList<>();

        chkJan = new JCheckBox("JAN");
        chkFeb = new JCheckBox("FEB");
        chkMarch = new JCheckBox("MARCH");
        chkApril = new JCheckBox("APRIL");
        chkMay = new JCheckBox("MAY");
        chkJun = new JCheckBox("JUN");
        chkJuly = new JCheckBox("JULY");
        chkAug = new JCheckBox("AUG");
        chkSep = new JCheckBox("SEP");
        chkOct = new JCheckBox("OCT");
        chkNov = new JCheckBox("NOV");
        chkDec = new JCheckBox("DEC");

        mp.setLayout(new FlowLayout(FlowLayout.LEFT));
        mp.setSize(300, 200);

        mp.add(chkJan);
        mp.add(chkFeb);
        mp.add(chkMarch);
        mp.add(chkApril);
        mp.add(chkMay);
        mp.add(chkJun);
        mp.add(chkJuly);
        mp.add(chkAug);
        mp.add(chkSep);
        mp.add(chkOct);
        mp.add(chkNov);
        mp.add(chkDec);

        model = new DefaultTableModel();
        table.setModel(model);
        model.addColumn("Months");

        JScrollPane sp = new JScrollPane(table);

        setLayout(new BorderLayout());

        add(sp, BorderLayout.CENTER);
        add(button, BorderLayout.SOUTH);
        add(mp, BorderLayout.NORTH);
        setSize(800, 600);

        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Month();
                Object[] row = {Months};

                model.addRow(row);
            }
        });

    }

    public void Month() {
        Months.removeAll(Months);
        if (chkJan.isSelected()) {
            Months.add("January");
        } else {
            Months.remove("January");
        }

        if (chkFeb.isSelected()) {
            Months.add("Febuary");
        } else {
            Months.remove("Febuary");
        }

        if (chkMarch.isSelected()) {
            Months.add("March");
        } else {
            Months.remove("March");
        }

        if (chkApril.isSelected()) {
            Months.add("April");
        } else {
            Months.remove("April");
        }

        if (chkMay.isSelected()) {
            Months.add("May");
        } else {
            Months.remove("May");
        }

        if (chkJun.isSelected()) {
            Months.add("Jun");
        } else {
            Months.remove("Jun");
        }

        if (chkJuly.isSelected()) {
            Months.add("July");
        } else {
            Months.remove("July");
        }

        if (chkAug.isSelected()) {
            Months.add("August");
        } else {
            Months.remove("August");
        }

        if (chkSep.isSelected()) {
            Months.add("September");
        } else {
            Months.remove("September");
        }

        if (chkOct.isSelected()) {
            Months.add("Octuber");
        } else {
            Months.remove("Octuber");
        }

        if (chkNov.isSelected()) {
            Months.add("November");
        } else {
            Months.remove("November");
        }

        if (chkDec.isSelected()) {
            Months.add("December");
        } else {
            Months.remove("December");
        }
    }

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;

                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Jtable.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Jtable().setVisible(true);
            }
        });
    }

}

Upvotes: 1

Views: 1238

Answers (2)

Alex S. Diaz
Alex S. Diaz

Reputation: 2667

Your problem is that Months is global, what you want is a local variable. If you edit this variable somewhere, every object that is using it (like the rows or you table) will be reflect its changes.

You could move Months to somewhere inside your methods where you have only local access, something like this:

button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        model.addRow(Months());
    }
});

And then, modify your Month method:

public Object[] Month() {
    List<String> Months = new ArrayList<>();
    if (chkJan.isSelected()) {
        Months.add("January");
    } else ...
    ...
    return new Object[]{Months};
}

Another thing, is a good practice to name your variables in lowercase Months could be months, and name your methods to something more readable like getSelectedMonths and last, is confusing to name your class Jtable since there's another with a similar name (JTable).

EDIT

After checking again, I guess many of your code is redundant, you could remove and optimize since you only need what Month returns.

Upvotes: 4

WillShackleford
WillShackleford

Reputation: 7018

Change

public void Month() {
        Months.removeAll(Months);
        ...

To:

public void Month() {
        Months = new ArrayList<>();
        ...

That way you are not modifying the Months that are already stored in the table.

Upvotes: 0

Related Questions