I am switching on button click to new JPanel with the use of:
JPanel newP = new ProjectPage();
contentPane.revalidate();
setContentPane(newP);
Where ProjectPage is:
public class ProjectPage extends JPanel
But how I create button in my new ProjectPage class that will take me back to my original panel?
My main screen class declared like so:
public class MainScreen extends JFrame
Use a CardLayout, as shown here.
See How to Use CardLayout for details.
As more general advice, do not extend JPanel or JFrame, but simply keep references to them & build them as needed for each use. Part of this problem seems to be scope - the relevant panels are not 'visible' to the calling code. Keeping references to them in the main class is an easy solution to that.
If you want to go back to the original JPanel, you have to keep a reference of it somewhere in your class by adding a field private JPanel oldPanel;
When you create your new panel, get the old Panel and save it in that field like:
oldPanel = getContentPane();
JPanel newP = new ProjectPage();
contentPane.revalidate();
setContentPane(newP);
and when you want to go back to your original panel, you do:
setContentPane(oldPanel);
Related
I've searched and could not find the answer I need to do the following: I have two java files: one JFrame, one JPanel. I configured a button in the JFrame to open up the JPanel from within the main frame with a new size of 800,800. Now, I want to close the JPanel and go back to the original JFrame (the one that originally was at size 500,500 with an image). It seems simply straightforward, but I've created an instance of the main frame from within the JPanel and set the jPanel to (this.setVisible(false)). I created a new jFrame object and set its visibility to true. What happens is, a new instance of the JFrame appears alright, but the JFrame at 800,800 with no image still appears as well. I've tried several configurations of getContentPane(), setContentPane() and even tried passing a JFrame parameter to the constructor of the JPanel. I'm not sure where I am going wrong with this, but any help would be much appreciated. All I want is the original JFrame with the original size and image displayed. Thank you in advance.
private void jButton_closeActionPerformed(java.awt.event.ActionEvent evt) {
this.setVisible(false);
mainMenuFrame = new MainMenuFrame();
mainMenuFrame.setVisible(true);
invalidate(); validate();
repaint();
}
you could open and close the jpanel from within your JFrame. the button would be also added to the jframe instead of the jpanel. for easier accessing use the jpanel as member variable
I'm doing a program that is composed by multiple panels in a JFrame.
I need to do every elements in differents classes (It's because in my school, we need to have every elements separeated in different classes for clean code), but every example that I see with my kind of problem, they do everything in one class.
And I think that my problem comes from having multile classes so I show you my classes.
I have a panel in wich I need to put 2 panel, here is the code :
public class Inscription extends JPanel{
private PanneauBoutons panneauBoutons = new PanneauBoutons();
private PanneauFormulaire panneauFormulaire = new PanneauFormulaire();
public Inscription(){
this.setLayout(new BorderLayout());
this.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
this.add(panneauFormulaire,BorderLayout.CENTER);
this.add(panneauBoutons,BorderLayout.SOUTH);
this.setVisible(true);
}
}
And here is the Panel panneauFormulaire :
public class PanneauFormulaire extends JPanel{
private JLabel labelMatricule;
private JTextField zoneTexteMatricule;
public PanneauFormulaire(){
this.setLayout(new GridLayout(8,2,10,10));
this.setBorder(BorderFactory.createLineBorder(Color.black));
labelMatricule = new JLabel("Matricule : ");
this.add(labelMatricule);
zoneTexteMatricule = new JTextField(30);
this.add(zoneTexteMatricule);
this.setVisible(true);
}
So the problem Inscription don't appear on the main Frame if I don't do setBounds, but I want a BorderLayout...
(I tested and with a set bounds I can see the borders, so I think that it means the panel are really added to the Frame so why without setBounds I see anything?).
And the other problem is that the panel PanneauFormulaire don't appear on the Inscription panel...
So if I miss something, can you help me? thank you
And here it is the class that extends JFrame :
public class FenetrePrincipale extends JFrame {
private Container cont;
private Inscription inscriptionForm;
public FenetrePrincipale(){
super("IESN");
setBounds(100,100,1200,960);
getContentPane().setLayout(null);
setLocationRelativeTo(null);
setResizable(false);
...
inscription.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
cont.removeAll();
inscriptionForm = new Inscription();
inscriptionForm.setOpaque(true);
cont.add(inscriptionForm);
invalidate();
repaint();
}
});
You should NOT be using a null layout and setBounds(). Swing was designed to be used with layout managers.
but when I click on an option in the menu, the current panel need to be change by another one,
Then you should be using a CardLayout.
Read the section from the Swing tutorial on How to Use CardLayout for working examples. So download the example and use it as the starting point of your project. The code will be better structured then what you currently have and it is easier to change working code than it is to fix broken code.
so why without setBounds I see anything?
That is because you set your layout to null in getContentPane().setLayout(null);.
Java containers comes with a default layout which you are allowed to set to a different one. How the components are arranged in the container are dependent on the layout you use. The layout will directly affects the location, alignment, spacing, dimension, preferredSize of the components.
However, if you choose not to use any layout (.setLayout(null)). Swing will not know how you want the components to be arranged, hence you see nothing in your content pane.
Since you wanted "absolute control" over the components, you will be expected to set the bounds (location and dimension) of each added component manually by yourself. This is why you are not seeing any components (even if you already added it) until you set the bounds for them.
Java, elements don't appear in a Panel with a GridLayout or FlowLayout, but with a setBounds they do
Every layout has their own characteristics and for some of them the order of your codes does makes a difference. Hence, I will advise you to go through what each layout can do for you. Then, depending on your needs, choose one (or a combination of a few) and study how to use it.
And here it is the class that extends JFrame :
You probably won't want to extends to a JFrame. You can always make a customized Container like JPanel and add it to the frame.
(Why would you want to paint your paintings on a frame instead of a piece of paper?)
I'm trying to reuse a JPanel inside a JDialog instead of replicating another JPanel that has the exact features. I tried removing and adding the component to the JDialog, but it's not working as I expected. What is the recommended approach to this issue?
write a class "myFeaturesJPanel" that extends jPanel having the needed features. then just add a own instance to your popUpPanel and your original panel
I have two classes, one AnalogClock class and one MainInterface class.
I've created a timeChanged method in the AnalogClock class and it gets called whenever the time has changed. My AnalogClock is basically a JPanel with a drawing. In MainInterface I setup a JFrame and add an Object of my AnalogClock.
Is it possible to change the Title of my Window whenever 'timeChanged' is called? I tried to use getParent() or getRootParent() but they don't recognise setTitle().
Use getWindowAncestor method from SwingUtilities.
//This gives you the first Window Object that contains the panel component
Window window = SwingUtilities.getWindowAncestor(panel);
//Cast it to JFrame
JFrame frame = (JFrame) window;
//Now, change the title
frame.setTitle("New Title");
The simplest way would be to pass a reference of the JFrame to the JPanel and invoking setTitle(). Using getParent(), you will have identify the proper type of the returned Container and then once you've found your JFrame reference, cast to it and call setTitle.
I usually do it according to the first suggestion.
It sounds like timeChanged should be in your MainInterface class because timeChanged needs to reference both the AnalogClock and the JFrame. The reason is that your AnalogClock probably should not be coupled to your JFrame.
I am developing a Java Desktop Application with GUI implemented in SWING.
I hava a JFrame. I have added three JPanels on that. One JPanel panel1 has a Start Button. Now I want to disable various componets on other JPanels when a user presses the start button on the panel1.
Now how can I access the components of those other panels from panel1.
I know that one approach is to first get the container of panel1
panel1.getParent();
Then get the components of the container
container.getComponents();
and use them as per need.
Q1. Is there any other way by which I can perform the same task? (I think this is the only way)
Q2. After getting the components list of the container, how to differentiate one container with other?
You can make instance variables that reference the panels when you create them, and use those variables to reference the panels.
public class myFrame extends JFrame {
public static JPanel buttonPanel;
public static JPanel statusPanel;
public static void main(String[] args) {
buttonPanel = new JPanel();
}
}
I'd probably have a separate layer of the application -- one that holds references to the various panels and the start button -- handle this action. So, when the start button is clicked, it calls a method on some kind of Controller object; the Controller object, which has references to the other JPanels, disables the other components.
I would add an ActionListener from outside to the Start Button:
StartPanel panel1 = ...
JPanel panel2 = ....
panel1.getStartButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setEnabledTree(panel2, false);
}
}
I'd pass references to the other panels into the panel with the start button. Or simply have a method in the container which does exactly what you want and make a call to it.