0
votes

I want to create a JTable having the last column with advanced options icon. On clicking this last column in the JTable, I want a new JPanel to pop up allowing user to enter input for required 4 string input fields. This JPanel when dismissed, should return to the original JTable.

I am not sure where to save the data for 4 fields from the new JPanel. As their would be 4 string input fields per JTable row, just displayed in the JPanel.

Can my JTabel cell hold an object saving the data?

UseCase: I have a JTable with 10 columns. It is getting very cluttered so I want to move 5 columns to a new panel which will be launched on clicking an advanced options icon in the original JTable last column.

Sample code on how to associate the data from the JPanel with the row in JTable will be highly appreciated.

1
The TableModel can hold anything that you want it to hold, and this can include an object with 10 or more properties, and not all of these need to be displayed in the JTable itself.Hovercraft Full Of Eels
You want a JDialog to pop up, not a JPanel.Gilbert Le Blanc
Use a JDialog to show your "advance" fields. A TableModel can hold a pojo, which only displays a subset of its values. When clicked, you would request the value (object) for the given selected row and pass this object to your panel. When accepted, you would extract the values from the panel, update the object and use your model to trigger a updateMadProgrammer
Use this TablePopupEditor to evoke a JOptionPane.trashgod
@TinyStrides: All data should be stored in the TableModel; getTableCellEditorComponent() receives the row.trashgod

1 Answers

0
votes

In order to show a pop-up when cell is clicked, you need a cell editor class. The main purpose of this class is to provide custom editors for cells, but you can use it to trigger some action when your cell is clicked:

public class InfoCellEditor extends AbstractCellEditor implements TableCellEditor {

    @Override
    public java.awt.Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        InfoObject info = (InfoObject) value;
        editButton = new JButton(new InfoAction(info));
        editButton.setText("INFO");
        editButton.setEnabled(true);
    }

    private class InfoAction extends AbstractAction {

        InfoObject info;

        public InfoAction(InfoObject info) {
            super();
            this.info = info;
        }

        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, info.toString());
            stopCellEditing();
        }
    }
}

Then, extend JTable class and implement getColumnClass and isCellEditable methods:

public class MyTable extends JTable {

    public MyTable() {
        super();
        setDefaultEditor(InfoObject.class, new InfoCellEditor());
    }

    @Override
    public Class getColumnClass(int columnIndex) {
        if(columnIndex == 4)
            return InfoObject.class;
        else
            return String.class;
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        if(column == 4)
            return true;
        else
            return false;
    }
}

Lastly, you should make sure that InfoObject instances are inserted to 5th column. And you can also implement a TableCellRenderer for some custom visual representation of that column.

Object headers = new Object[COLUMN_COUNT];
Object cells[][] = new Object[ROW_COUNT][];
...
cells[0][4] = new InfoObject(data[0]);
cells[1][4] = new InfoObject(data[1]);
table.setModel(new DefaultTableModel(cells, headers));
table.getModel().fireTableDataChanged();
table.setVisible();