Results 1 to 9 of 9
  1. #1
    njschwartz is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default Label vs. JLabel

    Hi all. This is my first post. I am relatively new to Java. I took a couple of community college level intro to programing courses, but it was all pretty basic. I am now trying to learn a bit more on my own.

    Anyway, the question I have relates to the difference between JLabel and Label. I am following a book example I found that basically adds a list of items vertically using the BoxLayout. If these items are Labels, they are nicely spaced and reposition as the window is resized. If they are JLabels they end up all bunched together and do not adjust with resizing. My question is why? I attempted to write the code myself based on the example picture and I used JLabel. It took me forever to figure out why my labels looked different than the image. Is there something I am missing about the difference between the two types of labels, is this just something you learn from doing/using the two?

    I know this is a pretty basic question, but I want to actually understand why things are done the way they are. Thanks a lot for any information!

    Nate

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    One is heavyweight one is lightweight, one is AWT and the other is Swing. You should never ever mix them. Google about the differences between AWT and Swing to find information about it.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    njschwartz is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    What do you mean when you say never mix them? I should not have a gui with both swing and awt components? I guess that means the book and example I am using is not the best then probably. Since everything else in this particular gui I was making includes swing components, can you tell me how to format a JLabel to act like the Label is i.e. automatically moving with resize, spacing etc.? Thanks so much for the reply.

    Nate

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Yes, never ever mix them. Stay with Swing.
    The Sun tutorial about Swing is quite good:
    Trail: Graphical User Interfaces (The Java™ Tutorials)
    And
    Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by PhHein View Post
    Yes, never ever mix them. Stay with Swing.
    The Sun tutorial about Swing is quite good:
    Trail: Graphical User Interfaces (The Java™ Tutorials)
    And
    Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
    That's a very good point actually. Lots of people mess-up with those two. Using Swing we can do anything what had done with AWT. Only thing is that we had to have a better understanding of it.

    Most of the people didn't use AWT those days I guess, including me.

  6. #6
    njschwartz is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    Awesome! Thanks for the replies all. I do not understand why a learning book what teach such a bad practice like that. I will take a good look at the learning trial and see what I can learn.

    Nate

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by njschwartz View Post
    Awesome! Thanks for the replies all. I do not understand why a learning book what teach such a bad practice like that. I will take a good look at the learning trial and see what I can learn.

    Nate
    That's the challenge, selecting the most suitable materials to study, not only in Java, in any.

  8. #8
    njschwartz is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    I have been messing around reading the Java Swing Tutorials and am still unable to figure out how to get my JLabels to expand like I would like them too. I pasted my code below. If you look at the gui, each name should align with a row of check boxes. Interestingly, and what sparked my initial question is that if you simply remove the 'J' from the line nameBox.add(new JLabel(instruments[i])); it looks just like I would want, except it mixes Swing and AWT which I am not supposed to do. I apologize if what I am asking for is elementary if someone could please point me in the right direction I would greatly appreciate it!

    Nate

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class BeatBoxGui1 {
    
    	public static void main(String[] args) {
    		BeatBoxGui1 bbg = new BeatBoxGui1();
    		bbg.createGui();
    	}
    	
    	public void createGui() {
    		//create a frame
    		JFrame frame = new JFrame();
    		
    		//create a grid layout for the checboxes and a JPanel that uses the grid layout
    		GridLayout grid = new GridLayout(16,16);
    		grid.setVgap(1);
    		grid.setHgap(2);
    		JPanel center = new JPanel(grid);
    		
    		//create a border layout and a JPanel that uses it
    		BorderLayout layout = new BorderLayout();
    		JPanel background = new JPanel(layout);
    		
    		//Create a border around the background panel
    		background.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
    		
    		//Create buttons	
    		JButton start = new JButton("Start");
    		JButton stop = new JButton("Stop");
    		JButton tempoUp = new JButton("Tempo Up");
    		JButton tempoDown = new JButton("Tempo Down");
    		
    		//Create a Box container with vertical flow and add the buttons
    		Box buttonBox = new Box(BoxLayout.PAGE_AXIS);
    		buttonBox.add(start);
    		buttonBox.add(stop);
    		buttonBox.add(tempoUp);
    		buttonBox.add(tempoDown);
    		
    		//create a string array to hold instrument names
    		String[] instruments = {"Bass Drum", "Closed Hi-Hat", "Open Hi-Hat", "Acoustic Snare","1","2","3","4","5","6","7","8","9","10","11","12"};
    		
    		//Create labels for each instrument place them in a box container and align them verticaly
    		Box nameBox = new Box(BoxLayout.PAGE_AXIS);
    		for (int i = 0; i < instruments.length; i++) {		
    			nameBox.add(new JLabel(instruments[i]));
    		}
    		
    		//create the checkboxes and add them to a panel
    		for (int x = 0; x < 16; x++) {
    			for (int y = 0; y < 16; y++) {
    				JCheckBox cb = new JCheckBox();
    				center.add(cb);
    			}
    		}
    		
    		//Add buttons, name labels, and checkboxes to the background panel and add the background to the frame
    		background.add(BorderLayout.EAST, buttonBox);						
    		background.add(BorderLayout.WEST, nameBox);
    		background.add(BorderLayout.CENTER, center);
    		frame.getContentPane().add(background);
    		
    		
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setBounds(50,50,300,300);
    		frame.pack();
    		frame.setVisible(true);
    	}
    }

  9. #9
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    You've got the wrong layout there. The two boxes are separate and the components inside them have different heights. I'd have the JLabels and the JCheckBoxes on one JPanel using a GridLayout:
    How to Use GridLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

    That will look very strange. Now you can have a look at the GridBagLayout
    http://java.sun.com/docs/books/tutor...t/gridbag.html
    Last edited by PhHein; 04-15-2010 at 12:37 PM.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-06-2010, 11:02 PM
  2. First SWT Label
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 04:38 PM
  3. Example of SWT Label
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-09-2008, 12:02 PM
  4. Using java.awt.Label
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-02-2008, 06:28 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
  •