change contents in one panel from another panel within the same frame

I have a project where you can click on a button in one panel to change the contents of another panel.   Here's a really really brief layout of what I have:

Main Frame
                Left Panel
                Center Panel
                Right Panel


Here's what's going on:   I open the program, Main frame is instantiated, along with that it also instantiantes all 3 panels.    Now once the program is completely loaded it sits and waits for a user response.    The center panel has a button which will be accompanied by an Id in the ActionListener class.  When the user clicks this button I want the contents of the Right Panel to be changed.   Everything in the Right Panel has a 'Static' state, meaning buttons, labels, textfields are all static.    

I was told that you can call that right panel from the center panel and just change the contents without having to restart the whole main frame.   Is this possible?   I was told to use the 'static' state within the right panel for the objects.     Here's some brief code:

//from the center panel

public class CenterPanel extends JPanel{
    JButton viewButton;
    JPanel viewPanel;
    JComboBox userCombo;

    Dimension buttonSize = new Dimension(60, 20);
    Dimension panelSize = new Dimension(410, 35);

    public CenterPanel(){
        viewButton = new JButton("View");
        viewButton.setPreferredSize(buttonSize);
        viewButton.addActionListener(new viewAction("4"));
        viewPanel = new JPanel();
        viewPanel.setPreferredSize(panelSize);
        viewPanel.setMinimumSize(panelSize);
        viewPanel.setBorder(timeBorder);

        //Constructs panel
        JPanel centerPanel = new JPanel();
        viewPanel.setLayout(new FlowLayout());
        viewPanel.add(viewButton);
        centerPanel.add(viewPanel);
        }

        add(centerPanel);
    }

    public class viewAction implements ActionListener{
        String slotId;
        public viewAction(String id){
            slotId = id;
        }
        public void actionPerformed(ActionEvent e) {
            DetailPanel detail = new DetailPanel();
            detail.tryMe(slotId);
        }

    }
}

//and here's from the right panel:

public class RightPanel extends JPanel {
    public static JLabel userTimeLabel = new JLabel();
    Dimension textBoxSize = new Dimension(140, 15);

    public RightPanel(){
        userTimeLabel.setPreferredSize(textBoxSize);
        userTimeLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
        userTimeLabel.setText("");
        userTimeLabel.setFont(boxFont);

        //Constructs panel
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new FlowLayout());
        topPanel.add(userTimeLabel);
        add(topPanel);

        add(topPanel);
    }

    public RightPanel(String slotId){
        userTimeLabel.setPreferredSize(textBoxSize);
        userTimeLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
        userTimeLabel.setText(slotId);
        userTimeLabel.setFont(boxFont);

        //Constructs panel
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new FlowLayout());
        topPanel.add(userTimeLabel);
        add(topPanel);
    }
}

I basically want to call the right panel from the center panel and change 'userTimeLabel.setText()'    Any suggestions???????
             
calvinklein1Asked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
>  I was told to use the 'static' state within the right panel for the objects.

You probably shouldn't do that really. You could do something like the following following from what 91mustang has said:

public class CenterPanel extends JPanel{
    RightPanel right;
    JButton viewButton;
    JPanel viewPanel;
    JComboBox userCombo;

    Dimension buttonSize = new Dimension(60, 20);
    Dimension panelSize = new Dimension(410, 35);

    public CenterPanel(RightPanel r){
        right = r;
        viewButton = new JButton("View");
        viewButton.setPreferredSize(buttonSize);
        viewButton.addActionListener(new viewAction("4"));
        viewPanel = new JPanel();
        viewPanel.setPreferredSize(panelSize);
        viewPanel.setMinimumSize(panelSize);
        viewPanel.setBorder(timeBorder);

        //Constructs panel
        JPanel centerPanel = new JPanel();
        viewPanel.setLayout(new FlowLayout());
        viewPanel.add(viewButton);
        centerPanel.add(viewPanel);
        }

        add(centerPanel);
    }

    public class viewAction implements ActionListener{
        String slotId;
        public viewAction(String id){
            slotId = id;
        }
        public void actionPerformed(ActionEvent e) {
            DetailPanel detail = new DetailPanel();
            detail.tryMe(slotId);
            r.setLabelText("whatever");
        }

    }
}


public class RightPanel extends JPanel {
    private JLabel userTimeLabel = new JLabel();
    Dimension textBoxSize = new Dimension(140, 15);

    public RightPanel(){
        userTimeLabel.setPreferredSize(textBoxSize);
        userTimeLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
        userTimeLabel.setText("");
        userTimeLabel.setFont(boxFont);

        //Constructs panel
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new FlowLayout());
        topPanel.add(userTimeLabel);
        add(topPanel);

        add(topPanel);
    }

    public RightPanel(String slotId){
        userTimeLabel.setPreferredSize(textBoxSize);
        userTimeLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
        userTimeLabel.setText(slotId);
        userTimeLabel.setFont(boxFont);

        //Constructs panel
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new FlowLayout());
        topPanel.add(userTimeLabel);
        add(topPanel);
    }

    public void setLabelText(String text){
        userTimeLabel.setText(text);
    }

}
0
 
91mustangCommented:
>>I basically want to call the right panel from the center panel and change 'userTimeLabel.setText()'    Any suggestions???????

What do you want to change the text only??

if so :

userTimeLabel.setText("new text");
0
 
calvinklein1Author Commented:
I know how to change the text.   I'm talking about changing it from another panel within the same frame.   But there should be a way without having to reinstantiate the panel by using static variables???   is that true?   does anyone know what i'm talking about???
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
91mustangConnect With a Mentor Commented:
sorry i misunderstood the quetion.
What you need to do is create a method in RightPanel that will allow you to change the value of the labels text:

public void setLabelText(String text){
        userTimeLabel.setText(text);
    }


The from you application call that method with the new text as a argument:

RightPanel right = new RightPanel();

right.setLabelText("this is new Text);//this can be called any time after right is instantiated

hope it helps
0
 
calvinklein1Author Commented:
anyone ever tried using observable/observer before?   maybe apply it to this example.   none of the the above examples are working for me.
0
 
91mustangCommented:
>>none of the the above examples are working for me.

You mean the code that myself and object suggested?  That code should work. post the code you are using, as well as any errors you recive please.
0
 
objectsCommented:
> anyone ever tried using observable/observer before?

yes, but its not really applicable here.
0
 
objectsCommented:
if you don't want the centre panel to know about the right panel then add a method to add a listener to the button:

public void addActionListener(ActionListener l)
{
   viewButton.addActionListener(l);
}

That way the main panel can add itself as a listener to change the label text.
0
 
calvinklein1Author Commented:
Thank you everyone for your help on this question!!!
0
 
objectsCommented:
:)
0
All Courses

From novice to tech pro — start learning today.