2011-07-04 9 views
2

以下のプログラムでボタンを押すと、JComboBoxのポップアップメニューが表示され、そのままになります。setPopupVisibleがsetvisible trueの後に続く場合、キャンセルされます

ただし、コンボボックスが既に表示されている場合は、2回目のプッシュでのみ表示されます。私は最初のプッシュでそれを表示させることができる方法はありますか?

public class Problem extends JPanel 
implements ActionListener{ 

private static String SEARCH = "start search"; 
private static String SELECTED = "Selected"; 

private JTextField field2; 
private JTextField field1; 

private JComboBox list = new JComboBox(); 

public Problem() { 
    super(new GridBagLayout()); 

    //Construct the panel 
    field2=new JTextField(""); 
    field1=new JTextField("7564"); 
    JButton search=new JButton("Search"); 
    search.addActionListener(this); 
    search.setActionCommand(SEARCH); 

    list.addActionListener(this); 
    list.setActionCommand(SELECTED); 


    //Add everything to this panel. 
    GridBagConstraints c = new GridBagConstraints(); 

    c.gridx = 0; 
    c.gridy = 0; 
    c.gridwidth = 8; 
    c.gridheight = 3; 
    c.fill = GridBagConstraints.BOTH; 
    c.weightx = 1; 
    c.weighty = 0; 
    c.anchor = GridBagConstraints.NORTH; 

    add(field2,c); 

    list.setVisible(false); 
    add(list,c); 

    c.gridx = 8; 
    c.gridy = 0; 
    c.gridwidth = 8; 
    c.gridheight = 3; 
    c.fill = GridBagConstraints.BOTH; 
    c.weightx = 1; 
    c.weighty = 0; 
    c.anchor = GridBagConstraints.NORTH; 

    add(field1,c); 

    c.gridx = 16; 
    c.gridy = 0; 
    c.gridwidth = 4; 
    c.gridheight = 3; 
    c.fill = GridBagConstraints.BOTH; 
    c.weightx = 1; 
    c.weighty = 0; 
    c.anchor = GridBagConstraints.NORTH; 

    add(search,c); 


} 

public void actionPerformed(ActionEvent ae) { 



    if (ae.getActionCommand()==SEARCH){ 
     String f2=field2.getText(); 
     String f1=field1.getText(); 
     if(f2.equals("")){ 

      f2=selection(f1); 
      f2=null; 
     } 
     if(f2!=null){ 
      field2.setText(f2); 
     } 
     else{ 
      System.out.println("setPopupVisible runs"); 
      field2.setVisible(false); 
      list.setVisible(true); 
      field2.setText(""); 
      list.setPopupVisible(true); 
     } 
    } 
    else if(ae.getActionCommand().equals(SELECTED)){ 
     System.out.println("select"); 
     String listtext=(String) list.getSelectedItem(); 
     list.removeAllItems(); 
     if(listtext==null||!listtext.contains(": ")){ 
      return; 
     } 
     String f2=listtext.split(": ")[0]; 
     list.setVisible(false); 
     field2.setVisible(true); 
     field2.setText(f2); 
    } 
} 

private String selection(String sn) { 
    System.out.println("selection runs"); 

    String name="7"; 
    list.removeActionListener(this); 
    list.removeAllItems(); 
     list.addItem(name); 
    list.addActionListener(this); 
    return null; 
} 

private static void createAndShowGUI() { 
    //Create and set up the window. 
    JFrame frame = new JFrame("Stuff"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    //Create and set up the content pane. 
    Problem newContentPane = new Problem(); 
    newContentPane.setOpaque(true); //content panes must be opaque 
    frame.setContentPane(newContentPane); 

    frame.pack(); 
    frame.setVisible(true); 
    frame.setSize(800, 600); 
} 

public static void main(String[] args) throws FileNotFoundException { 
    javax.swing.SwingUtilities.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      createAndShowGUI(); 
     } 
    }); 
} 
} 
+0

+1 [sscce](http://sscce.org/)。 – trashgod

答えて

2

コンポーネントの可視性を切り替えるときは、カスタムコンポーネントを実行する前にコンポーネントが内部アップデートを完了していることを確認します。これはf.iです。コンボの表示に関連するコンボのポップアップを表示する呼び出しを遅らせる:

  list.setVisible(true); 
      SwingUtilities.invokeLater(new Runnable() { 
       public void run() { 
        list.setPopupVisible(true); 
       } 
      }); 
関連する問題