Results 1 to 5 of 5
  1. #1
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default my JButtons won't show up

    Hello,

    Please have a look at this screen capture of a JFrame I'm programming:



    You'll notice that there are no buttons on the right hand side. This is a problem. I'm trying to create a JPanel to the right of the maze you see in the middle with two buttons in it, and nothing's showing up.

    Here's the code for the JFrame (I have added /* */ comments for your convenience - i.e. at the relevant lines of code):

    Java Code:
    package framecomponents;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    import java.awt.Color;
    import java.awt.GridBagLayout;
    import java.awt.GridBagConstraints;
    import java.awt.Dimension;
    import java.awt.Container;
    
    public class MazeManiaFrame extends JFrame {
    
        // panels
        private GamePanel   gamePanel  = null;
        private EastPanel	eastPanel  = null;
        private SouthPanel  southPanel = null;
    
    /************************** CONSTRUCTOR **************************/
    
        public MazeManiaFrame() {
    
    	// setup frame properties
    	setTitle("Maze Mania");
    	setSize(800, 800);
    	setLocation(200, 50);
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    	Container container = getContentPane();
    	container.setLayout(new GridBagLayout());
    
    	// create gamePanel
    	gamePanel = new GamePanel();              /* maze you see in the middle */
    	gamePanel.setBackground(Color.WHITE);
    
    	// create JPanel on each side of GamePanel
    	JPanel NWPanel    = new JPanel();
    	JPanel NorthPanel = new JPanel();
    	JPanel NEPanel    = new JPanel();
    	eastPanel  	  = new EastPanel();  /* creation of EastPanel */
    	JPanel SEPanel    = new JPanel();
    	southPanel 	  = new SouthPanel(); /* creation of SouthPanel */
    	JPanel SWPanel    = new JPanel();
    	JPanel WestPanel  = new JPanel();
    
                 /* East and South panels set their own preferred size in their constructors */
    
    	// set their preferred size
    	NWPanel.setPreferredSize(new Dimension(50, 50));
    	NorthPanel.setPreferredSize(new Dimension(400, 50));
    	NEPanel.setPreferredSize(new Dimension(50, 50));
    	SEPanel.setPreferredSize(new Dimension(50, 50));
    	SWPanel.setPreferredSize(new Dimension(50, 50));
    	WestPanel.setPreferredSize(new Dimension(50, 400));
    
    	// add panels to main container with constraints
    	GridBagConstraints constraints = new GridBagConstraints();
    	constraints.gridx = 0;
    	constraints.gridy = 0;
    	container.add(NWPanel, constraints);
    	constraints.gridx = 1;
    	container.add(NorthPanel, constraints);
    	constraints.gridx = 2;
    	container.add(NEPanel, constraints);
    	constraints.gridy = 1;
    	container.add(eastPanel, constraints);  /* add eastPanel */
    	constraints.gridy = 2;
    	container.add(SEPanel, constraints);
    	constraints.gridx = 1;
    	container.add(southPanel, constraints); /* add southPanel */
    	constraints.gridx = 0;
    	container.add(SWPanel, constraints);
    	constraints.gridy = 1;
    	container.add(WestPanel, constraints);
    	constraints.gridx = 1;
    	container.add(gamePanel, constraints);
    
    	pack();
        }
    
    /************************** END CONSTRUCTOR **************************/
    
    
    /************************** ACCESSORS **************************/
    
    /*** GET ***/
    
        // get panels
        public GamePanel  getGamePanel()  {return gamePanel;}
        public SouthPanel getSouthPanel() {return southPanel;} 
        public EastPanel  getEastPanel()  {return eastPanel;} 
    
        // get panel components
        public ClockLabel getClockLabel() {return southPanel.getClockLabel();}
    
    /************************** END ACCESSORS **************************/
    
    }
    And here's the code for EastPanel:

    Java Code:
    package framecomponents;
    
    import javax.swing.JPanel;
    import javax.swing.JButton;
    
    import java.awt.GridBagLayout;
    import java.awt.GridBagConstraints;
    
    public class EastPanel extends JPanel {
    
        public EastPanel() {
    
    	setLayout(new GridBagLayout());
    	setPreferredSize(new Dimension(200, 400));
    
    	JButton button1 = new JButton("button 1");
    	button1.setPreferredSize(new Dimension(150, 30));
    
    	JButton button2 = new JButton("button 2");
    	button2.setPreferredSize(new Dimension(150, 30));
    
    	GridBagConstraints constraints = new GridBagConstraints();
    	constraints.gridx = 0;
    	constraints.gridy = 0;
    	add(button1, constraints);
    	constraints.gridy = 1;
    	add(button2, constraints);
        }
    }
    The strange thing is I programmed EastPanel exactly as I did SouthPanel, yet southPanel shows up (with the clock below the maze), but the eastPanel doesn't (at least the buttons aren't showing).

    Actually, there is one subtle difference between EastPanel and SouthPanel. SouthPanel has these lines of code:

    Java Code:
    	constraints.weightx = 1;
    	constraints.anchor = GridBagConstraints.LINE_START;
    before adding the clock JLabel. But I tried this. I tried doing this before adding the buttons, and it didn't make a difference.

    Can anyone see what I'm doing wrong?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    This is the ideal situation for your creating and posting an SSCCE. Do this and you'll likely get a helpful answer quickly. Much luck!

  3. #3
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default

    Okay, I attached a SSCCE. However, I was not able to reproduce the problem. The buttons show up fine in the SSCCE. So I still don't know where the problem lies.
    Attached Files Attached Files

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by gib65 View Post
    Okay, I attached a SSCCE. However, I was not able to reproduce the problem. The buttons show up fine in the SSCCE. So I still don't know where the problem lies.
    Shoot.

    Then we'll probably not know either. You might want to try to start with a copy of your application and bit by bit simplify it until the problem goes away, and the last step deleted may be the problem step. (or post your entire app here... yikes).

    Luck!
    Last edited by Fubarable; 09-25-2010 at 04:23 AM.

  5. #5
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default

    No need. I solved the problem.

    I erased all the class files in the framecomponents package folder (where MazeManiaFrame, EastPanel, SouthPanel, and a few other frame related files are) and tried to recompile.

    Usually, when I compile I type "javac *.java" and it works.

    This time is didn't for some reason. No class files appeared in the framecomponents folder (even though compiling reported no errors).

    I had to go "javac framecomponents/*.java".

    Why would this be?

    Not only this, but it seems like any change I now make to a file in framecomponents requires the same thing when compiling: javac framecomponents/*.java.

    I just recently split my files up into packages and folders for those packages. Is there something more complicated I need to know about compiling with packages?

Similar Threads

  1. Replies: 3
    Last Post: 05-27-2010, 11:54 AM
  2. Help with JButtons...
    By ashton in forum New To Java
    Replies: 8
    Last Post: 01-26-2009, 10:38 AM
  3. JButtons
    By jadaleus in forum Advanced Java
    Replies: 4
    Last Post: 10-17-2008, 03:49 AM
  4. netbeans 6.0 not show commpunent or show blank page
    By fahimaamir in forum NetBeans
    Replies: 1
    Last Post: 01-26-2008, 07:20 AM
  5. JButtons
    By fgasimzade in forum SWT / JFace
    Replies: 1
    Last Post: 12-25-2007, 06:39 AM

Posting Permissions

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