Results 1 to 8 of 8
  1. #1
    k3eper is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default JButton's dont appear

    Hi all

    Im new to Java and am currently following the "Java: In easy steps 4th edtion" to try teach myself.

    Im currently on a section relating to push buttons. Ive entered the code as suggested in the book but the buttons dont appear, the window launchs but its just blank.

    Can someone help me out please?

    Java Code:
    import javax.swing.*;
    
    class Buttons extends JFrame
    {
    	JPanel pnl = new JPanel();
    	ImageIcon tick = new ImageIcon("tick.png");
    	ImageIcon cross = new ImageIcon("cross.png");
    	
    	JButton btn = new JButton("Click Me");
    	JButton tickBtn = new JButton(tick);
    	JButton crossBtn = new JButton("STOP", cross);
    	
    	public Buttons()
    	{
    		super("Swing Window");
    		setSize(500, 200);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		add(pnl);
    		setVisible(true);
    		pnl.add(btn);
    		pnl.add(tickBtn);
    		pnl.add(crossBtn);
    	}
    		
    	public static void main (String[] args)
    	{
    	Window gui = new Window();
    	}
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: JButton's dont appear

    You need to add the components to the user interface. The buttons have been added to a JPanel (pnl1), now that panel needs to be added to the JFrame. More information and code snippets can be found at:
    Lesson: Using Swing Components (The Java™ Tutorials > Creating a GUI With JFC/Swing)

  3. #3
    k3eper is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Re: JButton's dont appear

    thank you for the reply

    I dont understand, its exactly as it is in the book.

    Could you please alter the code in my first post as an example?

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

    Default Re: JButton's dont appear

    What if you simply move the setVisible(true) to the bottom of the constructor so that it is called after all components have been added?

    Also, your main method creates a Windows object, not a Buttons object. I think that you want to create Buttons so that its code will be called.

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: JButton's dont appear

    Disregard everything in my previous post except the link, which has valuable information and is important for you to visit (the JPanel is added, just in a different order) - I misread your code. As Fubarable mentioned, call setVisible after the components have been added

  6. #6
    k3eper is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Re: JButton's dont appear

    I have already tried setVisible at the end but same results :(

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

    Default Re: JButton's dont appear

    Quote Originally Posted by k3eper View Post
    I have already tried setVisible at the end but same results :(
    Have you followed my other recommendation? Does your main method still look like this?

    Java Code:
        public static void main (String[] args)
        {
           Window gui = new Window();
        }

    Or have you changed it as I recommended to:

    Java Code:
        public static void main (String[] args)
        {
           new Buttons();
        }
    The Buttons class's code won't run if you don't tell it to in the main method. Simple as that.

  8. #8
    k3eper is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Re: JButton's dont appear

    Arr thats done it, thank you for your time and patience. For some reason (newb) I thought the Window call was actualy spawning the window. thank you again

Similar Threads

  1. ActionListener for JButton after changing Button to JButton
    By ravi.joshi53 in forum Java Applets
    Replies: 2
    Last Post: 10-07-2011, 07:35 AM
  2. Hello! and I need help. I dont know were to start
    By Fall0ut in forum New To Java
    Replies: 10
    Last Post: 05-19-2010, 06:26 PM
  3. Using libraries that others dont have(j3d)
    By scorpion9 in forum Advanced Java
    Replies: 7
    Last Post: 02-19-2010, 11:24 AM
  4. Sometimes get the right results sometimes dont
    By Battlefeldt in forum New To Java
    Replies: 0
    Last Post: 12-18-2009, 01:03 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
  •