I'm trying to create a virtual numpad in Java. The objective is to click a button (0...9) and input is reciprocated in another application with highlighted text field like notepad or calculator. In order to do that I used the robot class but it doesn't work. My code goes like this (for button click 0):
if(e.getSource()==b0){
java.awt.Robot r = new java.awt.Robot();
gui.setFocusable(false);
r.keyPress(KeyEvent.VK_0);
gui.setFocusable(true);
}
I've tried to make the JFrame out of focus on button click so that input goes to highlighted application. Is there any way I can always keep my application on top without giving it focus?
In order to show your frame on top always use:
Jframe.setAlwaysOnTop(true);
see more here
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 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 need some advice on how to write text into another application's line edit when that control receives mouse focus.
For example:
I opened google.com and clicked in the "Search" field (so the mouse focus is in "Search" field).
I need to programmatically write text in that field.
I have used Robot class but some symbols are not supported. Are there any ways to solve this problem?
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 had just a few class of java on college, but I want to do a thing that I don't know how.
Is basically a window with a SplitPane, on Left side I have a menu made with toggle buttons, and on the Right side I need to change the content based on each button.
Theres any way to design the ViewA and ViewB on separated JFrame Form and load then inside my Right Side when I click on menu items?
Another idea is, put the ViewA and ViewB put a JTabbedPane on the Right Side, and hide the Tabs. So there's any way to hide the tabs?
I have none experience developing in java, any problem about this concept (difficult, loading time, memory, maintenance), If you guy know a better way to to this, I just don't want a lot of windows popping up.
A really simply way would be to simply have a set of jPanels in the right side, with only one ever set to Visible.
Basically, for each toggle on the left side, you will have an Event Listener that does this:
private void toggle1ActionPerformed(java.awt.event.ActionEvent evt) {
jPanel1.setVisible(false);
jPanel2.setVisible(false);
jPanel3.setVisible(true);
}
Simply changing the true value depending on the individual toggle.
In Netbeans, if using the GUI editor, you can simply double click the toggle button to generate the listener and appropriate method, then fill in the code for it.