Java and working with different instances - java

I have a program where I can click on a button and it calls in a new panel, making the current one false through action listener. This panel uses up the whole size of the JFrame, giving me a form to fill out and buttons to press.
Is this the correct method to do this? Or is there a better way as I am running into bugs where the newly called JPanel appears on other JPanels. I could use setVisible(false) but then certain elements such as buttons do not appear.

According to what you want, it appears to me you would probably want to look at CardLayout where you can swtich 2 or more panels in the frame.
Conceptually, each component that a CardLayout manages is like a playing card or trading card in a stack, where only the top card is visible at any time. You can choose the card that is showing in any of the following ways:
By asking for either the first or last card, in the order it was added to the container
By flipping through the deck backwards or forwards
By specifying a card with a specific name

Related

How to create dynamic wizard?

I am making project with GUI. The thing is, that I have a button and what I need to do is that after clicking this button I need to change Frame layout. For example, like when you are installing some program and you click "next" button, the Frame layout changes and you can see some different content. Basicly, dynamic wizard.
I have tried use another Frame, but it opens in another window and that is not what I want. I want to open it in the same window.
Another thing I have tried is set visibility of these components I don't want to be displayed to false, but I find it unprofessional and it is overlook in making a desing, when I have components over themselfs.
So do you guys have any idea? Thank you.
Most of the times for a wizard like GUI, you should have JFrame and a set of JPanels. In each step you can pass the shared data as constructor arguments to each panel, and when you are making one of them invisible and make another one visible, you can get some date from the previous step panel and pass it to the next step panel(if needed).
It is very common that your panels extend the JPanel and have some argument in their constructor(s). You use these data for initializing your panel and managing the state of the overall progress.
There is no a total plan for all situations. So you should decide what to do which is best fit for your case.
Try not to have multiple JFrames.
Hope this would be helpful.

How to add Transitions (Fade, dissolve etc ) in Java GUI

I am trying to make a game for my semester project. I want to show a transition when user clicks on options button of my game menu or when user clicks on credits button. I want to show transition when one panel replaces another. Is it even possible? I am using java swing library.
You should use a CardLayout to swap views (JPanels) in your GUI. To get a fade effect is not the most simple thing to do, but it can be done with a little work. I've done it successfully here where I create a special class called SwappingImgPanel that extends JPanel, and that fades one image into another using a Swing Timer. Specifically, the program does this:
The program adds all the swapping components to the CardLayout using JPanel.
It also adds a single SwappingImgPanel, a JPanel created to draw two images, one of the component that is fading out, and one of the component that is fading in.
When you swap components, you create images of the two components, the one currently visible, and the one that will next be visible.
You send the images to the SwappingImgPanel instance
You call swap() on the SwappingImgPanel instance.
The SwappingImgPanel will then draw both images but uses a Swing Timer to change the Graphic object's composite value. This is what causes an image to be partially visible.
When the SwappingImgPanel's Timer is done, a done() method is called which sets the SwappingImgPanel's State to State.DONE.
The main GUI is listening to the SwappingImgPanel's state value, and when it achieves State.DONE, the main GUI shows the actual next component (and not an image of it).

Making multiple frames in a single program

I always wondered on how can I make a program with multiple JFrames. I mean I just want one class to handle all the GUIs and stuff but how can I effectively do this? A lot of tutorials say that we make JFrame by inheriting from JFrame. But what If I want many frames?
Ex:
Title of Application in one frame with some options
Menu is one frame
Main working application is one frame
Like in a game.
But I am not sure if I am pertaining to JPanel? I am completely puzzled with the 2. I just want one un-moving frame but basically the content of the frame is changing.
When I click START for example, it will change to the gaming style of frame.
you are looking for a JFrame with a CardLayout. Some background:
A JFrame is the physical window. It comes with a title bar and three buttons: minimize, maximize, and close. Think of this as a picture frame.
A JPanel is a "content holder" of sorts. Typically, you put your other components (buttons, animations, whatever) on a JPanel, and then slap that JPanel into a JFrame. Using our picture frame example, a JPanel would be the photo paper you put in the picture frame. The other components would then be the actual contents of the picture itself, and what you have at the end is a nice picture...or in your case, an application.
Setting the JFrame to utilize a CardLayout essentially lets you have multiple JPanels inside the same JFrame at once, while still only showing one at a time. So for your application, you would have (at least) two JPanels: one for the menu, and one for the game. When the app starts, you show the MenuPanel. When the user clicks "start", you switch to the GamePanel. The MenuPanel will be put in the background and will be inaccessible until you call it to the foreground again.
If, on the other hand, you create multiple JFrames, you will have two or more physically separate windows that can be dealt with individually. This can actually be kind of cool for game development. Although it takes more time to build and link the GUI for the second window, you can then have that window affect game settings in realtime (rate of fire, bullet strength, player speed, etc.)
I think that what you are after is the Card Layout:
A CardLayout object is a layout manager for a container. It treats
each component in the container as a card. Only one card is visible at
a time, and the container acts as a stack of cards. The first
component added to a CardLayout object is the visible component when
the container is first displayed.
You can see how it is used here.
This layout manager allows you to manage situations where your frame needs to be shared across various functions. In your case for instance, you could have a functionality to handle the settings section of the game and another one to handle the actual game itself.
You could then use the manager to switch between these particular items.
you can also use Desktopane() and InternalFrame() for multiple frame.
Internalframe quite similar to Jframe but it need to setVisible(true) or show() everytime.
Which ever IDE you are using, you can create multiple JFrames in the same package, and have separate codes for each of them.
If you want to link each frame, you will have to create instances from each JFrame. for example, if when the button is pressed, we need to invoke a new Frame (that we have already created)
NewJFrame1 frame1=new NewJFrame1();
frame1.setVisible(true);
then you can decide what to with your current JFrame.
eg : (Hide, Close)

Removing JPanel from a JPanel

I have a State Manager for a game that contains a stack of States. Now in one of my states ("Menu"), I have two JButtons in a JPanel (the JPanel is added to the main JPanel that displays everything). When I click the JButton "Play", I remove the Menu state from the stack. However, the JButtons stay on the screen (even though when i peek() I see that the Menu has been removed from the stack).
I don't want to remove the buttons from the State, because I want to be able to go back to Menu and see my buttons there again. How can I remove the buttons along with the state?
Have you considered using a CardLayout?
A CardLayout object is a layout manager for a container. It treats
each component in the container as a card. Only one card is visible at
a time, and the container acts as a stack of cards. The first
component added to a CardLayout object is the visible component when
the container is first displayed.
Assuming each state corresponds to a single JComponent, you can use the .setVisible(false); method to hide the item when the state is removed.
In this case, when you initialize the main component, you'd want to make sure all components have been added - then when you add/remove states, you can just toggle the visibility.

Trouble with Java GUI

I am creating a GUI for my project. I am completely stuck at one point. I was able to create a GUI which will make some simple queries like the release number, command name, and few other boolean questions.
Now I want the user to press CONTINUE button to move to the next page of the GUI form. I have created the continue button, but now I need to register an event to it. This is where I am stuck. I have no idea what event can I register to it which will move the GUI to next page. (By moving to the next page I mean, the different GUI pages we see when we are installing a software for our computer.)
For instance, if I am installing iTunes, I'd first select the radio button for "I accept the terms and conditions" and then I'd press the CONTINUE or the NEXT button to move ahead. If I need to come back, I'd press the BACK button.
One logical answer would be to create another GUI form and then link it to the one I created first.
EDIT:: this is the first time I am working in Java, so I might have ignored some obvious facts.
Look into using a CardLayout, adding several JPanels to the CardLayout-using container, and then to swap to the next view (the next JPanel), call the layout's next(...) method in the JButton's ActionListener. You could also randomly access components held by the CardLayout using its show(...) method.
To learn more about this including sample code, please have a look at the CardLayout Tutorial, and the API.
Well, register an ActionListener or an Action with the button. To do that wizard style, have a look at the CardLayout layout manager to switch cards or use a tabbed pane, hide the tabs and switch them inside the action or action listener.
if i understood, i think you should use the CardLayout
If you are using Swing, you can using several instances of JPanel over a one instance of JFrame.
You can have something like that structure:
JFrame
+------JPanel:root
|
+---JPanel:current // This panel change by other instance
|
+---JPanel:controlPanel // This panel contains you button
In you button need add a ActionListener with the method addActionListener
The panel control may need changes your elements, may the text of button or remove the listener and change by other.
I hope that can help

Categories

Resources