Adding JPanels to a JFrame - java

I'm trying to make a simple program with Java, Swing, that shows you a Window and by clicking the button "Start" it should show you a new panel with a login interface (enter username, password, login).
I've read a question asked here that says that it's better to use only one JFrame and it makes sense because I don't want to start a new window between clicking "start" button and going to the login interface. But, how should I do this? I've created two JPanel containers named loginPanel and startPanel, but I don't know how to set visible loginPanel when the application runs, and then by clicking a button (in startPanel) set visible loginPanel (and obviously stop showing startPanel).
I'm using the graphical Swing interface that comes with NetBeans (because the IDE have greyed the zone to edit the code by yourself, IDK why).
Could anyone give me a example of what I'd like to do?

Related

JFrame issue (close focus)

I have a very basic experience with java and have 2 questions.
Question 1: I have a button that opens a new JFrame, which works
perfectly. On the second iJFrame I have a button which -should- make
the app hide (lose focus).
I have looked around and found that this is easily done with JFrameName".setFocusableWindowState(false
The problem is I can't seem to be able to name the current jframe JFrame I'm in, so I can't call the function. I usually call the JFrame I've made into view with this in the public main of my starting code :
JFrameName newframe = new JFrameName();
newframe.setVisible(true);
Where exactly do I declare the name of the JFrameName instance I've made in my JFrame class so iI can call the setFocusableWindowState function?
Question 2: The above question is done because I want to link a
keyboard shortcut to a button. this keyboard shortcut should then be
used - within another window, not the java application. my question:
can iI manually define keyboard shortcuts (for example
control+alt+delete) or (control+f1) within java so my program will
execute the button hits for me?
I'm not sure I understand the question wholly. But maybe this will help.
Instead of making a JFrame, since it is not a "final" class, you also have the option of creating a new class that extends JFrame. The new class can have additional methods. One of those methods could be one to allow you to tell the new JFrame the name of the JFrame that you want to return focus to.
As far as I know, it's possible to create a KeyListener that can tell if a shift or alt or control key are pressed. I'd have to research the details to know for sure. A good starting point should be the tutorial How to Write a Key Listener.
I have a button which -should- make the app hide (lose focus).
If you want the window to hide then normally you would use:
window.setVisible( false ).
This is the opposite of showing the window.
The setWindowFocusableState(false) will still keep the window visible, you just won't be able to make any component on the window have focus.
The problem is I can't seem to be able to name the current jframe JFrame I'm in
That information can be found by coding the following in the ActionListener of your button:
JButton button = (JButton)e.getSource();
Window window = SwingUtilities.windowForComponent( button );
window.setVisible( false );

Multiple jPanels in a jFrame and switching the jPanels out

right now I´m trying to programm a little Tetris clone.
Therefore I want to have ONE jFrame, which should contain multiple jPanels (for the Main Menu, the game itself, Options, etc etc...).
I searched a bit and a lot of people say that one should use a CardLayout.
So I went to my NetBeans GUI Builder, made a jFrame switched it to CradLayout and added 2 Panels;
the first panel only contains a button, and the second panel contains my "game" (my graphical display of my Tetrismatrix, the graphical display of the next Block, and a exit button).
Pictures for better understanding:
The "Main Menu" is only a button that says "Start game" (can´t post more than 2 links because I´m new).
My current "Game Menu"
The structure of the "Card Layout in the NetBeans GUI Builder"
To achieve the switch between the Panels I´m using this (found after a little research):
#Action
public void cardSwitcher() {
CardLayout cl = (CardLayout) (gamePanel.getLayout());
cl.next(gamePanel);
}
Pressing the "Start Game" button then calls the method cardSwitcher().
When I now run my jFrame it starts just fine, I see my Start Game button and everything. But as soon as I press the button I get an ClassCastException.
"javax.swing.GroupLayout cannot be cast to java.awt.CardLayout"
So now my question is, can I even achieve my goal to have 1 Frame with multiple jPanels in it switching these with the CardLayout or is there a easier/better way to do this?
Thanks in advance for any help.
PS: I´m sorry for wrong spelling or bad grammar, I´m not a native speaker.
In addition if the question is already answered and I was to dumb to find the Post about it I´m going to remove this post immediately. And I´m always open for constructive critic.
Yes you can switch panels in cardLayout. You should take the cardLayout directly from the component on which you defined it (probably JFrame#getLayout()) not from panel inside the cardLayout (from what you've written I assume that gamePanel is inside cardLayout).

Java GUI Design view not showing completely but code works

I am new to Java GUI I deigned and window and menu item using java design tool.But when I want to create window for menu item say New contact I did not find an option to do that in event handler so I did it manually by coding it.But when I go to design part and click on New Contact it does not show the window I created via code.
Here is the screen shot of deign view -when I click on New Contact nothing happens.
Now in the source code when I run it I get the window I coded
Is there any possible way I can make it work in design part? I did not find any option to do it in Add Event Handler
You do not want a JFrame to show another JFrame -- that's a bad GUI design since it means that your application is actually two applications. Better to show a dialog window such as a JDialog. Please see The Use of Multiple JFrames, Good/Bad Practice?
If you want to design a 2nd window, create a new Java program in NetBeans, one that creates this second window (again, better for it to be a JDialog, not a JPanel)
Give it a constructor that allows passing in the parent window, and then pass that to the JDialog super constructor
And then in your ActionListener code above, create a new object of this new program, passing in the current JFrame.
In the future, please post code as code-formatted text, not as an image, since this way we can copy, paste, compile and run it if we want, allowing us to better understand your code and your problem.

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.

Closing a JFrame from a JPanel

I have a JFrame with a login(JPanel) which can be selected from a menubar. The menubar also shows options depending on users roles. When a user first logs in an instance of the JFrame is created, my problem comes when i select login on the menubar and instanciate en new JFrame since i obviously have 2 JFrames open and i can't find a way to hide or close the previous JFrame from the JPanel. Is there any way to hide/dispose all open windows right before i instanciate a new JFrame or any other possible solution? Thanks in advance for your time!
Start by taking a look at The Use of Multiple JFrames, Good/Bad Practice?.
Instead of using a second JFrame, using a JDialog of some kind to show the login window. A modal dialog will block the caller when the dialog is made visible, allowing the code to continue once it's closed. This is very helpful for gain information from the user...
For example, use this to gather the credentials (and possibly authenticate) the user and allow the caller to extract the results when the dialog is closed.
Have a look at How to Make Dialogs for more details

Categories

Resources