Event that gets fired when a JInternalFrame is maximized/minimized - java

I have written the following code to handle an event incase a Jinternalframe is minimized or maximized.
jif.addComponentListener(new ComponentListener() {
public void componentResized(ComponentEvent e) {
Boolean isMax = ((JInternalFrame)e.getComponent()).isMaximum();
if(isMax == false)
{
// TODO Auto-generated method stub
}
else if(isMax == true )
{
// TODO Auto-generated method stub
}
}
public void componentMoved(ComponentEvent e) {
// TODO Auto-generated method stub
}
public void componentShown(ComponentEvent e) {
// TODO Auto-generated method stub
}
public void componentHidden(ComponentEvent e) {
// TODO Auto-generated method stub
}
}
);
The problem is that the isMax part gets triggered when the frame loads. I want this event to happen only after the Jinternalframe is completely loaded. Also i have noticed this event triggers when i minimize the container applet.
Is there any workaround for this?

You might be able to use a PropertyChangeListener and InternalFrameListener:
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import javax.swing.*;
import javax.swing.event.*;
public class InternalFrameEventTest {
public JComponent makeUI() {
final JDesktopPane desktop = new JDesktopPane();
JPanel p = new JPanel(new BorderLayout());
p.add(desktop);
p.add(new JButton(new AbstractAction("New") {
#Override public void actionPerformed(ActionEvent e) {
JInternalFrame f = new JInternalFrame("title", true, true, true, true);
InternalFrameHandler handler = new InternalFrameHandler();
f.addInternalFrameListener(handler);
f.addPropertyChangeListener(handler);
f.setSize(240, 120);
f.setVisible(true);
desktop.add(f);
}
}), BorderLayout.NORTH);
return p;
}
public static void main(String... args) {
EventQueue.invokeLater(new Runnable() {
#Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new InternalFrameEventTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
class InternalFrameHandler implements PropertyChangeListener, InternalFrameListener {
//PropertyChangeListener
#Override public void propertyChange(PropertyChangeEvent e) {
if (JInternalFrame.IS_MAXIMUM_PROPERTY.equals(e.getPropertyName())) {
System.out.println("isMaximum: " + e.getNewValue());
}
}
//InternalFrameListener
#Override public void internalFrameClosing(InternalFrameEvent e) {
System.out.println("internalFrameClosing");
}
#Override public void internalFrameClosed(InternalFrameEvent e) {
System.out.println("internalFrameClosed");
}
#Override public void internalFrameOpened(InternalFrameEvent e) {
System.out.println("internalFrameOpened");
}
#Override public void internalFrameIconified(InternalFrameEvent e) {
System.out.println("internalFrameIconified");
}
#Override public void internalFrameDeiconified(InternalFrameEvent e) {
System.out.println("internalFrameDeiconified");
if (e.getInternalFrame().isMaximum()) {
System.out.println("isMaximum: " + e.getInternalFrame().isMaximum());
}
}
#Override public void internalFrameActivated(InternalFrameEvent e) {
System.out.println("internalFrameActivated");
}
#Override public void internalFrameDeactivated(InternalFrameEvent e) {
System.out.println("internalFrameDeactivated");
}
}

Related

KeyListener will not respond to keyboard input [duplicate]

This question already has answers here:
Keylistener not working for JPanel
(4 answers)
Closed 8 months ago.
I am trying to add a JPanel to a JFrame. The JPanel is supposed to listen to keyboard input and mouse input. When i open the window it does work for the mouse but the keyboard gets ignored and i just can't see the problem, since i wrote the same stuff for both Listeners.
This is the code for the Panel
public class testjpanel extends JPanel implements MouseListener,KeyListener {
testjpanel (){
this.addMouseListener(this);
this.addKeyListener(this);
}
#Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
#Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println(1);
}
#Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println(1);
}
#Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
and here is the rest
public class main extends JFrame {
public main() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500,500);
this.setVisible(true);
this.setLayout(null);
testjpanel a= new testjpanel();
a.setVisible(true);
a.setBounds(0,0,500,500);
a.setLayout(null);
this.add(a);
}
}
This is a rather non-standard way to use a keyboard or mouse listener. A better, consistently reliable way is as follows.
Create a class to be your MouseManager:
public class MouseManager implements MouseListener {
#Override
public void mouseClicked(MouseEvent arg0) {
}
#Override
public void mouseEntered(MouseEvent arg0) {
}
#Override
public void mouseExited(MouseEvent arg0) {
}
#Override
public void mousePressed(MouseEvent arg0) {
}
#Override
public void mouseReleased(MouseEvent arg0) {
}
}
And the Keyboard Manager:
public class KeyboardManager implements KeyListener {
#Override
public void keyPressed(KeyEvent arg0) {
}
#Override
public void keyReleased(KeyEvent arg0) {
}
#Override
public void keyTyped(KeyEvent arg0) {
}
}
After this, all you have to do is add them to your window like this below example:
public class MyFrame extends JFrame {
public MyFrame() {
this.setTitle("Your title");
this.setSize(new Dimension(500, 500));
this.addMouseListener(new MouseManager())
this.addKeyListener(new KeyboardManager());
}
}
Having the keyboard and mouse managers/listener in their own classes keeps things tidy and makes them easier to work with; for functionality, add them to the JFrame instead of your JPanel.

Java - how do I check whether a JFrame is closed

When a user clicks the red 'X' button of a JFrame, how do I detect whether the JFrame is open or closed? I have a swing timer where the JFrame keeps updating it's label until the user closes down the JFrame.
int delay = 1000; //milliseconds
final Timer timer = new Timer(delay, null);
timer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
tempLabel.setVisible(true);
String tmp = "test";
tempLabel.setText("Temperature : " + tmp);
// timer.stop();
}
});
timer.start();
You have to implement either the WindowStateListener or the WindowListener. If you use the WindowListener it could look like this:
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.Timer;
public class Foo implements WindowListener {
private Timer timer;
public static void main(String args[]){
initTimerComponent();
}
private void initTimerComponent() {
int delay = 1000; //milliseconds
timer = new Timer(delay, null);
timer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
tempLabel.setVisible(true);
String tmp = "test";
tempLabel.setText("Temperature : " + tmp);
}
});
timer.start();
}
#Override
public void windowOpened(WindowEvent e) { }
#Override
public void windowClosing(WindowEvent e) {
timer.stop();
}
#Override
public void windowClosed(WindowEvent e) { }
#Override
public void windowIconified(WindowEvent e) { }
#Override
public void windowDeiconified(WindowEvent e) { }
#Override
public void windowActivated(WindowEvent e) { }
#Override
public void windowDeactivated(WindowEvent e) { }
}
You have to implement them all as WindowListener is an interface and the first concrete class implementing an interface is forced to implement all its abstract methods. But you actually need just one method.
Use this method
public void windowClosing(WindowEvent e) {
timer.stop();
}
to stop your timer as soon as the window is closing after the user clicked the red X.
Answer
addWindowListener(new WindowAdapter() {
//for closing
#Override
public void windowClosing(WindowEvent e) {
JOptionPane.showMessageDialog(null, "Closing");
}
//for closed
#Override
public void windowClosed(WindowEvent e) {
}
});

How to create a class that implements mouseListener

