2013-01-25 86 views
0

需要幫助JComboBox組件。我已經從MySQL數據庫加載數據並將這些數據寫入組合。我使用四個組合框ItemListener。當一個組合框更改所有子組合框時,將從數據庫中重新加載數據。第一和兩個工作正常,但第三和第四個不顯示價值正常,但有價值。只有我不顯示它。當我選擇組合框中的空白字段後,我看到了很好的結果。JComboBox顯示值

的源代碼:

public class Vyhladat extends Okno { 
    private static final long serialVersionUID = 1L; 
    JComboBox nominalBOX,statBOX,podpisBOX,tlacDoskaBOX; 
    Font sherif = new Font("Sherif",Font.BOLD,20); 
    Font normal = new Font("Sherif",Font.PLAIN,20); 
    JFrame uh = new JFrame(); 
      private String adresa="jdbc:mysql://localhost:3306/jarodb"; 
    private String meno="JKARAK"; 
    private String heslo="bankovka"; 
    String nominal,stat,podpis,tlacDoska,statH; 
    private String nominalSQL = "SELECT DISTINCT(nominal) FROM prehlad"; 
    private String statSQL = "SELECT DISTINCT(concat(stat,'/',seria)) FROM prehlad WHERE nominal=? ORDER BY stat"; 
    private String podpisSQL = "SELECT DISTINCT(podpis) FROM prehlad WHERE nominal=? AND stat=? "; 
    private String tlacDoskaSQL = "SELECT DISTINCT(doska) FROM prehlad WHERE nominal=? AND stat=? AND podpis=? "; 

    Vector nominalV=new Vector(); 
    Vector statV=new Vector(); 
    Vector podpisV=new Vector(); 
    Vector tlacDoskaV=new Vector(); 

