2014-10-06 4 views
-1

JTabbedPane을 활용하는 프로그램이 있습니다. 한 창에서 동일한 pane의 입력을 기반으로 객체의 arrayList를 업데이트하는 버튼이 있습니다.두 개의 별도 JTabbedPane간에 ArrayList를 전달하는 방법

첫 번째 창에서 arrayList를 기반으로하는 개체 정보로 두 번째 창 업데이트 자체를 수행하고 싶습니다.

그러나 창 사이에 목록을 전달하는 방법을 잘 모르겠습니다. 첫 번째 창에서 업데이트 단추를 누르면 배열을 창 # 2로 밀어 넣을 수 있습니까?

다음은 기본 파일입니다. 두 탭

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"

private class ButtonListener implements ActionListener 
    { 
    public void actionPerformed(ActionEvent event) 
    { 
       //Add Computer to list 
       Computer comp = new Computer(); 
       comp.setBrandName(brandField.getText()); 
       comp.setPrice(Double.parseDouble(priceField.getText())); 
       comp.setMemory(Integer.parseInt(memoryField.getText())); 
       comp.setCPU(typeField.getText(), Integer.parseInt(speedField.getText())); 

       compList.add(comp); 
       } 
       stringField.setText(listString); 


       alertLabel.setText("Computer Added"); 
      } 
     } 

여기서 다른 구획되는 로직을 모두 적용 버튼 리스너를 인스턴스화 인스턴스화. 끝에있는 for 루프는 arrayList를 push 할 때 필요한 것이다. 이 목록을받은 후 목록

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); 

    } 
} 

} 
+0

분명히 방법입니다. 우리가 정확히 당신이 일하고있는 것을 볼 수 있도록 코드를 게시 해주십시오. – ControlAltDel

+0

코드가 게시되었습니다. 모든 GUI 설치를 생략했습니다 @ControlAltDel – jrounsav

+1

이 문제를 해결하는 가장 좋은 방법은 기성품 인 Swing/etc 구성 요소를 설정하고 제어하는 ​​1 클래스에서 사용하는 것입니다. 그렇게하면 List 변경시 수행해야 할 작업에 대한 논리를 모든 항목에 액세스 할 수있는 최상위 계층에서 처리 할 수 ​​있습니다. – ControlAltDel

답변

0

두 번째 창을 업데이트, 최초의 ArrayList를 업데이트하는 데 사용되는 동일한 수신기를 사용할 수있는 각 개체에 대한 체크 박스 상자를 채 웁니다. 예 :

jButton1.addActionListener(new ActionListener() { 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     // Update the first ArrayList 
     // Update the second pane 
    } 
관련 문제