Link to home
Start Free TrialLog in
Avatar of calvinklein1
calvinklein1

asked on

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???????
             
Avatar of 91mustang
91mustang

>>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");
Avatar of calvinklein1

ASKER

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???
SOLUTION
Avatar of 91mustang
91mustang

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
anyone ever tried using observable/observer before?   maybe apply it to this example.   none of the the above examples are working for me.
>>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.
> anyone ever tried using observable/observer before?

yes, but its not really applicable here.
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.
Thank you everyone for your help on this question!!!