2012-05-12 19 views
-1

即使這是一種有效的方法,JComboBox#setVisible也不會隱藏我的JComboBox。難道我做錯了什麼?如果不是,有沒有其他的選擇?JCombobox.setVisible(false);

+5

*「我做錯了什麼?」*是的。爲了更好地(或任何)幫助,請發佈[SSCCE](http://sscce.org/)。 –

+1

該方法按預期工作 – Robin

+0

_Ecce vis de [sscce](http://sscce.org/)infra!_ – trashgod

回答

2

既然你不顯示在發佈的SSCCE任何興趣,這裏是我證明你有問題說什麼都是假的,沒有什麼可以直到完成你發佈你的代碼。

這工作對我很好,

import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 

public class FrameTest extends JFrame implements ActionListener { 

    JComboBox test; 

    public FrameTest() { 
     setLayout(new FlowLayout()); 
     setSize(550, 100); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JButton hideJCombo = new JButton("Hide my JCombobox!"); 
     JButton showJCombo = new JButton("Show my JCombobox!"); 

     String course[] = {"This", "is", "a", "sample", "for", "StackOverflow"}; 
     test = new JComboBox(course); 

     add(hideJCombo); 
     add(test); 
     add(showJCombo); 


     hideJCombo.setActionCommand("hide"); 
     showJCombo.setActionCommand("show"); 

     hideJCombo.addActionListener(this); 
     showJCombo.addActionListener(this); 

    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new FrameTest().setVisible(true); 

      } 
     }); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 

     if ("hide".equals(e.getActionCommand())) { 
      test.setVisible(false); 
      System.out.println("hide"); 
     } else if ("show".equals(e.getActionCommand())) { 
      test.setVisible(true); 
     } 

    } 
} 
1

不知道爲什麼我花時間創建SSCCE,但此代碼正常工作。我建議你與你的代碼進行比較和搜索的差異

import javax.swing.JCheckBox; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.event.ItemEvent; 
import java.awt.event.ItemListener; 

public class ComboboxDemo { 
    private static JFrame createFrame(){ 
    JFrame result = new JFrame("ComboboxDemo"); 

    final JComboBox<String> combobox = createCombobox(); 
    result.add(combobox, BorderLayout.CENTER); 

    JCheckBox toggleVisibility = new JCheckBox("Toggle visibility"); 
    toggleVisibility.setSelected(combobox.isVisible()); 
    toggleVisibility.addItemListener(new ItemListener() { 
     @Override 
     public void itemStateChanged(ItemEvent e) { 
     combobox.setVisible(e.getStateChange() == ItemEvent.SELECTED); 
     } 
    }); 
    result.add(toggleVisibility, BorderLayout.SOUTH); 

    result.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    result.pack(); 
    return result; 
    } 

    private static JComboBox<String> createCombobox(){ 
    return new JComboBox<>(new String[]{"foo", "bar", "StackOverflow", "Hello World"}); 
    } 

    public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
     createFrame().setVisible(true); 
     } 
    }); 
    } 
} 
1

,直到您發佈的代碼沒有人能夠回答你的問題。但是,對於「替代方案」的問題,我會回答。

  1. 您可以禁用它使用「的setEnabled(假)」

  2. 如果它是一個JPanel裏面,則可以使用「刪除()」刪除方法和方法的其他重載版本

  3. 您可能可以使用「setEditable(false)」將其設置爲不可編輯。我還沒有試過它