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

Thread: Components not appearing (need help!)

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Components not appearing (need help!)

    Good day! I'm a complete noob in GUI programming so I wanted to know whats wrong with my code. I can compile the program without a problem but when the GUI appears none of my components appear. Any help would be appreciated. thanks!

     
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
     
    public class MenuList	{
     
    	private JFrame page1;
    	private JPanel panel;
    	private JButton burger;
    	private JButton rice;
    	private JLabel label;
     
    	public MenuList()	{
     
    	gui();
     
    	}
     
    	public void gui()	{
     
    	page1 = new JFrame("FAST FOOD");
    	page1.setVisible(true);
    	page1.setSize(600,400);
    	page1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    	panel = new JPanel(new BorderLayout());
     
    	//BUTTONS
    	burger = new JButton("Burger");
    	burger.addActionListener(new ActionListener() { 
    		public void actionPerformed(ActionEvent e)	{
    		JOptionPane.showMessageDialog(null, "BURGER"); }
    	});
    	rice = new JButton("Rice");
    	label = new JLabel("ORDER HERE");
    	//END (BUTTONS)
     
    	//LAYOUT
    	GroupLayout layout = new GroupLayout(panel);
    	layout.setAutoCreateGaps(true);
    	layout.setAutoCreateContainerGaps(true);
     
    	layout.setHorizontalGroup(layout.createSequentialGroup()
    		.addComponent(burger)
    	.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING))
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING))
    	);
    	//END (LAYOUT)
     
    	}
     
    	public static void main(String[] args)	{
     
    	new MenuList();
     
    	}
    }


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

    Default Re: Components not appearing (need help!)

    When do you add your components to the JPanel? When do you add your JPanel to the JFrame?
    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
    Mar 2013
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Components not appearing (need help!)

    I don't think I've added my components to the Jpanel yet. Same goes for my Jpanel to Jframe.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,438
    My Mood
    Hungover
    Thanks
    144
    Thanked 634 Times in 542 Posts

    Default Re: Components not appearing (need help!)

    Quote Originally Posted by hawkeye01 View Post
    I don't think I've added my components to the Jpanel yet. Same goes for my Jpanel to Jframe.
    That's your problem then. Components aren't visible until you add them to something.
    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!

  5. #5
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: Components not appearing (need help!)

    When I write GUI code, I follow a specific layout of how I arrange my code. There are different ways of placing the event-listener code in the GUI, so feel free to use a different way if you wish. Keep in mind, the JFrame is what ultimately contains all of your JComponents but computers aren't smart enough to automatically put the JComponents onto the JFrame. Likewise, if you make a JPanel, you have to add whichever JComponents to it, then add it to the JFrame.

     
    // import statements 
     
    public class myGui extends JFrame {
          // GUI components that are used by the ActionListener (or whichever other event listener)
     
          public class myEventListener implements ActionListener (or whichever other event listener) {
              // event-listener code
     
          }
     
          public myGui() {
     
                  // code for creating the GUI and its components
                  // at the end, register the event listeners. 
          }
    }


    --- Update ---

    When I write GUI code, I follow a specific layout of how I arrange my code. There are different ways of placing the event-listener code in the GUI, so feel free to use a different way if you wish. Keep in mind, the JFrame is what ultimately contains all of your JComponents but computers aren't smart enough to automatically put the JComponents onto the JFrame. Likewise, if you make a JPanel, you have to add whichever JComponents to it, then add it to the JFrame.

     
    // import statements 
     
    public class myGui extends JFrame {
          // GUI components that are used by the ActionListener (or whichever other event listener)
     
          public class myEventListener implements ActionListener (or whichever other event listener) {
              // event-listener code
     
          }
     
          public myGui() {
     
                  // code for creating the GUI and its components
                  // at the end, register the event listeners. 
          }
    }

  6. The Following User Says Thank You to SunshineInABag For This Useful Post:

    hawkeye01 (March 24th, 2013)

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,438
    My Mood
    Hungover
    Thanks
    144
    Thanked 634 Times in 542 Posts

    Default Re: Components not appearing (need help!)

    Quote Originally Posted by SunshineInABag View Post
    When I write GUI code, I follow a specific layout of how I arrange my code. There are different ways of placing the event-listener code in the GUI, so feel free to use a different way if you wish. Keep in mind, the JFrame is what ultimately contains all of your JComponents but computers aren't smart enough to automatically put the JComponents onto the JFrame. Likewise, if you make a JPanel, you have to add whichever JComponents to it, then add it to the JFrame.

     
    // import statements 
     
    public class myGui extends JFrame {
          // GUI components that are used by the ActionListener (or whichever other event listener)
     
          public class myEventListener implements ActionListener (or whichever other event listener) {
              // event-listener code
     
          }
     
          public myGui() {
     
                  // code for creating the GUI and its components
                  // at the end, register the event listeners. 
          }
    }
    Some nitpicking- you almost never have a reason to extend JFrame, and your classes should start with upper-case letters.
    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!

  8. The Following User Says Thank You to KevinWorkman For This Useful Post:

    hawkeye01 (March 24th, 2013)

  9. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Components not appearing (need help!)

    I was able to solve my problem by adding my components to the panel and adding my panel to my frame. thanks for the help guys!

Similar Threads

  1. Components?
    By Gravity Games in forum Java Theory & Questions
    Replies: 31
    Last Post: August 5th, 2012, 04:20 PM
  2. Strange boxes appearing
    By fishnj1333 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 11th, 2012, 05:36 AM
  3. Layout components...help please
    By alphasil in forum AWT / Java Swing
    Replies: 6
    Last Post: January 26th, 2012, 07:28 AM
  4. Problem with Bearing calculation appearing as NaN
    By welshcrossy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 23rd, 2012, 10:23 AM
  5. Replies: 1
    Last Post: August 15th, 2011, 09:26 AM