Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Problem with running a GUI Form

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Problem with running a GUI Form

    Hi,

    I'm new to java programming and am having a problem with running a GUI Form. I'm using NetBeans and it is showing an error at this part of the coding:


    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextField1;
    I have copied the coding from a book and it is supposed to allow the user to enter some text (in a textfield) and then push the button, which results the text that was entered appear (as a label) below the button after the button is pressed. How do I make it work? Please can you help. Here's the coding:

    package test1;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.awt.event.ActionListener.*;
     
     
    public class PushMe extends javax.swing.JFrame implements ActionListener {
     
        JTextField jTextField1 = new JTextField(15);
        JButton jButton1 = new JButton ("please push me");
        JLabel jLabel1 = new JLabel ("Enter some text and push the button", JLabel.CENTER);
     
     
        /**
         * Creates new form PushMe
         */
        public PushMe() {
            initComponents();
     
           setTitle("Push Me");
           setLayout(new FlowLayout());
           setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           setSize(220,120);
           setLocation(400,300);
           add(jTextField1);
           add(jLabel1);
           jButton1.addActionListener(this);
           setVisible(true);
     
     
     
        }
     
     
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            jButton1 = new javax.swing.JButton();
            jTextField1 = new javax.swing.JTextField();
            jLabel1 = new javax.swing.JLabel();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                    jButton1ActionPerformed1(evt);
                }
            });
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(41, 41, 41)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 287, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(72, Short.MAX_VALUE))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(121, 121, 121))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(86, 86, 86)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(34, 34, 34)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(30, 30, 30)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(62, Short.MAX_VALUE))
            );
     
            pack();
        }// </editor-fold>                        
     
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     
     
            // TODO add your handling code here:
        }                                        
     
        private void jButton1ActionPerformed1(java.awt.event.ActionEvent evt) {                                          
           String myText;
           myText = jTextField1.getText();
           jLabel1.setText("You entered: " + myText);
     
        }                                         
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
     
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(PushMe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(PushMe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(PushMe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(PushMe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
            /*
             * Create and display the form
             */
            java.awt.EventQueue.invokeLater(new Runnable() {
     
                public void run() {
                    new PushMe().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JTextField jTextField1;
        // End of variables declaration                   
     
        @Override
        public void actionPerformed(ActionEvent ae) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with running a GUI Form

    it is showing an error
    Pleas copy the full text of the error message and paste it here.
    Is it a compiler or a run time error?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem with running a GUI Form

    Thanks, the following shows the error message for the coding below.

    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration
    For each of these components (label, textfield and button) the error states: jButton1 is already defined in test1.PushMe.

    It says the same for the jTextfield1 and jLabel1. 'test1.PushMe' is the name of the Project/Package.

    Also, its a compile time error.

    Where is shows the error, it cannot be modified because NetBeans did it once I placed the components onto the form. So, maybe this part of my coding is causing the problem because I may have declared it again here -

    JTextField jTextField1 = new JTextField(15);
    JButton jButton1 = new JButton ("please push me");
    JLabel jLabel1 = new JLabel ("Enter some text and push the button", JLabel.CENTER);
    But if the above coding is the problem then it makes it harder because the book I copied it from has shown it like this whereas NetBeans has already declared it. Not sure if I'm making sense

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with running a GUI Form

    is already defined
    The message says what the problem is. A variable can only be defined one time. Remove the other definitions.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Aug 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem with running a GUI Form

    ok thanks for your help but if you don't mind I have another question -

    JTextField jTextField1 = new JTextField(15);
    JButton jButton1 = new JButton ("please push me");
    JLabel jLabel1 = new JLabel ("Enter some text and push the button", JLabel.CENTER);
    I've done what you said but now how can I write the above piece of code so that it does what its supposed to but without defining it again. Not sure how to write it differently so that for example the 'jTextField1' is displayed 15 columns wide, the 'jButton1' says 'please push me' and so on?

    Another thing, is it better to place the code under here -

    public class PushMe extends javax.swing.JFrame implements ActionListener {
    or under here -

    public PushMe() {
    initComponents();

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with running a GUI Form

    without defining it again
    Why would you need to define it again? It should only be defined once.

    is it better to place the code
    What code? You show a class definition and a method definition.
    If you are defining variables that you want to access in all the methods of the class, define them at the class level.
    If the variables are only used in a method, define them in the method.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following 2 Users Say Thank You to Norm For This Useful Post:

    Pane87 (August 5th, 2012), saopayne (August 17th, 2012)

Similar Threads

  1. Problem with running a GUI form
    By Pane87 in forum AWT / Java Swing
    Replies: 0
    Last Post: August 4th, 2012, 06:03 AM
  2. Problem running .jar in Windows 7
    By SpiceProgrammer in forum Java Theory & Questions
    Replies: 3
    Last Post: December 21st, 2011, 01:28 AM
  3. Running into a problem with my program.
    By letsgetlifted in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 9th, 2011, 07:09 PM
  4. Problem in a web that uses javascript in his login form
    By kamarilla in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 1st, 2011, 05:47 PM
  5. Java Swing :Back Button from one form to another form
    By srinivasan_253642 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 26th, 2009, 09:51 AM