Results 1 to 3 of 3

Thread: Empty JFrame

  1. #1
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default Empty JFrame

    I'm developing a main menu for an application with two drop down menus and 5 normal control buttons. I set the buttons up first and had them displaying (and the exit button working) but when I added the combo boxes the JFrame just shows nothing. And I can't work out why.

    Java Code:
    /*
    A rough working for a GUI interface
    */
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    // Import the GUI packages
    
    public class Layout {
    	public static void main(String[] args) {
    		JFrame window = new JFrame();
    		JPanel dropdowns = new JPanel();
    		JPanel menu = new JPanel();
    		JPanel content = new JPanel();
    		// Declare the JFrame and menu and content panels
    
    		window.setLayout(new BorderLayout(10,10));
    		window.setVisible(true);
    		window.setSize(600,200);
    		window.setMinimumSize(new Dimension(600,200));
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		// Set the frame properties
    
    		JComboBox to = new JComboBox();
    			to.addItem("Leicester"); to.addItem("Loughborough"); to.addItem("Nottingham"); to.addItem("Derby"); to.addItem("Lincoln");
    		JComboBox from = new JComboBox();
    			from.addItem("Leicester"); from.addItem("Loughborough"); from.addItem("Nottingham"); from.addItem("Derby"); from.addItem("Lincoln");
    		// Declare the drop down menus and add the options
    
    		final JButton price = new JButton("Route Price");
    		final JButton time = new JButton("Route Time");
    		final JButton stops = new JButton("Route Stops");
    		final JButton admin = new JButton("Admin");
    		final JButton exit = new JButton("Exit");
    		// Declare the main menu buttons
    
    		class ButtonHandler implements ActionListener {
    			public void actionPerformed(ActionEvent e) {
    				JButton clicked = (JButton) e.getSource();
    				if(clicked == exit) {
    					System.exit(0);
    				}
    				// Get the JButton the user clicked
    			}
    		}
    		// Create a class to handle the buttons
    
    		ButtonHandler buttons = new ButtonHandler();
    		price.addActionListener(buttons);
    		time.addActionListener(buttons);
    		stops.addActionListener(buttons);
    		admin.addActionListener(buttons);
    		exit.addActionListener(buttons);
    		// Call the ButtonHandler class and apply it to the buttons
    
    		menu.setLayout(new FlowLayout());
    		menu.setVisible(true);
    		// Set the menu properties
    
    		dropdowns.setLayout(new FlowLayout());
    		dropdowns.setVisible(true);
    		// Set the dropdowns properties
    
    		dropdowns.add(to); dropdowns.add(from);
    		menu.add(price); menu.add(time); menu.add(stops); menu.add(admin); menu.add(exit);
    		window.add(dropdowns);
    		window.add(menu);
    		// Add the buttons to the menu, and add the menu to the frame
    	}
    }
    Any help would be appreciated.
    Last edited by Wednesday Bass; 03-25-2011 at 03:15 PM.

  2. #2
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Or not. Got it solved by moving some of the code about. Not entirely sure why that worked.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Not entirely sure why that worked.
    By default a JFrame will use a BorderLayout (and you code also sets the layout be be a BorderLayout). When you don't specify where in the BorderLayout a component should be added, the component gets added to the CENTER.

    Java Code:
    window.add(dropdowns);
    window.add(menu);
    You are trying to add two components to the CENTER which won't work. Read the section from the Swing tutorial on How to Use Border Layout for proper usage of this layout manager.

Similar Threads

  1. Empty Form?
    By Vorfin in forum New To Java
    Replies: 13
    Last Post: 07-04-2010, 11:54 PM
  2. Replies: 5
    Last Post: 06-14-2010, 06:10 PM
  3. Replies: 4
    Last Post: 04-22-2009, 11:02 PM
  4. Empty ResultSet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-09-2008, 08:36 PM
  5. BufferedReader empty
    By Peter in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 06:52 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
  •