2013-07-24 94 views
1

你好大師的我是一個初學者的Java swing開發人員,我正試圖構建一個簡單的應用程序...這裏是一個簡化版本。更新組件基於哪個選項卡其調用

設置:1的JFrame,2 BorderLayout的基於標籤面板(A和B)各有1 textfiled,與組合框和的ItemListener每個選項卡中的(北)初始化

問題共享JPanel類:如何控制更新textfield的文本基於哪個面板來自例如。如果我在TabA中選擇Apples,Item Listener也會更新TabB上的TextField。我想要完成的是確定來自TabA或TabB的呼叫,並只更新與該選項卡關聯的文本字段。

希望這有一定道理

public class Main extends JFrame { 

    private static final long serialVersionUID = 1L; 
    private JPanel contentPane; 
    public JTextField tfPanelA; 
    public JTextField tfPanelB; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        Main frame = new Main(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the frame. 
    */ 
    public Main() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 450, 300); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     contentPane.setLayout(new BorderLayout(0, 0)); 
     setContentPane(contentPane);    
     JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP); 
     contentPane.add(tabbedPane, BorderLayout.CENTER);   
     JPanel panelA = new JPanel(); 
     tabbedPane.addTab("Tab A", null, panelA, null); 
     panelA.setLayout(new BorderLayout(0, 0));   
     sharedPanel s1 = new sharedPanel(this); 
     panelA.add(s1, BorderLayout.NORTH);   
     tfPanelA = new JTextField(); 
     panelA.add(tfPanelA); 
     tfPanelA.setColumns(10);    
     JPanel panelB = new JPanel(); 
     tabbedPane.addTab("Tab B", null, panelB, null); 
     panelB.setLayout(new BorderLayout(0, 0));   
     sharedPanel s2 = new sharedPanel(this); 
     panelB.add(s2, BorderLayout.NORTH); 
     tfPanelB = new JTextField(); 
     panelB.add(tfPanelB); 
     tfPanelB.setColumns(10); 
    }  
} 

//共享類...

public class sharedPanel extends JPanel { 

    private Main app; 
    private String[] clist = {"Apples","Oranges","Bananas"}; 
    /** 
    * Create the panel. 
    */ 
    public sharedPanel(final Main app) { 
     this.app=app; 
     setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));   
     JComboBox<String> comboBox = new JComboBox<String>(); 
     comboBox.setModel(new DefaultComboBoxModel<String>(clist));   
     comboBox.addItemListener(new ItemListener() { 
      public void itemStateChanged(ItemEvent e) { 
      //update PanelA's textfield ONLY if called from PanelA 
      // do this if called from PanelA 
       app.tfPanelA.setText(e.getItem().toString()); 
      // do this if called from PanelB 
       app.tfPanelB.setText(e.getItem().toString()); 
      } 
     });   
     add(comboBox);  
    }  
} 
+0

採取了這一點:'私有靜態最後長的serialVersionUID = 1L;' – pstanton

+0

備註:堅持Java命名約定和以大寫字母開頭的類名,即'SharedPanel'。 – Howard

+1

爲了更快得到更好的幫助,發佈[SSCCE](http://www.sscce.org) –

回答

1

既然你爲每個標籤創建的sharedPanel一個新實例,只需提供您想要更新它的文本字段的引用...

你的東西最終會更喜歡。 ..

public class sharedPanel extends JPanel { 

    private JTextField field; 
    private String[] clist = {"Apples","Oranges","Bananas"}; 
    public sharedPanel(final JTextField field) { 
     this.field=field; 
     setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); 

     JComboBox<String> comboBox = new JComboBox<String>(); 
     comboBox.setModel(new DefaultComboBoxModel<String>(clist)); 

     comboBox.addItemListener(new ItemListener() { 
      public void itemStateChanged(ItemEvent e) { 
       sharedPanel.this.field.setText(e.getItem().toString()); 
      } 
     }); 

     add(comboBox); 

    } 

} 

更新了一個「模範」的例子

這只是一個可能的使用模型非常簡單的例子,以彌合普通面板和其他面板之間的變化。這意味着通用面板不關心其他任何事情並更新提供的模型,該模型將事件發送給感興趣的人員,他們可以根據需要採取適當的行動。

enter image description hereenter image description here

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.event.ItemEvent; 
import java.awt.event.ItemListener; 
import java.util.ArrayList; 
import java.util.List; 
import javax.swing.DefaultComboBoxModel; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTabbedPane; 
import javax.swing.JTextField; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import javax.swing.event.ChangeEvent; 
import javax.swing.event.ChangeListener; 

