2011-04-09 12 views
2

2つのJPanelを含むトップレベルコンテナ(JFrame)があります。 JPanelの子の1つに、変更されるプロパティがあり、他のJPanelコンポーネント(JProgressBar)のいずれかで更新をトリガする必要があります。親コンポーネント(JFrame)から子オブジェクトにアクセスする方法

プロパティの変更が発生するJPanelからこのコンポーネントにアクセスするにはどうすればよいですか?これが正しい方法でない場合は、プロパティーの変更を伝播する他の方法がありますか?

答えて

3

何らかの種類のObserverを使用する必要があります。ここでは、JFrameにパネルのプロパティの変更を認識させ、それに応じて他のパネルを更新する簡単な例を示します。あなたのデザインが複雑になり、お互いの変更を認識しなければならない多くの異なるコンポーネントがある場合は、mediator patternを考慮する必要があります。

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 


public class ObserverFrame extends JFrame { 

    Panel1 panel; 
    JPanel panel2; 
    JLabel label; 

//to kick off the example 
public static void main(String args[]){ 
    new ObserverFrame();   
} 

//constructor of the JFrame 
public ObserverFrame(){ 
    super("Observer Example"); 

    //the panel that we want to observe 
    //notice that we pass a reference to the parent 
    panel = new Panel1(this); 
    add(panel, BorderLayout.NORTH); 

    //another panel to be updated when the property changes 
    panel2 = new JPanel(); 
    label = new JLabel("Panel to be updated"); 

    panel2.add(label); 

    add(panel2, BorderLayout.SOUTH); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setSize(500, 500); 
    setVisible(true); 
} 

//this method will be called by the panel when the property changes. 
public void trigger(Panel1 panel) { 
    label.setText(String.valueOf(panel.getProperty())); 
} 


//inner class for convenience of the example 
class Panel1 extends JPanel{ 
    ObserverFrame parent; 
    JButton b1; 
    private int property; 

    //accept the parent 
    public Panel1(ObserverFrame p){ 
     this.parent = p; 
     b1 = new JButton("Click Me"); 
     //click the button to change the property 
     b1.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e){ 
       setProperty(getProperty() * 2); //update property here 
      } 
     }); 
     property = 10; 
     add(b1); 
    } 

    //the property that we care about 
    public int getProperty() { 
     return property; 
    } 

    //when the setter is called, trigger the parent 
    public void setProperty(int property) { 
     this.property = property; 
     parent.trigger(this); 
    } 


    } 

} 
関連する問題