java swing定制表格单元格?

时间:2012-01-31 16:58:27

标签: java swing jtable

在java Swing表中,如何将一个单元格拆分为两个,一个是TextField,另一个是复选框。我做了一些代码,但不起作用。感谢

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;
        }
    }

1 个答案:

答案 0 :(得分:3)

1)在这种情况下,您必须为LayoutManager定义,因为JLabel/JComponent(默认情况下返回TableCellRenderer)不会implemented any LayoutManager

2)将JPanel nested another JComponentsJPanel默认为FlowLayout)放入单元格

3)大多数可以将JTextField置于一个列,将Boolean值(返回JCheckBox)放到另一列