
Signature
Daniel Dyer
http://www.footballpredictions.net
>> 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.