By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,160 Members | 1,983 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,160 IT Pros & Developers. It's quick & easy.

JLabel and Jframe

Shinzon
P: 7
Ok so I have been working on this for a moment now and wondering how to add multiple jlabels to a jframe. the code looks like this:


Expand|Select|Wrap|Line Numbers
  1. JLabel jl = new JLabel("DVD ID #= " + dvd[0][0]); //Displays DVD id 
  2.  
  3.        JLabel j2 = new JLabel("Name of DVD = " + dvd[0][1]); //Displays dvd name
  4.  
  5.        JFrame frame = new JFrame( "Matt's DVD's" ); // Sets frame for label
  6.        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  7.        frame.getContentPane().add(jl);
  8.        frame.setSize( 420, 170 ); // set frame size
  9.        frame.setVisible( true ); // display frame
  10.  
I got it to display one of them, but it will not display a second one. What do I need to add to make it work? Any help would be awesome
Jul 15 '07 #1
Share this Question
Share on Google+
5 Replies


10K+
P: 13,264
Ok so I have been working on this for a moment now and wondering how to add multiple jlabels to a jframe. the code looks like this:

Expand|Select|Wrap|Line Numbers
  1. JLabel jl = new JLabel("DVD ID #= " + dvd[0][0]); //Displays DVD id 
  2.  
  3.        JLabel j2 = new JLabel("Name of DVD = " + dvd[0][1]); //Displays dvd name
  4.  
  5.        JFrame frame = new JFrame( "Matt's DVD's" ); // Sets frame for label
  6.        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  7.        frame.getContentPane().add(jl);
  8.        frame.setSize( 420, 170 ); // set frame size
  9.        frame.setVisible( true ); // display frame
  10.  
I got it to display one of them, but it will not display a second one. What do I need to add to make it work? Any help would be awesome
You now need to learn about layout managers. GridLayout might be the easiest for starters so you may want to learn that first. You should also consider using panels to better format your JFrame
Jul 15 '07 #2

Expert 10K+
P: 11,448
The Swing approach to visual components is a layered one: there are components
and containers. A container also is a component but it can contain other components.
A simple component is e.g. a JLabel, a JMenuItem etc.

A container needs a LayoutManager that determines the location of the other
components and their size. Most LayoutManagers negotiate with the contained
components about their actual size. LayoutManagers also determine how many
components can be contained, e.g. the BorderLayout can only contain five
other components.
Note that a container can contain other containers as well because containers
are also components.

I normally sketch things on paper to figure out where and how I want to display
the entire GUI. In your case a GridLayout may come in handy: set the LayoutManager
of the contentPane of the JFrame to a GridLayout, feed it all the JLabels and
you're in business.

kind regards,

Jos
Jul 15 '07 #3

Shinzon
P: 7
In your case a GridLayout may come in handy: set the LayoutManager
of the contentPane of the JFrame to a GridLayout, feed it all the JLabels and
you're in business.
Any links or some reading on how the layout would work? Because as it stands I can only add one jlabel. Basically what I want is the ability to have as many jlabels as possible in the container of jframe.
Jul 15 '07 #4

10K+
P: 13,264
Any links or some reading on how the layout would work? Because as it stands I can only add one jlabel. Basically what I want is the ability to have as many jlabels as possible in the container of jframe.
You can read about here.

I hope you've downloaded the whole tutorial.
Jul 15 '07 #5

P: 46
place your components like Jlabel in a swing container, then in calling the contentPane, Jframe add the container as its contentpane. Window is the only container for JFrame. A JPanel is a swing container for swing's components.
The following code illustrates this. The constructor adds the components, you Jlabels to itself, since it inherits from Jpanel and then Jframe takes it as its contentpane.
Expand|Select|Wrap|Line Numbers
  1. package examples;
  2.  
  3. import javax.swing.*;
  4.  
  5. public class Trials extends JPanel {
  6.  
  7.     //i added three labels; you can as much as you like
  8.     JLabel label_1_ID, label_1_name, label_2_ID, label_2_name;
  9.     String[][] mattDvds = {{"1","The homecoming"}, 
  10.                         {"2", "Castaway"}, 
  11.                         {"3" ,"Matt's Trunk"}};
  12.     /** Creates a new instance of Trials */
  13.     public Trials() {
  14.         //note that the default layout for JPanel is flowlayout
  15.         //so you can change layout with subsequent labels
  16.         label_1_ID = new JLabel("DVD ID# = "+ mattDvds[0][0]);
  17.         add(label_1_ID);
  18.         label_1_name = new JLabel("DVD Name: "+ mattDvds[0][1]);
  19.         add(label_1_name);
  20.         label_2_ID = new JLabel("DVD ID# = "+mattDvds[1][0]);
  21.         add(label_2_ID);
  22.         label_2_name = new JLabel("DVD Name: "+mattDvds[1][1]);
  23.         //...you can add other labels. try using a second method and a loop
  24.     }
  25.  
  26.     //create a method for the frame a la java tutorial
  27.     public static void dFrame(){
  28.         JFrame frame = new JFrame("Matt's DVDs");
  29.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30.  
  31.         //extends from JPanel, your contentpane
  32.         JComponent contentPane = new Trials(); 
  33.         contentPane.setOpaque(true);
  34.         frame.getContentPane().add(contentPane);
  35.  
  36.         frame.pack();
  37.         frame.setVisible(true);
  38.  
  39.     }
  40.  
  41.     public static void main(String[] args){
  42.         javax.swing.SwingUtilities.invokeLater(new Runnable(){
  43.             public void run(){
  44.                 dFrame();
  45.             }
  46.         });
  47.     }
  48. }
  49.  
Jul 16 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.