Results 1 to 4 of 4
  1. #1
    Borneq is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default The trouble with handling the button clicks

    I compile it under Eclipse. I have a static main method: public static void main(String[] args)- main must be static? I have three buttons and a panel created in the main:
    MainPanel JPanel = new JPanel ();
    JButton Button1 = new JButton ();
    JButton Button2 = new JButton ();
    JButton Button3 = new JButton ();
    Now I want to add a click event handler for Button1:
    Button1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    Button1ActionPerformed(evt);
    }
    });
    Cannot it find Button1ActionPerformed so Quick Fix insert "private void Button1ActionPerformed(ActionEvent evt)" directly below the code actionPerformed in an object ActionListener.
    Now I want to insert the code:
    if (Button2.isVisible())
    Button2.setVisible(false);
    else
    Button2.setVisible(true);
    And there are error in Button2: "Cannot refer to a non-final variable Button2 inside an inner class defined in a different method"
    When I change Button2 as final is not showing me the error. But what are the consequences of the final?
    Instead, I want to move Button1ActionPerformed beyond main method.
    However, after moving only when I want to save, appears dialog "Hot Code Replace Failed - Add method not implemented" - what to do to not show up?
    In order to be be visible Button2 I insert in the class MainForm.
    ****private JButton Button1;
    ****private JButton Button2;
    ****private JButton Button3;
    ****private JPanel mainPanel;
    Now for "Button2 = new JButton()" I have error "Cannot make a static reference to the non-static field Button2", also for called Button1ActionPerformed I have "Cannot make a static reference to the non-static Button1ActionPerformed(ActionEvent) from the type MainForm".
    That means that every field to which I refer from them main must be static and also every function? And how to get rid warning dialog when I save text?

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class NotStatic {
        private JPanel getButtonPanel() {
            JButton button1 = new JButton("Button 1");
            JButton button2 = new JButton("Button 2");
            JButton button3 = new JButton("Button 3");
            button1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    button1ActionPerformed(evt);
                }
            });
            JPanel panel = new JPanel();
            panel.add(button1);
            panel.add(button2);
            panel.add(button3);
            return panel;
        }
    
        private void button1ActionPerformed(ActionEvent e) {
            System.out.println("button1ActionPerformed");
        }    
    
        public static void main(String[] args) {
            // Instantiate an instance of the enclosing class.
            // Save a reference to it in a local variable, "app".
            NotStatic app = new NotStatic();
            // Use this variable to call methods in the enclosing class.
            // This avoids the heartache of making everything [i]static[/i].
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(app.getButtonPanel(), "Last");
            f.setSize(400,200);
            f.setLocation(200,200);
            f.setVisible(true);
            // You can still call static methods if you like.
            test();
        }
    
        private static void test() {
            System.out.println("hello world");
        }
    }

  3. #3
    Borneq is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default

    Very thanks, it works! I try another way: use "class Frame extends JFrame"
    It works but is one warning: "The serializable class Frame does not declare a static final serialVersionUID field of type long"
    Why must be serialVersionUID? "public final class MainForm" not must.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Borneq View Post
    Very thanks, it works! I try another way: use "class Frame extends JFrame"
    It works but is one warning: "The serializable class Frame does not declare a static final serialVersionUID field of type long"
    Why must be serialVersionUID? "public final class MainForm" not must.
    A serialVerionUID is used for the (de)serialization of objects of that class; if those UIDs match objects can be deserialized. Note that eclipse issues just a warning; go to Projects>Properties>Java Compiler>Errors/Warnings, enable project specific settings and click the first item in the second group: ignore warnings for a class without serial version UIDs.

    kind regards,

    Jos

Similar Threads

  1. How to swap to images on clicks
    By ntagrafix in forum AWT / Swing
    Replies: 2
    Last Post: 12-03-2009, 08:13 PM
  2. security warning dialog button handling
    By smtra in forum Advanced Java
    Replies: 0
    Last Post: 07-17-2009, 11:02 AM
  3. having trouble with event handling and JTextField
    By v1nsai in forum New To Java
    Replies: 5
    Last Post: 06-01-2009, 02:19 PM
  4. [SOLVED] Intercepting mouse clicks
    By carderne in forum New To Java
    Replies: 6
    Last Post: 02-28-2009, 07:10 PM
  5. code for number of clicks clicked on a web page???
    By Sveta_cool in forum Advanced Java
    Replies: 2
    Last Post: 02-01-2008, 02:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •