How to transfer message between multiple JPanels? - java

I have a JFrame containing three JPanel. The first JPanel contains a JTextField and a JButton. Once the JButton pressed, a JLabel at the second JPanel can show the text input from the JTextField. And then, the third JPanel will change its background according to the JLabel at the second JPanel.
My question is:
How to access the content of JTextField at the first JPanel and then transfer it to the other two JPanel?

you can create
Constructor
Control
please carrefully read all comments by #Hovercraft Full Of Eels to both options

So you have three panels:
JPanel panel1;
JTextField textFieldOnFirstPanel;
JButton buttonOnFirstPanel;
JLabel labelOnSecondPanel;
JPanel panel2;
JPanel panel3;
Keep a reference to all these three panels and all the components in you main object, this could be your JFrame Object itself.
Based on the events, update these components accordingly.

You will first store the data from the first text field in a variable. You could do this in the actionPerformed method when the button is pressed.
Following this you use the setText function to change your JLabel's text.
And last you change the JPanel color by calling its setBackground method.
String text = textField.getText();
label.setText(text);
myJPanel.setBackground(Color.white);

I think that the most clean way to achieev your goal is to access the getter of the field text after receiving notifications from changes as enabled by the classical Observer/Observable pattern. You may have here for details about this pattern .
http://en.wikipedia.org/wiki/Observer_pattern
My 2 pieces
Jerome

Related

How can I use an ActionListener on a JButton to change the background of a JLabel component inside of a JPanel container?

I am working with a JFrame Gui, and I have not found a way to change the background of a JLabel using the event handler actionlistener. The main problem is that I have a JPanel created with 4 JLabels inside. I am unsure why I'm not able to use the JLabel variables that are inside the JPanel container. I've tried creating a field for the JLabel, but it returns null when I try to use the .getBackground() method. I've also tried getting the components of the JPanel using a for loop, and changing the labels through that. So far nothing, hopefully this question makes sense, please help me understand this.
https://i.stack.imgur.com/Hnoj5.png
This image shows the refactored method that has my JPanel container with its 4 JLabel components.
https://i.stack.imgur.com/Gw7Xs.png
This image shows the actionlistener part of my code.
why don't you declare a jframe first? Example
JFrame frame = new JFrame();
and then you create a JPanel after that.
JPanel panel = new JPanel();
and then add your jlabel and stuff in the jpanel and then that is when you call your jframe.
example
frame.add(panel);
i have the same project before the only difference about our problem is that I forgot to use the JPanel, but I have a JFrame. Create a JFrame first.

Array JLabel ActionListener multiple JPanels

I have an array of JLabels and I want to add an ActionListener to them. Every label should display a panel and the other should be removed. How can I realize this?
You cannot directly add an ActionListener to a JLabel - it doesn't have that functionality. Instead, you should create a MouseAdapter, override the mouseClicked method, and use JLabel.addMouseListener to add it to your JLabels. The best way to get it to, as you say, "display a panel and the other should be removed" would be to use a CardLayout.
I have an array of JLabels and I want to add an ActionListener to
them.
JLabel doesn't have ActionListener support. You can use a undecorated JButton instead
Every label should display a panel and the other should be removed.
How can I realize this?
Use a CardLayout

Get focus on a JTextField from another panel when a CardLayout changes

hi need help on a Swing application that I am doing. I have a dialog with two panels, the first panel has a CardLayout and the second has a FlowLayout. The first layout has buttons that change the card layout and the buttonclick is entered to a specific textfield on the second panel. Every time the card layout changes, the textfield on the second panel loses its focus. How to get the focus of a specific textfield of the second panel?
when you are clicking on a button to go anohter panel
write the code inside the "actionPerformed" of that button
actionPerformed(ActionListenet al)
{
//code..
textField.requestFocus();//what the textfield u wnat
}
refer this link Setting the focus to a text field

Related to GUI creation using Swing in Java

I am facing problem during event handling.
The problem is like this:
My GUI has mainPanel (JPanel) which in turn consists of a panel with three buttons (namely btn1, btn2, btn3) at its WEST position.
I have created 3 more panels namely pnl1, pnl2, pnl3 (each panel has one label and one text area) using three different functions of same class.
My requirement is that if I click btn1 / btn2 / btn3 then pnl1 / pnl2 / pnl3 respectively must appear at mainPanel's CENTER position.
You will have to add action listener to the btn1, btn2, btn3. Then when the respective button is clicked you write a function that will display the required respective panels to the center of GUI.
To do so you can use cardLayout.
If you add detail to your question then we can help you with better answer or suggestions.
What's the purpose of the label and text area?
Add a screenshot of your GUI and some code that you have written.
Sounds like you want to put a CardLayout in the center, here's a tutorial.
You could consider creating a JPanel with a CardLayout for the CENTER panel. The CardLayout could contain 4 UI's ( the pnl1,pnl2,pnl3 and an empty panel ), and clicking on those buttons could activate the correct panel on the CardLayout

setBorder on JTextField does not work ? or does it?

The code is like this:
JTextField txt = new JTextField();
txt.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.red));
However the text field is ignoring my call to setBorder.
No changes whatsoever.
I were to replace it with a JLabel (for instance)
JLabel txt = new JLabel();
txt.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.red));
I would see the red border.
Can anybody tell me why? Or even better explain to me how to add a border in the JTextField?
Check out this explanation/recommendation from the Java API
In general, when you want to set a
border on a standard Swing component
other than JPanel or JLabel, we
recommend that you put the component
in a JPanel and set the border on the
JPanel.
So... you should nest your JTextField in a JPanel or JLabel, and put the border on the JPanel or JLabel. Voila!

Categories