KDee916
KDee916

Reputation: 1

Setting a Column Name for a JTable with editable number of columns and rows

What my window basically looks like

enter image description here

I've been trying to code a way to make a dynamic jtable where the column titles are Y, Xsub1, Xsub2, Xsub3,..., Xsub30. The number of columns and rows of the table are determined by specific textfields asking for the desired number for each, where I made the code to limit it to only 30 columns. I was able to do it to the extent of Xsub30 using long code, but it says that there is an error in the coding. It can only go up to Xsub10 without getting a error, even though the codes of Xsub1 up to Xsub30 are similar.I searched through the net to look for a way to code it using "for" or "if", so that it would be shorter, but so far my codes still have an error. The variables are all initialized, by the way. And I am using Netbeans IDE 8.0.2. Please help me fix this.

    rows = Integer.parseInt(rowsField.getText() ) ;
    col = Integer.parseInt(colField.getText() ) ;
    Object[][] rowArray = new Object[rows][col] ;


    valuesTable = new javax.swing.JTable();
    valuesTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF );


    if (col <1)
    {
        JOptionPane.showMessageDialog(this, "Sorry, that input is invalid");
    }

    if (col >30)
    {
        JOptionPane.showMessageDialog(this, "Sorry, that input is out of bounds");
    }

    if (rows <1)
    {
        JOptionPane.showMessageDialog(this, "Sorry, that input is invalid");
    }


    for (int x = 0; x < columnNames.length; x++)
    {valuesTable.getColumn(x).setHeaderValue(columnNames[x]);}


    if (col>=1 && col<=30)
    {
        valuesTable.setModel(new javax.swing.table.DefaultTableModel(
            rowArray, columnNames
        )



        {
         Class[] types = new Class[]{
                java.lang.Double.class, java.lang.Double.class
            };

            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }
        });

        jScrollPane1.setViewportView(valuesTable);

    }

Upvotes: 0

Views: 334

Answers (1)

aterai
aterai

Reputation: 9833

As @mKorbel said, you need to use TableColumnModel:

valuesTable.getColumnModel().getColumn(0).setHeaderValue("Y");
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class DynamicColumnTest {
  private final JTable valuesTable = new JTable();
  public JComponent makeUI() {
    valuesTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    JPanel p = new JPanel(new BorderLayout());
    p.add(new JScrollPane(valuesTable));
    p.add(new JButton(new AbstractAction("Apply col+1") {
      private int col = 1;
      @Override public void actionPerformed(ActionEvent e) {
        int rows = 5; //Integer.parseInt(rowsField.getText());
        col = 1 + col % 30; //Integer.parseInt(colField.getText());
        Object[][] rowArray = new Object[rows][col];
        Object[] columnNames = Collections.nCopies(col, "Xsub").toArray();
        valuesTable.setModel(new DefaultTableModel(rowArray, columnNames));

        valuesTable.getColumnModel().getColumn(0).setHeaderValue("Y");
        for (int x = 1; x < columnNames.length; x++) {
          //XXX: IllegalArgumentException: Identifier not found
          //XXX: valuesTable.getColumn(x).setHeaderValue(columnNames[x]);
          String s = String.format("%s%d", columnNames[x], x);
          valuesTable.getColumnModel().getColumn(x).setHeaderValue(s);
        }
      }
    }), BorderLayout.NORTH);
    return p;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new DynamicColumnTest().makeUI());
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}

Upvotes: 3

Related Questions