如何在表格单元格中添加复选框和组合框? [英] how to add checkbox and combobox in table cell?

查看:191
本文介绍了如何在表格单元格中添加复选框和组合框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个包含表格和一些按钮的表单。

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

一张图片胜过千言万语:

A picture is worth a thousand words:

怎么能我将复选框和组合框放到表中?

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

我正在使用NetBeans。我尝试使用拖放但没有工作。

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

这是我的表格代码。

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()));
    }  


推荐答案

这是组合单元格插件复制演示:

Here is combo cell insets replicate demo:

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);
      }
    }
  }
}

这篇关于如何在表格单元格中添加复选框和组合框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