JList隐藏了所选元素

时间:2015-12-04 19:47:49

标签: java swing visibility jlist

我有一个基于DefaultListModel构建的JList。我还在列表中添加了ListSelectionListener。我不知道为什么,但每当我在列表中选择一个项目时,它会正确地选择它并突出显示/增加它所在的行,但不会显示文本。我知道我已经选择了这个项目,但我看到的只是一个条形。我对GUI一般都很新,所以任何帮助都会受到赞赏。

这是我的所有涉及GUI的代码(我认为)。就像我说的那样,我是新手,所以大部分都是自动生成的,而且非常邋。。

public GUIlab_Cowley() {
    this.setSize(480, 360);

    //options bar
    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    //file
    JMenu mnFile = new JMenu("File");
    menuBar.add(mnFile);

    //file>exit
    JMenuItem mntmExit = new JMenuItem("Exit");
    mnFile.add(mntmExit);
    mntmExit.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }
    });
    getContentPane().setLayout(new BorderLayout(0, 0));

    //additems menu
    JPanel panel = new JPanel();
    panel.setBackground(SystemColor.activeCaption);
    panel.setPreferredSize(new Dimension(125, 10));
    getContentPane().add(panel, BorderLayout.WEST);
    GridBagLayout gbl_panel = new GridBagLayout();
    gbl_panel.columnWidths = new int[]{0, 0};
    gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
    gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
    gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
    panel.setLayout(gbl_panel);

    //additem
    JButton btnNewButton = new JButton("Add Item");
    btnNewButton.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent arg0) 
        {
            try{
                item newObject = new item(textField.getText(), textArea.getText());
                listModel.addElement(newObject);
                for(int a = 0; a < listModel.getSize()-1; a++)
                {
                    item temp = (item)listModel.getElementAt(a);
                    if(temp.getName().equals(newObject.getName()) && a != -1)
                    {
                        listModel.removeElement(newObject);
                        throw new TwoManyItemsException();
                        //i looked up how to use JOptionPane at http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html
                    }
                }
            }
            catch (TwoManyItemsException e)
            {
                JOptionPane.showMessageDialog(null, "2 items in the list cannot have the same name",
                        "Error", JOptionPane.ERROR_MESSAGE);
                //i looked up how to use JOptionPane at http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html
            }

            //I found .getText() at 
            //http://stackoverflow.com/questions/5752307/how-to-retrieve-value-from-jtextfield-in-java-swing
            //original author - Harry Joy
        }
    });
    btnNewButton.setPreferredSize(new Dimension(125, 25));
    GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
    gbc_btnNewButton.insets = new Insets(0, 0, 5, 0);
    gbc_btnNewButton.gridx = 0;
    gbc_btnNewButton.gridy = 0;
    panel.add(btnNewButton, gbc_btnNewButton);

    //name the list item
    textField = new JTextField();
    textField.setPreferredSize(new Dimension(125, 20));
    GridBagConstraints gbc_textField = new GridBagConstraints();
    gbc_textField.insets = new Insets(0, 0, 5, 0);
    gbc_textField.fill = GridBagConstraints.HORIZONTAL;
    gbc_textField.gridx = 0;
    gbc_textField.gridy = 2;
    panel.add(textField, gbc_textField);
    textField.setColumns(10);

    //enter a description box
    textArea = new JTextArea();
    textArea.setSize(new Dimension(5, 1));
    textArea.setPreferredSize(new Dimension(5, 1));
    GridBagConstraints gbc_textArea = new GridBagConstraints();
    gbc_textArea.fill = GridBagConstraints.BOTH;
    gbc_textArea.gridx = 0;
    gbc_textArea.gridy = 4;
    panel.add(textArea, gbc_textArea);


    //center panel
    final JPanel panel_1 = new JPanel();
    getContentPane().add(panel_1, BorderLayout.CENTER);
    panel_1.setLayout(new BorderLayout(0, 0));

    JLabel lblNewLabel = new JLabel("Item List");
    lblNewLabel.setPreferredSize(new Dimension(41, 25));
    lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
    panel_1.add(lblNewLabel, BorderLayout.NORTH);

    //list panel
    list.setBackground(SystemColor.activeCaption);
    list.setSelectionBackground(SystemColor.activeCaption);
    list.setSelectionForeground(SystemColor.activeCaption);
    panel_1.add(list, BorderLayout.CENTER);

    JButton btnNewButton_1 = new JButton("Delete Item");
    btnNewButton_1.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent e) 
        {
            System.out.println("Tried to remove something");
            listModel.removeElementAt(list.getSelectedIndex());
            //I looked at oracle's instructions, but I ended up using this answer on stackoverflow
            //http://stackoverflow.com/questions/9402658/delete-selected-item-from-jlist
            //original author - Joop Eggen
        }
    });
    listModel.addListDataListener(new ListDataListener()
    {

        @Override
        public void contentsChanged(ListDataEvent arg0) {
            // TODO Auto-generated method stub
        }

        @Override
        public void intervalAdded(ListDataEvent arg0) {
            // TODO Auto-generated method stub
        }

        @Override
        public void intervalRemoved(ListDataEvent arg0) {
            // TODO Auto-generated method stub
            panel_1.repaint();
        }
        //i found addlistdatalistener here https://docs.oracle.com/javase/7/docs/api/javax/swing/ListModel.html
        //and followed the autogenerated stubs
    });
    panel_1.add(btnNewButton_1, BorderLayout.SOUTH);
    JPanel panel_2 = new JPanel();
    getContentPane().add(panel_2, BorderLayout.EAST);
    panel_2.setPreferredSize(new Dimension(125, 10));
    panel_2.setLayout(null);

    textPane = new JTextPane();
    textPane.setBounds(0, 0, 125, 301);
    list.addListSelectionListener(new ListSelectionListener()
    {
        public void valueChanged(ListSelectionEvent e)
        {
            int selectedIndex = list.getSelectedIndex();
            item temp = (item)listModel.elementAt(selectedIndex);
            textPane.setText(temp.getDesc());



            //http://stackoverflow.com/questions/11695454/select-item-in-list-show-text-in-text-area
            //original author - Joel
        }
    });
    panel_2.add(textPane);
}

0 个答案:

没有答案