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 10 of 10

Thread: JButtons Disappearing

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Location
    Australia
    Posts
    13
    My Mood
    Relaxed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default JButtons Disappearing

    I'm using a GridBagLayout to align these buttons to the left(WEST) side of the screen. Although i have come across a problem where only one button shows up, the grassTile button. I've tried different positions for the JPanel but nothing seems to work.

    What's wrong?


                    // Grass Tile
    		final JButton grassTile = new JButton();
    		grassTile.setIcon(new ImageIcon("res/grass.png"));
    		grassTile.setVisible(true);
    		grassTile.addActionListener(new grassTile());
    		grassTile.setMargin(new Insets(0, 0, 0, 0));
    		grassTile.setBorder(null);
    		c.gridx = 0; c.gridy = 3;
    		guiPanel.add(grassTile, c);
     
    		// Stone Tile
    		ImageIcon stone = new ImageIcon("res/stone.png");
    		JButton stoneTile = new JButton(stone);
    		c.gridx = 0; c.gridy = 4;
    		stoneTile.addActionListener(new stoneTile());
    		stoneTile.setMargin(new Insets(0, 0, 0, 0));
    		stoneTile.setBorder(null);
    		guiPanel.add(stoneTile, c);
     
    		// Floor Tile
    		ImageIcon floor = new ImageIcon("res/floor.png");
    		JButton floorTile = new JButton(floor);
    		c.gridx = 0; c.gridy = 5;
    		floorTile.addActionListener(new floorTile());
    		floorTile.setMargin(new Insets(0, 0, 0, 0));
    		floorTile.setBorder(null);
    		guiPanel.add(floorTile, c);


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: JButtons Disappearing

    Can you post an MCVE that demonstrates the behavior? Note that this shouldn't be your whole project, but it should be complete enough so we can see the problem. Start a new project and only add enough code to repeat the problem.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Location
    Australia
    Posts
    13
    My Mood
    Relaxed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: JButtons Disappearing

    The image files are in the correct place and the buttons that aren't showing are the ones with images.

    package test2;
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
     
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class problem {
     
    	public static int width = 1280;
    	public static int height = width / 16 * 9;
    	public static int size = width * height;
     
    	public static void main(String[] args) {
    		createFrameAndPanels();
    	}
     
    	public static void createFrameAndPanels() {
    		JFrame frame = new JFrame("problem");
    		frame.setVisible(true);
    		frame.setSize(width,height);
    		frame.setResizable(false); 
    		frame.setLocationRelativeTo(null);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		// Level Panel
    		JPanel levelPanel = new JPanel(new GridBagLayout());
    		frame.add(levelPanel);
    		levelPanel.setBackground(Color.BLACK);
     
    		// GUI Panel
    		JPanel guiPanel = new JPanel(new GridBagLayout());
    		frame.getContentPane().add(guiPanel, BorderLayout.WEST);
     
    		GridBagConstraints c = new GridBagConstraints();
    		c.insets = new Insets(3, 3, 3, 3);
     
    		JButton button1 = new JButton("button1");
    		c.gridx = 0; c.gridy = 0;
    		guiPanel.add(button1, c);
     
    		JButton button2 = new JButton("button2");
    		c.gridx = 0; c.gridy = 1;
    		guiPanel.add(button2, c);
     
    		JButton button3 = new JButton("button3");
    		c.gridx = 0; c.gridy = 2;
    		guiPanel.add(button3, c);
     
    		// Grass Tile
    		final JButton grassTile = new JButton();
    		grassTile.setIcon(new ImageIcon("/scr/grass.png"));
    		grassTile.setVisible(true);
    		grassTile.setMargin(new Insets(0, 0, 0, 0));
    		grassTile.setBorder(null);
    		c.gridx = 0; c.gridy = 3;
    		guiPanel.add(grassTile, c);
     
    		// Stone Tile
    		ImageIcon stone = new ImageIcon("/scr/stone.png");
    		JButton stoneTile = new JButton(stone);
    		c.gridx = 0; c.gridy = 4;
    		stoneTile.setMargin(new Insets(0, 0, 0, 0));
    		stoneTile.setBorder(null);
    		guiPanel.add(stoneTile, c);
     
    		// Floor Tile
    		ImageIcon floor = new ImageIcon("/scr/floor.png");
    		JButton floorTile = new JButton(floor);
    		c.gridx = 0; c.gridy = 5;
    		floorTile.setMargin(new Insets(0, 0, 0, 0));
    		floorTile.setBorder(null);
    		guiPanel.add(floorTile, c);
    	}
    }

    Is that what you were looking for?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: JButtons Disappearing

    The code basically works for me. I changed the width to 800 and the image locations to my folder:
          grassTile.setIcon(new ImageIcon("http://www.javaprogrammingforums.com/images/duke.gif")); // "/scr/grass.png"));

    images is a folder in the current directory when the code is executed.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2013
    Location
    Australia
    Posts
    13
    My Mood
    Relaxed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: JButtons Disappearing

    I did it and double checked the file path and i am still getting this. It's the exact same on mac and pc!

    Screen Shot 2014-09-19 at 5.42.33 am.jpg

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: JButtons Disappearing

    To see where the computer is looking for the image, create a File object with the path to the image that you are using and print out the value of that File object's absolute path.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2013
    Location
    Australia
    Posts
    13
    My Mood
    Relaxed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: JButtons Disappearing

    I probably should have said this earlier, but when i first did this on my pc the first image worked and displayed. The others had the same path but didn't show up, that's the problem. It's almost like they're out of bounds and don't fit in the seeable area!

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: JButtons Disappearing

    Try it with images that fit.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Dec 2013
    Location
    Australia
    Posts
    13
    My Mood
    Relaxed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: JButtons Disappearing

    The images are 32x32. Even when i take out the 3 visible buttons (button1, button2, button3), they still don't show up.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: JButtons Disappearing

    The code works for me.
    What printed when you used the File object I suggested in post#6?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. The case of the disappearing double quotes
    By oldalistair in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 30th, 2014, 01:36 PM
  2. Mysterious disappearing classes.
    By mstabosz in forum Java IDEs
    Replies: 3
    Last Post: August 1st, 2013, 12:18 PM
  3. JTextField with JButtons
    By devindadude in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 10th, 2013, 10:22 AM
  4. [SOLVED] Disappearing applet, where's it going?
    By bradleysm in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 29th, 2011, 09:41 PM
  5. [SOLVED] Grass disappearing :(
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 18th, 2010, 09:09 PM