SalacceoVanz
SalacceoVanz

Reputation: 51

Passing values between classes

I have a JPanel form which contains a JList and some JButton. The JPanel looks like this

enter image description here

When I click the Add List button, a separate JFrame form is displayed. The JFrame form will look like this

enter image description here

When the add button on the JFrame is clicked, I need to add the value of the JTextfield (named List Name) to the JList on the previous JPanel. I wonder how to pass the value from the JFrame to the JPanel? Any suggestion would be appreciated.

Here is a code of the JPanel form (using Designer GUI)

package multimediaproject;

public class musicPlayerPanel extends javax.swing.JPanel {

    public musicPlayerPanel() {       
        initComponents();   
    }

    private void initComponents() {
          //...here is the generated code by using designer GUI       
    }                      

    // Variables declaration - do not modify                     
    //..generated code
    // End of variables declaration                   
}

Here is the code of JFrame form (using Designer GUI)

package multimediaproject;

public class addListFrame extends javax.swing.JFrame {

    public addListFrame() {
        initComponents();

        this.setLocation(515, 0);
        setVisible(true);        
    }

    private void initComponents() {
    //..here is the generated code by using Designer GUI       
    }                       

    private void addBtnActionPerformed(java.awt.event.ActionEvent evt){                                      
        //some validation
        if(...)
        {
            //validation
        }

        else
        {
           //IF VALUE IS CORRECT, ADD the List Name JTextfield value to the JList on the previous JPanel

          errorMessage.setText("");
        }

}                                      

public static void main(String args[]) {

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new addListFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    //....generated code
    // End of variables declaration                   
}

Upvotes: 0

Views: 2498

Answers (3)

nachokk
nachokk

Reputation: 14413

UPDATE with your code.

You can take advantage of PropertyChangeListener and PropertyChangeSupport (This classes implements Observer Pattern).

I give you an example you for guidance:

public class MusicPlayerPanel extends JPanel {

private JList list;
private JButton addButton;
private PropertyChangeListener listener = new MyPropertyChangeListener();

//..in some place
addButton.addActionListener(new ActionListener(){
  @Override
  public void actionPerformed(ActionEvent e){
        JFrame form = new FrameForm();
        form.addPropertyChangeListener(FrameForm.BUTTON_CLICKED,listener);
        form.setVisible(true);
  }

});

//in another place

private class MyPropertyChangeListener implements PropertyChangeListener{

    @Override
    public void propertyChange(PropertyChangeEvent evt){
         if(evt == null)
              return;

        if(evt.getPropertyName().equals(FrameForm.BUTTON_CLICKED)){
           String value = (String) evt.getNewValue();
           ((DefaultListModel)list.getModel()).addElement(value); 
        }

    }

}


}

And the frame form like this:

public class AddListFrame extends JFrame{


 private JTextField textfield;
 private JButton submitButton;
 public static final String BUTTON_CLICKED ="buttonClicked";

 // in some place
   submitButton.addActionListener(new ActionListener(){
         @Override
         public void actionPerformed(ActionEvent evt){
               firePropertyChange(BUTTON_CLICKED,null,textfield.getText());
         }

    });
}

Note: In java by convention, classes start with uppercase and follow a camel style. This is very important for readability.

Upvotes: 2

Paul Samsotha
Paul Samsotha

Reputation: 208954

"when I click the add button, a separate jFrame form is displayed. The jFrame contain a jTextfield and a submit button."

Did you seriously create an entirely new JFrame for a JTextField and a JButton?!

Have you not heard of JOptionPane? That's exactly what you are trying to replicate. The only code you need is this:

String s = JOptionPane.showInputDialog(component, message);    
model.addElement(s);

The first line will cover all your code for your custom JFrame.

Take a look at this example

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class JOPDemo extends JPanel {

    JList list;
    JButton button = new JButton("Add Name");
    String name;
    DefaultListModel model;

    public JOPDemo() {
        model = new DefaultListModel();
        list = new JList(model);

        setLayout(new BorderLayout());
        add(list, BorderLayout.CENTER);
        add(button, BorderLayout.SOUTH);



        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                name = JOptionPane.showInputDialog(this, "Enter a name");
                model.addElement(name);
            }
        });
    }

    public Dimension getPreferredSize() {
        return new Dimension(300, 300);

    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame();
        frame.add(new JOPDemo());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();

            }
        });
    }
}

Edit

What you can do is have an inner class which is your JFrame. Personally, I'd go with a JDialog though.

From comment: Have your frame as an inner class of your main class. That way it can access all the variables from your main class. You can have a String listName also in the GUI class. From the other frame when you hit add, it sets the listName in GUI class, then adds to the list.

public class GUI {
    String listName;
    JList list;
    InnerFrame inner = new InnerFrame();

    private class InnerFrame extends JFrame {
        JButton addButton;
    }
}

Upvotes: 0

shadab
shadab

Reputation: 1

OK.This is easy if i understand your problem. Step 1:Create setter and getter for your JList object reference. Step 2:On button click , when you open new JFrame pass your panel reference in constructor of class which inherits JFrame. Step 3:By doing this you are able to call getter method on panel reference. Step 4:Now you have reference of JList,do what you want.

I hope this is best solution of your problem

Upvotes: 0

Related Questions