Results 1 to 9 of 9
  1. #1
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default help with Swing (GUI)

    Hi, I was wondering if somebody could help me out with some gui code. I do not want to use auto builder so please don't suggest it.

    My problem is laying out things properly, i have tried in quite a few ways with border layout and flow layout but my buttons get huge and .setSize is compleetly useless if i try to do it on buttons/labels/panels.

    so i'm going to include a picture here of how i want it to look and if somebody can please give me some ideas on how i can make this happen.

    http://img541.imageshack.us/i/58888705.png/
    I also don't know how to set the size of my buttons and text fields, change font on labels and style.

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

    Default

    Suggested reading/viewing: A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container) This link will hopefully let you become familiar with layouts, but at the same time I recommend you play with all of these layouts if you wish to become familiar with all that there is with regards to laying out a user interface using Swing. As for 'setSize' - use setPreferredSize +/- the other setMaximum/MinumumSize if you wish to restrict the size of a component.
    Last edited by doWhile; 04-02-2011 at 03:46 AM.

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

    Default

    One key to using the layouts and which makes them more useful is that you will usually nest JPanels each using its own layout, so in effect you will be nesting layouts. For instance your overall GUI can use a BorderLayout, and it can have a major and minor title in two JLabels that are added to the Borderlayout.NORTH JPanel via a BoxLayout. The JPanel in the BorderLayout.SOUTH position could use a GridLayout and hold a bunch of JButtons, etc... so your're only limited by your ingenuity.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    I do not want to use auto builder so please don't suggest it.
    More power to you!

    db

  5. #5
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    I've tried reading up on these layouts and i'm having some real problems. First thing is ( and i have searched a while before posting) I do not know how to set size of my buttons/text field area to a size i want. It is always the size of my panel. How can I change this?

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

    Default

    Quote Originally Posted by Bagzli View Post
    I've tried reading up on these layouts and i'm having some real problems. First thing is ( and i have searched a while before posting) I do not know how to set size of my buttons/text field area to a size i want. It is always the size of my panel. How can I change this?
    Usually you don't want to set the size, but rather using appropriate layouts, let the buttons and components size themselves based on their contents and the layout manager.

    For better help, consider creating a small compilable runnable program that does nothing but displays some things in a simple GUI, has no logic (i.e., the buttons don't do anything), but shows something similar to what you're trying to do, and describe how the code isn't working. Then we can run it, test it, modify it and better be able to help you. This is called creating an SSCCE (please see the link).

  7. #7
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    Ok here's my attempt at SSCCE

    Java Code:
    package guiTesting;
    
    import java.awt.*;
    import javax.swing.*;
    
    public class MainMenuLayout extends JFrame
    {
    	JLabel lbl_logout = new JLabel("Logout");
    	JLabel lbl_welcome = new JLabel("Welcome");
    	JLabel lbl_recipe = new JLabel("Recipe of the Day");
    	
    	JButton btn_favourites = new JButton("Favourites");
    	JButton btn_recipeList = new JButton("Recipe List");
    	
    	JTextArea txt_recipeTextArea = new JTextArea();
    	
    	JPanel pnl_top = new JPanel(new BorderLayout());
    	JPanel pnl_middle = new JPanel(new BorderLayout());
    	JPanel pnl_bottom = new JPanel();
    	
    	
    	
    	private void addToPanel()
    	{
    		btn_favourites.setPreferredSize(new Dimension(20, 20));
    		btn_recipeList.setPreferredSize(new Dimension(20, 20));
    		
    		pnl_top.add(lbl_welcome, BorderLayout.WEST);
    		pnl_top.add(lbl_logout, BorderLayout.EAST);
    		pnl_middle.add(lbl_recipe, BorderLayout.WEST);
    		pnl_middle.add(btn_favourites, BorderLayout.EAST);
    		pnl_middle.add(btn_recipeList, BorderLayout.EAST);
    		pnl_bottom.add(txt_recipeTextArea, BorderLayout.CENTER);
    		
    	}
    	public MainMenuLayout()
    	{
    		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		this.setSize(300, 300);
    		
    		this.setLocationRelativeTo(null);
    		this.setResizable(false);
    		
    		this.addToPanel();
    		this.add(pnl_top, BorderLayout.NORTH);
    		this.add(pnl_middle, BorderLayout.CENTER);
    		this.add(pnl_bottom, BorderLayout.SOUTH);
    		
    	}
    	
    	public static void main(String args[])
    	{
    		new MainMenuLayout().setVisible(true);
    	}
    }
    I need it to look like this
    ImageShack® - Online Photo and Video Hosting

    I don't know what i'm doing wrong, can anyone help?

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You may have to do a bit more manipulating of layout managers to achieve this.

    For example, you could use a grid layout for the row with the buttons and labels, which is added to a panel on the north of the border layout for the main panel.

    This grid layout will contain the buttons(in a button box maybe), and the labels. To create the spaces perhaps some empty jpanels stored in the grid will come in handy?

    Java Code:
    JButton x = new JButton("x");
    JButton y = new JButton("y");
    JPanel north = new JPanel(new GridLayout(2, 3));
    for(int i = 0; i < 5; i++){
      north.add(new JPanel());
    }
    Box buttonBox = new Box(BoxLayout.X_AXIS);
    buttonBox.add(x);
    buttonBox.add(y);
    north.add(buttonBox);
    Try something like that. I find that the best way to learn gui programming is to sort of feel your way through the classes. Look at all the layout managers and try to create a simple but effective solution.

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

    Default

    Java Code:
    btn_favourites.setPreferredSize(new Dimension(20, 20));
    btn_recipeList.setPreferredSize(new Dimension(20, 20));
    It was suggested that you don't try to size components yourself. Swing knows better than you do what the size should be based on the text/font/LAF etc...

    Java Code:
    pnl_middle.add(btn_favourites, BorderLayout.EAST);
    pnl_middle.add(btn_recipeList, BorderLayout.EAST);
    You can only add a single component to any BorderLayout position. You need to create a separate panel to hold the two buttons. I would use a panel with a FlowLayout that is right aligned.

    Java Code:
    JTextArea txt_recipeTextArea = new JTextArea();
    The text area doesn't appear because it doesn't have a preferred size. Look at the JTextArea API for the contruction that allows you to specify the rows/columns of the text area.

    Java Code:
    this.setSize(300, 300);
    Don't set the size. Use the pack() method so all components will be shown at their preferred sizes. Also, the pack() should be done after all the components have been added to the frame and before the frame is made visible.

    You can also use other combination of layout managers, which is why you need to play/experiment to find out when to use the different layout managers.
    Last edited by camickr; 04-04-2011 at 12:21 AM.

Similar Threads

  1. Swing
    By tmotse in forum AWT / Swing
    Replies: 0
    Last Post: 03-31-2010, 10:18 AM
  2. swing
    By mnatalka in forum New To Java
    Replies: 5
    Last Post: 12-03-2008, 07:26 AM
  3. What is next to Swing
    By javaplus in forum AWT / Swing
    Replies: 1
    Last Post: 01-13-2008, 10:16 PM
  4. where is the swing jar?
    By katie in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 10:58 PM
  5. map javax.swing.text.Element to javax.swing.text.View
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 07:02 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
  •