2012年8月12日日曜日

JTableのセルに、自動検索コンボボックスを設置

JTableのセルに、自動検索コンボボックスを設置する。 (On JTable , Setting the cellEditor with the combobox searching item automatically )




import java.awt.EventQueue;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JTextField;

import jp.co.custom.text.RegExpClass;
import jp.co.custom.text.TextUtl;

public class KeyListenerOfComboBoxClass extends KeyAdapter {
 private JComboBox m_oComboBoxOfTarget = null;
 DefaultComboBoxModel m_oComboBoxModelOfSource = null;
 private boolean m_doShowPopupMenu = false;
  
 public KeyListenerOfComboBoxClass(JComboBox oArgComboBoxOfTarget){
  m_oComboBoxOfTarget = oArgComboBoxOfTarget;
  m_oComboBoxModelOfSource = new DefaultComboBoxModel();
  int lastIndex = oArgComboBoxOfTarget.getModel().getSize() - 1;
  String tmpValue = null;
  int i = 0;
  for(i = 0 ; i <= lastIndex ; i++){
   tmpValue = oArgComboBoxOfTarget.getModel().getElementAt(i);
   m_oComboBoxModelOfSource.addElement(tmpValue);
  }
 }

 private static boolean hasPutDeleteKey(int argKeyCode){
  switch(argKeyCode){
   case KeyEvent.VK_DELETE:
   case KeyEvent.VK_BACK_SPACE:
    return true;
  }
  return false;
 }
 
 private static void clearTextField(JComboBox oArgComboBox){
  ((JTextField)oArgComboBox.getEditor().getEditorComponent()).setText("");
 }
 
 private static void initializeComboBox(JComboBox oArgComboBox , ComboBoxModel aArgComboBoxModel){
  clearTextField(oArgComboBox);
  oArgComboBox.setModel(aArgComboBoxModel);
  oArgComboBox.setSelectedIndex(-1);
 }
 
 private static ComboBoxModel getSuggestedModel(DefaultComboBoxModel argComboBoxModelOfSource , String argEnterdText) {
     DefaultComboBoxModel oComboBoxModelOfResult = new DefaultComboBoxModel();
     int lastIndex = argComboBoxModelOfSource.getSize() - 1;
     int i = 0;
     String tmpValue = null;
     for(i = 0 ; i <= lastIndex ; i++){
      tmpValue = argComboBoxModelOfSource.getElementAt(i);
   if(RegExpClass.isMatch(tmpValue , argEnterdText)){
    D.sp(tmpValue);
    oComboBoxModelOfResult.addElement(tmpValue);
   }
     }
     return oComboBoxModelOfResult;
  }  

 private static void setSuggestionModel(JComboBox oArgComboBox , ComboBoxModel aArgComboBoxModel , String argEnterdText) {
  oArgComboBox.setModel(aArgComboBoxModel);
     ((JTextField)oArgComboBox.getEditor().getEditorComponent()).setText(argEnterdText);
 } 
 
 private static boolean isBlankTextField(KeyEvent oArgEvent){
  String enteredText = ((JTextField)oArgEvent.getSource()).getText(); 
  if(TextUtl.isBlankText(enteredText)){
   return true;
  }
  return false;
 }
 
 @Override
 public void keyTyped(final KeyEvent oArgEvent) {
  EventQueue.invokeLater(new Runnable() {
   @Override public void run(){
    if(hasPutDeleteKey(oArgEvent.getKeyCode())){
     initializeComboBox(m_oComboBoxOfTarget,m_oComboBoxModelOfSource);
    }else{
     String enteredText=((JTextField)oArgEvent.getSource()).getText();
     if(0==enteredText.length()){
      initializeComboBox(m_oComboBoxOfTarget,m_oComboBoxModelOfSource);
     }else{
      ComboBoxModeloComboBoxModelSuggested=getSuggestedModel(m_oComboBoxModelOfSource , enteredText);
      if(0==oComboBoxModelSuggested.getSize()){
       setSuggestionModel(m_oComboBoxOfTarget,new DefaultComboBoxModel() , enteredText);
      }else{
       setSuggestionModel(m_oComboBoxOfTarget,oComboBoxModelSuggested,enteredText);
       m_doShowPopupMenu=true;
      }
     }
    }
   
   }
  });
 }

 @Override
 public void keyPressed(KeyEvent oArgEvent) {

 }

 @Override
 public void keyReleased(KeyEvent oArgEvent) {
     if(hasPutDeleteKey(oArgEvent.getKeyCode())){
      if(isBlankTextField(oArgEvent)){
       initializeComboBox(m_oComboBoxOfTarget , m_oComboBoxModelOfSource);
      }
     }
     if(m_doShowPopupMenu){
      m_oComboBoxOfTarget.showPopup();
      m_doShowPopupMenu = false;
     }
 }
}







・・・

JComboBox oComboBox = new JComboBox();
oComboBox.setEditable(true);
JTextField oTextField = (JTextField)oComboBox.getEditor().getEditorComponent();
oTextField.setText("");
oTextField.addKeyListener(new KeyListenerOfComboBoxClass(oComboBox));

TableColumnModel oColModel = oJTable.getColumnModel();
oColModel.getColumn(0).setCellEditor(getCellEditorWithComboBox(aCsv , tmpItemName));
oColModel.getColumn(0).setCellRenderer(new ComboCellRendere());

・・・


0 件のコメント: