keyListener
From: Cippo741 (cippo741_at_inwind.it)
Date: 02/27/04
- Next message: Andrew Thompson: "Re: strangely inverted text in JEditorPane"
- Previous message: Christian Kaufhold: "Re: I lose my HeaderRenderer when I install CellRenderer"
- Next in thread: Adam: "Re: keyListener"
- Reply: Adam: "Re: keyListener"
- Reply: Andrew Thompson: "Re: keyListener"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 27 Feb 2004 15:38:53 GMT
Hy to all, excuse my bad english.
I'm trying to insert a keylistener such that, pressing whichever key during
the execution, show message dialog with the key pressed. I'm almost sure I
didn't understand. I added keylistener to frame.getContentPane(). Is that
right? and then I write frame.setContentPane(pannello). Am I loosing the
listener? Need I the focus? till now, I never saw message dialog :-).
suggestions? thanks. cippo741
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Prova
{
JButton Avanti = new JButton("Next");
disegno destro = new disegno();
disegno sinistro = new disegno();
disegno centro = new disegno();
JLabel Lista0=new JLabel("Now it's all ok, go on");
JLabel Lista1=new JLabel("This is first line'");
JLabel Lista2=new JLabel("This is second line");
JLabel Lista3=new JLabel("This is third line");
KeyListener kl = new KeyListener()
{//KEYLISTENERRRRRRRR
public void keyPressed(KeyEvent e)
{
JOptionPane.showMessageDialog(destro,
"Pressed key "+ e.paramString(), "Information", JOptionPane.
INFORMATION_MESSAGE);
}
public void keyReleased(KeyEvent e)
{
System.out.println("keyReleased" + e.paramString());
}
public void keyTyped(KeyEvent e)
{
System.out.println("keyTyped" + e.paramString());
}
};
public void createAndShowGUI()
{
JFrame.setDefaultLookAndFeelDecorated(true);
final JFrame frame = new JFrame();
frame.setSize(800,600);
WindowCloser listener=new WindowCloser();
frame.addWindowListener(listener);
WindowDeiconified listener2=new WindowDeiconified();
frame.addWindowListener(listener2);
JPanel pannello=new JPanel();
pannello.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1;
c.weighty = 1;
c.fill = c.BOTH;
frame.getContentPane().addKeyListener(kl);
Avanti.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(
destro,
"Welcome!!!",
"Informazione",
JOptionPane.INFORMATION_MESSAGE);
destro.removeAll();
centro.setLayout( new GridBagLayout() );
GridBagConstraints c3 = new GridBagConstraints();
c3.gridy++;
centro.add(Lista1, c3);
c3.gridy++;
centro.add(Lista2, c3);
c3.gridy++;
centro.add(Lista3, c3);
frame.validate();
frame.repaint();
}
});
sinistro.setBackground(Color.green);
centro.setBackground(Color.white);
destro.setBackground(Color.red);
destro.add(Avanti);
destro.add(Lista0);
c.gridx++;
pannello.add(sinistro,c);
c.gridx++;
pannello.add(centro,c);
c.gridx++;
pannello.add(destro,c);
frame.setContentPane(pannello);
frame.setVisible(true);
}
public static void main(String args[])
{
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
Prova ciccio = new Prova();
ciccio.createAndShowGUI();
}
});
}
class disegno extends JPanel
{
public disegno()
{
setPreferredSize(new Dimension(250,500));
}
}
private class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
}
private class WindowDeiconified extends WindowAdapter
{
public void windowDeiconified(WindowEvent e)
{
centro.repaint();
System.out.println(this);
}
}
}
- Next message: Andrew Thompson: "Re: strangely inverted text in JEditorPane"
- Previous message: Christian Kaufhold: "Re: I lose my HeaderRenderer when I install CellRenderer"
- Next in thread: Adam: "Re: keyListener"
- Reply: Adam: "Re: keyListener"
- Reply: Andrew Thompson: "Re: keyListener"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]