Array JLabel ActionListener multiple JPanels - java

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

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.

Adding content to a JPanel

I'm new to Java and actually designing the GUI for an application.
My main is a JFrame with 5 buttons and 1 panel which will have the "content", for the first button for example, I've designed a Jframe which has a JTabbedPane.
Now I would like to know how can I incorporate the content from that frame to the "content" panel when clicking on the button ?
I tried to use .add but I get:
java.lang.IllegalArgumentException: adding a window to a container
(seems we can't add Jframe to Jpanel).
I also tried the setVisible way but it doesn't meet what I need since it will hide the panel completely and I will get a tiny window with the buttons.
![Jframe content][1]
![Main Jframe with buttons and Jpanel to show the jframe content][2]
The code is generated by netbeans, and I forgot to mention that I did research on adding a Jframe into another Jframe but here isn't my problem at all.
I tried by changing the Jframe by JInternalFrame but clicking on button doesn't do anything.
Button has
contentPanel.add(new GestionUtilisateur());
So basically when you click on the "Gestion Utilisateur" button for example, you get that JTabbedPane that has to appear in the content area (which is blank here)
You should not be putting JFrames inside JPanels. If you have multiple panels you would like to display, depending on something like a button, look in to LAYOUTS.
In particular, it sounds like a CardLayout would work well for your needs. CardLayouts allow you to swap which panel is displayed in a frame by bringing it to the "front" of a list of panels. This would let you display your JTabbedPane on one button click, then click another to change the content pane.
JFrame can not be added in a JPanel.
use JInternalFrame
Make and hold references to JPanels containing your content. A JFrame is really just that, it's a frame (though you can add a single component to it).
You can't add a JFrame to a JPanel. If you want multiple components to be visible use layouts such as BorderLayout, GridBag, etc. Check out some of the Swing layout tutorials here.
Content should be designed as JPanel (you can design it with drag&drop just like JFrame) but if you really have to put a JFrame to JPanel for some reason, you can do it by
myJPanel.add(myJFrame.getContentPane());
however i would suggest modification of your program.

Java multiple GUI windows creation

I have made a simple GUI using a GridLayout(5,3) , it is action performed and it implements action listener as well. The are some calculation and algorithms that working according to what inputs or buttons the user provides. Everything works just fine up to this point.
At some point in my code, the user gets a pop up massage that he is correctly logged in to the system using this common method JOptionPane.showMessageDialog(....) . All i want is, after he press the OK button, is to create an additional form that pop ups, and looks similar to the one above i made with GridLayout(5,3) so that my user can store additional info about him.
I really cant get it to work, and i have no idea how to start this.
Any ideas are very welcomed! Cheers and thanks in advance :)
if add this:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
GridLayout grid=new GridLayout(10,1);
pane.setLayout(grid);
it only adds more lines to my gridlayout. And all above buttons and labels remains. How can i get rid of the previous labels and buttons?
You state:
if add this:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
GridLayout grid=new GridLayout(10,1);
pane.setLayout(grid);
it only adds more lines to my gridlayout. And all above buttons and labels remains. How can i get rid of the previous labels and buttons?
You have at least three options if you want to swap "views" on the JFrame.
If you want to use the same GUI with the same JTextComponents but have the components empty of text, then you'll need to go through your text components and call setText("") on all of them. If you want to keep the same JButtons and labels but change their text, then similarly you will need to go through all of them calling setText("something else").
If you want totally new components to replace the old ones, the most straight forward way I believe is to use a CardLayout to hold your JPanel that has all your components. When you want to swap the JPanel for another, make sure that the new JPanel has been added to the CardLayout-using JPanel and then call next() on the CardLayout object.
Another way is to manually swap out JPanels held by the JFrame's contentPane by calling removeAll() on the contentPane, then add(nextJPanel) on it, then revalidate(), then repaint().

How to transfer message between multiple JPanels?

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

How do I add a bunch of JPanels to my JFrame using Java Swing?

I generate a bunch of JPanels and then pass them into a class that extends JFrame. How do I add an indefinite number of JPanels to this JFrame. I was also reading about JScrollPane should I incorporate this somehow into the design?
Example Code:
class foo extends JPanel
{
//generate JPanels
}
class bar extends JFrame
{
//grab some amount of foo classes and put them into this JFrame and show it
}
Also is there anything I need to watch out for when showing this JFrame?
Thanks
How do I add an indefinite number of JPanels to this JFrame?
CardLayout, JDesktopPane/JInternalFrame, JTabbedPane, JScrollPane - there are a number of options.
Also is there anything I need to watch out for when showing this JFrame?
(shrugs)
Construct and show GUI components on the EDT.
pack() the GUI before setting the position and calling setVisible(true).
Don't rely on the default layouts of content panes.
Don't implement custom painting in a top level container.
..
JFrame -> JScrollPane -> fathers JPanel then you'll decide which of LayoutManager will lay your bunch of JPanels, by defalut FlowLayout, don't forget to play with PreferedSize for childsPanels

Categories