将JList添加到JPanel与JLabel不同(除了明显的原因)为什么?

时间:2012-03-02 20:57:49

标签: java swing jlabel jlist

我们有一个控制器,我们有一个预先声明的JList和JLabel,我们将其添加到JPanel。在初始布局/添加代码之外,我可以更新JLabel(例如,更改其文本),但我无法更改JList(例如jlist.setSelection(index))的选择,它将更新UI。代码如下:

public class Test {
    private JPanel myPanel;
    private JList myList;
    private JLabel myLabel;

    public Test() {
         //Some init code here...
         this.myPanel = new JPanel();
         this.myPanel.setLayout(new GridBagLayout());

         GridBagConstraints gbc = new GridBagConstraints();

         String[] values = {"Value1", "Value2", "Value3", "Value4"}; //etc. etc.
         this.myList = new JList(values);
         this.myPanel.add(this.myList, gbc); //Add to panel

         this.myLabel = new JLabel("Label1");
         this.myPanel.add(this.myLabel, gbc); //Add to panel

         //Add some code here to add it to a frame or something to display
    }


    public void updateLabel(String workingNewLabel) {

         //The following works...
         this.myLabel.setText(workingNewLabel); 
          // as in the label component in the JPanel will 
          //now be updated to the new value of workingNewLabel
    }

    public void changeSelectionInListToSomeIndex(int nonWorkingNewIndex) {

         //The following does NOT update the JList in the panel... 
         //the selection remains whatever it was last set to.

         this.myList.setSelectedIndex(nonWorkingNewIndex);
    }
}

我已经能够通过迭代myPanel中的所有组件寻找JList组件然后将其设置为myList来解决这个问题。

//Code that goes after the line this.myPanel.add(this.myList, gbc);
for(Component component : this.myPanel.getComponents() ) {
     //Iterate through it all until...
     if (component.getClass() == this.myList.getClass()) {
         this.myList = (JList) component; //cast the component as JList
     }
 }

为什么我需要为JList而不是JLabel执行此操作?这是一种解决方法,但它看起来非常糟糕。

提前致谢! -Daniel

1 个答案:

答案 0 :(得分:2)

@ JB是对的。这是一个有效的sscce

test

/** @see http://stackoverflow.com/q/9540263/230513 */
public class Test {

    private static Test test = new Test();
    private JPanel myPanel;
    private JList myList;
    private JLabel myLabel;

    public Test() {
        myPanel = new JPanel();
        myPanel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        String[] values = {"Value1", "Value2", "Value3", "Value4"};
        myList = new JList(values);
        myPanel.add(this.myList, gbc);
        myLabel = new JLabel("Label1");
        myPanel.add(this.myLabel, gbc);
        myPanel.add(new JButton(new AbstractAction("Select Value3") {

            @Override
            public void actionPerformed(ActionEvent e) {
                test.updateList(2);
            }
        }));
    }

    public void updateLabel(String label) {
        myLabel.setText(label);
    }

    public void updateList(int index) {
        myList.setSelectedIndex(index);
    }

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

            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.add(test.myPanel);
                f.pack();
                f.setLocationByPlatform(true);
                f.setVisible(true);
            }
        });
    }
}