Search code examples
javaswingjtablejava-17

Changing one JTable changes other JTable in java


I am making a program in which I need many JTables. So for setting the font, column size etc. I am using this method:

public static void Enhance(JTable t){
        
       t.setRowHeight(70);
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment(JLabel.CENTER);
        t.setDefaultRenderer(String.class, centerRenderer);

        t.setAlignmentX(JTable.CENTER_ALIGNMENT);
     
        t.getModel().addTableModelListener(new TableModelListener() {
            @Override
            public void tableChanged(TableModelEvent e) {
                   resizeColumnWidth(t);
                    
            }
        });


        int index = 0;
        t.setAutoCreateColumnsFromModel(true);

        while (index < t.getColumnModel().getColumnCount()) {
            
            TableColumn a = t.getColumnModel().getColumn(index);
            a.setMinWidth(130);
            a.setPreferredWidth(130);


        

            index++;

        }
        JTextField textField = new JTextField();
textField.setFont(new Font("Open Sans", Font.PLAIN, 27));
textField.setBorder(new LineBorder(Color.BLACK));
DefaultCellEditor dce = new DefaultCellEditor( textField );
t.getColumnModel().getColumn(0).setCellEditor(dce);
for(int i =1 ; i<t.getColumnCount();i++){
    
    TableColumn sportColumn = t.getColumnModel().getColumn(i);
            JComboBox comboBox = new JComboBox();
            comboBox.setFont(new Font("Open Sans", Font.PLAIN,25));
            comboBox.addItem("Hulk- Maths");
            comboBox.addItem("THor - Computer");
            comboBox.addItem("Iron M- Geo");
            comboBox.addItem("Black P- History");
            comboBox.addItem("War M- Science");
            comboBox.addItem("Java  - Easy");
            comboBox.addItem("Custom");
            sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
            comboBox.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent e) {
            
             Object selectedItem = comboBox.getSelectedItem();
                boolean editable = selectedItem instanceof String && ((String)selectedItem).equals("Custom");
                comboBox.setEditable(editable);
        }
            });
            

}
     
t.setShowGrid(true);
        t.setFont(new Font("Open Sans",Font.PLAIN,25));
        t.getTableHeader().setFont(new Font("Open Sans", Font.PLAIN, 27));
        t.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
        t.setBackground(Color.WHITE);
        t.setSelectionBackground(new Color(172, 208, 252));
        t.setRowHeight(50);
        t.setSelectionForeground(Color.black);
        t.getTableHeader().setBackground(new Color(23, 180, 252));
        t.setGridColor(Color.lightGray);
        t.getTableHeader().setForeground(Color.white);
        
     
        
        
        
    }

I am using this method like this:-

JTable t1 = new JTable(data1, title);//data is a String[][] Object and title is a String[] Object.

Enhance(t1);

Like this I have 4 more tables.

But the problem is when I make edits to t1 , The JTable t2 is also changing.

If I enter

"Java"

in the first cell of t1, the first cell of t2 will also be

"Java" 

How can I solve this so that edits made to t1 is not visible in t2?

EDIT (Debugging details):

Even this code is also not working

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

/*
 * Made By JFan
 */

/**
 *
 * @author JFan
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        JFrame f = new JFrame();
        f.setSize(1000,1000);
        f.setLayout(null);
        
        String[][] data = {
            
            {"","","","",""},
            {"","","","",""},
            {"","","","",""},
            {"","","","",""},{"","","","",""}
        };
        String[] t = {
          "1","2","3","4","5"  
        };
        JTable t1 = new JTable(data,t);
        JScrollPane j = new JScrollPane(t1);
        j.setBounds(0, 200, 500, 500);
        f.add(j);
        
         JTable t2 = new JTable(data,t);
        f.add(j);
        
     
        
        JButton  b = new JButton("Click");
        b.setBounds(0,0,100,100);
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
           
                j.setViewportView(t2);
                
            }
        });
        
        f.add(b);
           f.setVisible(true);
        
    }
    
}

Solution

  • Don't use:

    JTable t2 = new JTable(data,t);
    

    to create the JTable. This will create a simple TableModel using the Array as the storage for the data.

    As you noticed, this Array is shared by all tables.

    Instead use:

    DefaultTableModel model = new DefaultTableModel(data, t);
    JTable t2 = new JTable( model );
    

    Now the data from the Array will be copied to the DefaultTableModel.

    Therefore each table has its own copy of the data.