在新窗口中打开JList的内容

时间:2012-12-17 02:27:26

标签: java swing jlist

我是Java的新手。

我创建了两个JLists,您可以在其中添加和删除“购物车”商品。

用户添加完所有商品后,可以点击submit按钮在新窗口中查看所选商品。

我的第一个列表是itemList(填充了数组中的项目),第二个列表是shoppinglist,其中填充了用户使用JButton选择的任何内容。

创建其他数组以处理将项目移入和移出JLists的按钮的操作。我尝试了一些方法,但是在shopinglist被点击后,submit中显示已被选中并显示在新窗口中的项目未成功。

非常感谢任何帮助。

//Create itemList
itemList = new JList(shopping);
contentPane.add(itemList);
itemList.setVisibleRowCount(10);
    itemList.setFixedCellHeight(20);
    itemList.setFixedCellWidth(140);
    itemList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);


    //Add JScrollPane to maintain size
    JScrollPane list1 = new JScrollPane(itemList);
    //contentPane.add(list1);

    //Create shoppingList
    shoppingList = new JList(items);
    contentPane.add(shoppingList);
    shoppingList.setVisibleRowCount(10);
    shoppingList.setFixedCellHeight(20);
    shoppingList.setFixedCellWidth(140);
    shoppingList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

    //Add JScrollPane to maintain size
    JScrollPane list2 = new JScrollPane(shoppingList);

    JPanel buttonPanel = new JPanel();

    //contentPane.add(list2);

    Buttonin = new JButton(">>");
    //Buttonin.setBounds(144, 46, 60, 23);
    Buttonin.addActionListener(this);
    buttonPanel.add(Buttonin);

    ButtonOut = new JButton("<<");
    //ButtonOut.setBounds(144, 80, 60, 23);
    ButtonOut.addActionListener(this);
    buttonPanel.add(ButtonOut);

    JPanel submitPanel = new JPanel();

    submitButton = new JButton("Submit");
    submitPanel.add(submitButton);

    submitButton.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent az) {
            JFrame frame = new JFrame ("Go Shopping!");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(new MyPanel2());
            frame.pack();
            frame.setVisible(true);

        }
    });


    contentPane.add(list1);
    contentPane.add(buttonPanel);
    contentPane.add(list2);
    contentPane.setOpaque(true);
    contentPane.add(submitPanel);
    return contentPane;


}




public void actionPerformed(ActionEvent e) 
{
    int i = 0;

    //When in buttonin is pressed index and value of selected item is output to array

    if (e.getSource() == Buttonin)
    {

        int[] fromindex = itemList.getSelectedIndices();
        Object[] from = itemList.getSelectedValues();

        //add items to the shoppingList
        for (i = 0; i < from.length; i++)
        {
            items.addElement(from[i]);

        }
        //Must remove items that are selected from the itemList
        for (i = (fromindex.length-1); i >= 0; i--)
        {
            shopping.remove(fromindex[i]);
        }

    }   


    //When out button is pressed index and value of selected item is output to new array

    else if (e.getSource() == ButtonOut)
    {
        Object[] to = shoppingList.getSelectedValues();
        int [] toindex = shoppingList.getSelectedIndices();

        //add items to previous list
           for(i = 0; i < to.length; i++)
            {
                shopping.addElement(to[i]);
            }
         //Must remove what's deselected
           for (i = (toindex.length-1); i >= 0; i--)
            {
                items.remove(toindex[i]);
            }
    }

好的,忍受我(非常非常新的java)这是我如何设置构造函数来引用ProfileFrame对象?如果是这样,我如何更改主要内容以反映新的构造函数?

public class GoShopping extends JPanel {

private JList shopList;

public GoShopping(ProfileFrame slist) {
//construct components


shopList = new JList(slist.getListModel());
shopList.setBounds(6, 6, 123, 166);//don't worry I'm changing the layout
add(shopList);
}


public static void main (String[] args) {
JFrame frame = new JFrame ("MyPanel");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

//I need new GoShopping to reflect the new constructor, but not sure how to make it    work
frame.getContentPane().add (new GoShopping());
frame.pack();
frame.setVisible (true);
}
}

1 个答案:

答案 0 :(得分:0)

您正在MyPanel2构造函数中创建一个新的ProfileFrame对象,因为它与可视化的ProfileFrame对象不是同一个实例,所以它的JList模型将为null。一个糟糕的解决方案是使用静态变量 - 请不要这样做。更好的解决方案是将对真实可视化ProfileFrame对象的引用传递给MyPanel2构造函数,并从此实例中调用公共方法。

同样,不要使用null布局和setBounds(...),而是使用布局管理器,除非你喜欢使事情变得比他们需要的更困难。