public class KL implements KeyListener {
public static void main(String[] args) {
final JPopupMenu popup = new JPopupMenu();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
}
#Override
public void keyPressed(KeyEvent arg0) {
System.out.println(arg0.getKeyChar());
}
#Override
public void keyReleased(KeyEvent e) {
System.out.println(e.getKeyChar());
}
#Override
public void keyTyped(KeyEvent e) {
System.out.println(e.getKeyChar());
}
}
That's my class, it's probably something really stupid on my part, but my KeyListener here is not working. Nothing comes up on the console.
Let's start with the fact that you're not attached the listener to anything, then move on to the fact that you really should be using Key Bindings
And with example
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestTableEditing {
public static void main(String[] args) {
new TestTableEditing();
}
public TestTableEditing() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel key;
private int counter = 0;
public TestPane() {
key = new JLabel("...");
add(key);
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), "A.pressed");
am.put("A.pressed", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("A was pressed");
key.setText("A was pressed " + (++counter));
}
});
}
}
}
I know this is an old post but I wanted to put this online so someone like myself can find it....
I worked on this problem for hours before figuring it out. Make sure that your Component has focus. For example I have all of my activity going on in a custom JPanel named SpaceShipPanel:
class SpaceShipPanel
{
//instance variables
//Now my constructor
SpaceShipPanel(){
//bla bla blah
setFocusable(true);//THIS LINE IS WHAT SAVED ME!!
}
}
From what I hear, keyBindings are the best route but the class I'm taking didn't cover this topic. Hopefully this will save someone hours of beating their heads against the wall.
Related
I am testing my keyListener in java.
My system in Ubuntu 14.04. I setup a panel in main and init the key listener. I also set focusable to true and do requestFocusInWindow.
But when I run the program, the println never shows up in console. confused of that.
package key;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Key extends JPanel{
public void action(){
KeyListener k = new KeyListener(){
#Override
public void keyPressed(KeyEvent k){
System.out.println("key is pressed!");
}
#Override
public void keyReleased(KeyEvent e){
// TODO Auto-generated method stub
}
#Override
public void keyTyped(KeyEvent e){
System.out.println("key is typed!");
}
};
this.addKeyListener(k);
this.setFocusable(true);
this.requestFocusInWindow();
}
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setSize(400,300);
JPanel panel = new JPanel();
panel.setBackground(Color.BLUE);
frame.add(panel);
frame.setVisible(true);
Key k = new Key();
k.action();
}
}
any advice on that?
Use the key bindings API of KeyListener
Add k to frame before it's made visible
For example...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Key extends JPanel {
public void action() {
JLabel label = new JLabel("Waiting");
InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = getActionMap();
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, false), "press.a");
actionMap.put("press.a", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
label.setText("Pressed A");
}
});
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, true), "release.a");
actionMap.put("release.a", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
label.setText("Waiting");
}
});
add(label);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBackground(Color.BLUE);
frame.add(panel);
Key k = new Key();
k.action();
frame.add(k, BorderLayout.SOUTH);
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
I am trying to make a slideshow program. I want the next slide to appear when I click (There are only two slides not, but I will add more once the errors r sorted out).
The code compiles fine. But when i click, nothing happens.
What could possibly go wrong?
package project;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Frame extends JFrame{
Frame() {
setLayout(new BorderLayout());
ImageIcon slide = new ImageIcon("E:\\Books\\Computer\\Java\\Introduction to Java Programming\\exercise9e\\image\\slide0.jpg");
JLabel slidesLabel = new JLabel(slide);
add(slidesLabel,BorderLayout.CENTER);
slidesLabel.addMouseListener(new ClickListener());
}
public void nextSlide() {
ImageIcon slide = new ImageIcon("E:\\Books\\Computer\\Java\\Introduction to Java Programming\\exercise9e\\image\\slide1.jpg");
JLabel slidesLabel = new JLabel(slide);
add(slidesLabel,BorderLayout.CENTER);
System.out.println("x");
}
public static void main(String args[]) {
Frame frame = new Frame();
frame.setSize(800,600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}
public class ClickListener extends MouseAdapter {
#Override
public void mouseClicked(MouseEvent e) {
nextSlide();
}
}
}
So, the "main" problem is BorderLayout will only manage a single component within any of the five available locations it manages.
Adding another component into the position tends to cause issues, where the component that was first added won't be displayed, or in your case, will remain and could interfere with the new component
"A" solution would be to re-use the same JLabel for each slide, simply supply a new value for the icon property (or in this example, the text property)
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Frame extends JFrame {
private JLabel slidesLabel = new JLabel("Apple");
public Frame() {
setLayout(new BorderLayout());
add(slidesLabel, BorderLayout.CENTER);
slidesLabel.addMouseListener(new ClickListener());
}
public void nextSlide() {
slidesLabel.setText("Banana");
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
Frame frame = new Frame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ClickListener extends MouseAdapter {
#Override
public void mouseClicked(MouseEvent e) {
nextSlide();
}
}
}
This approach would allow you to place each icon into an array and simply have a counter which determines which slide is current, so when you click for the next slide, you simply increment the counter, get the next value from the array and apply it to the label
A better (and more appropriate) solution would be to actually use a CardLayout, see How to Use CardLayout for more details
With icons...
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Frame extends JFrame {
private JLabel slidesLabel = new JLabel();
private Icon[] icons;
private int currentSlide = -1;
public Frame() {
try {
// Personally, I'd use File#listFiles to list all the
// images in a directory, but that might be consider
// using our initiative...
icons = new Icon[]{
new ImageIcon(ImageIO.read(new File("..."))),
new ImageIcon(ImageIO.read(new File("..."))),
new ImageIcon(ImageIO.read(new File("...")))
};
slidesLabel.setVerticalAlignment(JLabel.CENTER);
slidesLabel.setHorizontalAlignment(JLabel.CENTER);
setLayout(new BorderLayout());
add(slidesLabel, BorderLayout.CENTER);
slidesLabel.addMouseListener(new ClickListener());
nextSlide();
} catch (IOException exp) {
exp.printStackTrace();
}
}
public void nextSlide() {
if (currentSlide < icons.length - 1) {
currentSlide++;
slidesLabel.setIcon(icons[currentSlide]);
}
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
Frame frame = new Frame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ClickListener extends MouseAdapter {
#Override
public void mouseClicked(MouseEvent e) {
nextSlide();
}
}
}
Why simply adding components to a BorderLayout is bad...
All I did was when nextSlide was called, create a new JLabel assign it the next icon and add it to the Frame (which is using a BorderLayout) and then re-sized the frame. Because the labels are transparent, they remain visible...
This is why you shouldn't simply add new components to a BorderLayout, but, in your case, simply update the properties of the existing JLabel to meet the changing needs.
I'm a super Java noob and need some help. I am trying to write a code that shows the word "on" on the JPanel for 5 (or however long I pass into y variable) seconds then changes the word to "off" on the same JPanel. Think of a stoplight that shows green for a period of time then goes to red. The code I have written below opens up two separate JFrames to display the different words. Any help or ideas would be greatly appreciated.
import javax.swing.*;
public class practice extends JFrame implements Runnable {
int x;
int y;
JLabel show = new JLabel("on");
JLabel show2 = new JLabel("off");
boolean yes;
public practice(boolean on, int x){
x=y;
yes = on;
setTitle("Stoplight");
setSize(500, 500);
setResizable(true);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void test(){
try {
Thread.sleep(y);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (yes == true){
add(show2);
}else if (yes == false)
add(show);
}
public void run() {
test();
}
public static void main (String[] args){
Thread t1 = new Thread(new practice(true, 50000));
Thread t2 = new Thread(new practice(false, 0));
t1.start();
t2.start();
}
}
You need remove the label 'on' of panel before add the label 'off' with the method remove(jcomponent)
As has already been hinted, you should use a javax.swing.Timer, which will allow you to schedule a callback after a specified period of time.
Unless you have a particular need, it's simpler to change the text of the label to have to remove the old label and add a new one (IMHO)
For example...
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
public class DynamicLabel {
public static void main(String[] args) {
new DynamicLabel();
}
public DynamicLabel() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane(5000));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel label;
public TestPane(int delay) {
setLayout(new GridBagLayout());
setBorder(new EmptyBorder(8, 8, 8, 8));
label = new JLabel("On");
add(label);
Timer timer = new Timer(delay, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
label.setText("Off");
}
});
timer.setRepeats(false);
timer.start();
}
}
}
Im trying to figure out how i can add number notifications to my dock icon when a new message is received (I've got a small chat app)
This is what i mean:
Any ideas how to accomplish this?
Try taking a look at com.apple.eawt.Application (I'm having a hard time finding JavaDocs)
import com.apple.eawt.Application;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestBadge {
public static void main(String[] args) {
new TestBadge();
}
public TestBadge() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel label;
private int count;
public TestPane() {
setLayout(new GridBagLayout());
add((label = new JLabel("0")));
Timer timer = new Timer(250, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
count++;
label.setText(Integer.toString(count));
Application.getApplication().setDockIconBadge(Integer.toString(count));
}
});
timer.start();
}
}
}
I should point out, that if you're running in an IDE capable of inspecting the installed classes, you should be able to get a list of the functionality provided by the Application class. I was using NetBeans and was able to find a listing of all the methods, problem is, some were documented and some weren't :P
How to edit the JLabel every seconds like (time left or score) in some games.
this is my code
static int l = 1;
static int s = 5000;
static int t = 90;
public static void main(String[] args) {
//Frame
final JFrame f = new JFrame();
f.setTitle("Picture Puzzle");
f.setSize(500,500);
f.setLocationRelativeTo(null);
f.setResizable(false);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
f.setVisible(true);
//some extra stuffs here
JLabel blevel00 = new JLabel("Level:" + l);
JLabel bscore00 = new JLabel("Score:" + s);
JLabel btime00 = new JLabel("Time:" + t);
p2.add(blevel00);
p2.add(bscore00);
p2.add(btime00);
//some extra stuffs here
start.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
while(t != 0 ) { //the t is the static int t = 90;
f.add(p2);
f.remove(p1);
f.setVisible(true);
f.revalidate();
f.repaint();
}
t--;
}
});
}
}
I tried this and nothing happens. any help will be appreciated.
Swing is a single threaded environment, that is, all alterations and modifications to the UI are expected to occur within the context of the Event Dispatching Thread.
Anything that blocks this thread, like a never ending loop or blocking I/O will prevent this thread from processing new events, including paint events.
Swing provides a number of solutions to this problem, in your case the best solution is probably to use a javax.swing.Timer. This will allow you to schedule a regular callback that is called within the context of the EDT, allowing you to make modifications to the UI on a regular bases.
Take a look at Concurrency in Swing and How to use Swing Timers for more details
Update with simple example
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class SimpleClock {
public static void main(String[] args) {
new SimpleClock();
}
public SimpleClock() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel time;
public TestPane() {
setLayout(new GridBagLayout());
time = new JLabel();
time.setFont(time.getFont().deriveFont(Font.BOLD, 48));
add(time);
updateTime();
Timer timer = new Timer(500, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
updateTime();
}
});
timer.start();
}
protected void updateTime() {
time.setText(DateFormat.getTimeInstance().format(new Date()));
}
}
}