KeyListener for a JFrame

I searched through this forum trying to figure out how to make a KeyListener work for an entire JFrame, but could not understand the answers all you java experts out there gave... Could someone please simplify it for me by explaining your code instead of just typing it in and saying "that's how you do it"? Because I really didn't get what to do. I have a class that implements a KeyListener and extends the JFrame I want the KeyListener for. I tried a simple "addKeyListener(this)" but the events for the Listener were never triggered. I would be very thankful for any and all responses given.

The following simple program
import javax.swing.*;
import java.awt.event.*;
public class KeyListenerTest {
     public static void main(String[] args) {
          KeyListenerFrame frame = new KeyListenerFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.addKeyListener(frame);
          frame.show();
class KeyListenerFrame extends JFrame implements KeyListener {
     public void keyPressed(KeyEvent evt) {
          System.out.println("keyPressed");
     public void keyReleased(KeyEvent evt) {
          System.out.println("keyReleased");
     public void keyTyped(KeyEvent evt) {
          System.out.println("keyTyped");
}opens a JFrame which triggers all of the three handled events whenever a key is pressed on the keyboard.
I am not sure whether this is could be an answer to your problem or rather I missed the point...
Bye

Similar Messages

  • How to add a KeyListener for the JFrame (when I'm typing in a JTextField)?

    I have some problem with KeyListener..
    I add a KeyListener (I named it "listener") for my JFrame and it works fine. Then I add JTextField to the JFrame. When I'm typing some text in the JTextField - my "listener" does not work. (cause my JTextField doesn't have a KeyListener).
    I just want to make an ability to process hot keys which user presses in my java program..
    Does anyone know how to do it?

    In future, please ask Swing questions in the [Swing Forum.|http://forums.sun.com/forum.jspa?forumID=57]
    Don't use KeyListener. In fact KeyListener is seldom useful. Use key binding: [http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html]
    Or you can have menu items with accelerators (hot keys).

  • Changing L&F for the JFrame?

    Hi all,
    I am currently writing my own look and feel, and I am having trouble with applying this on the JFrame-object. I managed to do this on the JInternalFrame (by using MetalInternalFrameUI)
    but I can not find a similiar class for a JFrame. I know that the look and feel for a JFrame is platform-dependent but still, I think it should be possible to do it. If not, is there any way to use JInternalFrame directly as a main container?
    Thank you,
    Regards Veroslav

    It is platform dependent, but in 1.4 or later it is possible to "hint" the system that a frame should not have the native decorations.
    http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JFrame.html#setDefaultLookAndFeelDecorated(boolean)
    http://java.sun.com/j2se/1.4.1/docs/api/java/awt/Frame.html#setUndecorated(boolean)

  • Image as a background for a JFrame

    Hi all,
    Is it possible to have an image as a background for a JFrame?
    How do you do it?
    plz help

    i personally inherit a panel...
      class ImagePanel extends JPanel {
          Image image;
          public ImagePanel(Image image) {
              this.image = image;
          public void paintComponent(Graphics g) {
              super.paintComponent(g); //paint background
              //Draw image at its natural size first.
              g.drawImage(image, 0, 0, this); //85x62 image
      } and call it with
      private Image icLogo =
    Toolkit.getDefaultToolkit().getImage(getClass().getResource("icLogo.gif"));
      private JPanel imgPanel = new ImagePanel(icLogo);Well, this should work with frames, too. In case it doesn't, you can simply add this panel to the frame.
    greets
    Jochen

  • Problem using KeyListener in a JFrame

    Let's see if anyone can help me:
    I'm programming a Maze game, so I use a JFrame called Window, who extends JFrame and implements ActionListener, KeyListener. Thw thing is that the clase Maze is a JPanel, so I add it to my Frame and add the KeyListener to both, the Maze and the Window, but still nothing seems to happen.
    I've tried to remove the KeyListener from both separately, but still doesn't work...
    Any suggestions???

    This is the code for my Window.java file. May be someone can see the error...
    package maze;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class Window extends JFrame implements ActionListener, KeyListener {
      private Maze maze;
      private JButton crear = new JButton("New");
      public Window(int size, String s) {
        super("3D Maze");
        boolean b = true;
        if (s.equalsIgnoreCase("-n"))
          b = false;
        else if (s.equalsIgnoreCase("-v"))
          b = true;
        Container c = getContentPane();
        c.setLayout(new BorderLayout());
        maze = new Maze(size,size,b);
        crear.addActionListener(this);
        addKeyListener(this);
        maze.addKeyListener(this);
        c.add(crear,BorderLayout.NORTH);
        c.add(maze,BorderLayout.CENTER);
        setSize(600,650);
        show();
      public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == e.VK_UP || e.getKeyCode() == e.VK_W)
          maze.walk();
        else if (e.getKeyCode() == e.VK_DOWN || e.getKeyCode() == e.VK_S)
          maze.back();
        else if (e.getKeyCode() == e.VK_LEFT || e.getKeyCode() == e.VK_A)
          maze.turnLeft();
        else if (e.getKeyCode() == e.VK_RIGHT || e.getKeyCode() == e.VK_D)
          maze.turnRight();
        else if (e.getKeyCode() == e.VK_V)
          maze.alternaMostrarCreacion();
        else if (e.getKeyCode() == e.VK_M)
          maze.switchMap();
      public void keyTyped(KeyEvent e) {}
      public void keyReleased(KeyEvent e) {
      public void actionPerformed(ActionEvent e) {
        maze.constructMaze();
      public static void main(String[] args) {
        int i = 30;
        String s = "-v";
        if (args.length != 0) {
          try {
            i = Integer.parseInt(args[0]);
            if (!(i >= 15 && i <= 50))
              i = 30;
            s = args[1];
          catch(Exception e) {}
        Window w = new Window(i,s);
        w.addWindowListener(
          new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
              ImageIcon im = new ImageIcon("./Logo.gif");
              String s = "";
              s += "3D MAZE\n\n";
              s += "Creator: Allan Marin\n";
              s += "http://metallan.topcities.com\n\n";
              s += ""+((char)(169))+" 2002";
              JOptionPane.showMessageDialog(null,s,"About...",JOptionPane.PLAIN_MESSAGE,im);
              System.exit(0);
    }

  • Accelerator keys for a JFrame containing JTextPane

    Hi, I have written a text editing window which extends JTextPane, and is embedded in a JFrame with a menubar. I have attached accelerator key shortcuts to each JMenuItem. However, none of them work, because my extension of JTextPane implements KeyListener, and thus controls keyboard input. How do I get the accelerator keys to still work? In general they are all Ctrl-key combinations with no meaningful function in the JTextPane.

    The only way I have found around this problem is to make your JFrame a KeyListener, and add it as a KeyListener of your JTextPane. In the keyPressed method of your JFrame call the processKeyEvent method. That will route the key to the menu. I have used this for a JTable and JTree under my JFrame, but not a JTextPane. It should work the same.
    I have heard rumors that this will be fixed in 1.4. Pretty lame that it is so difficult to do.

  • How to set background color for a JFrame

    Hi,
    i am new to swing programming. I have created a JFrame called framehelp and i placed some labels in the frame.
    I have set the background color as yellow for the frame ie by setBackground(new java.awt.Color(255, 255, 204));
    But after compiling the program i am still getting the background color of frame as gray.
    How can i change the background color of frame as lightish yellow.
    I have used NETBEANS to generate this code and i am posting the code..
    Kindly help me
    Thanks in adavance
    public class framehelp extends javax.swing.JFrame {
        /** Creates new form framehelp */
        public framehelp() {
            initComponents();
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
        private void initComponents() {
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            jLabel4 = new javax.swing.JLabel();
            jLabel5 = new javax.swing.JLabel();
            jLabel6 = new javax.swing.JLabel();
            jLabel8 = new javax.swing.JLabel();
            jLabel9 = new javax.swing.JLabel();
            jLabel10 = new javax.swing.JLabel();
            jLabel11 = new javax.swing.JLabel();
            jLabel12 = new javax.swing.JLabel();
            jLabel13 = new javax.swing.JLabel();
            jLabel7 = new javax.swing.JLabel();
            jLabel14 = new javax.swing.JLabel();
            jLabel15 = new javax.swing.JLabel();
            jLabel16 = new javax.swing.JLabel();
            jLabel17 = new javax.swing.JLabel();
            jLabel18 = new javax.swing.JLabel();
            jLabel19 = new javax.swing.JLabel();
            jLabel20 = new javax.swing.JLabel();
            jLabel21 = new javax.swing.JLabel();
            jLabel22 = new javax.swing.JLabel();
            jLabel23 = new javax.swing.JLabel();
            jLabel24 = new javax.swing.JLabel();
            jLabel25 = new javax.swing.JLabel();
            jLabel26 = new javax.swing.JLabel();
            jLabel27 = new javax.swing.JLabel();
            jLabel28 = new javax.swing.JLabel();
            jLabel29 = new javax.swing.JLabel();
            jLabel30 = new javax.swing.JLabel();
            jLabel31 = new javax.swing.JLabel();
            jLabel32 = new javax.swing.JLabel();
            jLabel33 = new javax.swing.JLabel();
            getContentPane().setLayout(null);
            setBackground(new java.awt.Color(255, 255, 204));
            setForeground(new java.awt.Color(204, 204, 204));
            addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowClosing(java.awt.event.WindowEvent evt) {
                    exitForm(evt);
            jLabel1.setText("The CAME framework software measurement process evaluation is used to determine the Level of  measurement in the ITarea. This measurementprocess evaluation is ");
            getContentPane().add(jLabel1);
            jLabel1.setBounds(40, 50, 41, 16);
            jLabel2.setText("performed individually for software coomponents ie SoftwareProduct, SoftwareProcess and SoftwareResource.");
            getContentPane().add(jLabel2);
            jLabel2.setBounds(40, 70, 41, 16);
            jLabel3.setText("Below we describe the steps which will guide through the measurement process evaluation. There are two types of evaluation considerd here, General evaluation");
            getContentPane().add(jLabel3);
            jLabel3.setBounds(40, 100, 913, 16);
            jLabel4.setText("and Evaluation with respect to a standard. The measurement intensions considerd by varoius measurement standards will be used in our tool based method.  ");
            getContentPane().add(jLabel4);
            jLabel4.setBounds(40, 120, 893, 16);
            jLabel5.setText("Information about the measurement standards can be found at     www.iso.org ,    www.ieee.org.");
            getContentPane().add(jLabel5);
            jLabel5.setBounds(40, 140, 542, 16);
            jLabel6.setText("Steps to calculate the MetricsLevel");
            getContentPane().add(jLabel6);
            jLabel6.setBounds(40, 190, 199, 16);
            jLabel8.setText("2 )  The opened frame consists of several  metrics and measures, evaluation level buttons, result textfields and selection choice lists. To calculate");
            getContentPane().add(jLabel8);
            jLabel8.setBounds(70, 240, 41, 16);
            jLabel9.setText("general Metricslevel without any standard, select general from Measurement standard choice list. then select Both from scaleType choice list and finally select metrics ");
            getContentPane().add(jLabel9);
            jLabel9.setBounds(90, 260, 949, 16);
            jLabel10.setText(" and measures from the frame and press MetricsLevel Button. The metricslevel for the selected metrics will be displayed in textfield1.");
            getContentPane().add(jLabel10);
            jLabel10.setBounds(90, 280, 756, 16);
            jLabel11.setText("3 ) To determine MetricsLevel with respect to a standard select a standard from MeasurementStandard choice list, select Both from ScaleType choice list and finally ");
            getContentPane().add(jLabel11);
            jLabel11.setBounds(70, 300, 933, 16);
            jLabel12.setText("select darkened metrics and measures and press MetricsLevel Button. The Metrics Level for the selected Metrics and standard will be diplayed in textfield1.");
            getContentPane().add(jLabel12);
            jLabel12.setBounds(90, 320, 883, 16);
            jLabel13.setText("4 ) Repeat steps 1 2, 3  for ProcessMeasurementEvaluation and ResourceMeasurementEvaluation menu items from the select menu.");
            getContentPane().add(jLabel13);
            jLabel13.setBounds(70, 340, 751, 16);
            jLabel7.setText("1 ) Select ProductMeasurementEvaluation menu item from the select menu,a frame called CAME framework software product measurement evaluation will be opened. ");
            getContentPane().add(jLabel7);
            jLabel7.setBounds(70, 220, 946, 16);
            jLabel14.setText("Steps to calculate MeasurementLevel");
            getContentPane().add(jLabel14);
            jLabel14.setBounds(50, 390, 214, 16);
            jLabel15.setText("1 ) Select ProductMeasurementEvaluation menu item from the select menu,a frame called CAME framework software product measurement evaluation will be opened.");
            getContentPane().add(jLabel15);
            jLabel15.setBounds(80, 420, 943, 16);
            jLabel16.setText("2 ) The opened frame consists of several  metrics and measures, evaluation level buttons, result textfields and selection choice lists. To calculate");
            getContentPane().add(jLabel16);
            jLabel16.setBounds(80, 450, 822, 16);
            jLabel17.setText("general Measurementlevel without any standard, select general from Measurement standard choice list. then select Quantitative measures from scaleType choice list ");
            getContentPane().add(jLabel17);
            jLabel17.setBounds(100, 470, 943, 16);
            jLabel18.setText("and finally select metrics and measures from the frame and press MeasurementLevel Button. The MeasurementLevel for the selected metrics will be displayed in textfield2.");
            getContentPane().add(jLabel18);
            jLabel18.setBounds(100, 490, 973, 20);
            jLabel19.setText("3 ) To determine MeasurementLevel with respect to a standard select a standard from MeasurementStandard choice list, select Quantitative measures from ScaleType choice list");
            getContentPane().add(jLabel19);
            jLabel19.setBounds(80, 510, 1009, 16);
            jLabel20.setText("and finally select darkened metrics and measures and press MeasurementLevel Button. The MeasurementLevel  will be diplayed in textfield2.");
            getContentPane().add(jLabel20);
            jLabel20.setBounds(100, 530, 799, 16);
            jLabel21.setText("4 )Repeat steps 1 2, 3  for ProcessMeasurementEvaluation and ResourceMeasurementEvaluation menu items from the select menu.");
            getContentPane().add(jLabel21);
            jLabel21.setBounds(80, 550, 748, 16);
            jLabel22.setText("Steps to calculate MeasurementProcessLevel");
            getContentPane().add(jLabel22);
            jLabel22.setBounds(50, 600, 262, 16);
            jLabel23.setText("1 ) Select ProductMeasurementEvaluation menu item from the select menu,a frame called CAME framework software product measurement evaluation will be opened.");
            getContentPane().add(jLabel23);
            jLabel23.setBounds(80, 630, 943, 16);
            jLabel24.setText("2 )The opened frame consists of several  metrics and measures, evaluation level buttons, result textfields and selection choice lists. To calculate");
            getContentPane().add(jLabel24);
            jLabel24.setBounds(80, 650, 819, 16);
            jLabel25.setText("general MeasurementProcesslevel without any standard, select general from Measurement standard choice list. then select Quantitative measures from scaleType ");
            getContentPane().add(jLabel25);
            jLabel25.setBounds(100, 670, 930, 16);
            jLabel26.setText("choice list, enter migrated metrics if any in textfield  below migrated metrics label and finally select metrics measures from the frame and press ");
            getContentPane().add(jLabel26);
            jLabel26.setBounds(100, 690, 818, 16);
            jLabel27.setText("MeasurementProcessLevel Button. The MeasurementProcessLevel for the selected metrics will be displayed in textfield3.");
            getContentPane().add(jLabel27);
            jLabel27.setBounds(100, 710, 691, 16);
            jLabel28.setText("3 ) To determine MeasurementProcessLevel with respect to a standard select a standard from MeasurementStandard choice list, select Quantitative measures from ");
            getContentPane().add(jLabel28);
            jLabel28.setBounds(80, 740, 937, 16);
            jLabel29.setText("from ScaleType choice list, enter migrated metrics if any in textfield below migrated metrics and finally select darkened metrics and measures and press  ");
            getContentPane().add(jLabel29);
            jLabel29.setBounds(100, 760, 873, 16);
            jLabel30.setText("MeasurementProcessLevel Button. The MeasurementprocessLevel for the selected metrics will be displayed in textfield3");
            getContentPane().add(jLabel30);
            jLabel30.setBounds(100, 780, 687, 16);
            jLabel31.setText("4 ) Repeat steps 1,2,3 for ProcessMeasurementEvaluation and ResourceMeasurementEvaluation menuitems.");
            getContentPane().add(jLabel31);
            jLabel31.setBounds(80, 800, 618, 16);
            jLabel32.setText("Steps to calculate MeasuredSoftwareProcessLevl");
            getContentPane().add(jLabel32);
            jLabel32.setBounds(60, 830, 285, 16);
            jLabel33.setText("1 ) Follow the same steps as MetricsLevel calcualtion but select metrics and measures covered by CAME Tools only.");
            getContentPane().add(jLabel33);
            jLabel33.setBounds(90, 860, 657, 16);
            java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
            setBounds((screenSize.width-1100)/2, (screenSize.height-940)/2, 1100, 940);
        /** Exit the Application */
        private void exitForm(java.awt.event.WindowEvent evt) {
            System.exit(0);
         * @param args the command line arguments
        public static void main(String args[]) {
            new framehelp().show();
        // Variables declaration - do not modify
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel10;
        private javax.swing.JLabel jLabel11;
        private javax.swing.JLabel jLabel12;
        private javax.swing.JLabel jLabel13;
        private javax.swing.JLabel jLabel14;
        private javax.swing.JLabel jLabel15;
        private javax.swing.JLabel jLabel16;
        private javax.swing.JLabel jLabel17;
        private javax.swing.JLabel jLabel18;
        private javax.swing.JLabel jLabel19;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel20;
        private javax.swing.JLabel jLabel21;
        private javax.swing.JLabel jLabel22;
        private javax.swing.JLabel jLabel23;
        private javax.swing.JLabel jLabel24;
        private javax.swing.JLabel jLabel25;
        private javax.swing.JLabel jLabel26;
        private javax.swing.JLabel jLabel27;
        private javax.swing.JLabel jLabel28;
        private javax.swing.JLabel jLabel29;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel30;
        private javax.swing.JLabel jLabel31;
        private javax.swing.JLabel jLabel32;
        private javax.swing.JLabel jLabel33;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JLabel jLabel6;
        private javax.swing.JLabel jLabel7;
        private javax.swing.JLabel jLabel8;
        private javax.swing.JLabel jLabel9;
        // End of variables declaration
    }

    1. When you post code, post a minimal example. Did we have to scroll through
    all those bloody labels.
    2. Can't say much for the quality of code NetBeans generates.
    3. To answer your question (sorry, I don't have an autographed copy of the photo):
    import java.awt.*;
    import java.net.*;
    import javax.swing.*;
    public class Example {
        public static void main(String[] args) throws MalformedURLException {
            final JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Container cp = f.getContentPane();
            cp.setBackground(new Color(0xff, 0xff, 0xee));
            cp.add(new JLabel("set the color on the content pane, not the frame"), BorderLayout.SOUTH);
            URL url = new URL("http://today.java.net/jag/bio/JagHeadshot-small.jpg");
            cp.add(new JLabel(new ImageIcon(url)));
            f.pack();
            SwingUtilities.invokeLater(new Runnable(){
                public void run() {
                    f.setLocationRelativeTo(null);
                    f.setVisible(true);
    }

  • A Grow Box for a JFrame

    I am this >||< (font size 8) close to getting this JGrowBox class thing to work exactly the way I want it to, and then, everybody can have a nice little grow box thingie to "attatch" to their JFrames. Ooooh.
    I have three problems. First, if the user tries to resize the frame normally (grabs the very corner of the JFrame) then the JFrame resizes in its normal ugly way, without repainting its components (which means that the JGrowBox thing just stays where it is, which looks stupid.) Second, if the user grabs the actual JGrowBox the way you can with all other stupid Windoze programs, the JFrame flickers horribly, and the CPU goes nuts. Third, I use static byte array representations of the GIF images. This is kinda lame. I'm guessing all the cool Java widgets use Java2D somehow.
    The dimensions used for the customized JComponent are specific to the arrangment of the rest of the layout. The JGrowBox is actually in the BorderLayout.EAST of a 18-pixel tall JPanel "status bar" thingie, which is in the BorderLayout.SOUTH of the contentPane. I didn't feel like going through the hassle of getting the thing to be universally positioned at the bottom-right of the parent JFrame, regardless of layout.
    The images are just a 16x16 screen captures of the Windows 2000 and Windows XP grow boxes, respectively, with the window grey replaced with black and black set as the transparent color, and then sort of converted into a static byte array. I suppose it might be a bit more space-saving to just use Java2D to draw out all the pixels and stuff, but I hate doing that kinda crap.
    Code:
    * JGrowBox.java
    * Created on July 28, 2005, 1:54 PM
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import javax.swing.*;
    import javax.swing.event.*;
    * @author [email protected]
    public class JGrowBox
        extends JComponent {
        // the associated JFrame window parent of the grow box
        private JFrame parent = null;
        // where the user clicked the mouse to resize using the grow box
        private Point p0 = null;
        // the image of the grow box thinga-ma-jigger
        private Image image = null;
        /** Creates a new instance of JGrowBox */
        public JGrowBox(final JFrame parent) {
            this.parent = parent;
            // get the name of the operating system
            String OS_NAME = System.getProperty("os.name");
            // anybody got GIFs for KDE or GNOME?
            if (OS_NAME.equals("Windows 2000")) {
                image = new ImageIcon(WINDOWS_2000_GROW_BOX.BYTES).getImage();
            else if (OS_NAME.equals("Windows XP")) {
                image = new ImageIcon(WINDOWS_XP_GROW_BOX.BYTES).getImage();
            // this stuff is specific to a given arrangement
            setSize(new Dimension(18, 15));
            setPreferredSize(new Dimension(18, 15));
            setMaximumSize(new Dimension(18, 15));
            setMinimumSize(new Dimension(18, 15));
            setFocusable(false);
            // getting the first mouseClick and changing the cursor and stuff
            addMouseListener(new MouseAdapter() {
                public void mouseEntered(MouseEvent e) {
                    parent.getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
                public void mouseExited(MouseEvent e) {
                    parent.getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                public void mousePressed(MouseEvent e) {
                    if (e.getButton() == MouseEvent.BUTTON1) {
                        p0 = e.getPoint();
                public void mouseReleased(MouseEvent e) {
                    if (!contains(e.getPoint())) {
                        parent.getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            // when the user drags the mouse, resize the parent JFrame (ugly!)
            addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseDragged(MouseEvent e) {
                    Point p1 = e.getPoint();
                    parent.getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
                    Rectangle bounds = parent.getBounds();
                    int x = bounds.width + (p1.x - p0.x);
                    int y = bounds.height + (p1.y - p0.y);
                    Point p2 = new Point(x, y);
                    int xMax = Toolkit.getDefaultToolkit().getScreenSize().width;
                    parent.setSize(Math.min(p2.x, xMax), p2.y);
            // hide the grow icon when the parent window is maximized
            parent.addComponentListener(new ComponentAdapter() {
                public void componentResized(ComponentEvent e) {
                    if (parent.getExtendedState() == JFrame.MAXIMIZED_BOTH) {
                        setVisible(false);
                    else {
                        setVisible(true);
        public void paintComponent(Graphics g) {
            if (image != null) {
                g.drawImage(image, 3, 3, this);
        public boolean contains(Point p) {
            Rectangle r = getBounds();
            return (p.x >= 0 && p.x <= r.width &&
                    p.y >= 0 && p.y <= r.height);
    class WINDOWS_2000_GROW_BOX {
        public final static byte[] BYTES = {
      71,   73,   70,   56,   57,   97,   16,    0,   16,    0,   -9,    0,    0,    0,    0,    0,
      -1,   -1,   -1,  -44,  -48,  -56, -110, -116, -106, -100, -102,  -69,    0,    0,   -1,    0,
       0, -128, -118, -103,  -35,  115,  126,  -96,   10,   36,  106,   10,   35,  103,    9,   32,
      95,   11,   37,  107,   13,   39,  109,   35,   54,  106,   10,   36,  105,    8,   27,   77,
      12,   39,  108,   14,   40,  109,   15,   42,  110,   15,   41,  110,   16,   42,  111,   17,
      44,  112,   17,   43,  112,   18,   45,  113,   19,   46,  114,   20,   47,  115,   15,   35,
      84,   21,   48,  116,   21,   48,  115,   22,   49,  116,   23,   50,  117,   24,   51,  118,
      25,   52,  119,   27,   54,  120,   28,   55,  121,   29,   56,  122,   30,   57,  123,   31,
      58,  124,   32,   59,  125,   34,   61,  126,   36,   63, -128,   37,   64, -127,   38,   65,
    -126,   30,   58,  123,   32,   60,  125,   33,   61,  126,   34,   62,  127,   39,   67, -125,
      41,   70, -123,   40,   68, -125,   41,   69, -124,   42,   70, -123,   43,   71, -122,   44,
      72, -121,   45,   74, -120,   45,   73, -120,   46,   75, -119,   47,   75, -118,   48,   77,
    -117,   49,   78, -116,   48,   76, -118,   50,   79, -115,   51,   80, -115,   52,   81, -114,
      53,   82, -113,   54,   83, -112,   54,   83, -113,   55,   84, -111,   56,   85, -110,   56,
      85, -111,   57,   86, -110,   58,   87, -109,   59,   88, -108,   60,   89, -107,   61,   91,
    -106,   61,   90, -107,   62,   91, -106,   63,   93, -104,   63,   92, -105,   64,   93, -104,
      65,   94, -103,   66,   95, -102,   67,   96, -101,   68,   98, -100,   69,   99,  -99,   68,
      97, -101,   70,  100,  -99,   72,  102,  -97,   71,  101,  -98,   73,  104,  -96,   73,  103,
    -96,   74,  104,  -95,   75,  105,  -94,   76,  107,  -93,   77,  108,  -92,   77,  107,  -92,
      76,  106,  -94,   78,  109,  -91,   79,  110,  -91,   80,  111,  -90,   82,  113,  -88,   81,
    112,  -89,   83,  114,  -87,   84,  115,  -86,   85,  116,  -85,   87,  118,  -84,   89,  120,
    -82,   88,  119,  -83,   90,  121,  -81,   89,  120,  -83,  105, -115,  -53,   91,  123,  -80,
      93,  125,  -78,   92,  123,  -80,   91,  122,  -81,   93,  124,  -79,   95,  126,  -77,   94,
    125,  -78,   96,  127,  -76,   97, -128,  -76,   98, -126,  -75,   98, -127,  -75,  100, -125,
    -73,  100, -124,  -73,   99, -126,  -74,  101, -123,  -72,  103, -121,  -70,  102, -123,  -71,
    102, -122,  -71,  105, -119,  -68,  104, -120,  -69,  107, -117,  -67,  106, -118,  -68,   31,
      59,  102,   36,   64,  104,  107, -116,  -66,  108, -115,  -66,  110, -113,  -64,  109, -114,
    -65,  112, -111,  -62,  111, -112,  -63,  113, -110,  -61,  112, -112,  -63,  114, -109,  -61,
    116, -107,  -59,  115, -109,  -60,  115, -108,  -60,  118, -105,  -57,  117, -106,  -58,  120,
    -103,  -55,  119, -104,  -56,  121, -102,  -54,  119, -104,  -57,  122, -101,  -54,  121, -102,
    -55,  124,  -99,  -52,  123, -100,  -53,  125,  -98,  -51, -117,  -83,  -32,   72,  124,  -61,
    126,  -96,  -50,  124,  -99,  -53,  126,  -97,  -51, -127,  -93,  -48, -128,  -95,  -49, -125,
    -91,  -46, -126,  -92,  -47, -123,  -89,  -44, -124,  -90,  -45, -121,  -87,  -42, -122,  -88,
    -43, -118,  -84,  -40,  106,  -98,  -36,   97, -119,  -72, -123,  -85,  -38, -120,  -85,  -41,
    -117,  -82,  -39, -119,  -84,  -41, -114,  -80,  -37, -115,  -81,  -38, -116,  -82,  -39, -112,
    -77,  -35, -113,  -79,  -36, -110,  -75,  -33, -111,  -77,  -34, -111,  -76,  -34, -107,  -72,
    -30, -108,  -73,  -31, -109,  -74,  -32, -106,  -71,  -30, -107,  -72,  -31, -104,  -69,  -28,
    -105,  -70,  -29, -102,  -67,  -26, -103,  -68,  -27,  109,  -93,  -34, -100,  -65,  -25, -101,
    -66,  -26, -102,  -67,  -27,  -97,  -61,  -22,  -98,  -63,  -23,  -98,  -62,  -23,  -99,  -64,
    -24,  -95,  -59,  -20,  -96,  -61,  -21,  -93,  -57,  -18,  -95,  -60,  -21,  -93,  -58,  -19,
    -97,  -73,  -46,  -95,  -72,  -46,   45,   88, -126,   74,  122,  -87,  -94,  -58,  -20,  -91,
    -55,  -17,  -90,  -54,  -16,  -70,  -52,  -34,   78,  -99,  -29,   74, -114,  -51,  109,  -71,
      -9,  -85,  -52,  -25, -124,  -81,  -47,   65, -115,  -62,  124,  -78,  -40,  -86,  -43,  -12,
    -77,  -63,  -53,  105, -127, -113,   67,  -98,  -51,   73,  -86,  -38,  103,  -74,  -33,   77,
    -70,  -21,  104,  -57,  -23,  -72,  -27,  -13, -127,  -45,  -23, -104,  -33,  -13,  -61,  -18,
      -6, -111,  -36,  -16, -101,  -49,  -37, -101,  -34,  -25,  127,  -47,  -37,  -85,  -27,  -40,
    -111,  -28,  -65,  -99,  -21,  -57,    0, -128,    0,  -48,   -8,  -51,   -1,   -1,    0, -128,
    -128,    0,  -69,  -73,  -97,  -83,  -99,  105,  123,  110,   81,  -87,  -91, -100,   -1,    0,
       0, -128,    0,    0, -110, -118, -118, -128, -128, -128,   64,   64,   64,   33,   -7,    4,
       1,    0,    0,    0,    0,   44,    0,    0,    0,    0,   16,    0,   16,    0,    0,    8,
      59,    0,    1,    8,   28,   72,  -80,  -96,  -63, -125,    8,   19,    6,   72,   88,   48,
    -128,   63, -122,    3,   29,   62, -124,   40,   17,   34, -128, -118,   11,   21,   -6,  123,
    -24,   80,   35,  -57, -115,    8,   49, -126,   60,   40, -110,  -93,  -63, -110,   23,   39,
      70,    4, -119,  114,  -27,  -57, -105,   22,   99,   50,   12,    8,    0,   59
    class WINDOWS_XP_GROW_BOX {
        public final static byte[] BYTES = {
      71,   73,   70,   56,   57,   97,   16,    0,   16,    0,   -9,    0,    0,   -8,  -70,   43,
    -45,  -94,  -97,    0,   90,  -20,  -39,  -39,  -40,    2,  106,   -2,  -26,  111,   82,  113,
    111,  100,  -11,  -11,  -15,  -14,  -14,  -18,  121,  -98,  -66,  -52,  -51,  -53, -111, -103,
    -48,  111,  114,  -78,  -83,  -80,  -45,  -13,  -21,  -76,  -30,  -36,  -53,  -74,  -57,   -8,
       0,    0,    0,    1,  -79,    3,  -39,  -30,   -7,  -98,  -74,  -41,  -36,   10,   25,   73,
    124,  -10,   -2,   -5,  -11,  -32,   77,   47,    0,   75,  -30,  -91,  -90,  -91,  -28,  -24,
      -8,  101,  -79,   -8,   53,  112,  -12,   83, -118,   -9, -110,  -44,   -8,  -84,  -88, -103,
       1,   19, -117,   62, -106,   -1,  -17,  -19,  -34,    3,  101,  -15,   33, -105,  -76,  -82,
    -70,  -19,  -77,  -14,   -5,   -7,  -56,   76,    0,   90,  -12,  -15,  -17,  -30,  -44,  -37,
      -8,  120,  -56,  -21,   20,   72,  -69,    0,  -98,  -57,    0,   60,  116,  -65,  -63,  -65,
       1,   68,  -48,    1,  -37,    0,   81,  -90,   -8,  -74,  123,    0,  -28,  -28, -109,  -51,
    -42,   -7,  -59,  -59,  -59,   -8, -103,  116, -105,  -78,  -17,  -97,  -95,  -98, -122, -119,
    -65,  -10,  -84,   76,   75,   75,   74,    1,   67,  -49, -122, -126, -122,    3,  113,   -1,
       8,   85,  -35,  104, -107,  -11, -100, -103, -100,  -33,  -33,  -35,    0,   30,  -95,  -86,
      47,   10,  -20,  -23,  -40,  -38,  -43,  -61,   -7,   -7,   -7,  -35,  -75,  -59,   44,   44,
      43,  -17,  -18,  -13,   43, -112,   -1,    0,   61,  -36,   98,   98,   98,  -63,  -59,  -27,
      -3,  -49,  108,    6,   44,  -86,  -45,  -59,  -80,   -2,   -3,   -6,  -28,  -27,  -28,   50,
    106,  -63,  -79,  -77,  -80,  -61,  -49,   -7,   85,  -68,  -44,  -16,  -45,  -18,   35,   89,
    111, -104,  -49,  -52,   53,  -84,  -56,  -86,  -85,  -69,    0,   88,  -26, -113, -115, -114,
      65, -110,  -89,   74,  -83,   96,  -70,  -70,    0,  -77,  118, -111,   -5,  -17,  -41,  -14,
    -13,   -8,  -19,  -21,  -26,   21, -124,  -24,  -45,  -41,  -19,    0,   72,  -15,  124,  121,
    119,  -94,  -98, -105,   22,  106,  -18,  -28,  -12,   -5,  -21,  -52,  -67,    6, -124,   92,
      -4,  -36, -111,    0,   85,  -22,  -99, -106,  -74,  -54, -114,   82,    0,   83,  -31,   97,
    -85,  -70,  -37,  -35,  117,  -19,  -28,  -58,   -5,   -9,  -12,  120,  -72,  -53,   18,  -92,
    -55,   -1,   -1,   -1,   81,  -22,   70, -112,   -7, -123,    7,   49,  -39,  105,   40,   19,
    -11,   -4,   -5,   -5,  -10,  -20,  -72,   94,  108,    3,   54,  -66,  -71,  -71,  -70,  120,
    -84, -121,    1,   97,  -21,    8,   49,  -39,  -24,  -25,  -25,  -55,  -49,  -22,  -11,  -13,
    -27,   -5,  -62,   59,   32, -122,   -2,    0,   54,  -21,    7,  120,  -92,  105, -107,  -52,
      36,  106,  -11,  -20,  -20,  -18,   -5,  -10,  -25,   34,  120,   -7,    0,   25,  -49,  -69,
    -67,  -36,   27,   94,  -27,   80, -102, -100,  -23,  -70,   83,  -21, -124,   89,    2,  107,
    -12,   80,  -80,  -90,  106, -114,  -96,   44,   89,  -60,    1,  100,   -7,    0,   81,  -27,
      -8,   -7,   -3,  -46,  -61,  -37,    0,   38,  -59,  -72,  -76,  -94,  -25,  -31,  -37,   39,
      96,  -27,   -6,  -63,   14,   -4,  -13,   -3, -109,  -92,  -73,    0,   38,  -45,   41, -103,
    -56,   27,   90,  -15,  -59,  -39,  127,  -74,  -67,  -68,   23,   85,  -33,   15,   92,  -25,
      10,  102,  -12,   -2,  -20,  -22,  -23,  -26,  -33,    3,   62,  -56,    6,   77,  -42,  -16,
    -16,  -21,    4,   84,  -25,  -24,  -27,  -45,   -4,   -4,   -5,    0,   55,  -45,  -96,  -64,
      43,    0,   85,  -27,   -1,  -17,  -53,  -33,  -28,  -50,    6,   88,  -28,  -31,  -26,  -24,
       0,   96,   -8,    0,   84,  -29,    0,  101,   -3,   -9,  -17,  -25,    0,   96,   -4,  -57,
    -53,   45,   -2,   -5,  -18,   -2,   -2,   -2,  -64,  -70,  -84,   63,   64,   61,  -11,  -12,
    -36,  -48,  -44,   80,  -69,  -85,  -66,  -60,  -58,   30,    0,  123,    2,    1,  114,  -34,
      -5,   -5,  -27, -110, -111, -107,   -9,  -21,  -32,  -97, -105, -127,   -6,  -23,   78,   -7,
    -26,   96,   58, -104,  -26,  -46,  -45,  -46,   42, -107,  -22,   -4,   -4,   -1,   -1,   -1,
    -83, -112,   -1,   -1,   50, -119,   -3,   32,   45,   52,   17,   20,   18,  108,  108,  108,
    -40,  -68, -113,   73, -123,  -43,   33, -115,  -34,  -15,  -14,  -23,   70,  -87, -109,  -13,
    -29,  -30,   21,  126,  -32,    3,  104,   -6,   -3,   -2,   -2,  -65,  -36,  -57,   21,  111,
      -9,   -1,   -2,   -3,   37,  -20,   32,   17,   78,  -33,  -21,  -24,  -32,   11,  104,   -9,
      14,   80,  -19,   -8,   -4,   -3,   -2,   -5,   -5,  -42,  -48,  -59,  -11,  -11,  -22,  -48,
    -33, -107, -122, -112,  105,  112,   89, -118,  -21,  -24,  -40,  126,  -92,  -14,  -97,  -96,
    -53,  -33,  -62,  -24,  -49,  -79,   59,  127,  109,   76,    7,   79,  -22,   33,   -7,    4,
       1,    0,    0,   17,    0,   44,    0,    0,    0,    0,   16,    0,   16,    0,    0,    8,
      61,    0,   35,    8,   28,   72,  -80,  -96,  -63, -125,    8,   19,   42,   36,   72, -118,
    -44,  -62, -127,   13,   -7,   60,   20,  -56,   71,  -30,  -60, -123,   13,    5,  102,   76,
      24,   49,   66,  -57, -124,   21,   41,   90,  -68,   88,  112,  -93,   73, -121,   37,   73,
      73,  -20,   -8, -111,   96,  -56,    8,   47,   95, -110, -100,   25,   33,   32,    0,   59
    }

    Yes, <expletive deleted>, I know what Google is.You need to calm down.I was perfectly calm. You're the one saying "Oh, but maybe you don't know what a "google" is.", which intended or not, sounds like you're calling me a jackass for not looking it up myself. Nevermind the fact that you said in the post before that that you didn't even know what it was called. So why should I assume that searching the web for "grow box" is going go give me any results at all that have to do with what you're trying to do?
    Hmm, perhaps you are just being argumentative, and
    wasting my time, but I'll assume that there are
    enough people out there like you who don't understand
    what the purpose of that doohickey in the southeast
    corner of a window is to make responding to your
    questions worth my time. I'm not doing it to waste your time. I'm quite sure I've never seen what you linked to on an application before. At least not like what that link showed. If you had said, for example, like Internet Explorer has that little larger box in the lower right corner which acts as a larger handle for window resizing, then I might have understood you from the start.
    By the way, are you
    suggesting by your question that it is perhaps
    unnecessary to fulfill the expectations of the end
    user with respect to the graphical user interface of
    a Java program, just because the given defacto Java
    component already has very nearly the bare minimum
    functionality expected of a comparable gui component
    that can be found in any other relatively advanced
    development kit?I would expect that a frame-like container component that supports manual resizing by the user would supply some location to click to initiate that resize. How is up to the component.
    Okay, so why would you need a handle to resize the
    frame?
    Reason #1: Because it aids the user in resizing the
    window. It's a bigger, albeit not by much, area on
    the screen for which the user can aim for in the
    mouse movement action to "grab" onto a portion of the
    current window in order to resize it, reducing the
    amount of time wasted on window arrangement.As mentioned above, if something like what IE has, or Netscape or FireFox has in the lower right, then fine.
    Reason #2: Because I'll bet you dollars to donuts
    that the web browser you are using right now to view
    this rapidly decaying topic has one. Shouldn't your
    program be at least as good as, say,
    IExploder? Why not at least try to make your
    Java program seem like it didn't just fall off the
    Swing turnip truck?I wouldn't use the terms "at least as good as" and IE in the same sentence. But that's just me. But as I mentioned above, I don't think you were clear on what you were talking about.
    You started by saying "grow box" like this was some common name for something assuming everyone would know what you were talking about. Then when asked, you admitted you didn't know what it was really called, which as mentioned, I think precludes one from "googling" that term, as the results, IMO, can be assume to not necessarily be what you are talking about. Then you post a specific link to a site that you say is more or less what you have in mind, but that's refering to very old Mac applications as an example, instead of (as in your most recent post) just refering to a very common modern application as an example.
    Reason #3: Because, maybe it looks cooler that way.
    I mean, geez, man. You don't have to
    o use it. It was just an idea. Gawd.I think you need to calm down. If you can't be more clear initially what you're talking or give modern references to site examples, then you shouldn't expect everyone on a text-only forum can read your mind and figure out what you really are looking for.
    Addressing the second problem I was having with this
    class, adding this line:
    System.setProperty("sun.awt.noerasebackground",
    ound", "true");
    gets rid of the flicker, but not the performance hit.
    Got this from the misnamed not_a_genius in "Re:
    : Transparent windows."
    (http://forum.java.sun.com/thread.jspa?threadID=391403
    &messageID=3067048#3067048).http://www.javadesktop.org/forums/thread.jspa?threadID=6437&tstart=15
    You basically are either going to have to live with the performance hit or the flicker, from what I can see. At least til Java 6 next year. Or you can check
    https://swinglabs.dev.java.net/
    and see if they have a fix that is backwards compatible with Java 1.5. I thought I heard said that they made something available to fix the "gray rectangle" problem... I could be wrong about that, though.
    Anyway, for real info, you can read thru:
    http://weblogs.java.net/blog/chet/archive/2005/04/swing_update_no_1.html

  • Setting the Background Graphics for a JFrame

    Hi,
    I am designing a GUI-based game and intend to set the background of my JFrame to a specific jgp graphic...I can't seem to find any method for setting the background in the JFrame API.
    Kindly assist!
    Cheers.

    Your search is perhaps marred by being too specific to frame. It is generally considered a bad idea to do any 'custom' thing directly to a frame. Try this search instead.
    [http://www.google.com/search?q=background+panel+java]

  • Tutorial/HowTo for Java JFrame image map

    I am a first semester Java student and am required to program a very simple game. I want to paint a Super Mario theme using 30x30 pixel blocks. I have been going through tutorials for a few days on displaying gif/jpg/png within a jframe and all use different methods. Does anyone know of a tutorial that focuses on building a jframe that is filled by a 2d array, each array element being a png?
    I am new to JFrames having spent most my time with console/text only programming.

    Welcome to the Sun forums.
    atozer wrote:
    I am a first semester Java student and am required to program a very simple game. A 'very simple' game would be console based. Perhaps 'hangman' would fit the bill.
    .. I want to paint a Super Mario theme using 30x30 pixel blocks. Huhh.. What does 'theme' mean to you? Searching for 'Super Mario theme' all I could see was hits for the theme music. I take it this would be better described in a screen-shot. Do you have a link to one?
    ..I have been going through tutorials for a few days on displaying gif/jpg/png within a jframe and all use different methods. Does anyone know of a tutorial that focuses on building a jframe that is filled by a 2d array, each array element being a png?1) It is generally not a good idea to consider a root component such as a JFrame, JApplet, JWindow or JDialog to be the 'main area' of the GUI. Instead it is more common/useful to put the main GUI into a JPanel, which can then be put into any of the root level components as needed.
    2) I suspect your tutorial requirements are too specific for any single tutorial. Instead look to tutorials on 2D rendering, and separately to tutorials dealing with images. The 'creating a GUI' part is worthy of its own tutorial as well.
    I am new to JFrames having spent most my time with console/text only programming.And we come back to. A simple game is a console based game.
    If somebody was set on learning to create GUIs, I would recommend they steer clear of GUI based games until they have completed a number of GUI based projects that do not involve custom painting (let alone the intricacies of doing custom painting in a responsive game that renders at nn FPS), which I suspect is what is needed to recreate a Super Mario game.
    KISS!

  • Icon for the JFrame

    Hi,
    I just wanted to start with WebStart. So I tried a very simple piece of code. A little Swing Window with a panel in it etc.I want the window to have its own Icon in the upper left corner under windows. First I wrote as I would do normaly for my java applications. There is the following in the main method of my own JFrame Class called Tis (public class Tis extends JFrame):
    public static void main(String[] arg) {                    // Hauptmethode
         Image programmIcon = Toolkit.getDefaultToolkit().getImage("tis.gif"); // Icon
               Tis myTis = new Tis();                    // Instanz von Tis erzeugen
         myTis.setIconImage(programmIcon);               // Icon zuweisen
         myTis.show();                         // Fenster anzeigen
    }Now I learned that I need A classLoader to access Files.So I changed the code to:
    public static void main(String[] arg) {                    // Hauptmethode
               Tis myTis = new Tis();                    // Instanz von Tis erzeugen
             ClassLoader cl = myTis.getClass().getClassLoader();     // Aktuellen Classloader holen
         Image programmIcon  = new ImageIcon(cl.getResource("tis.gif"));   // Icon erzeugen
         myTis.setIconImage(programmIcon);               // Icon zuweisen
         myTis.show();                         // Fenster anzeigen
    }The Problem is, that the classLoader will only load to a ImageIcon what is a Swing Class. But The JFrame's setIconImage() is inherited from the basis AWT Class and will not accept the ImageIcon class. I read through the docs and faqs, but found no answer to this. unfortunatly I cant find the source-code for the demos that sun is providing on this website. So at this moment I cant help myself any further.
    Someone an idea ?
    Thanks
    - Frankie

    And remember to use the exact case!
    Example:
    Under Windows you would get away first with
    getImage("tis.gif")in case the file is stored on the disk and later in the jar file
    as "Tis.gif" and you execute your programm via java foo.class.
    When you later execute from a jar (using java -jar foo.jar) and/or via Web Start you would suddenly miss that resource, as the jar loader is case sensitive!
    Regards,
    Marc

  • KeyListener for a simple game?

    I am begining to write a simple game, and I can't get the keyListener to work. I had written a pong game on jdk 1.3.1 and it worked fine. I used public boolean keyDown(Event e,int key){. I have been looking all over and can't find a keyListener that works on 1.4.1. Any help would be appreciated. So far I have:
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    public class Game extends Applet implements KeyListener,Runnable {
         Toolkit toolkit = Toolkit.getDefaultToolkit();
         static Image picture;
         Graphics offscreen,g;
         Image image;
         int x=1,y=10;
         static final int REFRESH_RATE = 200;
         Thread animation;
         public void update(Graphics g) {
              paint(g);
         public void init(){
              image=createImage(500,300);
              offscreen = image.getGraphics();
              picture = toolkit.getImage("Neo1.gif");
         public void start(){
              animation = new Thread(this);
              if(animation!=null){
                   animation.start();
         public void paint(Graphics g){
              offscreen.drawImage(picture,75+y,60,32,46,this);
              g.drawImage(image,0,0,this);
         public void keyTyped(KeyEvent e){
            //Anything I put here does not work???
         public void keyPressed(KeyEvent e){
         public void keyReleased(KeyEvent e){
         public void run(){
              while(true){
                   repaint();
                   try{
                        Thread.sleep (REFRESH_RATE);
                   } catch(Exception exc) { };
         public void stop(){
              if(animation!=null){
                   animation.stop();
                   animation=null;
    } [code/]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

    Heres my actual code that does work:
      class PlayingPanel extends JPanel implements KeyListener {
        public PlayingPanel() {
          this.setBackground(Color.black);
          this.grabFocus();
        public void paintComponent(Graphics g) {
          super.paintComponent(g);
          g.setColor(Color.black);
          g.drawRect(0, 0, this.getWidth(), this.getHeight());
          g.setColor(Color.white);
          g.drawString("Right: " + keys[0], 10, 15);
          g.drawString("Up: " +    keys[1], 10, 30);
          g.drawString("Left: " +  keys[2], 10, 45);
          g.drawString("Down: " +  keys[3], 10, 60);
          g.drawString("Released: " +  keys[4], 10, 75);
          g.drawString("Typed: " +  keys[5], 10, 90);
          g.drawString("Pressed: " +  keys[6], 10, 105);
        public boolean isFocusTraversable() {
          return true;
        public void keyReleased(KeyEvent e) {
          if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            keys[0] = false;
          } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            keys[2] = false;
          if (e.getKeyCode() == KeyEvent.VK_UP) {
            keys[1] = false;
          } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            keys[3] = false;
          keys[4] = true;
        public void keyPressed(KeyEvent e) {
          keys[6] = true;
        public void keyTyped(KeyEvent e) {
          if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            keys[0] = true;
          } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            keys[2] = true;
          if (e.getKeyCode() == KeyEvent.VK_UP) {
            keys[1] = true;
          } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            keys[3] = true;
          keys[5] = true;
      }Hope this helps.
    CoW

  • KeyListener for a command line application

    Hi, I'm currently writing a small command line application aimed at interacting with a database. I would like to implement features such as an automatic completion of the names with the TAB key. My problem is that the objects that sends KeyEvent are AWT components, and they do that when they have the focus. So I was wondering if any of you had already done such a thing. Until now, I tried lots of things, but there is no way I can catch a KeyEvent within the command line...
    Souk

    Write your own input routine that internally reads a character at a time instead of a complete line or string and test the character for tab etc. and take the appropriate action. This also means you will have to/can implement editing keys.
    If there is a Java class that has an dictionary to compare input with I do not know, because the last time I wrote a tool without gui was in 1985.
    Regards
    SH

  • Waiting for a JFrame to dispose

    Hello,
    i have this situation: i have 2 JFrames, let's say JFrame1 and JFrame2.
    JFrame1 invokes and displays JFrame2, like this:
    /* JFrame1 code */
    JFrame2 f2 = new JFrame2();
    f2.setVisible(true);
    //wait f2 to dispose
    other code...My problem is how to make JFrame1 to wait until the dispose call of the other frame, and then execute the "other code"...
    I tried some solutions..like a busy waiting on some flag..or using a CountDownLatch..but i didn't solve the problem...
    It seems that everything i use to stop the execution of JFrame1, stops JFrame2 also, even if i run it as a new thread.
    I hope someone can give me some hints!

    Use a modal JDialog.
    [http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html]

  • How do i remove the taskbar icon for a JFrame from a windows taskbar?

    I have an undecorated JFrame (no titlebar, minimize, restore, close buttons) and i want to remove it being shown on the windows taskbar. is there a way in Swing to do this?
    If not is there any other way i can show some content that appears free floating on the desktop and does not show up on the taskbar.

    GauravTaneja wrote:
    Can the JDialog be undecorated? as in i don't want the little close button or the title bar on the dialog. just the content pane to display something.I don't know. Check the API.
    Also, i want this to be on top of all windows.So make it a modal JDialog.

Maybe you are looking for

  • Transfer HI8 to One Touch DVD, Need suggestions!

    Hey everyone, I have a Hi8 Sony Cam that I have prob 60 tapes of home movies for (Yes I know my dad filmed everything)! I want to first tell you my ultimate goal is to transfer all of these tapes onto DVD's over the course of the next year or so. I w

  • Can we transport a smartform between IDES and DEV

    Hi experts ,                 as we got a problem in Dev syst , i had developed a samrtform in IDES. Can any one tell me tht ...Is there any way to transport a smartform from IDES TO DEV system or should i develop from scratch in DEV. regards, shashan

  • Patch showing not installed but it is on the server

    I have KB2904266 deployed on all my servers. On my compliance reports it is showing its missing on 17 servers. This patch was release in December 2013 and I can see that it was installed on the servers in January 2014 which lines up to what I have de

  • Multiple Colored Links in Dreamweaver CS4??

    Hi all, I have gone through a million articles and can't find the answer I'm looking for so hopefully someone here will help me.  I have an existing style sheet for my website, I know how to change the page preferences so that all the links can have

  • Again - Active Directory Management Pack - AD MP - SCOM 2012R2 - AD 2012R2 - Action / RunAs Account permissions

    Hi, after reading many Posts and Blogs i came to the conclusion that it is still unclear to me what is needed to Monitor Active Directory successfully and what is the securest way configuring the RunAs or Action Account. I hope the experts here can m