Java Swing UI元素不会一直更新

时间:2015-04-01 14:55:18

标签: java swing combobox

我正在尝试为只有CLI界面的一些工具制作一个简单的GUI。我认为有一个窗口有多个标签将是最好的方法来解决这个问题。我遇到的问题是,在填写信息/进行选择时,某些UI元素没有被更新。特别是组合框是最大的痛苦。

它显示所有选项,我可以选择不同的选项,但在选择某些内容后,GUI并不总是更新组合框以显示新选项。例如,它从选择“1”开始,然后我将其更改为“3”,事件触发器看到已选择“3”但组合框仍显示“1”。

我已经尝试添加重新绘制和验证帧选项卡甚至组合框本身没有运气强迫它更新。任何帮助,将不胜感激。

作为一个注释,这也发生在我也有一些文本字段,从文件选择器中选择一个文件/文件夹后显示文件路径,但不是经常在任何地方

package com.test.JavaSwing;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SpringLayout;
import javax.swing.SwingUtilities;

public class TestSwing_Main {
    /**
     * start here
     * @param args
     */
    public static void main(String[] args) {
        try {
            // Run the GUI construction in the Event-Dispatching thread for thread-safety
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    Control_GUI.createAndShowGUI(); // Let the constructor do the job
                }
            });
        } catch(Exception e) {
            //bad times
        }
    }
}


class Control_GUI extends JPanel {

    private static final long serialVersionUID = 1L;

    public static TabA tab1 = new TabA();

    public Control_GUI() {
        super(new GridLayout(1, 1));

        JTabbedPane tabbedPane = new JTabbedPane();

        JComponent panel4 = tab1.makeTabPanel();
        tabbedPane.addTab("Tab Name", null, panel4, "Tool Tip");

        //Add the tabbed pane to this panel.
        add(tabbedPane);

        //The following line enables to use scrolling tabs.
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from
     * the event dispatch thread.
     */
    public static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Tool Manager");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add content to the window.
        frame.add(new Control_GUI(), BorderLayout.CENTER);

        //Display the window.
        frame.setSize(700, 400);
        frame.setVisible(true);
    }
}

class TabA {

    private JComboBox<String> process = new JComboBox<>(
            new String[] {"1","2","3","4"});

    /**
     * empty constructor
     */
    public TabA() {}

    /**
     * Get the values from the text fields and check boxes
     * and convert them into arguments 
     * @return List<String>
     */
    public List<String> toolArgs() {
        List<String> temp = new ArrayList<String>();

        return temp;
    }

    /**
     * makes the contents of the Tool tab
     * 
     * @return JComponent
     */
    public JComponent makeTabPanel() {
        /*
         * set up panel and define layout
         */
        final JPanel panel = new JPanel(false);
        SpringLayout layout = new SpringLayout();
        panel.setLayout(layout);

        /*
         * define and set up components
         */
        //choose process
        JLabel processLabel = new JLabel("Process:");
        process.setEditable(false);

        /*
         * add components to panel
         */
        panel.add(processLabel);
        panel.add(process);

        /*
         * define positioning of components
         */
        //process
        layout.putConstraint(SpringLayout.WEST, processLabel, 5, SpringLayout.WEST, panel);
        layout.putConstraint(SpringLayout.NORTH, processLabel, 5, SpringLayout.NORTH, panel);
        layout.putConstraint(SpringLayout.WEST, process, 5, SpringLayout.EAST, processLabel);
        layout.putConstraint(SpringLayout.NORTH, process, 15, SpringLayout.NORTH, panel);

        //process dropdown action on select
        process.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String temp = (String) ((JComboBox<String>)e.getSource()).getSelectedItem();
                System.out.println(temp);
            }
        });

        return panel;
    }
}

修改 所以我认为我找出了问题,或者说代码本身没有问题。我尝试在不同的计算机上运行它,而无法重现该问题。它似乎只是我正在运行的MacBook pro版本的特定内容。我不能让它发生在比我更新的模型上,但在类似于我或更老的版本上,我可以实现它。不确定究竟是什么导致了这一点。

作为参考,我使用的是~2011 13英寸MacBook Pro

0 个答案:

没有答案