Changing focused window using java - java

I am currently programming a virtual keyboard using java. As you know, when the user click the buttons on my virtual keyboard, the text will appear in some other windows(say a word document). So how can I make this action? or to be clearer, how to change the window-in-focus when the user click the button and let the text appear in the word document?
Thanks!

I hope below example will help you to develop keyboard. Few points:
1. Used JWindow rather than JFrame or any other frame, to avoid your focusing issue.
2. Used Robot to transfer key press event to active cursor.
Run below application, and make sure your cursor is on textpad, press A button on window, character a is inserted at cursor position:
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JWindow;
public class Runningwindow extends JWindow
{
public static void main(String[] args) throws AWTException
{
Runningwindow window = new Runningwindow();
window.setBackground(Color.RED);
window.setPreferredSize(new Dimension(200, 200));
window.setLayout(new FlowLayout());
JButton button = new JButton("A");
Robot r = new Robot();
int keyCode = KeyEvent.VK_A; // the A key
button.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e)
{
r.keyRelease(keyCode);
}
public void mousePressed(MouseEvent e)
{
r.keyPress(keyCode);
}
});
window.add(button);
window.pack();
window.setVisible(true);
}
}

Related

Why won't my Java Swing Keybinds work with Tab but they work fine with Space?

I'm making a game that uses Java Swing's keybinding. In that game, all the keybinds work fine, until I swap tabs and tab back in (tabs here referring to windows and not the tab key), in which case certain keys (most notably the tab key) no longer work (many other of the other keys work fine).
I made a new test class to try and debug this, but it just created a new problem where I wrote identical keybinds for tab and space, but only space works. I believe it has to do with the tab key already having a default keybind with JFrame since before, I had solved an issue with space not working due to JButton having a default space keybind. So here, I tried to unbind the tab key before adding my keybind, but it still didn't work. Here is the base code I have right now:
import java.awt.BorderLayout;
import java.awt.*;
import java.awt.Dimension;
import javax.sound.midi.Instrument;
import javax.sound.midi.MidiChannel;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Synthesizer;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;
import java.awt.event.*;
public class test1 {
static JFrame frame;
public static void main(String[] args) {
frame = new JFrame();
int IFW = JComponent.WHEN_IN_FOCUSED_WINDOW;
frame.setSize(new Dimension(400, 400));
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
// frame.pack();
JRootPane rootpane = frame.getRootPane();
frame.setLayout(null);
frame.setVisible(true);
rootpane.getInputMap(IFW).put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "space");
rootpane.getInputMap(IFW).put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "space");
rootpane.getActionMap().put("space", new spaceAction());
}
public static class Frame extends JFrame implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
public Frame() {
super("Test");
}
}
public static class spaceAction extends AbstractAction {
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("a");
}
}
}
Here, pressing space prints "a", but pressing tab doesn't.
I tried looking up solutions to this, but most of the fixes involved using JComponent.WHEN_IN_FOCUSED_WINDOW, which I do use here.
Any help?

How do I add multiple buttons to JFrame?

I need something really basic.
I tried this:
import java.*;
import javax.swing.*;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class thisthing
{
public static void main(String[]args)
{
boolean done = false;
while (!done)
{
JFrame frame = new JFrame();
JButton button= new JButton("Add Interest");
frame.add(button);
JButton button1 = new JButton("Other Button");
frame.add(button1);
class AddInterestListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.out.println("hello, I was pressed");
}
}
class OtherButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.out.println("The Other button was pressed");
}
}
ActionListener listener = new AddInterestListener();
button.addActionListener(listener);
ActionListener listener1 = new OtherButtonListener();
button1.addActionListener(listener1);
frame.setSize(100, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
}
But then my computer had trouble making two buttons on one window when I ran it, so it tried to make two windows. I really don't know how to describe it except to say that it was a mess and I did something wrong.
In the end I need to add 15 buttons in a triangle shape (the famous AP peg game project)..so is there any way I can also re-position the buttons and mess with their sizes?
You could either (recommended) use another layoutmanager (or define one on your own). Or (really NOT recommended and extremely ugly solution, but it works):
frame.setLayout(null);
JButton button = new JButton("Hello world");
button.setBounds(20 , 20 , 100 , 30);
frame.add(button);
this will position the button at the given location and with the given size. Alternatively you could use
button.setSize(100 , 30);
button.setLocation(20 , 20);
Note: without a layoutmanager, you can position your components freely, but they will by default have the bounds (0 , 0 , 0 , 0), so you will always have to set the position and size to get anything visible. I only provide this solution because you asked for it, you shouldn't use it though.

Get KeyPressed when using JCanvas