    Vyhladat() 
    { 

     vlozPopis(nominalLAB,"NOMIN\u00C1L EUROBANKOVKY: ",0,0,sherif); 
     vlozPopis(statLAB,"\u0160T\u00C1T/S\u00C9RIA:",0,1,sherif); 
     vlozPopis(podpisLAB,"PODPIS:",0,2,sherif); 
     vlozPopis(tlacDoskaLAB,"TLA\u010COV\u00C1 DOSKA:",0,3,sherif); 

     gbc.gridx=1; 
     gbc.gridy=0; 
     nacitajVyber(nominalSQL,nominalBOX,nominalV,false,false,false); 
     nominalBOX = new JComboBox(nominalV); 

     nominalBOX.addItemListener(new ItemListener() 
     { 
      public void itemStateChanged(ItemEvent e) 
      { 
       nominal = (String)nominalBOX.getSelectedItem(); 

       if(nominal!=" ") 
       { 
        nacitajVyber(statSQL,statBOX,statV,true,false,false); 
       } 
       else{ 
        statBOX.removeAllItems(); 
        podpisBOX.removeAllItems(); 
        tlacDoskaBOX.removeAllItems(); 
        } 
      } 
     }); 
     nominalBOX.setPrototypeDisplayValue("500"); 
     nominalBOX.setFont(normal); 
     nominalBOX.setSelectedIndex(0); 
     nominalBOX.setToolTipText("Vyber z mo\u017Enost\u00ED nomin\u00E1lu bankoviek 5,10,20,50."); 
     add(nominalBOX,gbc); 

     gbc.gridx=1; 
     gbc.gridy=1; 
     statV.add(" "); 
     statBOX= new JComboBox(statV); 
     statBOX.addItemListener(new ItemListener() 
     { 
      public void itemStateChanged(ItemEvent e) 
      { 
       stat = (String)statBOX.getSelectedItem(); 

       if(stat!=null) 
       { 
        String [] statM= stat.split("/"); 
        statH = statM[0]; 
       } 

       if(stat!=" " & stat!=null) 
       { 
        nacitajVyber(podpisSQL,podpisBOX,podpisV,false,true,false); 
       } 
       else{ 
        podpisBOX.removeAllItems(); 
        tlacDoskaBOX.removeAllItems(); 
        } 
      } 
     }); 
     statBOX.setPrototypeDisplayValue("Portugalsko/E"); 
     statBOX.setFont(normal); 
     statBOX.setSelectedIndex(0); 
     statBOX.setToolTipText("Vyber z mo\u017Enost\u00ED \u0161t\u00E1t/s\u00E9riu."); 
     add(statBOX,gbc); 

     gbc.gridx=1; 
     gbc.gridy=2; 
     podpisV.add(" "); 
     podpisBOX = new JComboBox(podpisV); 
     podpisBOX.addItemListener(new ItemListener() 
     { 
      public void itemStateChanged(ItemEvent e) 
      { 
       podpis = (String)podpisBOX.getSelectedItem(); 

       if(podpis!=" " & podpis!=null) 
       { 
        nacitajVyber(tlacDoskaSQL,tlacDoskaBOX,tlacDoskaV,false,false,true); 
       } 
       else{ 
        tlacDoskaBOX.removeAllItems(); 
        } 
      } 
     }); 
     podpisBOX.setPrototypeDisplayValue("Jean-Claude Trichet "); 
     podpisBOX.setFont(normal); 
     podpisBOX.setSelectedIndex(0); 
     podpisBOX.setToolTipText("Vyber z mo\u017Enost\u00ED troch podpisov."); 
     add(podpisBOX,gbc); 

     gbc.gridx=1; 
     gbc.gridy=3; 
     tlacDoskaV.add(" "); 
     tlacDoskaBOX = new JComboBox(tlacDoskaV); 

     tlacDoskaBOX.addItemListener(new ItemListener() 
     { 
      public void itemStateChanged(ItemEvent e) 
      { 
       tlacDoska = (String)tlacDoskaBOX.getSelectedItem(); 

       if((nominal!=" " & nominal!=null) & (statH!=" " & statH!=null) & (podpis!=" " & podpis!=null) & (tlacDoska!=" " & tlacDoska!=null)) 
       { 
        zobraz.setEnabled(true); 
       } 

      } 
     }); 
     tlacDoskaBOX.setPrototypeDisplayValue("E010"); 
     tlacDoskaBOX.setFont(normal); 
     tlacDoskaBOX.setSelectedIndex(0); 
     tlacDoskaBOX.setToolTipText("Vyber z mo\u017Enost\u00ED tla\u010Dov\u00FDch dosiek."); 
     add(tlacDoskaBOX,gbc); 
} 
private void nacitajVyber(String sqlDotaz, JComboBox chr,Vector v, 
     boolean jedna,boolean dva, boolean tri) 
{ 
    try 
    { 
     Class.forName("com.mysql.jdbc.Driver"); 
     Connection conn = DriverManager.getConnection(adresa,meno,heslo); 
     PreparedStatement stmt = conn.prepareStatement(sqlDotaz); 
     if(jedna==true) 
      { 
      chr.removeAllItems(); 
     stmt.setString(1, nominal); 
     } 
     if(dva==true) 
     { 
        chr.removeAllItems(); 
      stmt.setString(1, nominal); 
      stmt.setString(2, statH); 
     } 
     if(tri==true) 
     { 
        chr.removeAllItems(); 
      stmt.setString(1, nominal); 
      stmt.setString(2, statH); 
      stmt.setString(3, podpis); 
     } 

     ResultSet rs = stmt.executeQuery(); 
      v.addElement(" "); 
      while (rs.next()) 
       {v.addElement(rs.getString(1).trim()); 
      System.out.println(rs.getString(1));} 



     validate(); 
     rs.close(); 
     stmt.close(); 
     conn.close(); 
    } 
    catch(Exception e){ 
     JOptionPane.showMessageDialog(uh,e.toString(), 
       "Chyba pripojenia", 
       JOptionPane.ERROR_MESSAGE); 
    } 

} 
} 
+6

'Vyhladat延伸Okno'哦,這很清楚。等等,不,另一件事。在以英語進行的論壇上尋求幫助時,請使用英文屬性名稱。此外,爲了更快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –

+0

我沒有得到這個問題。 –

+0

