Turn on thread page Beta
    • Thread Starter
    Offline

    0
    ReputationRep:
    Decided to transfer to NetBeans and see what the GUI maker is like now as I am comftable with swing.

    It is being a pain.

    I created a new JFrame in netbeans etc. So I have access to the widgets.

    I used the following code to see if I can just for the heck of it create buttons without dragging and dropping from the GUI maker:

    JButton button0;

    button0= new JButton();

    I get the following error message:

    init:
    deps-jar:
    Compiling 1 source file to C:\Documents and Settings\JavaApplication9\build\ classes
    C:\Documents and Settings\Desktop\RailCard\JavaAp plication9\src\Railcard\RailCard .java:123: <identifier> expected
    button= new JButton();
    1 error
    BUILD FAILED (total time: 1 second)

    I dont understand why????My code is correct surely? Thanks
    Offline

    1
    ReputationRep:
    Code:
    button0= new JButton();
    isnt correct Java, it should be:

    Code:
    JButton button0= new JButton();
    You do know how to instantiate objects right? :p:
    • Thread Starter
    Offline

    0
    ReputationRep:
    loool

    Yeah I did that too: got this error message:

    init:
    deps-jar:
    Compiling 1 source file to C:\Documents and Settings\Desktop\JavaApplication 9\build\classes
    C:\Documents and Settings\Desktop\JavaApplication 9\src\Railcard\RailCard.java:123: cannot find symbol
    symbol : class JButton
    location: class Railcard.RailCard
    JButton button0= new JButton();
    C:\Documents and Settings\Desktop\Railcard\RailCa rd.java:123: cannot find symbol
    symbol : class JButton
    location: class Railcard.RailCard
    JButton button0= new JButton();
    2 errors
    BUILD FAILED (total time: 1 second)

    So I thought it was due to this (NetBeans sticks it in automatically):

    public class RailCard extends javax.swing.JFrame {

    /** Creates new form RailCard */
    public RailCard() {
    initComponents();
    }

    which means it is inheriting everything from object JFrame?

    #I suspect that is the actual object JFrame????? (or is it a class called JFrame? cos that denotes the class we are inheriting from?)

    so I did javax.swing.*; instead. Same error <identifier> expected
    Offline

    1
    ReputationRep:
    What's on line 123? Would be helpful if you post all the code.

    Just tried this in Netbeans and it was fine, so that particular bit of code is fine in itself:

    Code:
    import java.awt.*;
    import javax.swing.*;
    
    /**
     *
     * @author Phil
     */
    public class Main {
        
        
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            JFrame window;
            JPanel panel;
            JButton button;
            
            window = new JFrame("Window");
            panel = new JPanel();
            button = new JButton("Press Me");
            
            panel.add(button);
            window.add(panel);
            
            window.pack();
            window.setVisible(true);
                    
        }
        
    }
    Are you actually importing the swing/awt packages? Looks like you're missing the one for JButton.
    • Thread Starter
    Offline

    0
    ReputationRep:
    (Original post by Phil.)
    What's on line 123? Would be helpful if you post all the code.

    Just tried this in Netbeans and it was fine, so that particular bit of code is fine in itself:

    Code:
    import java.awt.*;
    import javax.swing.*;
    
    /**
     *
     * @author Phil
     */
    public class Main {
        
        
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            JFrame window;
            JPanel panel;
            JButton button;
            
            window = new JFrame("Window");
            panel = new JPanel();
            button = new JButton("Press Me");
            
            panel.add(button);
            window.add(panel);
            
            window.pack();
            window.setVisible(true);
                    
        }
        
    }
    Yep that works fine - I suspect you created a new class and created the GUI from scratch.

    It is when you create a new class in "JFrameForm" it gives you problems.
    Code:
    package Railcard;
    
    import javax.swing.JOptionPane;
    
    /**
     *
     * 
     */
    public class RailCard extends javax.swing.JFrame {
        
        /** Creates new form RailCard */
        public RailCard() {
            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.
         */
        // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
        private void initComponents() {
            eligibleDialog = new javax.swing.JDialog();
            jButton2 = new javax.swing.JButton();
            jLabel3 = new javax.swing.JLabel();
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            CheckBox = new javax.swing.JCheckBox();
            AgeField = new javax.swing.JTextField();
            jButton1 = new javax.swing.JButton();
    
            jButton2.setText("Ok");
    
            jLabel3.setText("You are Eligible for a Rail Card");
    
            javax.swing.GroupLayout eligibleDialogLayout = new javax.swing.GroupLayout(eligibleDialog.getContentPane());
            eligibleDialog.getContentPane().setLayout(eligibleDialogLayout);
            eligibleDialogLayout.setHorizontalGroup(
                eligibleDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(eligibleDialogLayout.createSequentialGroup()
                    .addGroup(eligibleDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(eligibleDialogLayout.createSequentialGroup()
                            .addGap(171, 171, 171)
                            .addComponent(jButton2))
                        .addGroup(eligibleDialogLayout.createSequentialGroup()
                            .addGap(124, 124, 124)
                            .addComponent(jLabel3)))
                    .addContainerGap(132, Short.MAX_VALUE))
            );
            eligibleDialogLayout.setVerticalGroup(
                eligibleDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, eligibleDialogLayout.createSequentialGroup()
                    .addContainerGap(32, Short.MAX_VALUE)
                    .addComponent(jLabel3)
                    .addGap(26, 26, 26)
                    .addComponent(jButton2)
                    .addGap(24, 24, 24))
            );
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            jLabel1.setText("What is your age?");
    
            jLabel2.setText("Are you a full-time student?");
    
            CheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
            CheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
    
            jButton1.setText("jButton1");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(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.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel2)
                            .addGap(15, 15, 15)
                            .addComponent(CheckBox))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel1)
                            .addGap(30, 30, 30)
                            .addComponent(AgeField, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(133, 133, 133)
                            .addComponent(jButton1)))
                    .addContainerGap(194, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(43, 43, 43)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(AgeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(44, 44, 44)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel2)
                        .addComponent(CheckBox))
                    .addGap(42, 42, 42)
                    .addComponent(jButton1)
                    .addContainerGap(114, Short.MAX_VALUE))
            );
            pack();
        }// </editor-fold>                        
    
        //JButton button0;
        
        JButton button0= new JButton();
        
        
        
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
            
        }                                        
           
    
            
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new RailCard().setVisible(true);
                }
            });
        }
    
        
        // Variables declaration - do not modify                     
        private javax.swing.JTextField AgeField;
        private javax.swing.JCheckBox CheckBox;
        private javax.swing.JDialog eligibleDialog;
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        // End of variables declaration                   
        
    }
    Offline

    1
    ReputationRep:
    Add:

    Code:
    import javax.swing.*;
    at the top below the other import statement, or alternatively:

    Code:
    javax.swing.JButton button0= new javax.swing.JButton();
    instead of:

    Code:
    JButton button0= new JButton();
    • Thread Starter
    Offline

    0
    ReputationRep:
    (Original post by Phil.)
    Add:

    Code:
    import javax.swing.*;
    at the top below the other import statement, or alternatively:

    Code:
    javax.swing.JButton button0= new javax.swing.JButton();
    instead of:

    Code:
    JButton button0= new JButton();
    ahhh silly me. Thanks Is this correct:

    Code:
    javax.swing.JButton button0= new javax.swing.JButton();
     JFrame.add(button0); //Add button0 to default JFrame
    How can I make it appear on the default JFrame.

    because I am getting another <identifier> error for the JFrame..
    Offline

    1
    ReputationRep:
    You probably just want:

    Code:
    this.add(button0);
    As it looks like the class you're playing with there is your JFrame as it extends it, so you want to add it to this JFrame.
    • Thread Starter
    Offline

    0
    ReputationRep:
    (Original post by Phil.)
    You probably just want:

    Code:
    this.add(button0);
    As it looks like the class you're playing with there is your JFrame as it extends it, so you want to add it to this JFrame.
    I tried that as well, hmm got the same error? Did it work for you? Sorry and thanks
    Offline

    1
    ReputationRep:
    try:

    Code:
    .addComponent(button0);
    It's a bit hard to follow because it's all done by the GUI builder instead of by hand, and they seem to do things a little different to how I was taught.
    • Thread Starter
    Offline

    0
    ReputationRep:
    (Original post by Phil.)
    try:

    Code:
    .addComponent(button0);
    It's a bit hard to follow because it's all done by the GUI builder instead of by hand, and they seem to do things a little different to how I was taught.
    Same prob

    Code:
    javax.swing.JButton button0= new javax.swing.JButton();
        this.addComponent(button0);
    Yeah normally I would do it by hand, but I would prefer to mix the two so I can save time when my program has a lot more widgets.
    • Thread Starter
    Offline

    0
    ReputationRep:
    (Original post by DOJO)
    Same prob

    Code:
    javax.swing.JButton button0= new javax.swing.JButton();
        this.addComponent(button0);
    Yeah normally I would do it by hand, but I would prefer to mix the two so I can save time when my program has a lot more widgets.
    .addComponent does not work either.
    Offline

    1
    ReputationRep:
    Further proof that GUI builders are evil

    I'd suggest building the GUI from hand if you want to play around with the code, or if you just want a GUI use the GUI builder exclusively; it's far too much hassle to work with code from a GUI builder when it'd probably break it when you come to add more components later anyway.

    If you really want to add a button by hand look at the generated code and add yours in exactly the same way, but you'd learn a lot more doing it 100% by hand.
    • Thread Starter
    Offline

    0
    ReputationRep:
    (Original post by Phil.)
    Further proof that GUI builders are evil

    I'd suggest building the GUI from hand if you want to play around with the code, or if you just want a GUI use the GUI builder exclusively; it's far too much hassle to work with code from a GUI builder when it'd probably break it when you come to add more components later anyway.

    If you really want to add a button by hand look at the generated code and add yours in exactly the same way, but you'd learn a lot more doing it 100% by hand.
    Code:
    private void initComponents() {
            eligibleDialog = new javax.swing.JDialog();
            jButton2 = new javax.swing.JButton();
            jLabel3 = new javax.swing.JLabel();
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            CheckBox = new javax.swing.JCheckBox();
            AgeField = new javax.swing.JTextField();
            jButton1 = new javax.swing.JButton();
    
            jButton2.setText("Ok");
    
            jLabel3.setText("You are Eligible for a Rail Card");
    
            javax.swing.GroupLayout eligibleDialogLayout = new javax.swing.GroupLayout(eligibleDialog.getContentPane());
            eligibleDialog.getContentPane().setLayout(eligibleDialogLayout);
            eligibleDialogLayout.setHorizontalGroup(
                eligibleDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(eligibleDialogLayout.createSequentialGroup()
                    .addGroup(eligibleDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(eligibleDialogLayout.createSequentialGroup()
                            .addGap(171, 171, 171)
                            .addComponent(jButton2))
                        .addGroup(eligibleDialogLayout.createSequentialGroup()
                            .addGap(124, 124, 124)
                            .addComponent(jLabel3)))
                    .addContainerGap(132, Short.MAX_VALUE))
            );
            eligibleDialogLayout.setVerticalGroup(
                eligibleDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, eligibleDialogLayout.createSequentialGroup()
                    .addContainerGap(32, Short.MAX_VALUE)
                    .addComponent(jLabel3)
                    .addGap(26, 26, 26)
                    .addComponent(jButton2)
                    .addGap(24, 24, 24))
            );
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            jLabel1.setText("What is your age?");
    
            jLabel2.setText("Are you a full-time student?");
    
            CheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
            CheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
    
            jButton1.setText("jButton1");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(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.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel2)
                            .addGap(15, 15, 15)
                            .addComponent(CheckBox))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel1)
                            .addGap(30, 30, 30)
                            .addComponent(AgeField, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(133, 133, 133)
                            .addComponent(jButton1)))
                    .addContainerGap(194, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(43, 43, 43)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(AgeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(44, 44, 44)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel2)
                        .addComponent(CheckBox))
                    .addGap(42, 42, 42)
                    .addComponent(jButton1)
                    .addContainerGap(114, Short.MAX_VALUE))
            );
            pack();
        }// </editor-fold>
    That is the code...You can't edit it either, which is a pain

    Yeah, one other thing that I am finding difficult atm too is altho I feel comftable with swing, it is very very difficult to remember everything.

    What is the best way to remember all the techniques etc? If I get stuck at any point, should I peek into my past programs and remind myself how things are done?

    How did you get good at it all? I mean my basics are quite solid e.g. arrays/for/while/if and so on.

    Thanks for your help man
    Offline

    1
    ReputationRep:
    I'm just heading off now as it's getting a bit late, but I'll post something a bit more useful about learning how to build Java GUIs from scratch in the morning; if no-one has beat me to it, that is :p:
    Offline

    0
    ReputationRep:
    Just usually looking over past examples or the tutorials you learned from helps

    Remember, you don't actually have to remember it all, that is what reference is for
    Offline

    1
    ReputationRep:
    (Original post by slugonamission)
    Just usually looking over past examples or the tutorials you learned from helps

    Remember, you don't actually have to remember it all, that is what reference is for
    Definitely; the Java API is also great once you've got the basics out the way. A good place to start should be here.
    Offline

    0
    ReputationRep:
    And MSDN for .NET people
    Offline

    1
    ReputationRep:
    The netbeans GUI designer uses the GroupLayout by default which is how the actual designer bit works. add() won't work without extra parameters for the GroupLayout manager

    Fear not! Right click your frame and click something along the lines of 'select layout'. This will give you the option to choose a more familiar LayoutManager for your JFrame (FlowLayout, BorderLayout, BoxLayout etc.). Once you have chosen the layout you wish you can program and add your own buttons if you wish (of course there is no need to when you can drag and drop them ).

    P.S. The problem you were having earlier about not having the import statement. This should have been highlighted by a wiggly red underline (word spelling mistake-esque). If you see one of these, you can have the import statement generated automatically by hitting Alt-Shift-F. Saves some time; I dread to think how many times I hit Alt-Shift-F in a working day.
 
 
 
Reply
Submit reply
Turn on thread page Beta
TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

This forum is supported by:
Updated: July 18, 2007
Poll
Black Friday: Yay or Nay?
Useful resources

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reply
Hide
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.