Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-04-30
10
Medium Priority
?
307 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 800 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 92

Accepted Solution

by:
objects earned 1200 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
 
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.
Suggested Courses

773 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