並且這段代碼不完整,我想念了幾個局部變量的聲明,簡單的這個問題是不應答的,爲了更好地幫助早日發佈一個[SSCCE](http://sscce.org/),同意@安德魯湯普森,因爲在這種情況下,我只明白基於斯洛伐克語 – mKorbel

回答

0

的GUI可能無法正確重繪。您正在爲「事件調度線程」完成所有工作。這個線程調用你的itemStateChanged。但是這個線程也負責重新繪製GUI。你正在阻止它做到這一點,因爲你正在做它在數據庫中的辛勤工作。

相反,您可以使用不同的線程來完成您的辛勤工作。

 
    public void itemStateChanged(ItemEvent e) 
    { 
     new Thread(new Runnable() { 
      public void run() { 
       .... your code... 
      } 
     }).start(); 
    } 

或者看看SwingWorker,它以更好的方式做到這一點。

另外,itemStateChanged可以爲每個選擇調用兩次 - 這可能會導致您遇到問題。使用「e.getStateChange()」來檢查剛接收的事件類型。您應該收到一個選定的和未選中的(來自舊項目)。

+0

的描述嗯,這個答案可能是正確的,也可以正確創建Runnable#線程,注意Runnable#線程比舒適,比實現SwingWorker更容易,請是否有任何降低投票的理由 – mKorbel

3
  • 的JComboBox不知道莫名其妙然後墊層矢量改變,都重新初始化數組JComboBox的對飛,但這是錯誤的方式,

  • 上運行的任何更改使用XxxComboBoxModel用於存儲Items for JComboBox

  • 爲JDBC或FileIO專注可能會有需要的所有更新併發性問題,搖擺JComponents(在這種情況下的JComboBox及其XxxComboBoxModel)must be done on EDT

  • 調用從Runnable#ThreadSwingWorkerDatabase事件,重定向這個(potentionally)硬盤和長時間運行的任務交給了Workers Thread,否則Swing GUI的會凍結或無響應(用於鼠標和鍵盤事件),直到JDBC結束

  • SwingWorkers methods publish(), process() and done()相當不錯quaranteed所有輸出西港島線在EDT

來完成,例如

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

public class ComboBoxTwo extends JFrame implements ActionListener, ItemListener { 

    private static final long serialVersionUID = 1L; 
    private JComboBox mainComboBox; 
    private JComboBox subComboBox; 
    private Hashtable<Object, Object> subItems = new Hashtable<Object, Object>(); 

    public ComboBoxTwo() { 
     String[] items = {"Select Item", "Color", "Shape", "Fruit"}; 
     mainComboBox = new JComboBox(items); 
     mainComboBox.addActionListener(this); 
     mainComboBox.addItemListener(this); 
     //prevent action events from being fired when the up/down arrow keys are used 
     //mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE); 
     getContentPane().add(mainComboBox, BorderLayout.WEST); 
     subComboBox = new JComboBox();// Create sub combo box with multiple models 
     subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4 
     subComboBox.addItemListener(this); 
     getContentPane().add(subComboBox, BorderLayout.EAST); 
     String[] subItems1 = {"Select Color", "Red", "Blue", "Green"}; 
     subItems.put(items[1], subItems1); 
     String[] subItems2 = {"Select Shape", "Circle", "Square", "Triangle"}; 
     subItems.put(items[2], subItems2); 
     String[] subItems3 = {"Select Fruit", "Apple", "Orange", "Banana"}; 
     subItems.put(items[3], subItems3); 
//  mainComboBox.setSelectedIndex(1); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     String item = (String) mainComboBox.getSelectedItem(); 
     Object o = subItems.get(item); 
     if (o == null) { 
      subComboBox.setModel(new DefaultComboBoxModel()); 
     } else { 
      subComboBox.setModel(new DefaultComboBoxModel((String[]) o)); 
     } 
    } 

    @Override 
    public void itemStateChanged(ItemEvent e) { 
     if (e.getStateChange() == ItemEvent.SELECTED) { 
      if (e.getSource() == mainComboBox) { 
       if (mainComboBox.getSelectedIndex() != 0) { 
        FirstDialog firstDialog = new FirstDialog(ComboBoxTwo.this, 
          mainComboBox.getSelectedItem().toString(), "Please wait, Searching for ..... "); 
       } 
      } 
     } 
    } 

    private class FirstDialog extends JDialog { //sipmle simulation of JDBC events, by using Swing Timer 

     private static final long serialVersionUID = 1L; 

     FirstDialog(final Frame parent, String winTitle, String msgString) { 
      super(parent, winTitle); 
      setModalityType(Dialog.ModalityType.APPLICATION_MODAL); 
      JLabel myLabel = new JLabel(msgString); 
      JButton bNext = new JButton("Stop Processes"); 
      add(myLabel, BorderLayout.CENTER); 
      add(bNext, BorderLayout.SOUTH); 
      bNext.addActionListener(new ActionListener() { 

       @Override 
       public void actionPerformed(ActionEvent evt) { 
        setVisible(false); 
       } 
      }); 
      javax.swing.Timer t = new javax.swing.Timer(1000, new ActionListener() { 

       @Override 
       public void actionPerformed(ActionEvent e) { 
        setVisible(false); 
       } 
      }); 
      t.setRepeats(false); 
      t.start(); 
      setLocationRelativeTo(parent); 
      setSize(new Dimension(400, 100)); 
      setVisible(true); 
     } 
    } 

    public static void main(String[] args) { 
     JFrame frame = new ComboBoxTwo(); 
     frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 
}