I want to create a class that implement mouseListener interface (this class will override all mouseListener methods) instead of override all the methods in the main class. When I tring to do so I get an error.
This is my code:
import javax.swing.*;
public class Game {
public Game(){
JFrame frame = new JFrame();
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addMouseListener(new MyMouseListener());
}
public static void main(String[] args) {
Game app = new Game();
}
}
MyMouseListener code:
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class MyMouseListener implements KeyListener{
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
}
}
edit: I understand my problem was that I implement keyListener insteed of mouseListener in the class MyMouseListener so I changed this and MyMouseListener code is now:
public class MyMouseListener extends Game implements MouseListener{
#Override
public void mouseClicked(MouseEvent e) {
}
#Override
public void mousePressed(MouseEvent e) {
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
}
The problem you might be having is the frame not showing up, this is because you did not tell it to show the frame, use the following method:
frame.setVisible(true);
In addition as stated by Jorn Vernee, in your class MyMouseListener you have to implement the MouseListener interface instead of the KeyListener.
I tried this code out and it worked just fine:
import javax.swing.JFrame;
public class Game {
public Game(){
JFrame frame = new JFrame();
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addMouseListener(new MyMouseListener());
frame.setVisible(true);
}
public static void main(String[] args) {
Game app = new Game();
}
}
Code for MyMouseListener:
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class MyMouseListener implements MouseListener{
#Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("clicked");
}
#Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}

KeyListener won't listen on JPanel

It's a very simple program, but for some reason when I debug it and set breakpoints at the keyPressed, keyReleased and keyTyped method, the program never stops there.
mainKeyListener = new KeyListener() {
public void keyPressed(KeyEvent e) {
System.out.println("KEY PRESSED");
repaint();
}
}
#Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
};
Here I add it to a JPanel, which is the exact size of the frame and the only object on it:
JPanel backgroundPanel = new JPanel();
backgroundPanel.setBounds(0,0, 400, 500);
backgroundPanel.addKeyListener(mainKeyListener);
backgroundPanel.setFocusable(true);
getContentPane().add(backgroundPanel);
Your problem is laying in focused element. I think that your panel lost the focus.
Note:
To fire keyboard events, a component must have the keyboard focus. It can be solved in many ways for your example you can use KeyboardFocusManager for example like this:
KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
focusManager.addKeyEventDispatcher(new KeyEventDispatcher() {
public boolean dispatchKeyEvent(KeyEvent e) {
if(focusManager.getFocusOwner()!=backgroundPanel){
focusManager.redispatchEvent(backgroundPanel,e);
return true;}
else return false;
}
});
Also try to use Key Bindings http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html
Hi this should work for you.
public class Gui extends JFrame
{
private JPanel backgroundPanel = new JPanel();
public Gui() throws HeadlessException
{
this.setLayout(new GridLayout(1,1));
setPanelProps();
backgroundPanel.addKeyListener(createListener());
this.add(backgroundPanel);
this.setVisible(true);
this.setSize(new Dimension(400,500));
}
public void setPanelProps(){
backgroundPanel.setBounds(0, 0, 400, 500);
backgroundPanel.setSize(new Dimension(400,500));
backgroundPanel.setFocusable(true);
backgroundPanel.setBackground(new Color(50,60,70));
}
public KeyListener createListener(){
return new KeyListener() {
#Override
public void keyTyped(KeyEvent e)
{
System.out.println("KEY TYPED");
}
public void keyPressed(KeyEvent e) {
System.out.println("KEY PRESSED");
repaint();
}
#Override
public void keyReleased(KeyEvent e)
{
System.out.println("KEY RELEASED");
}
};
}
}
public class GuiRun
{
public static void main(String[] args)
{
Gui gui = new Gui();
}
}

Internal Frame Listener not working

