1 JTable列用于2个数据选项



我有一个DefaultTableModel设置像这样

String[] columns = { "Username", "Group", "Kick", "Ban" };
Object[][] data = { { "Name1", "Owner", "Kick", "Ban" }, { "Name2", "Admin", "Kick", "Ban" } };
DefaultTableModel dm = new DefaultTableModel(data, columns);

我想只有一个名为"选项"的大列为"踢"one_answers"禁"。我该怎么做呢?

可以通过以下方式禁用列大小调整:

table.getTableHeader().setResizingAllowed(false);

您不能使用DefaultTableModel。你必须创建你自己的表模型扩展Java DefaultTableModel。这是一个你可以根据你的目的改编的例子。

public class SplitCellsExample extends JFrame
{
 // data is Vector of Vector... each inner Vector (i.e. each record) 
 // has values of Object[2] representing a split cell value
// and each element of Object[2] has half of the cell value
Vector data;
JTable table;
public SplitCellsExample()
{
    super();
    data = createData();
    SplitCellTableModel model = new SplitCellTableModel();
    table = new JTable(model);
    table.getColumnModel().getColumn(0).setCellEditor(new SplitTableCellEditor());
    table.getColumnModel().getColumn(1).setCellEditor(new SplitTableCellEditor());
    table.getColumnModel().getColumn(0).setCellRenderer(new SplitTableCellRenderer());
    table.getColumnModel().getColumn(1).setCellRenderer(new SplitTableCellRenderer());
    JScrollPane scroller = new JScrollPane(table);
    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(scroller, BorderLayout.CENTER);
    getContentPane().add(new JLabel("Famous twins"), BorderLayout.NORTH);
}
public Vector createData()
{
    Vector result = new Vector();
    Vector rec = new Vector();
    Object[] value = new Object[] {"Benji Madden", "Joel Madden"};
    rec.add(value);
    value = new Object[] {"Mary-Kate Olsen", "Ashley Olsen"};
    rec.add(value);
    result.add(rec);
    rec = new Vector();
    value = new Object[] {"Charlie Read", "Craig Read"};
    rec.add(value);
    value = new Object[] {"Tegan Quin", "Sara Quin"};
    rec.add(value);
    result.add(rec);
    return result;
}
public static void main(String[] args)
{
    final SplitCellsExample frame = new SplitCellsExample();
    frame.setSize(500, 400);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    Runnable r = new Runnable() {
      public void run() {
        frame.setVisible(true);
      }
    };
    SwingUtilities.invokeLater(r);
}
/**********************************************************/
public class SplitCellTableModel extends DefaultTableModel
 {
public SplitCellTableModel()
{
}
public int getRowCount()
{
    return data.size();
}
public int getColumnCount()
{
    return 2;
}
public String getColumnName(int col)
{
    if (col == 0)
        return "Lads";
    else
        return "Lasses";
}
public Object getValueAt(int row, int col)
{
    Vector rec = (Vector)data.get(row);
    return rec.get(col);
}
public Vector getDataVector()
{
    return data;
}
public boolean isCellEditable(int row, int col)
{
    return true;
}
public void setValueAt(Object value, int row, int col)
{
    Vector rec = (Vector)data.get(row);
    rec.setElementAt(value, col);
}
 }
/**********************************************************************/
public class SplitTableCellEditor extends AbstractCellEditor implements TableCellEditor
{
JPanel editingPanel;
JTextField leftTextField;
JTextField rightTextField;
public SplitTableCellEditor()
{
    super();
    editingPanel = new JPanel();
    editingPanel.setLayout(new GridLayout(1, 2));
    leftTextField = new JTextField();
    editingPanel.add(leftTextField);
    rightTextField = new JTextField();
    editingPanel.add(rightTextField);
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int rowIndex, int colIndex)
{
    Object[] splitVal = (Object[])value; // cast to array of Object
    if (splitVal != null)
    {
        String leftStr = (String)splitVal[0];
        String rightStr = (String)splitVal[1];
        leftTextField.setText(leftStr == null ? "" : leftStr);
        rightTextField.setText(rightStr == null ? "" : rightStr);
    }
    else
    {
        leftTextField.setText("");
        rightTextField.setText("");
    }
    return editingPanel;
}
public Object getCellEditorValue()
{
    Object[] value = new Object[] {leftTextField.getText(), rightTextField.getText()};
    return value;
}
}
/****************************************************************/
public class SplitTableCellRenderer implements TableCellRenderer
{
JPanel rendererPanel;
JLabel leftLabel;
JLabel rightLabel;
public SplitTableCellRenderer()
{
    super();
    rendererPanel = new JPanel();
    rendererPanel.setLayout(new GridLayout(1, 2));
    leftLabel = new JLabel();
    leftLabel.setOpaque(true);
    leftLabel.setBackground(Color.white);
    rendererPanel.add(leftLabel);
    rightLabel = new JLabel();
    rightLabel.setOpaque(true);
    rightLabel.setBackground(Color.white);
    rendererPanel.add(rightLabel);
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col)
{
    Object[] splitVal = (Object[])value; // cast to array of Object
    if (splitVal != null)
    {
        String leftStr = (String)splitVal[0];
        String rightStr = (String)splitVal[1];
        leftLabel.setText(leftStr == null ? "" : leftStr);
        rightLabel.setText(rightStr == null ? "" : rightStr);
    }
    else
    {
        leftLabel.setText("");
        rightLabel.setText("");
    }
    return rendererPanel;
}
 }
  }

相关内容

  • 没有找到相关文章

最新更新