how to add checkbox and combobox in table cell?
Asked Answered
O

2

5

I am creating one form which contain table and some buttons.

A picture is worth a thousand words:

enter image description here

How can I get the checkbox and comboboxes into the table?

I am using NetBeans. I tried using drag and drop but didn't work.

Here is my form code.

public class HttpOutput extends javax.swing.JPanel {
    HttpElements current_Http_EleObject;
    /**
     * Creates new form HttpOutput
     */
    public HttpOutput(HttpElements httpelements) {
        initComponents();
        current_Http_EleObject=httpelements;
         TableColumn includeColumn = jTable1.getColumnModel().getColumn(0);
            includeColumn.setCellEditor(new DefaultCellEditor(new JCheckBox()));
    }  
Outguard answered 11/10, 2012 at 12:9 Comment(1)
That's a great name: ComboBob!Austral
G
11

Here is combo cell insets replicate demo:

enter image description here

import java.awt.*;
import java.awt.event.*;
import java.util.EventObject;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class ComboCellInsetsDemo {
  public JComponent makeUI() {
    String[] columnNames = {"Name", "Check", "Condition"};
    Object[][] data = {
      {"bbb", false, "="}, {"aaa", true, "<"}
    };
    DefaultTableModel model = new DefaultTableModel(data, columnNames) {
      @Override public Class<?> getColumnClass(int column) {
        return getValueAt(0, column).getClass();
      }
    };
    JTable table = new JTable(model);
    table.setRowHeight(36);
    table.setAutoCreateRowSorter(true);
    TableColumn column = table.getColumnModel().getColumn(2);
    column.setCellRenderer(new ComboBoxCellRenderer());
    column.setCellEditor(new ComboBoxCellEditor());
    return new JScrollPane(table);
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new ComboCellInsetsDemo().makeUI());
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}

class ComboBoxPanel extends JPanel {
  private String[] m = new String[] {">", "<", "=", "<="};
  protected JComboBox<String> comboBox = new JComboBox<String>(m) {
    @Override public Dimension getPreferredSize() {
      Dimension d = super.getPreferredSize();
      return new Dimension(40, d.height);
    }
  };
  public ComboBoxPanel() {
    super();
    setOpaque(true);
    comboBox.setEditable(true);
    add(comboBox);
  }
}
class ComboBoxCellRenderer extends ComboBoxPanel
                           implements TableCellRenderer {
  public ComboBoxCellRenderer() {
    super();
    setName("Table.cellRenderer");
  }
  @Override public Component getTableCellRendererComponent(
      JTable table, Object value, boolean isSelected,
      boolean hasFocus, int row, int column) {
    setBackground(isSelected?table.getSelectionBackground()
                            :table.getBackground());
    if(value!=null) {
      comboBox.setSelectedItem(value);
    }
    return this;
  }
}
class ComboBoxCellEditor extends ComboBoxPanel
                         implements TableCellEditor {
  public ComboBoxCellEditor() {
    super();
    comboBox.addActionListener(new ActionListener() {
      @Override public void actionPerformed(ActionEvent e) {
        fireEditingStopped();
      }
    });
    addMouseListener(new MouseAdapter() {
      @Override public void mousePressed(MouseEvent e) {
        fireEditingStopped();
      }
    });
  }
  @Override public Component getTableCellEditorComponent(
      JTable table, Object value, boolean isSelected, int row, int column) {
    this.setBackground(table.getSelectionBackground());
    comboBox.setSelectedItem(value);
    return this;
  }

  //Copid from DefaultCellEditor.EditorDelegate
  @Override public Object getCellEditorValue() {
    return comboBox.getSelectedItem();
  }
  @Override public boolean shouldSelectCell(EventObject anEvent) {
    if(anEvent instanceof MouseEvent) {
      MouseEvent e = (MouseEvent)anEvent;
      return e.getID() != MouseEvent.MOUSE_DRAGGED;
    }
    return true;
  }
  @Override public boolean stopCellEditing() {
    if(comboBox.isEditable()) {
      comboBox.actionPerformed(new ActionEvent(this, 0, ""));
    }
    fireEditingStopped();
    return true;
  }

  //Copid from AbstractCellEditor
  //protected EventListenerList listenerList = new EventListenerList();
  transient protected ChangeEvent changeEvent = null;

  @Override public boolean isCellEditable(EventObject e) {
    return true;
  }
  @Override public void  cancelCellEditing() {
    fireEditingCanceled();
  }
  @Override public void addCellEditorListener(CellEditorListener l) {
    listenerList.add(CellEditorListener.class, l);
  }
  @Override public void removeCellEditorListener(CellEditorListener l) {
    listenerList.remove(CellEditorListener.class, l);
  }
  public CellEditorListener[] getCellEditorListeners() {
    return listenerList.getListeners(CellEditorListener.class);
  }
  protected void fireEditingStopped() {
    // Guaranteed to return a non-null array
    Object[] listeners = listenerList.getListenerList();
    // Process the listeners last to first, notifying
    // those that are interested in this event
    for(int i = listeners.length-2; i>=0; i-=2) {
      if(listeners[i]==CellEditorListener.class) {
        // Lazily create the event:
        if(changeEvent == null) changeEvent = new ChangeEvent(this);
        ((CellEditorListener)listeners[i+1]).editingStopped(changeEvent);
      }
    }
  }
  protected void fireEditingCanceled() {
    // Guaranteed to return a non-null array
    Object[] listeners = listenerList.getListenerList();
    // Process the listeners last to first, notifying
    // those that are interested in this event
    for(int i = listeners.length-2; i>=0; i-=2) {
      if(listeners[i]==CellEditorListener.class) {
        // Lazily create the event:
        if(changeEvent == null) changeEvent = new ChangeEvent(this);
        ((CellEditorListener)listeners[i+1]).editingCanceled(changeEvent);
      }
    }
  }
}
Gemmell answered 11/10, 2012 at 16:18 Comment(0)
M
7
includeColumn.setCellEditor(new DefaultCellEditor(new JCheckBox()));
Memorialist answered 11/10, 2012 at 12:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.