Search code examples
javaswingjtablejbuttontablecelleditor

Clickable JButton in JTable, and want to know the row they have clicked


I have a JButton inside a JTable in the last column, and when client click on that, It will show a JFrame. But I don't know how I can get the row so I can get the object in the row and send it to JFrame constructor?

it is my table:

table = new JTable(model);
    JTableHeader tableHeader = table.getTableHeader();
    tableHeaderRenderer = table.getTableHeader().getDefaultRenderer();
    tableHeader.setDefaultRenderer(new TableCellRenderer() {
        private JLabel label;
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int row, int column) {
            if (selectedColumn == value) {
                label = (JLabel) tableHeaderRenderer.getTableCellRendererComponent(table,
                        value, true, true, row, column);
                label.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                label.setBorder(BorderFactory.createCompoundBorder(label.getBorder(), 
                          BorderFactory.createEmptyBorder(0, 5, 0, 0)));
                label.setHorizontalAlignment(SwingConstants.LEFT);
            } else {
                label = (JLabel) tableHeaderRenderer.getTableCellRendererComponent(table,
                        value, false, false, row, column);
                label.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                label.setBorder(BorderFactory.createCompoundBorder(label.getBorder(), 
                          BorderFactory.createEmptyBorder(0, 5, 0, 0)));
                label.setHorizontalAlignment(SwingConstants.CENTER);
            }
            return label;
        }
    });
    table.getColumnModel().getColumn(3).setCellRenderer(new TableCellRenderer() {
        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int row, int column) {
            JPanel buttonPanel = new JPanel(new BorderLayout());
            buttonPanel.setBackground(Color.white);
            JButton button = Shorter.button("Details", true);
            buttonPanel.add(button);
            return buttonPanel;
        }
    });
    table.getColumnModel().getColumn(3).setCellEditor(new TableCellEditor() {
        public boolean stopCellEditing() {return false;}
        public boolean shouldSelectCell(EventObject arg0) {return false;}
        public void removeCellEditorListener(CellEditorListener arg0) {}
        public boolean isCellEditable(EventObject arg0) {return true;}
        public Object getCellEditorValue() {return null;}
        public void cancelCellEditing() {}
        public void addCellEditorListener(CellEditorListener arg0) {}
        public Component getTableCellEditorComponent(JTable arg0, Object arg1,
                boolean arg2, int arg3, int arg4) {
            JFrame frame = new JFrame();
            frame.setVisible(true);
            return null;}
    });

when frame loaded I want to know which row selected to add to frame constructor...


Solution

  • But I don't know how I can get the row so I can get the object in the row and send it to JFrame constructor?

    there are three choices

    1. event from JButton inside a JTable in the last column must to returs proper coordinates from JTables view, you have to convert view to model in the case that JTables view is

      • sorted

      • filtered

      • column(s) is/are reordered

      • column(s) is/are hidden (removed from JTables view)

    2. (and/or with) add ListSelectionListener to JTable

    3. Mouse Events can returns that too, read official Oracle tutorial How to use Tables - Specifying Tool Tips for Cells for working code example