I am creating a simple Java application using JCanvas, I need to get the key code of a key pressed by the user: The following is a simplified version of the Java Code
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.EventObject;
import java.awt.event.*;
// myJavaFiles project contains JCanvas & JEventQueue Classes
import myJavaFiles.*;
import javax.swing.*;
public static void main(String[] args) {
JCanvas canvas = new JCanvas();
JEventQueue events = new JEventQueue();
events.listenTo(canvas, "canvas");
JFrame frame = new JFrame();
frame.setSize(700, 700);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setFocusable(true);
frame.add(canvas);
while (true) {
if (events.hasEvent()){
EventObject event = events.waitEvent();
if(JEventQueue.isKeyPressed(event)){
int keycode = events.getKeyCode(event);
// USE KEYCODE!!!
}
}
canvas.sleep(10);
canvas.clear();
}
}
Everything works, (I omitted a lot of the non relevant code), except for getting the key pressed, I did notice that events.hasEvent doesn't even seem to be true when I press a key!
Please help! What am I doing wrong?
It's a bit difficult without the code for JCanvas and JEventQueue, but normally I would use a key listener for a panel that contains the canvas:
//frame.add(canvas);
final JPanel panel = new JPanel();
panel.add(canvas);
frame.getContentPane().add(panel);
panel.addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(final KeyEvent keyEvent) {
System.out.println("keyEvent.getKeyCode(): " + keyEvent.getKeyCode());
}
});

How to use SystemTray correctly in Java?

I tried to implement a small (test) programm using the SystemTray in java. I got problems and wrote a (M)WE and pubushed it with http://pastebin.com/nYBkaLSy (sorry for not doing any safty checks).
What happens (under Ubuntu/KDE with openJDK):
The program starts and the button is clickable. The SystemTray is registered (all right so far)
If I click (single left mouse) on the image in the tray the button is no more clickable. Instead it seems to be disabled. A right-click on the tray icon OR the frame content (!!!) opens the popup. After closing this (clicking anywhere) the button active again. (this bahavior I cannot explain. My code does not implie this as far as I see)
If I click on the minimize button of the window manager the frame disappears (correct)
On choosing show from the popup of the tray icon restores the frame and the frame content is clickable (ok so far).
Unfortunately the popup is disabled after that. I cannot open it again and if I minimize the window again I cannot bring the window up again. (also I do not see the point in the code)
Now I am unsure if I understood the principles wrongly or if there is some other bug around. SO please help me to clear my questions.
EDIT:
I inserted the code (a bit modifie) here.
Please note that the created image is just dummy code. In my implementation I load an external image (see comments) which produces the same result. So the iconToImage() hint in the comments seems not to be a problem.
import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestTray {
public static void main(String[] args) {
// Create tmp image
Image image = new BufferedImage(32, 32, BufferedImage.TYPE_INT_RGB);
image.getGraphics().drawOval(2, 2, 30, 30);
// Alternative: Load PNG image
//URL imUrl = TestTray.class.getResource("clock.png");
//ImageIcon icon = new ImageIcon(imUrl);
//image = icon.getImage();
TrayIcon ti = new TrayIcon(image);
ti.setImageAutoSize(true);
try {
SystemTray.getSystemTray().add(ti);
} catch (AWTException e) {
e.printStackTrace();
}
// Create JFrame and set default things
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JButton("Test-Button"));
frame.pack();
frame.setVisible(true);
// Add listener to hide window in case of minimization
frame.addWindowStateListener(new WindowStateListener() {
#Override
public void windowStateChanged(WindowEvent ev) {
if(ev.getNewState() == JFrame.ICONIFIED)
frame.setVisible(false);
}
});
// Create popup for System tray and register it
PopupMenu popup = new PopupMenu();
MenuItem menuItem;
menuItem = new MenuItem("Show");
menuItem.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
frame.setVisible(true);
frame.setExtendedState(JFrame.NORMAL);
}
});
popup.add(menuItem);
menuItem = new MenuItem("Exit");
menuItem.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
popup.add(menuItem);
ti.setPopupMenu(popup);
}
}

Java JDialog messes up JMenuBar on mac

