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 );
Related
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?
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.
Okay so long story short, here is what is happening and what I am trying to do.
I have a class called GameGUI, this is a JFrame.
This JFrame is populated with tiles(JLabels with ImageIcons)
I use my arrow keys to move my guy around the JFrame (just updates the JLabel Images)
I made a settings option, this is a new JFrame that I setVisbile(true), make changes, then setVisible(false)
After I setVisible(false) my arrow keys no longer make my character move on GameGUI.
I have tried the following, oh which none work: (all guesses based off Googling my problem)
GameGUI gg = new GameGUI();
gg.setFocusable(true);
gg.addKeyListener(null);
gg.requestFocusInWindow();
I cannot seem to find a way to get my KeyListener to move back to the GameGUI after I open (make visible) this settings menu then close it (make invisible). I do have radio buttons within the settings menu which is why I believe it gains focus, due to a physical mouse click.
Any help on getting focus back onto GameGUI would be much appreciated!
Thanks!
5.After I setVisible(false) my arrow keys no longer make my character move on GameGUI.
Don't use a KeyListener. Swing was designed to be used with Key Bindings.
See Motion Using the Keyboard, which will explain your probable problem and give the solution using Key Bindings.
I'm trying to make a swing application that whenever you click a button it transfers from one jframe to another seamlessly.
This is what code I'm using currently to hide one jframe and display the other
private void switch() {
this.setVisible(false);
new Register().setVisible(true);
}
NOW HERE'S THE PROBLEM:
lets say the person drags the window to the center of the screen, whenever the above method is called the register jframe would open on the left hand corner, and the current open jframe would hide itself. How would I make it open where the previous jframe was. Also if there is a better way to do what I'm attempting please inform me.
You could use in the second JFrame:
setLocationRelativeTo(firstJFrame);
Or you could use
setLocation(firstJFrame.getLocation());
In my program I have two JFrame instances. When I click next button I want to show next frame and hide current frame. So I use this.setVisible(false) and new Next().setVisible(true). But in Next window if I click back button I want to set previous frame to be visible again and next frame must be ended (which means it must be exited).
Is there any special method(s) to do this? How can I do it?
Consider using CardLayout instead of hunting for how many JFrames there are. Then..
only one JFrame would be needed
any of Next/Back Actions will be only switching between cards
There are lots of examples in this forum - e.g. as shown here.
That is an odd & quirky GUI. I suggest instead to run a JFrame for the main GUI, and when the user wants to search, pop a JOptionPane (or modal JDialog) to accept the details to search for. This will not have the effect described above, but will follow the 'path of least surprise' for the end user.
If you want to destroy a JFrame releasing all associated resources you shold call dispose() method on it.
You may place your JFrames on a list data structure and keep a reference to current position according to the window you are displaying. In that way it will be easy to move to next and previous. But note that each frame added to the list will use memory and will have its state as you placed it in to the list.
If you are trying to create a wizard like UI, you should look up Sun(oracle)tutorial here.
create the instance of your main window in next() window.. and use same method which you chosed befoe to hide your main window, for example if your main window is named as gui then what we have to do is.
gui obj = new gui();
and if you click on back button now than do these also
this.setVisibility(false);
obj.setVisibility(true);
that's all you need.
good luck.