Java 如何在两个单独的JTabbedPane之间传递ArrayList
我有一个使用JTabbedPane的程序。在一个窗格中,我有一个按钮,可以根据来自同一窗格的输入更新对象的arrayList 我希望第二个窗格根据第一个窗格中的arrayList使用对象信息更新自身 但是,我不知道如何在窗格之间传递列表。当按下第一个窗格上的“更新”按钮时,是否有办法将阵列推到窗格#2 这是主文件。实例化两个选项卡Java 如何在两个单独的JTabbedPane之间传递ArrayList,java,oop,arraylist,jtabbedpane,Java,Oop,Arraylist,Jtabbedpane,我有一个使用JTabbedPane的程序。在一个窗格中,我有一个按钮,可以根据来自同一窗格的输入更新对象的arrayList 我希望第二个窗格根据第一个窗格中的arrayList使用对象信息更新自身 但是,我不知道如何在窗格之间传递列表。当按下第一个窗格上的“更新”按钮时,是否有办法将阵列推到窗格#2 这是主文件。实例化两个选项卡 public class Assignment6 extends JApplet { private int APPLET_WIDTH = 650, APP
public class Assignment6 extends JApplet
{
private int APPLET_WIDTH = 650, APPLET_HEIGHT = 350;
private JTabbedPane tPane;
private StorePanel storePanel;
private PurchasePanel purchasePanel;
private ArrayList computerList;
public void init()
{
computerList = new ArrayList();
storePanel = new StorePanel(computerList, purchasePanel);
purchasePanel = new PurchasePanel(computerList);
tPane = new JTabbedPane();
tPane.addTab("Store Inventory", storePanel);
tPane.addTab("Customer Purchase", purchasePanel);
getContentPane().add(tPane);
setSize (APPLET_WIDTH, APPLET_HEIGHT); //set Applet size
}
}
第一个面板实例化一个按钮侦听器,该侦听器将所有逻辑应用于数组列表“compList”
这是另一个窗格。我需要将arrayList推送到末尾的for循环。收到列表后,它会为列表中的每个对象填充一个复选框
public PurchasePanel(ArrayList compList)
{
west = new JPanel();
east = new JPanel();
totalField = new JTextField();
this.compList = compList;
setLayout(new GridLayout(0,2));
add(west);
add(east);
east.setLayout(new BorderLayout());
east.add(currentTotalLabel, BorderLayout.NORTH);
east.add(totalField, BorderLayout.CENTER);
west.setLayout( new BoxLayout(west, BoxLayout.Y_AXIS));
for(Object c : compList){
System.out.println("Made it");
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String str = ("BrandName:" + (((Computer) c).getBrandName() +"CPU:" + (((Computer) c).getCPU() +"Memory:" + ((Computer) c).getMemory() + "M" +"Price:" + fmt.format(((Computer) c).getPrice()))));
JCheckBox chk = new JCheckBox(str);
west.add(chk);
}
}
}
您可以使用用于更新第一个ArrayList的侦听器来更新第二个窗格。比如:
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Update the first ArrayList
// Update the second pane
}
肯定有办法。你为什么不发布你的代码,这样我们就可以准确地看到你在用什么?发布代码。我省略了所有的GUI设置@ControlAltDel解决这个问题的最好方法是在一个类中使用现成的Swing/etc组件来设置和控制它们。这样,如果您的列表发生了更改,您需要做什么的逻辑可以在顶层处理,而顶层可以访问所有内容
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Update the first ArrayList
// Update the second pane
}