I am having some problems regarding to the JMenuBar and I cant seem to figure it out.
I will start with an abriviation of the problem: The program consists of a JFrame, a JDialog and a JMenuBar. Initially, you will get to see a JFrame with the JMenuBar in the top. But at some point, the JDialog will pop up where a user can fill in some text fields. The problem that I'm having is that as soon as the focus goes to the JDialog, the JMenuBar disappears. What I want is that the JMenuBar stays in the top of the screen all the time, except if the whole program is NOT in focus. Here are 2 screenshots, in the first screen shot, the JFrame is selected and in the other one the JDialog is selected.
So what i actually want is instead of only seeing the JMenuBar when the focus is on the JFrame, i want to see the JMenuBar all the time. Since a JDialogs can not have the JMenuBar in the top, like a JFrame has, i decided not to have multiple JMenuBars, but just the one that should be visible all the time.
At last i will give a part of the code that is as small as possible (and still working) and also contains the problem:
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;
/**
* #author Guus Leijsten
* #created Oct 27, 2012
*/
public class MenuBarProblem extends JFrame {
public MenuBarProblem() {
super("Frame");
this.setMinimumSize(new Dimension(270, 200));
this.setPreferredSize(new Dimension(800, 530));
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JRootPane root = this.getRootPane();
//Menu
JMenu fileMenu = new JMenu("File");
JMenuItem file_exit = new JMenuItem("Exit");
file_exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
file_exit.setToolTipText("Exit application");
file_exit.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
fileMenu.add(file_exit);
JMenuBar menu = new JMenuBar();
menu.add(fileMenu);
root.setJMenuBar(menu);
this.setVisible(true);
JDialog d = new JDialog(this, "Dialog");
d.setSize(200, 100);
d.setLocation(0, (int)root.getContentPane().getLocationOnScreen().getY());
d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
d.setVisible(true);
}
public static void main(String[] args) {
String os = System.getProperty("os.name").toLowerCase();
if(os.indexOf("mac") >= 0) {
System.setProperty("apple.laf.useScreenMenuBar", "true");
}
new MenuBarProblem();
}
}
If I can be honoust, i think that the problem lies in the part of JRootPane. But we'll see ;)
Did anyone else encountered this problem and managed to solve it alrady, or is there anybody that wants to give it a shot?
Thanks in advance!
Added content:
In the following example I will show a version that gives some functionality to the play.
This is the program i'm making:
The second image shows the state in which the right menu is undocked.
Obviously the JMenuBar should still be visible and operational because without it, a lot of functionalities of the program will be disabled.
At this point i'm starting to think that it is impossible for the JMenuBar to stay visible when the dialog (undocked menu) is undocked, and focussed on.
I know that the JMenuBar on a JDialog can not be in the mac osx style (top of screen), so are there any other techniques i can use for undocking, which does give me a mac osx style JMenuBar?
One key to solving this problem, pun intended, is to let a key binding share a common menu action, as shown below. Note how a menu item, your dialog's content and an (otherwise superfluous) button can all use the same Action instance. A few additional notes:
Kudos for using getMenuShortcutKeyMask().
Swing GUI objects should be constructed and manipulated only on the event dispatch thread (EDT).
System properties should be set before starting the EDT.
Make the dialog's setLocation() relative to the frame after its geometry is known.
A common Mac idiom uses the following predicate:
if (System.getProperty("os.name").startsWith("Mac OS X") {…}
See also this example.
For local use in the dialog itself, also consider JToolBar.
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
/**
* #see https://stackoverflow.com/a/13100894/230513
*/
public class MenuBarProblem extends JFrame {
private static final int MASK =
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
private static final String exitName = "Exit";
private static final KeyStroke exitKey =
KeyStroke.getKeyStroke(KeyEvent.VK_W, MASK);
private final ExitAction exitAction = new ExitAction(exitName);
public MenuBarProblem() {
super("Frame");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JMenu fileMenu = new JMenu("File");
JMenuItem fileExit = new JMenuItem(exitAction);
fileMenu.add(fileExit);
JMenuBar menu = new JMenuBar();
menu.add(fileMenu);
JDialog d = new JDialog(this, "Dialog");
JPanel p = new JPanel();
p.getInputMap().put(exitKey, exitName);
p.getActionMap().put(exitName, exitAction);
p.add(new JButton(exitAction));
d.add(p);
d.pack();
d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
this.setJMenuBar(menu);
this.pack();
this.setSize(new Dimension(320, 240));
this.setLocationByPlatform(true);
this.setVisible(true);
d.setLocation(this.getRootPane().getContentPane().getLocationOnScreen());
d.setVisible(true);
}
private static class ExitAction extends AbstractAction {
public ExitAction(String name) {
super(name);
this.putValue(Action.MNEMONIC_KEY, exitKey.getKeyCode());
this.putValue(Action.ACCELERATOR_KEY, exitKey);
}
#Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
public static void main(String[] args) {
System.setProperty("apple.laf.useScreenMenuBar", "true");
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new MenuBarProblem();
}
});
}
}
Solved!
Using a JFrame with the use of setAlwaysOnTop(true) gives me the desired effect of having a JMenuBar when the focus changes.

Categories

Resources