How can chang jpanel or replace the jpanel in java swing? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How can chang jpanel or replace the jpanel in java swing?

i want chang the by click a button if i click again jpanel come back?

14th Jun 2018, 1:19 AM
Rahman Rezaie 🇦🇫
Rahman Rezaie 🇦🇫 - avatar
3 Answers
+ 3
how explain more or write it code
14th Jun 2018, 8:21 AM
Rahman Rezaie 🇦🇫
Rahman Rezaie 🇦🇫 - avatar
0
Toggle the panel visibility using setVisible(true/false) method.
14th Jun 2018, 1:46 AM
Lucas Sousa
Lucas Sousa - avatar
0
Example: if you had a panel variable called "myPanel" you can just do myPanel.setVisible(true); This will make you panel stay visible. If you need hide it use myPanel.setVisible(false); then your panel will get hidden. To do do this by clicking a button you can create a button and write logic inside its "actionListener", such as myButton.addActionListener(new ActionListener(){ //your logic of toggle visibility goes here... }); You need look that you should check if your panel are visible or not before hiding/showing it up. You only will hide if panel are being shown and will only show if panel are hidden. To check this you can use the boolean method "isVisible()" such as: if (myPanel.isVisible()){ myPanel.setVisible(false); } else { myPanel.setVisible(true); } or in a single line myPanel.setVisible(!myPanel.isVisible()); Hope you understand. GL
14th Jun 2018, 6:45 PM
Lucas Sousa
Lucas Sousa - avatar