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.setPreferredSiz e(buttonSi ze);
viewButton.addActionListen er(new viewAction("4"));
viewPanel = new JPanel();
viewPanel.setPreferredSize (panelSize );
viewPanel.setMinimumSize(p anelSize);
viewPanel.setBorder(timeBo rder);
//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(ActionEven t 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.setPreferred Size(textB oxSize);
userTimeLabel.setAlignment X(JLabel.L EFT_ALIGNM ENT);
userTimeLabel.setText("");
userTimeLabel.setFont(boxF ont);
//Constructs panel
JPanel topPanel = new JPanel();
topPanel.setLayout(new FlowLayout());
topPanel.add(userTimeLabel );
add(topPanel);
add(topPanel);
}
public RightPanel(String slotId){
userTimeLabel.setPreferred Size(textB oxSize);
userTimeLabel.setAlignment X(JLabel.L EFT_ALIGNM ENT);
userTimeLabel.setText(slot Id);
userTimeLabel.setFont(boxF ont);
//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???????
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.setPreferredSiz
viewButton.addActionListen
viewPanel = new JPanel();
viewPanel.setPreferredSize
viewPanel.setMinimumSize(p
viewPanel.setBorder(timeBo
//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(ActionEven
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.setPreferred
userTimeLabel.setAlignment
userTimeLabel.setText("");
userTimeLabel.setFont(boxF
//Constructs panel
JPanel topPanel = new JPanel();
topPanel.setLayout(new FlowLayout());
topPanel.add(userTimeLabel
add(topPanel);
add(topPanel);
}
public RightPanel(String slotId){
userTimeLabel.setPreferred
userTimeLabel.setAlignment
userTimeLabel.setText(slot
userTimeLabel.setFont(boxF
//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???????
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
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(ActionLi stener l)
{
viewButton.addActionListen er(l);
}
That way the main panel can add itself as a listener to change the label text.
public void addActionListener(ActionLi
{
viewButton.addActionListen
}
That way the main panel can add itself as a listener to change the label text.
ASKER
Thank you everyone for your help on this question!!!
:)
What do you want to change the text only??
if so :
userTimeLabel.setText("new