public class TabbedModel { 

    protected static final String[] MAIN_LIST = {"Apples","Oranges","Bananas"}; 

    public static void main(String[] args) { 
     new TabbedModel(); 
    } 

    public TabbedModel() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JTabbedPane tabPane = new JTabbedPane(); 
       tabPane.addTab("A", new ATab()); 
       tabPane.addTab("B", new ATab()); 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(tabPane); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class DefaultCommonModel implements CommonModel { 

     private List<ChangeListener> listeners; 
     private String value; 

     public DefaultCommonModel() { 
      listeners = new ArrayList<>(25); 
     } 

     @Override 
     public void addChangeListener(ChangeListener listener) { 
      listeners.add(listener); 
     } 

     @Override 
     public void removeChangeListener(ChangeListener listener) { 
      listeners.remove(listener); 
     } 

     @Override 
     public void setValue(String aValue) { 
      if (aValue == null ? value != null : !aValue.equals(value)) { 
       value = aValue; 
       fireStateChanged(); 
      } 
     } 

     @Override 
     public String getValue() { 
      return value; 
     } 

     protected void fireStateChanged() { 

      if (!listeners.isEmpty()) { 
       ChangeEvent evt = new ChangeEvent(this); 
       for (ChangeListener listener : listeners) { 
        listener.stateChanged(evt); 
       } 
      } 

     } 

    } 

    public interface CommonModel { 

     public void addChangeListener(ChangeListener listener); 
     public void removeChangeListener(ChangeListener listener); 

     public void setValue(String value); 
     public String getValue(); 

    } 

    public class CommonPanel extends JPanel { 

     private CommonModel model; 
     private JComboBox comboBox; 

     public CommonPanel(CommonModel model) { 
      this.model = model; 
      setLayout(new GridBagLayout()); 
      comboBox = new JComboBox(new DefaultComboBoxModel<>(MAIN_LIST)); 
      comboBox.addItemListener(new ItemListener() { 
       @Override 
       public void itemStateChanged(ItemEvent e) { 
        String value = (String) e.getItem(); 
        CommonPanel.this.model.setValue(value); 
       } 
      }); 
      add(comboBox); 
     } 

    } 

    public class ATab extends JPanel { 

     private List<JTextField> fields; 

     public ATab() { 
      setLayout(new GridBagLayout()); 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridwidth = GridBagConstraints.REMAINDER; 

      DefaultCommonModel model = new DefaultCommonModel(); 
      model.addChangeListener(new ChangeListener() { 
       @Override 
       public void stateChanged(ChangeEvent e) { 
        CommonModel model = (CommonModel) e.getSource(); 
        for (JTextField field : fields) { 
         field.setText(model.getValue()); 
        } 
       } 
      }); 
      CommonPanel commonPanel = new CommonPanel(model); 

      add(commonPanel, gbc); 

      fields = new ArrayList<>(25); 
      for (int index = 0; index < random(); index++) { 
       JTextField field = new JTextField(10); 
       add(field, gbc); 
       fields.add(field); 
      } 
     } 

     protected int random() { 

      return (int)Math.round(Math.random() * 9) + 1; 

     } 

    } 

} 
+0

好吧,這是有道理的...如果可以說我有TabA和TabB上的x對象(Textfield/combobox /複選框等),我將如何處理這個...我是否在我的共享中創建多個構造函數類來處理這個? – user2611315

+0

我可能會創建一些可更新的模型,然後將事件觸發回監聽器,該監聽器可用於修改問題 – MadProgrammer

+0

中的標籤的實際組件,感謝您的快速響應MP,我是新手而不是確定你的意思是「創建一種可更新的模型」,你能詳細說明還是提供可以實現這一點的代碼片段? – user2611315

1

既然你的兩個實例,你SharedPanel您可以添加文本框將被更新爲參考構造函數:

SharedPanel s1 = new SharedPanel(this, tfPanelA); 

and

public SharedPanel(final Main app, final JTextField tf) { 
    ... 
    comboBox.addItemListener(new ItemListener() { 
     public void itemStateChanged(ItemEvent e) { 
      tf.setText(e.getItem().toString()); 
     } 
    }); 
    ... 
} 
+0

+1擊敗了我;大壩你格式化;) – MadProgrammer

相關問題