Results 1 to 9 of 9
  1. #1
    fsuarjun03 is offline Member
    Join Date
    Mar 2010
    Posts
    13
    Rep Power
    0

    Default What am I doing wrong here? (JButton issue)

    I've been trying to troubleshoot this and I cannot seem to figure out what I am doing wrong. There are only two errors (that I know of) in this code. I can't seem to understand why button.length in "for (int i = 0; i <=button.length; i++)" is underlined as an error and why the line after that "button = new JButton(button[i]);" is also an error.

    What do you think I should do? I am trying to create a Gridbag layout and that is the method that I am using for it.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    
    public class MyWindow
    {
        public static void main(String[] arg)
        {
            MyJFrame f = new MyJFrame(Constants.TITLE);
            JButton b;
            GridBagConstraints c = new GridBagConstraints();
    
    
    
            Toolkit toolkit = f.getToolkit();
    
            Dimension size = toolkit.getScreenSize();
    
            Rectangle r = new Rectangle(Constants.X_POS, Constants.Y_POS,
                    size.width/Constants.X_FACTOR, size.height/Constants.Y_FACTOR);
            f.setBounds(r);
            f.addWindowListener(new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                    JOptionPane.showMessageDialog(null, "The Window is closing");
                }
            });
            Container e = f.getContentPane();
    
    
    //>>>>>This is where I am having the problem<<<<<<
    
            for (int i = 0; i <= button.length; i++)
    	   {
    		button = new JButton(button[i]);
    
    		switch(i)
    		{
    			 case 0:
    				b = new JButton("Write");
    				c.gridx = 0; // Specify the (x,y) coordinate for this component
    				c.gridy = 0; // (x,y) = (0,0)
    				c.insets = new Insets(0,50,0,0);
    			break;
    		 	case 1:
    				b = new JButton("Draw");
                                    c.gridx = 1;
    				c.gridy = 0;
    				c.insets = new Insets(20,0,40,200);
                                    b.addActionListener(new ActionListener()
                                    {
                                                public void actionPerformed(ActionEvent e)
                                                {
                                                    MyGraphics myGraphics = new MyGraphics();
                                                }
                                    }
                                            );
                        break;
    			case 2:
    				b = new JButton("Image", new ImageIcon());
    				c.gridx = 0;
    				c.gridy = 1;
    				c.insets = new Insets(20,300,0,0);
                                    b.addActionListener(new ActionListener()
                                    {
                                                 public void actionPerformed(ActionEvent e)
                                                 {
                                                     Browser br = new Browser();
                                                 }
    
                                    }
                                                    )
                                                        ;
    
            		break;
    		 	case 3:
    				b = new JButton("Print");
    				c.gridx = 0;
    				c.gridy = 2;
    				c.ipadx = 200;
    				c.insets = new Insets(25,50,0,50);
    			break;
    			}
    
            e.setCursor( Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            e.setBackground(Color.white);
            e.setLayout(new GridBagLayout());
            c.fill = GridBagConstraints.HORIZONTAL;
    
    
            f.setVisible(true);
        }
    }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default

    I don't know what you're trying to do.

    There is no length variable in the JButton class, and you can't use the index[] operator on something that isn't an array. And there is no JButton constructor that takes a JButton as an argument.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    fsuarjun03 is offline Member
    Join Date
    Mar 2010
    Posts
    13
    Rep Power
    0

    Default

    I'm an idiot, ignore the second error. I changed the index operator everywhere but there and didn't realize it.

    This is just one class from a class of 10. In this class, I am attempting to create buttons in the JFrame and arrange them in a gridbag layout (I haven't put the specific coordinates yet). My teacher instructed me to do it this way although I am a little confused by his way. He had provided the "for (int i = 0; i <= button.length; i++)" and told me that I should do the rest (which I did). I'm getting the red error line under the "button" and not length. It says that it cannot find the symbol variable button. I'm just really confused. This one little error is stopping my entire application from running.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default

    Well, what is button supposed to be? An array? Where do you declare it? I see you have a JButton b, but not an array of JButtons.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    The variable 'button' isn't defined, and if it was, this line thinks it'a both an array and a JButton, which doesn't make sense:
    Java Code:
    button = new JButton(button[i])

  6. #6
    fsuarjun03 is offline Member
    Join Date
    Mar 2010
    Posts
    13
    Rep Power
    0

    Default

    Ok. So I changed a few things around. Still getting an error but I think its a step in the right direction.

    I changed

    for (int i = 0; i <= b.length; i++)
    {
    b = new JButton("");

    EDIT:

    Not supposed to be an array. Just trying to create a gridbag layout with those various buttons in it.
    Last edited by fsuarjun03; 04-29-2011 at 07:36 PM.

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

    Default

    Quote Originally Posted by fsuarjun03 View Post
    Ok. So I changed a few things around. Still getting an error but I think its a step in the right direction.

    I changed

    for (int i = 0; i <= b.length; i++)
    {
    b = new JButton("");
    If 'b' has type JButton it most certainly doesn't have a public 'length' field.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    fsuarjun03 is offline Member
    Join Date
    Mar 2010
    Posts
    13
    Rep Power
    0

    Default

    What would you guys do if you were in my position? How would you correct my mistake? I'm getting frustrated at myself because this one little error is preventing my entire project from running.

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default

    Honestly, this is a pretty weird way to go about setting up 4 JButtons. When are you adding them to anything? I would ditch the for loop completely, and work on them one at a time.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. JButton with Icon & text alignment issue
    By ShardaD in forum AWT / Swing
    Replies: 4
    Last Post: 01-11-2011, 11:10 AM
  3. JButton issue
    By globo in forum New To Java
    Replies: 16
    Last Post: 11-14-2010, 02:35 AM
  4. Replies: 7
    Last Post: 08-29-2010, 10:33 AM
  5. Lo4j issue - logs are written in a wrong file
    By Dhamo in forum Advanced Java
    Replies: 0
    Last Post: 05-09-2010, 09:11 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
  •