我想创建一个包含少量列的JTable,其中包含JButtons。我可以在JTable单元格中创建JCheckBoxes和JComboBoxes,但不能创建JButtons。我该怎么做?
答案 0 :(得分:1)
在我的项目中,这就是我为一个包含3列的4行表所做的工作,其中第一列的每一行都有JToggleButton
:
必要的对象......
private JTable table1;
private DefaultTableModel table1Model;
private JScrollPane scrollPane1;
String[] columnName = {"Button", "Boolean", "Int" };
并在我们创建表格的代码下...
{
JToggleButton[] jTableButton = new JToggleButton[] { new JToggleButton("one"), new JToggleButton("two"), new JToggleButton("three"), new JToggleButton("four")};
table1Model =
new DefaultTableModel(
new Object[][] { { jTableButton[0], false, 1 },
{ jTableButton[1], false, 1 },
{ jTableButton[2], false, 1 },
{ jTableButton[3], false, 1 }},
columnName);
table1 = new JTable(table1Model){
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public boolean isCellEditable(int rowIndex, int colIndex) {
int value = (Integer) table1Model.getValueAt(rowIndex, 2);
String blueColor = Color.BLUE;
String redColor = Color.RED;
String selectedColor = Color.GREEN;
String colorValue = selectedColor;
if ( value > 0) {
colorValue = blueColor; //blue
}
else {
colorValue = redColor; //red
}
return !colorValue.equals(redColor); // Disallow the editing of red cell
}
@Override
public Class<?> getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
@Override
public Component prepareRenderer(TableCellRenderer renderer,int rowIndex, int vColIndex)
{
int value = (Integer) table1Model.getValueAt(rowIndex, 2);
String blueColor = Color.BLUE;
String redColor = Color.RED;
String selectedColor = Color.GREEN;
String colorValue = selectedColor;
if ( value > 0) {
colorValue = blueColor; //blue
}
else {
colorValue = redColor; //red
}
Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
if (!isCellSelected(rowIndex, vColIndex))
{
if (vColIndex != 0 ) {
c.setEnabled(!colorValue.equals(redColor));
}
else {
c.setEnabled(!colorValue.equals(redColor));
if (colorValue.equals(redColor))
c.setBackground(Color.decode(colorValue));
}
if (c instanceof JToggleButton && vColIndex == 0){
JToggleButton button = (JToggleButton) c;
button.setToolTipText(((JToggleButton) c).getText());
}
if ( Color.decode(colorValue).getRed() >= 224
&& Color.decode(colorValue).getGreen() >= 224
&& Color.decode(colorValue).getBlue() >= 224){
c.setForeground(Color.gray);
}
else {
c.setForeground(Color.white);
}
}
return c;
}
};
scrollPane1.setViewportView(table1);
table1.setPreferredSize(new java.awt.Dimension(187,290));
table1.setFont(new java.awt.Font("Segoe UI",0,14));
table1.getColumnModel().getColumn(0).setCellRenderer(new JToggleButtonRenderer());
}
<强> JToggleButtonRenderer.java 强>
class JToggleButtonRenderer extends JToggleButton implements TableCellRenderer {
/**
*
*/
private static final long serialVersionUID = 1L;
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (value == null)
return null;
if (isSelected) {
// cell (and perhaps other cells) are selected
}
if (hasFocus) {
// this cell is the anchor and the table has the focus
}
return (Component) value;
}
// The following methods override the defaults for performance reasons
@Override
public void validate() {}
@Override
public void revalidate() {}
@Override
protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {}
@Override
public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {}
}
如您所见,重要的部分是表格模型table1Model
添加新行...
//id is something you want put on the button text
table1Model.addRow(new Object[] { new JToggleButton(id), false, 1} );
<强>编辑:强>
添加了缺少的JToggleButton单元格渲染器