1个JTable列,用于2个数据选项

时间:2014-08-04 13:30:30

标签: java swing jtable defaulttablemodel jtableheader

我有一个像这样设置的DefaultTableModel

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

DefaultTableModel dm = new DefaultTableModel(data, columns);

我想只有一个名为“选项”的大栏目,用于“踢”和“禁令”。我该怎么做呢?

2 个答案:

答案 0 :(得分:0)

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

table.getTableHeader().setResizingAllowed(false);

答案 1 :(得分:0)

您无法使用DefaultTableModel。您必须创建自己的tableModel,它扩展了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;
}
 }

  }