在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;
}
}
答案 0 :(得分:3)
1)在这种情况下,您必须为LayoutManager
定义,因为JLabel/JComponent
(默认情况下返回TableCellRenderer
)不会implemented any LayoutManager
2)将JPanel
nested another JComponents(JPanel
默认为FlowLayout
)放入单元格
3)大多数可以将JTextField
置于一个列,将Boolean
值(返回JCheckBox
)放到另一列