My java program has multiple classes
One Class is responsible for creating a Jframe
Another class is responsible for writing text , which will be displayed on the jframe
Then another class responsible for other functionalities and so on.
eg
new Classname.CreatedJFrame();// generate jframe
new Classname.DisplayText(); //generate text which will be displayed on jframe
new Classname.DoSomething(); this will do something else;
These above functions are being looped up.
I am taking input via jframe which will detect which key was pressed via keyboard(0-9) However the cursor does not always stays over Jframe , I have to quickly click jframe and type the input (if delayed again i have to click the jframe area to bring pointer on focus)
Is there any solution by which the focus can always be on the jframe and input can be provided any time.
Thanks in advance
Related
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 );
I have a class which extends JFrame.
The steps which I am going to do after I run my code are:
Minimize the program
Click on the icon of the program from docker to reload it
Now, I would like it to display another frame to be shown while keeping the previous frame setVisible to false. The new frame is basically a login page where after I type in a password will it show the window which was previously minimized.
I'm relatively new to Java and I'm trying to make some kind of quiz. I created 3 JFrames, all in the same package. On my main frame, there are two buttons (one for the english version and the other one for the german version). I want to switch JFrames after pressing these buttons (so that I can, by pressing "English", see and interact with my english quiz frame). Looking it up didn't help me the slightest, because I'm not really experienced with it. Is it even possible to do it like this? If not, how could I do it?
The standard approach is to use the Card Layout, which allows you to use the same JFrame as you populate it with different things at different points in your application. So initially, your JFrame would show the loading screen, then the user presses a button and you load a new set of components without discarding the current JFrame you have. In some cases, you might also need to make some size adjustments.
It is difficult to say without seeing any code, but usually, what is done is that you do something like so:
new Frame(args);
this.dispose();
The code above assumes that the constructor of Frame takes care of launching and making the components visible. The this.dispose(); disposes of the current JFrame (assuming your class extends JFrame).
You have two buttons in your frame 1 right? So first, double click the button which says "English". Lets say the variable name for that button is jButton1. Inside that button type this.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
EnglishFrame eng = new EnglishFrame();
eng.setVisible(true);
}
Then double click the other button which says "German" (jButton2). Inside that type this.
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
GermanFrame german = new GermanFrame();
german.setVisible(true);
}
this.dispose() - This will cause the jFrame window to close
Then you create an object of the other two forms. (In your case the form for English and Germany)
.setVisible(true) - This will show you the form.
Create a single Jframe window. After that create JPanels with all the compenents such as buttons, textfields and labels you want. Make sure the panel is the same size as your Jframe. Panel's work about the same as JFrame's, code wise.
This code will stitch everything together for you:
panel.setSize(Jframe.getSize()) //That wont
panel.add(button); //Just remember you need to add more code to position the buttons correctly.
//If you using netbeans builder:
//You just have to use this one line in the constructor/intialiser method
frame.add(panel); //This will add the panel to the Jframe/Window
//No need to add extra code for positioning.
If you want to swap between the panels. In the button press method, use this
frame.setContentPane(panel); //panel = panel you want to change too.
frame.repaint(); //Ensures that the frame swaps to the next panel and doesn't get stuck.
frame.revalidate(); //Ensures that the frame swaps to the next panel and doesn't get stuck.
When you first start the java application you have to set the content pane or else it will appear as a blank window.
frame.setContentPane(panel); //Starting Panel
frame.setVisible(true); //Make the frame visible
Sorry if the explanation is bad, I don't have enough time to explain it fully.
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());
I have a question, im making a application where you can first select a name from a jList in a jFrame. After you've selected a name and pressed the proceed button, a second jFrame pops up.
On this jFrame there are a couple of textfield which i want to automatically fill up with information about the selected name selected in the first jFrame.
And this information which is automatically filled in is different depending on the chosen name.
I already have the first jFrame with 4 names in a arraylist but now im stuck on the second part where the chosen name is transfered to the second frame along with the extra information which must be done automatically
I hope someone can help me, i would really appreciate it.
THank you.
Well, heres how I would do it:
Exit the current jframe
Pass the data in the arrayList to a function that creates a new JFrame
Fill the new JFrame
Creating a a Swing application with multiple JFrames can be problematic.
A better and simpler approach would be to use CardLayout and to have panels within a single frame. Your ArrayList could be a member variable in the main JFrame class and could be easily be updated with data as you progress.
See: How to Use CardLayout