怎样在指定的table单元格处生成下拉列表

类别:Java 点击:0 评论:0 推荐:

在写table时花了本人几天的时间,最后终于搞定了,与大家共同分享。
以下是调用方法:
/****************************************************************/

DefaultTableModel model = new DefaultTableModel(); //
    /*设置表格模型*/
    model.setDataVector(rowDatas, tableHeadName);

    /*表格使用模型*/
//    JBTable table = new JBTable(tableModel);
    JBTable table = new JBTable();
    table.setRowSelectionAllowed(true); //add
    table.setModel(model);
    JTextField text = new JTextField(); //

    JComboBox com_size = new JComboBox(); //字体大小
    com_size.addItem("1");
    com_size.addItem("2");
    com_size.addItem("3");
    com_size.addItem("4");
    com_size.addItem("5");
    com_size.addItem("6");

    JBMutiTypeEditor com_edit = new JBMutiTypeEditor(text, com_size); //
    TableColumn aColumn1 = table.getColumn("属性");
    aColumn1.setCellEditor(com_edit);

/****************************************************************/
上边代码没什么特别的,关键在以下的类。

package jbpd.application;

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

class JBMutiTypeEditor
    extends DefaultCellEditor {
  JTable editingTable = new JTable();
  JTextField textComponent;
  JComboBox comboComponent;
  boolean textOrNot = true;

  public JBMutiTypeEditor(JTextField text, JComboBox combo) {
    super(text);
    textComponent = text;
    comboComponent = combo;
  }

  public void setTextOrNot(boolean textOrNot) {
    this.textOrNot = textOrNot;
  }

  public Component getTableCellEditorComponent(JTable table, Object value,
                                               boolean isSelected,
                                               int row, int column) {
    editingTable = table;
    String flag = (String) table.getValueAt(row, 0);
    //与jbpage表中的“页面字体”列对应
    if (flag.equals("存储方式") || flag.equals("是否被选中")) {
      String item = (value != null) ? value.toString() : "1";
      DefaultComboBoxModel model = (DefaultComboBoxModel) comboComponent.
          getModel();
      for (int i = 0; i < model.getSize(); i++) {
        String aItem = (String) model.getElementAt(i);
        if (aItem.equals(item)) {
          comboComponent.setSelectedIndex(i);
        }
      }
      return comboComponent;
    }
    if (flag.equals("字体大小") && row == 3) {
      String item = (value != null) ? value.toString() : "1";
      DefaultComboBoxModel model = (DefaultComboBoxModel) comboComponent.
          getModel();
      for (int i = 0; i < model.getSize(); i++) {
        String aItem = (String) model.getElementAt(i);
        if (aItem.equals(item)) {
          comboComponent.setSelectedIndex(i);
        }
      }
      return comboComponent;
    }
    else {
      textComponent.setText( (value != null) ? value.toString() : "");
      return textComponent;
    }
  }

  public boolean stopCellEditing() {
    JTable aTable = this.editingTable;
    int rowNo = aTable.getEditingRow();
    String flag = "";
    if (rowNo != -1) {
      flag = (String) aTable.getValueAt(rowNo, 0);
    }
    if (flag.equals("存储方式")|| flag.equals("是否被选中")) {
      this.delegate.setValue(comboComponent.getSelectedItem());
    }
    if(flag.equals("字体大小")){
      this.delegate.setValue(comboComponent.getSelectedItem());
    }
    return super.stopCellEditing();
  }
}

本文地址:http://com.8s8s.com/it/it12102.htm