1

In java Swing table, how to split a cell into two, one is TextField, another one is a checkbox. I have done some codes, but doesn't work. thanks

public class CustomTableCellRenderer extends DefaultTableCellRenderer {
        public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
            Component cell = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, column);
            JTextField fld = new JTextField();
            JCheckBox chx = new JCheckBox();
            cell.add(fld);  // Doesn't work
            cell.add(chx);  // Doesn't work

            return cell;
        }
    }
user595234
  • 6,007
  • 25
  • 77
  • 101
  • A renderer is used to render (display) some data. It's not used to enter data. Why would you want to have such a renderer? Also, the component returned by `super.getTableCellRendererComponent(...)` is a JLabel. You're not supposed to add elements to a JLabel. – JB Nizet Jan 31 '12 at 17:06

1 Answers1

3

1) in this case you have to define for LayoutManager, because JLabel/JComponent (by default return TableCellRenderer) doesn't implemented any LayoutManager

2) put JPanel nested another JComponents (JPanel has by default FlowLayout) into Cell

3) most confortable will be put JTextField to one Column and Boolean value (returns JCheckBox) to another Column

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319