2016-06-14 76 views
0

我有一個使用JCombobox的java窗體。有一個「文本字段」,「組合框」和「添加」按鈕。當我在文本字段中輸入文本時,然後單擊添加按鈕,文本字段中的文本將填充到組合框中,但不會按a-z排序。如何在java中對項目進行排序combobox

form screenshot

下面是我在添加按鈕的代碼。

private void cmdAddActionPerformed(java.awt.event.ActionEvent evt) {          
    if(MyInput.checkText(txtField)){ //MyInput is my own method code to check if the text is entered. 
     String st=txtField.getText(); 
     combox.addItem(st); 
     String st1=""; 
     String st2=""; 
     int com=st1.compareTo(st2); 
     if(com<0){  //I create this string comparison 
         //I need to add the "sort" code here 
     }else{ 

     } 
    }else 
     JOptionPane.showMessageDialog(this, "Please enter text."); //When no text is entered, this message appears. 
} 

完整的源代碼:

import PlugIn.MyInput; 
import javax.swing.JOptionPane; 

public class JCombo extends javax.swing.JFrame { 

    /** 
    * Creates new form JCombo 
    */ 
    public JCombo() { 
     initComponents(); 
    } 

    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     txt = new javax.swing.JTextField(); 
     combo = new javax.swing.JComboBox(); 
     cmdAdd = new javax.swing.JButton(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     txt.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       txtActionPerformed(evt); 
      } 
     }); 

     combo.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       comboActionPerformed(evt); 
      } 
     }); 

     cmdAdd.setText("Add"); 
     cmdAdd.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       cmdAddActionPerformed(evt); 
      } 
     }); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(88, 88, 88) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
        .addComponent(txt) 
        .addComponent(combo, 0, 188, Short.MAX_VALUE)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
       .addComponent(cmdAdd) 
       .addContainerGap(63, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(40, 40, 40) 
       .addComponent(txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(combo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(cmdAdd)) 
       .addContainerGap(206, Short.MAX_VALUE)) 
     ); 

     pack(); 
    }// </editor-fold>       

    private void txtActionPerformed(java.awt.event.ActionEvent evt) {          
     // TODO add your handling code here: 
    }         

    private void cmdAddActionPerformed(java.awt.event.ActionEvent evt) {          
     if(MyInput.checkText(txtField)){ //MyInput is my own method code to check if the text is entered. 
      String st=txtField.getText(); 
      combox.addItem(st); 
      String st1=""; 
      String st2=""; 
      int com=st1.compareTo(st2); 
      if(com<0){  //I create this string comparison 
          //I need to add the "sort" code here 
      }else{ 

      } 
     }else 
      JOptionPane.showMessageDialog(this, "Please enter text."); //When no text is entered, this message appears. 
    }          

    private void comboActionPerformed(java.awt.event.ActionEvent evt) {          
     // TODO add your handling code here: 
    }          

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     /* Set the Nimbus look and feel */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(JCombo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(JCombo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(JCombo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(JCombo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new JCombo().setVisible(true); 
      } 
     }); 
    } 

    // Variables declaration - do not modify      
    private javax.swing.JButton cmdAdd; 
    private javax.swing.JComboBox combo; 
    private javax.swing.JTextField txt; 
    // End of variables declaration     
} 

回答

0

使用SortedComboBoxModel:(How to sort the jComboBox elements in java swing?

String[] items = { "one", "two", "three" }; 
SortedComboBoxModel<String> model = new SortedComboBoxModel<String>(items); 
JComboBox<String> comboBox = new JComboBox<String>(model); 
comboBox.addItem("four"); 
comboBox.addItem("five"); 
comboBox.setSelectedItem("one"); 
0

你必須把自己的順序照顧。

  • 使用getModel()獲取當前列表
  • 找出其中的項目應該去
  • 使用insertItemAt(E item, int index)該項目將INT正確的位置。
相關問題