Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / April 2005

Tip: Looking for answers? Try searching our database.

Column of JLabels?

Thread view: 
Stevey - 19 Apr 2005 19:15 GMT
How do I build a column of JLabels?

When a user performs a certain event I want to add a new JLabel to a
suitable container which will display them as a vertical column of JLabels
(i.e. a list).

I tried using a JPanel with BoxLayout and with GridLayout, but for some
reason I can't get them to add an element when the event is performed. I can
get the JPanel to add if I use FlowLayout but this does not look how I want
it to be (as the JLabels append horizontally).

Any suggestions?
Daniel Dyer - 19 Apr 2005 19:18 GMT
> How do I build a column of JLabels?
>
[quoted text clipped - 9 lines]
> want
> it to be (as the JLabels append horizontally).

Are you remembering to call revalidate() on the container after you add  
the new label?

Are you declaring the GridLayout to have a variable number of rows  
(http://java.sun.com/j2se/1.5.0/docs/api/java/awt/GridLayout.html#GridLayout(int,  
int))?

Are you sure that a JList (perhaps with a custom renderer) wouldn't be  
more appropriate?

Dan.

Signature

Daniel Dyer
http://www.footballpredictions.net

Stevey - 19 Apr 2005 19:32 GMT
>> How do I build a column of JLabels?
>>
[quoted text clipped - 12 lines]
> Are you remembering to call revalidate() on the container after you add
> the new label?

I wasn't, but I am now. Doesn't appear to make any difference.

> Are you declaring the GridLayout to have a variable number of rows
> (http://java.sun.com/j2se/1.5.0/docs/api/java/awt/GridLayout.html#GridLayout(int,
> int))?

Yeah.

pnl_old_questions.setLayout(new GridLayout(0,1));

With the above code it just doesn't seem to add the components to
pnl_old_questions (well it does when I initially set up the display, but not
when the user performs an action). However if I change the above code to
FlowLayout it adds components fine (but looks wrong). Same problem with
BoxLayout.

> Are you sure that a JList (perhaps with a custom renderer) wouldn't be
> more appropriate?

Fairly sure... the purpose of the list is purely to display a kind of 'log'
of the user's actions, not take in any further input from the user based on
this list.

Thanks for the suggestions.
Arnaud Berger - 20 Apr 2005 08:05 GMT
Hi,

This is a sample you may use :

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class AddLabels {

   public static void main(String[] args) {

       JFrame frame =new JFrame();
       frame.setSize(200,200);

       Container cont=frame.getContentPane();
       cont.setLayout(new BorderLayout());
       JPanel buttonPanel=new JPanel();
       final JPanel labelsPanel=new JPanel();

       /* GridLayout should also work */
       /*labelsPan.setLayout(new GridLayout(0,1));*/
       labelsPanel.setLayout(new BoxLayout(labelsPanel,BoxLayout.Y_AXIS));

       JButton addLabel=new JButton("add");

       addLabel.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent arg0) {
               labelsPanel.add(new JLabel("one label"));
               labelsPanel.revalidate();
       }});

       buttonPanel.add(addLabel);
       cont.add(buttonPanel,BorderLayout.NORTH);
       /* a scrollPane to scroll when the number of labels doesn't fit the
panel */
       cont.add(new JScrollPane(labelsPanel),BorderLayout.CENTER);

       frame.setVisible(true);
}
}

Regards,

Arnaud

> >> How do I build a column of JLabels?
> >>
[quoted text clipped - 16 lines]
>
> > Are you declaring the GridLayout to have a variable number of rows

(http://java.sun.com/j2se/1.5.0/docs/api/java/awt/GridLayout.html#GridLayout
(int,
> > int))?
>
[quoted text clipped - 16 lines]
>
> Thanks for the suggestions.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.