Results 1 to 16 of 16
  1. #1
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default empty space between JLabels in a JFrame.

    Hi,

    I've been asked to make a JFrame which looks like this:

    empty space between JLabels in a JFrame.-frame.png

    I've managed to create most of the things included, though no matter what I've tried, I couldn't create the empty gap between the panels.

    first I've created 3 main panels to be insertedinto the frame.

    Then I've tried gridlayout + set gap.. but the gap looked like a part of the panels.. and not an empty space containing the color of the frame in the background.

    I've tried adding color the the gap, but there's no such method...

    Then I've insted used flowlayout (because i saw i needed the panels in different sizes), but still met the same problems as before.

    Then I've tried setBorder(BorderFactory.createEmptyBorder()), still without success.. I guess because the panels already had the titleborder and couldn't have another 1...

    I could add 2 more panels just as spaces.. and paint them grey.. but I believe it's just a stupid way and there's a much simpler way of doing it...

    If you could show me or at least give me a hint, it'd be awesome.

    Thanks!!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: empty space between JLabels in a JFrame.

    Have a look at the Box class; it can generate invisible filler components for you.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: empty space between JLabels in a JFrame.

    Quote Originally Posted by Mapisto View Post
    Then I've tried setBorder(BorderFactory.createEmptyBorder()), still without success.. I guess because the panels already had the titleborder and couldn't have another 1...
    There's a Border that compounds (hint, hint) two Borders.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: empty space between JLabels in a JFrame.

    Thanks! I'll try!

  5. #5
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: empty space between JLabels in a JFrame.

    I've tried the compound border, and I think it works.. but then another problem occured.

    I've copied some of the code:
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.GridLayout;
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JPanel;
    import javax.swing.border.CompoundBorder;
    import javax.swing.border.EmptyBorder;
    import javax.swing.border.TitledBorder;
    
    
    public class whatever{
    
    	public static void main(String[] args) {
    		// Create a new ApplicationForm
    		new ApplicationForm();
    		}
    	
    }
    
    class ApplicationForm extends JFrame {
    	
    	private JPanel p1= new JPanel();//top- personal info
    	private JPanel p2= new JPanel();//mid- list of references
    	private JPanel p3= new JPanel();//bot- programming lang
    	
    	//Inside panels for P1
    	private JPanel p11= new JPanel(new GridLayout(4,1));//labels fname etc..
    	private JPanel p12= new JPanel();//text fields for top..
    	
    	//Inside panels for P2
    	private JPanel p21= new JPanel();
    	private JPanel p22= new JPanel();
    	
    	//Inside panels for P3
    	private JPanel p31= new JPanel();
    	private JPanel p32= new JPanel();
    	
    	
    	private JTextField FirstName = new JTextField();
    	private JTextField LastName = new JTextField();
    	private JTextField Address = new JTextField();
    	private JTextField Cellular = new JTextField();
    	
    	private JLabel LFirstName= new JLabel("*First Name:");
    	private JLabel LLastName= new JLabel("*Last Name:");
    	private JLabel LAddress= new JLabel("Address:");
    	private JLabel LCellular= new JLabel("*Cellular:");
    	
    	private Font font= new Font("Times New Roman", Font.ITALIC, 15);
    	
    	private GridLayout grid= new GridLayout(3,1);
    	
    	public ApplicationForm (){
    		
    		super("Application Form");
    		
    		
    		
    		 
    		
    		//all the details for the frame.
    		setSize(550,400);
    		setVisible(true);
    		setAlwaysOnTop(isAlwaysOnTop());
    		setResizable(false);
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setLayout(new FlowLayout());// instead of grid! don't forget to delete grid if aint needed!!!!
    		setBackground(Color.BLUE);
    The problem is that when i run the program, without adding anything to the frame, just the frame as it is with the sets I've made for it, it won't show me the color blue! but ugly light gray : \ now.. when my computer lagged a little (yes, sad i know..) I saw that for a split of a second it was blue! but then immidiately turned back to light gray... I've tried to figure out what's wrong.. I've run the program without adding any panels to the frame.. and nothing helped :(

    What's causing the background color to stay gray??

    Thanks!
    Last edited by Mapisto; 04-12-2012 at 10:12 PM.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: empty space between JLabels in a JFrame.

    You're looking at the background of panels p1, p2 and p3 and it's grey in all three panels. You set the background of the content pane of your JFrame to blue but it is completely covered by those three panels.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: empty space between JLabels in a JFrame.

    But I haven't added the panels to the frame.
    How can the frame be covered by them? I only created them, but didn't put the "add" command.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: empty space between JLabels in a JFrame.

    The part of the code that you've shown us doesn't show it, but the comments in your code part do suggest that you've added them somewhere.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: empty space between JLabels in a JFrame.

    I did, but for the test, I put // before every 1 of them.
    I swear :|

    The full code is LONG.. so I put just the part that matters. actualy, for the test, I've put // before all the lines after what I've pasted here.

    So no, it isn't that simple I'm afraid :(

  10. #10
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: empty space between JLabels in a JFrame.

    I've checked it again, just to be sure, and it still gives me the same result.

    I've removed everything that isn't pasted here and still the frame is gray.

    Please, anyone got any idea what could cause it ?

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: empty space between JLabels in a JFrame.

    A JFrame contains one component: a JRootPane. Read the API for JRootPane and you'll know why the background color of a JFrame is never seen.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: empty space between JLabels in a JFrame.

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: empty space between JLabels in a JFrame.

    Thanks,

    I've never heard about the JRootPane before, it's good to know.

    Eventualy I've changed the command to getContentPane().setBackground(Color.GRAY); (added getContentPane(). ) and it did the trick :)

    Another problem I've encountered now is with the borders, I'm not sure I've done it right using the compound border (Great took btw! thanks!!).

    I've designed the "flowlayout" of the frame to be without any gaps and only gave an empty border (compounded titled border) to panel p2.

    I've made the empty border so it will appear only at the bottom and the top of the frame.

    Though, the empty border won't appear :( only the titled border will appear on panel 2 :(

    This time I'll copy the whole thing, because I believe the problem could be anywhere and i just couldn't find it :| no matter what i've changed.

    (I believe I've made the program very simple and easy to read, tell me if I'm wrong :| ).




    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.GridLayout;
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JRootPane;
    import javax.swing.JTextField;
    import javax.swing.JPanel;
    import javax.swing.border.CompoundBorder;
    import javax.swing.border.EmptyBorder;
    import javax.swing.border.TitledBorder;
    import javax.swing.border.LineBorder;
    
    
    public class whatever {
    
    	public static void main(String[] args) {
    		// Create a new ApplicationForm
    		new ApplicationForm();
    		}
    	
    }
    
    class ApplicationForm extends JFrame {
    	
    	private JPanel p1= new JPanel();//top- personal info
    	private JPanel p2= new JPanel();//mid- list of references
    	private JPanel p3= new JPanel();//bot- programming lang
    	
    	//Inside panels for P1
    	private JPanel p11= new JPanel(new GridLayout(4,1));//labels fname etc..
    	private JPanel p12= new JPanel();//text fields for top..
    	
    	//Inside panels for P2
    	private JPanel p21= new JPanel();
    	private JPanel p22= new JPanel();
    	
    	//Inside labels for P3
    	
    	private JLabel[] L3= new JLabel[10];
    	
    	
    	
    	
    	
    	private JTextField FirstName = new JTextField();
    	private JTextField LastName = new JTextField();
    	private JTextField Address = new JTextField();
    	private JTextField Cellular = new JTextField();
    	
    	private JLabel LFirstName= new JLabel("*First Name:  ");
    	private JLabel LLastName= new JLabel("*Last Name:  ");
    	private JLabel LAddress= new JLabel("Address:  ");
    	private JLabel LCellular= new JLabel("*Cellular:  ");
    	
    	private Font font= new Font("Times New Roman", Font.ITALIC, 15);
    	
    	private GridLayout grid3= new GridLayout(2,5);
    	private GridLayout grid1= new GridLayout(4,1);
    	
    	private LineBorder Lborder= new LineBorder(Color.BLACK,2);
    	
    	public ApplicationForm (){
    		
    		super("Application Form");
    		
    		
    		
    		 
    		
    		//all the details for the frame.
    		
    		setSize(550,400);
    		setVisible(true);
    		setAlwaysOnTop(isAlwaysOnTop());
    		setResizable(false);
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setLayout(new FlowLayout(0,0,0));
    		getContentPane().setBackground(Color.GRAY);
    		
    		
    		p1.setPreferredSize(new Dimension(545,120)); 
    		p2.setPreferredSize(new Dimension(545,165));
    		p3.setPreferredSize(new Dimension(545,70));
    		
    		//all the title borders for each of the 3 main panels. 
    		TitledBorder p1Tborder= new TitledBorder("Personal Information");
    		TitledBorder p2Tborder= new TitledBorder("List of references");
    		TitledBorder p3Tborder= new TitledBorder("Programing Languages");
    		
    		//sets right alignments for p1
    		LFirstName.setHorizontalAlignment(JLabel.RIGHT);
    		LLastName.setHorizontalAlignment(JLabel.RIGHT);
    		LAddress.setHorizontalAlignment(JLabel.RIGHT);
    		LCellular.setHorizontalAlignment(JLabel.RIGHT);
    		
    		
    		//fonts for top panel
    		LFirstName.setFont(font);
    		LLastName.setFont(font);
    		LAddress.setFont(font);
    		LCellular.setFont(font);
    		
    		//color for labels of top panel
    		LFirstName.setForeground(Color.RED);
    		LLastName.setForeground(Color.RED);
    		LCellular.setForeground(Color.RED);
    		
    		
    		p11.add(LFirstName);
    		p11.add(LLastName);
    		p11.add(LAddress);
    		p11.add(LCellular);
    		
    		p12.setLayout(grid1);
    		p12.add(FirstName);
    		p12.add(LastName);
    		p12.add(Address);
    		p12.add(Cellular);
    		
    		
    		
    		p1.setLayout(new GridLayout(1,2));
    		
    		p1.add(p11);
    		p1.add(p12);
    		
    		p1.setBorder(p1Tborder);
    		
    		
    		//P2 designing
    		
    		
    		
    		
    		
    		
    		p2.setBorder(new CompoundBorder(p2Tborder,BorderFactory.createEmptyBorder(10,0,10,0))); //compounds and sets title border and empty border on panel 2 (top and bot).
    		
    		
    		
    		
    		
    		
    		
    		//P3 designing
    		
    		grid3.setHgap(2);
    		p3.setLayout(grid3);
    		
    		
    		L3[0]= new JLabel("JAVA");
    		L3[1]= new JLabel("C");
    		L3[2]= new JLabel("C++");
    		L3[3]= new JLabel("C#");
    		L3[4]= new JLabel("Prolog");
    		
    		
    		for(int i=5; i<10; i++){
    			L3[i]= new JLabel();
    		}
    		
    		for(int k=0; k<10; k++){
    			L3[k].setBorder(Lborder);
    			L3[k].setFont(font);
    			L3[k].setHorizontalAlignment(JLabel.CENTER);
    			p3.add(L3[k]);
    		}
    		
    		
    		
    		
    		
    		
    	
    		
    		
    		
    		p3.setBorder(p3Tborder);
    		
    		
    		
    		
    		add(p1);
    		add(p2);
    		add(p3);
    		
    	}
    	
    	
    	
    	
    	
    }

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: empty space between JLabels in a JFrame.

    (I believe I've made the program very simple and easy to read, tell me if I'm wrong :| ).
    4 unused imports
    2 unused fields
    20 or more extra blank lines

    I don't call that easy to read.

    You've got an EmptyBorder inside a TitledBorder on a panel that has no content; where do you expect the 'emptiness' to show?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  15. #15
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: empty space between JLabels in a JFrame.

    Well,
    It isn't done yet.. when it's done there will be no more unused imports/fields nor extra blank lines.
    I'm new to programming, so I just wanted to know what your standards are :|

    about the compound border, I've replaced the places of the titled and the empty. like this:

    Java Code:
    p2.setBorder(new CompoundBorder(BorderFactory.createEmptyBorder(5,0,5,0),p2Tborder));
    The problem now is the first problem again, it won't show me the background color of the frame, but the light-gray color of the panel.

    I thought that's what the empty border is supposed to do.

    What kind of border could show me the background color of the frame?

    Thanks again for your time and patience!

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: empty space between JLabels in a JFrame.

    What kind of border could show me the background color of the frame?
    None. That's not what a Border is for.

    Why are you so hung up on seeing the background color of a top-level Window that contains, as I already told you, a JRootPane? You've read the JRootPane API, now work with what the API gives you instead of hankering after what it doesn't.

    If you want a colored band around (or above/below) that TitledBorder, look at the other Border implementations and you'll find (at least) one that can do that. Or even write your own Border implementation if you like. There are just 3 methods to implement.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Space Warrior - Multiplayer Space Shooter
    By stes in forum Reviews / Advertising
    Replies: 2
    Last Post: 01-11-2012, 09:19 AM
  2. Empty JFrame
    By Wednesday Bass in forum AWT / Swing
    Replies: 2
    Last Post: 03-25-2011, 08:30 PM
  3. Replies: 5
    Last Post: 03-05-2011, 01:43 AM
  4. getting rid of empty space at the end of a string
    By BoomPony in forum New To Java
    Replies: 3
    Last Post: 02-14-2011, 10:40 PM
  5. Replies: 0
    Last Post: 07-05-2009, 01:31 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
  •