Solved

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

Posted on 2004-04-30
10
295 Views
Last Modified: 2008-02-01
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???????
             
0
Comment
Question by:calvinklein1
  • 4
  • 3
  • 3
10 Comments
 
LVL 4

Expert Comment

by:91mustang
ID: 10961663
>>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
 

Author Comment

by:calvinklein1
ID: 10962091
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
 
LVL 4

Assisted Solution

by:91mustang
91mustang earned 200 total points
ID: 10963289
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
 
LVL 92

Accepted Solution

by:
objects earned 300 total points
ID: 10964623
>  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
 

Author Comment

by:calvinklein1
ID: 10967438
anyone ever tried using observable/observer before?   maybe apply it to this example.   none of the the above examples are working for me.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 4

Expert Comment

by:91mustang
ID: 10968488
>>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
 
LVL 92

Expert Comment

by:objects
ID: 10969324
> anyone ever tried using observable/observer before?

yes, but its not really applicable here.
0
 
LVL 92

Expert Comment

by:objects
ID: 10969416
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
 

Author Comment

by:calvinklein1
ID: 10974461
Thank you everyone for your help on this question!!!
0
 
LVL 92

Expert Comment

by:objects
ID: 10974503
:)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now