2017-02-10 80 views
0

當用戶在表單中寫入東西時,我必須在面板中添加dinamically jcheckboxes。這是我的代碼Dinamically在jpanel中添加jcheckbox

主要

public class EmptyFrame extends JFrame{ 
    private static final long serialVersionUID = 1L; 

    /*top panels*/ 
    //to add technicians 
    private JButton newTechnician; 
    private NewTechForm ntForm; 
    private JPanel panelForm; 
    private JPanel panelChekBoxes; 
    TechCheckBoxGroup techniciansGroup; 
    private List<String> technicians; 
    //main container 
    private Container pane = getContentPane(); 
    //components 
    GroupLayout gl = new GroupLayout(pane); 

    EmptyFrame(){ 
     preinit(); 
     init(); 
    } 
    private void preinit(){ 
     panelChekBoxes=new JPanel(); 
     panelForm=new JPanel(); 
     techniciansGroup=new TechCheckBoxGroup(panelChekBoxes); 
    } 
    private void init(){ 
     /*top options*/ 
     ntForm=new NewTechForm(panelForm);    
     newTechnician=new JButton("Add technician"); 
     newTechnician.addActionListener(
         new AddTechnicianAction(techniciansGroup,ntForm) 
        ); 
     ntForm.getPanel().add(newTechnician); 
     /*end top options*/ 

     for(String technic : technicians){ 
      techniciansGroup.addCheckBoxes(
           new JCheckBox(technic));  
     } 
     createWindowLayout(
       new JLabel("Technicians"), 
       techniciansGroup.getCheckBoxes(), 
       ntForm.getPanel()); 
    } 
    public void createWindowLayout(JComponent... arg) { 

     pane = getContentPane(); 
     gl = new GroupLayout(pane); 
     pane.setLayout(gl);   
     gl.setAutoCreateContainerGaps(true); 
     gl.setAutoCreateGaps(true); 
     gl.setHorizontalGroup(gl.createParallelGroup() 
       .addGroup(gl.createSequentialGroup() 
         .addComponent(arg[0]) 
         .addComponent(arg[1]) 
         .addComponent(arg[2]) 
         ) 
     ); 
     gl.setVerticalGroup(gl.createSequentialGroup() 
       .addComponent(arg[0]) 
       .addGroup(gl.createParallelGroup() 
        .addComponent(arg[0]) 
        .addComponent(arg[1]) 
        .addComponent(arg[2])) 
      ); 
     pack(); 
    } 
    public static void main(String[] args) { 
     EventQueue.invokeLater(() -> { 
       EmptyFrame ex = new EmptyFrame(); 
       ex.setVisible(true); 
     }); 
    } 
} 

主ARE提出了一種形式,一個複選框組第一是ntForm,第二個是techniciansGroup。當我插入表單內的名字我想補充一個複選框的複選框組內,這裏的按鈕,複選框組和表單類:

AddTechnicianAction

這將是一流的,一切會發生

public class AddTechnicianAction implements ActionListener{ 

    TechCheckBoxGroup technicians; 
    NewTechForm form; 
    JTable table; 

    public AddTechnicianAction(TechCheckBoxGroup arg0, NewTechForm arg1){ 
     technicians=arg0; 
     form=arg1; 
    } 

    @Override 
    public void actionPerformed(ActionEvent arg0) { 
     System.out.println("Add new tech: "+this.form.getSurnameText().getText()+" "+this.form.getNameText().getText()); 
     technicians.addCheckBoxes(new JCheckBox(this.form.getSurnameText()+" "+this.form.getNameText())); 
     System.out.println(technicians); 
    } 

} 

NewTechForm

這是形式

public class NewTechForm { 

    private JLabel nameLabel; 
    private JLabel surnameLabel; 
    private JTextField nameText; 
    private JTextField surnameText; 
    private JPanel panel; 

    public NewTechForm(JPanel panel){ 
     nameLabel= new JLabel("Nome: ", JLabel.RIGHT); 
     surnameLabel = new JLabel("Cognome: ", JLabel.CENTER); 
     nameText = new JTextField(6); 
     surnameText = new JTextField(6); 
     this.panel=panel; 
     panel.add(nameLabel); 
     panel.add(nameText); 
     panel.add(surnameLabel); 
     panel.add(surnameText); 

    } 

    public JLabel getNameLabel() { 
     return nameLabel; 
    } 

    public JTextField getNameText() { 
     return nameText; 
    } 

    public JTextField getSurnameText() { 
     return surnameText; 
    } 

    public JLabel getSurnameLabel() { 
     return surnameLabel; 
    } 

    public JPanel getPanel() { 
     return panel; 
    } 

} 

問題是TechCheckBoxGroup內部發生了一些事情,但不是我期待的事情。面板在執行操作後有一個新的複選框,但看起來該面板(TachCheckBoxGroup中的obj)不是主類中的一個,並且實際上沒有任何東西在窗口中呈現。很明顯,我不明白關於揮杆範圍的事情,有什麼更好的做法來做我想做的事情?或者這是好方法,我想念什麼?

回答

0

我認爲堆棧溢出問題的答案很重要,所以出於這個原因,我發佈了我的問題答案,即使它不是很容易看出這是正確的解決方案,現在我解釋得更好。雖然我試圖解決錯誤行爲的問題,但我問自己,如果我是在做錯誤的考慮,那是因爲我試圖讓所有的組件沒有真正的溝通,所以我想讓所有的組件沒有真正的溝通

mediator

事實上,我不可能在主窗口中用上面寫的代碼捕獲事件。搜索和搜索我終於找到這個好回答here。所以我基本上改變了NewTechForm類,使它成爲一個帶有內部窗體的jpanel,與CheckGroupBox同樣的東西,我將它作爲一個面板,內部帶有複選框,並將所有事件發送到主窗口中的偵聽器。