The following code below shows an internal frame listener for the Gui class. The internal frame listener should either set the save (JMenuItem) as enable or disabled but it does nothing. I don't know where the problem is. I have tried a couple of things but nothing works.
Here is the code:
public class Gui implements InternalFrameListener, ActionListener{
MyInternalFrame frame;
JMenuItem save;
public Gui( JDesktopPane desktop, final JMenuItem save) {
// TODO Auto-generated constructor stub
frame = new MyInternalFrame(title, img);
this.save = save;
frame.setVisible(true);
desktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
frame.addInternalFrameListener(this);
// options(name, desktop);
}
class MyInternalFrame extends JInternalFrame {
static final int xPosition = 30, yPosition = 30;
public MyInternalFrame(String title, ImagePlus img) {
super(title, true,true, true, true);
setSize(img.getHeight(), img.getWidth());
// Set the window's location.
setLocation(xPosition * openFrameCount, yPosition * openFrameCount);
}
}
public void actionPerformed(ActionEvent e){
frame.addInternalFrameListener(this);
System.out.println("beingg called");
}
#Override
public void internalFrameActivated(InternalFrameEvent arg0) {
// TODO Auto-generated method stub
save.setEnabled(true);
}
#Override
public void internalFrameClosed(InternalFrameEvent arg0) {
// TODO Auto-generated method stub
save.setEnabled(false);
}
#Override
public void internalFrameClosing(InternalFrameEvent arg0) {
// TODO Auto-generated method stub
save.setEnabled(false);
}
#Override
public void internalFrameDeactivated(InternalFrameEvent arg0) {
// TODO Auto-generated method stub
save.setEnabled(false);
}
#Override
public void internalFrameDeiconified(InternalFrameEvent arg0) {
// TODO Auto-generated method stub
save.setEnabled(true);
}
#Override
public void internalFrameIconified(InternalFrameEvent arg0) {
// TODO Auto-generated method stub
save.setEnabled(false);
}
#Override
public void internalFrameOpened(InternalFrameEvent arg0) {
// TODO Auto-generated method stub
save.setEnabled(true);
}
}
A MCVE showing that this is working in general. My guess would be that you are using a wrong instance of the save menu item. In doubt, add debug output
#Override
public void internalFrameIconified(InternalFrameEvent arg0)
{
System.out.println("Disabling "+save);
save.setEnabled(false);
}
to your methods.
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;
public class InternalFrameListenerTest
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridLayout(1, 1));
JMenuBar menubar = new JMenuBar();
JMenu menu = new JMenu("Menu");
menubar.add(menu);
JMenuItem menuItem = new JMenuItem("Save");
menu.add(menuItem);
frame.setJMenuBar(menubar);
JDesktopPane d = new JDesktopPane();
Gui gui = new Gui(d, menuItem);
frame.getContentPane().add(d);
frame.setSize(500,500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class Gui implements InternalFrameListener, ActionListener
{
MyInternalFrame frame;
JMenuItem save;
public Gui(JDesktopPane desktop, final JMenuItem save)
{
frame = new MyInternalFrame("Title");
this.save = save;
frame.setVisible(true);
desktop.add(frame);
try
{
frame.setSelected(true);
}
catch (java.beans.PropertyVetoException e)
{
}
frame.addInternalFrameListener(this);
}
class MyInternalFrame extends JInternalFrame
{
static final int xPosition = 30, yPosition = 30;
public MyInternalFrame(String title)
{
super(title, true, true, true, true);
setSize(200,100);
setLocation(50,50);
}
}
public void actionPerformed(ActionEvent e)
{
frame.addInternalFrameListener(this);
System.out.println("beingg called");
}
#Override
public void internalFrameActivated(InternalFrameEvent arg0)
{
save.setEnabled(true);
}
#Override
public void internalFrameClosed(InternalFrameEvent arg0)
{
save.setEnabled(false);
}
#Override
public void internalFrameClosing(InternalFrameEvent arg0)
{
save.setEnabled(false);
}
#Override
public void internalFrameDeactivated(InternalFrameEvent arg0)
{
save.setEnabled(false);
}
#Override
public void internalFrameDeiconified(InternalFrameEvent arg0)
{
save.setEnabled(true);
}
#Override
public void internalFrameIconified(InternalFrameEvent arg0)
{
save.setEnabled(false);
}
#Override
public void internalFrameOpened(InternalFrameEvent arg0)
{
save.setEnabled(true);
}
}

Categories