java - JComobox 未显示在 JDialog 中

标签 java swing

我有两个类。 当我在 addCourses() 方法中添加粗体 3 行时,对话框不会在面板中显示组合框 但是当我从 addCourses 中删除并将这些粗线放入构造函数中时,JComboBox 将显示在面板中。

但是数据不会显示,因为数据项将在创建构造函数后更新到 ComboBox。

如何解决这个问题。

<小时/>

this.mainPanel.add(courseCombo, BorderLayout.NORTH);
this.mainPanel.add(sessionCombo, BorderLayout.CENTER);
this.mainPanel.add(courseButton, BorderLayout.SOUTH);

<小时/>
public class Updator {

CourseListFrame clf = new CourseListFrame();

for(...){
      clf.addContentsToBox(displayName, className);
}

clf.addCourses();
}

第二类是

public class CourseListFrame extends JDialog implements ActionListener {

    public JPanel mainPanel = new JPanel(new BorderLayout(2, 2));
    public JButton courseButton = new JButton(("Submit"));
    public JComboBox courseCombo;
    public JComboBox sessionCombo;
    public Multimap<String, String> map;   // = HashMultimap.create();
    public static CourseListFrame courseListDialog;

    public CourseListFrame() {
        super(this.getMainFrame());
        this.getContentPane().add(mainPanel);

        map = HashMultimap.create();
        courseCombo = new JComboBox();
        courseCombo.addItem("Select Courses");
        courseCombo.addActionListener(this);
        sessionCombo = new JComboBox();
    }

    public void addContentsToBox(String course, String session) {
        map.put(course, session);
        courseCombo.addItem(course);
    }

    public void actionPerformed(ActionEvent e) {
        JComboBox cb = (JComboBox) e.getSource();
        String str = (String) cb.getSelectedItem();
        setSessionCombo(str);
    }



    public void setSessionCombo(String course) {
        if (map.containsKey(course)) {
            sessionCombo.removeAllItems();
            Iterator it = map.get(course).iterator();
            while (it.hasNext()) {
                sessionCombo.addItem(it.next());
            }
        }
    }

    public void addCourses() {
        this.mainPanel.add(courseCombo, BorderLayout.NORTH);
        this.mainPanel.add(sessionCombo, BorderLayout.CENTER);
        this.mainPanel.add(courseButton, BorderLayout.SOUTH);

    }

    public static void showCourseListDialog() {
        if (courseListDialog == null) {
            courseListDialog = new CourseListFrame();
        }
        courseListDialog.pack();
        courseListDialog.setVisible(true);
        courseListDialog.setSize(260, 180);
    }
}

最佳答案

它们不显示的原因是您可能正在调用静态 showCourseListDialog() 来显示您的对话框。此方法将测试您的静态 courseListDialog 是否为空,如果是,则创建一个并将该对话框设置为可见,而不是您实例化的 clf

如果在 showCourseListDialog() 中实例化“singleton”后调用 addCourses() 方法,则应该没问题:

public static void showCourseListDialog() {
    if (courseListDialog == null) {
        courseListDialog = new CourseListFrame();
        courseListDialog.addCourses();// <<---- this is key!
    }
    courseListDialog.pack();
    courseListDialog.setVisible(true);
    courseListDialog.setSize(260, 180);
 }

也就是说,通过使用 static courseListDialog,显然您希望该对话框成为单例。如果是这样的话,我至少会让你的构造函数私有(private)。您希望主动避免陷入可以构造单例的多个实例的情况。您仍然需要在 showCourseListDialog 中处理竞争条件,但由于您只会在 EDT 中调用此方法,因此应该是安全的。

看看this以及有关 Java 中单例开发的其他主题(不要忘记阅读 con arguments,其中将其描述为反模式)

关于java - JComobox 未显示在 JDialog 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2979062/

相关文章:

java - swing Drop 事件多个文件

java - 为什么我的 jscrollpane 在 java swing 中导致奇怪的绘画调用?

基于 Java 文本的应用程序

java - BufferedReader/Scanner 最后一行读取问题

java - 使用 Retrofit 发送 POST 请求时无法获取 POST 参数

java - 使用 Guava CacheBuilder/MapMaker 计算自定义缓存大小

java - 更新单词的属性集时,它会在 JTextPane 中抛出 IllegalStateException

java - 具有特定颜色的 JPanel 绘图

java - 如何在java中跨多个类使用变量?

PHP 的 mysql_real_escape_string() 的 Java 等价物