Results 1 to 10 of 10
  1. #1
    Adrien is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Exclamation so whats going on? (things aren't showing up)

    I have the following code below, which is well commented and explains pretty much what it does. in theory it creates a window 800x600, puts "Sample Application" in the top, under the words (SOUTH i believe) creates a wrapper panel with a grid lay out of two, thus holding two more panels, one containing buttons and one containing a description of the buttons.

    Now when run all that shows up is "Sample Application" why is that?

    Java Code:
    package OtherGUI;
    
    import java.awt.BorderLayout;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.util.Vector;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.border.EtchedBorder;
    import javax.swing.border.TitledBorder;
    
    public class MainWindow 
    {
    	private Vector<JPanel> panel;
    	
    	/**
    	 * Call MainWindow which in turns calls the CreateWindow that contains
    	 * all the properties and contents of the entire application.
    	 * 
    	 * @param args
    	 */
    	
    	public static void main(String args[])
    	{
    		new MainWindow();
    	}
    	
    	/**
    	 * Call the CreateWindow method that contains all the 
    	 * contents and properties of the Frame. 
    	 */
    	
    	public MainWindow()
    	{
    		panel = new Vector<JPanel>(4);
    		JPanel temp = CreateContent();
    		panel.add(temp);
    		
    		
    		CreateWindow();
    	}
    	
    	/**
    	 * Create the basis of the frame, the size, and location on the
    	 * desktop of the application. populate the application with the
    	 * CreateContent() method
    	 * 
    	 */
    	
    	public void CreateWindow()
    	{
    		JFrame window = new JFrame("Main Window");
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		window.setBounds(10, 10, 800, 600);
    		
    		JLabel title = new JLabel("Sample Application");
    		title.setFont(new Font("Serif", Font.PLAIN, 26));
    		
    		window.add(title, BorderLayout.CENTER);
    		
    		CreateContent();
    		
    		window.setVisible(true);
    	}
    	
    	/**
    	 * Gather all the contents together and display
    	 * them with in a grid Layout.
    	 */
    	
    	private JPanel CreateContent()
    	{
    		JPanel addButtons = Buttons();
    		
    		//Gather all the conents
    		JPanel contents = new JPanel();
    		contents.setLayout(new GridLayout(4,2));
    		contents.add(addButtons);
    		
    		return addButtons;
    	}
    	
    	/**
    	 * Create a Panel specifically for Buttons.
    	 * in this case the four main buttons which are
    	 * Movies, Customer, Games and Home.
    	 * 
    	 * @return - buttonHolder which is the JPanel 
    	 * 			 that contains the buttons
    	 */
    	
    	private JPanel Buttons()
    	{
    		JPanel wrapper = new JPanel();
    		wrapper.setLayout(new GridLayout(2,4));
    		
    		
    		JButton home = new JButton("home");
    		JButton customer = new JButton("customer");
    		JButton movies = new JButton("movies");
    		JButton games = new JButton("games");
    		
    		//des = description
    		JTextArea desOne = new JTextArea("Sample Text");
    		desOne.setEditable(false);
    		JTextArea desTwo = new JTextArea("Sample Text");
    		desTwo.setEditable(false);
    		JTextArea desThree = new JTextArea("Sample Text");
    		desThree.setEditable(false);
    		JTextArea desFour = new JTextArea("Sample Text");
    		desFour.setEditable(false);
    		
    		//desHolder = descriptionHolder
    		JPanel desHolder = new JPanel();
    		desHolder.setLayout(new GridLayout(1, 4));
    		desHolder.add(desOne);
    		desHolder.add(desTwo);
    		desHolder.add(desThree);
    		desHolder.add(desFour);
    		
    		//Hold all the buttons
    		JPanel buttonHolder = new JPanel();
    		buttonHolder.setLayout(new GridLayout(1,4));
    		buttonHolder.add(home);
    		buttonHolder.add(customer);
    		buttonHolder.add(movies);
    		buttonHolder.add(games);
    		buttonHolder.setBorder(new TitledBorder(new EtchedBorder(), "Navagation"));
    		
    		/*
    		 * Hold both contents of Button Holder and
    		 * desHolder 
    		 */
    		
    		wrapper.add(buttonHolder);
    		wrapper.add(desHolder);
    		
    		return buttonHolder;	
    	}
    
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    The CreateContent method returns a JPanel which you want to add to your gui.
    Java Code:
    public void CreateWindow()
    {
        ...
        window.add(title, BorderLayout.NORTH);
        window.add(CreateContent(), BorderLayout.CENTER);
        ...

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

    Default

    Is this your code or someone else's? If someone else's, what is your source?

    As per hardwire, you are only adding the JLabel to the JFrame and nothing else.

  4. #4
    Adrien is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default

    Ok so that fixed that issue but upon running the code my theory is a mess, I have a etched border Called "Navigation" that does not contain or appear to contain the wrapper panel that splits the panel in two thus allowing there to be two panel win in located next to each other.

    So in theory it should look something like:

    { [] [] }

    where the curly braces are the wrapper and the two inner square brackets are the two panels known as buttonHolder and desHolder.

    instead I get something like:

    [Navagation]

    which then takes up the whole center "pane"

    what in my code could be causing this?

    Because I add buttonHolder and desHolder to the wrapper to which has a 2,4 and button Holder has a 1,4 so contents should be put out like so:

    wrapper
    ---------
    [nav][des]
    [b1 ][d1 ]
    [b2 ][d2 ]
    [b3 ][d3 ]
    [b4 ][d4 ]
    ----------

    you see where I am going with this?
    So how would I fix this?


    to the last poster above me:

    my code, im trying to learn not rip off some one else work.

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

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

    Default

    Quote Originally Posted by Adrien View Post
    to the last poster above me:

    my code, im trying to learn not rip off some one else work.
    Oh, I'm not trying to accuse you of ripping off anyone; it's just that the code looks very familiar, as if I recently saw a snippet of it either here or in the Sun fora. Heck, I borrow code all the time, often to study it and find out what's decent about it.

    One other unsolicited suggestion: read up on Java naming conventions and use them whenever possible. By sticking to conventions, you make it much easier for others (including us!) to read and understand your code.

    Again, much luck!

  7. #7
    Adrien is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default

    this was posted on sun fora but they locked my thread so i came here.

    as for naming conventions I am just hacking this together for now, after I have this second issue Fixed I will deffently be renaming things, and fixing the readabillity

  8. #8
    Adrien is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    what JPanel does Button() return? the wrapper panel or the button holder panel?
    OMG i see now It returns ButtonHolder but should return wrapper >_<

    but now the desHolder is out side navigation, the buttons take up all the space when they should be button size and the text areas are ok for now.

    New code for Button();

    Java Code:
    private JPanel Buttons()
    	{
    		JPanel wrapper = new JPanel();
    		wrapper.setLayout(new GridLayout(1,2));
    		
    		
    		JButton home = new JButton("home");
    		JButton customer = new JButton("customer");
    		JButton movies = new JButton("movies");
    		JButton games = new JButton("games");
    		
    		//des = description
    		JTextArea desOne = new JTextArea("Sample Text");
    		desOne.setEditable(false);
    		JTextArea desTwo = new JTextArea("Sample Text");
    		desTwo.setEditable(false);
    		JTextArea desThree = new JTextArea("Sample Text");
    		desThree.setEditable(false);
    		JTextArea desFour = new JTextArea("Sample Text");
    		desFour.setEditable(false);
    		
    		//desHolder = descriptionHolder
    		JPanel desHolder = new JPanel();
    		desHolder.setLayout(new GridLayout(4,1));
    		desHolder.add(desOne);
    		desHolder.add(desTwo);
    		desHolder.add(desThree);
    		desHolder.add(desFour);
    		
    		//Hold all the buttons
    		JPanel buttonHolder = new JPanel();
    		buttonHolder.setLayout(new GridLayout(1,4));
    		buttonHolder.add(home);
    		buttonHolder.add(customer);
    		buttonHolder.add(movies);
    		buttonHolder.add(games);
    		buttonHolder.setBorder(new TitledBorder(new EtchedBorder(), "Navagation"));
    		
    		/*
    		 * Hold both contents of Button Holder and
    		 * desHolder 
    		 */
    		
    		wrapper.add(buttonHolder);
    		wrapper.add(desHolder);
    		
    		return wrapper;	
    	}
    Last edited by Adrien; 02-20-2010 at 06:34 PM.

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

    Default

    Quote Originally Posted by Adrien View Post
    this was posted on sun fora but they locked my thread so i came here.

    as for naming conventions I am just hacking this together for now, after I have this second issue Fixed I will deffently be renaming things, and fixing the readabillity
    If it were my dollar, I'd change it now, for now is when I would want to motivate others to want to read my code and help me. Feel free to post any updates at any time.

    Much luck.

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

    Default

    Quote Originally Posted by Adrien View Post
    but now the desHolder is out side navigation, the buttons take up all the space when they should be button size and the text areas are ok for now.
    The buttons are only doing what the layout managers are telling them to do. A GridLayout creates a grid of equal sized cells, so per your code, your buttons are guaranteed to be the same size as your text areas. Solution: don't rely on just GridLayout. Instead experiment with others. For example, try commenting out either createPanelA() or createPanelB() below (and uncommenting the other), to see what you're doing:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import javax.swing.*;
    
    public class LayoutEg {
      private static void createAndShowUI() {
        //!! comment out one of the two lines below:    
        //JPanel mainPanel = createPanelA();
        JPanel mainPanel = createPanelB();
        
        JFrame frame = new JFrame("LayoutEg");
        frame.getContentPane().add(mainPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      private static JPanel createPanelA() {
        JPanel buttonPanel = new JPanel(new GridLayout(1, 0));
        for (int i = 0; i < 3; i++) {
          JButton button = new JButton("Button " + (i + 1));
          buttonPanel.add(button);
        }
        
        JPanel mainPanel = new JPanel(new GridLayout(2, 0));
        mainPanel.setPreferredSize(new Dimension(500, 300));
        mainPanel.add(buttonPanel);
        mainPanel.add(new JLabel("Bottom Label", SwingConstants.CENTER));
        return mainPanel;
      }
    
      private static JPanel createPanelB() {
        JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 10, 0));
        for (int i = 0; i < 3; i++) {
          JButton button = new JButton("Button " + (i + 1));
          buttonPanel.add(button);
        }
        
        JPanel mainPanel = new JPanel(new BorderLayout(20, 20));
        mainPanel.setPreferredSize(new Dimension(500, 300));
        mainPanel.add(buttonPanel, BorderLayout.NORTH);
        mainPanel.add(new JLabel("Bottom Label", SwingConstants.CENTER), BorderLayout.CENTER);
        return mainPanel;
      }
    
      public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
            createAndShowUI();
          }
        });
      }
    }

Similar Threads

  1. Move things in Applet
    By Chasingxsuns in forum New To Java
    Replies: 6
    Last Post: 02-19-2014, 08:21 AM
  2. Replies: 28
    Last Post: 09-15-2010, 10:21 AM
  3. Program That Can Do Things On The Internet
    By IronLegion in forum Eclipse
    Replies: 1
    Last Post: 12-09-2009, 08:06 PM
  4. A good Java installation package can make things much easier
    By freezea in forum Reviews / Advertising
    Replies: 2
    Last Post: 02-01-2009, 12:03 PM
  5. May someone explain what these things means in Java
    By quickfingers in forum New To Java
    Replies: 2
    Last Post: 01-26-2008, 05:45 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
  •