File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes using JLabel to title a JScroller Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "using JLabel to title a JScroller" Watch "using JLabel to title a JScroller" New topic
Author

using JLabel to title a JScroller

Tony Carolla
Greenhorn

Joined: Dec 05, 2005
Posts: 13
I am trying to make a list of items, with a title at the top. I have created a frame, with an embedded panel (using BoxLayout, Y_AXIS), with an embedded JLabel, and a JScrollPane with a JList:

JFrame-
-JPanel
-JLabel
-JScrollPane
-JList

For some reason, the JLabel starts about 1/4 of the way to the right, and not left-justified. The JScrollPane starts on the left. I tried creating the JLabel and specifying JLabel(title, horizontal alignment) with SwingConstants.LEFT, but it doesnt' change anything. HELP!

here is the code:

import javax.swing.*;
import java.awt.*;

public class PanelTest {
public static void main(String[] args) {
Dimension winsize = new Dimension(250, 400);
String frametitle = new String("Panel Test");
String listtitle = new String("Item List");
String[] listcontents = new String[] {"List Item 1", "List Item 2", "List Item 3", "List Item 4"};

MyFrame frame = new MyFrame(winsize, frametitle, listtitle, listcontents);
}
}

class MyFrame extends JFrame {
MyPanel panel;

public MyFrame(Dimension winsize, String frametitle, String listtitle, String[] listcontents) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle(frametitle);
setSize(winsize);
panel = new MyPanel(listtitle, listcontents);
add(panel);
setVisible(true);
}
}

class MyPanel extends JPanel {
JLabel label;
MyList list;

public MyPanel(String title, String[] listcontents) {
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
label = new JLabel(title);
list = new MyList(listcontents);
add(label);
add(list);
}
}

class MyList extends JScrollPane {
String[] listcontents;

public MyList(String[] listcontents) {
super(new JList(listcontents));
}
}
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
list = new MyList(listcontents);
list.setAlignmentX(0.0f); //<-----------add this line
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Dunn's suggestion is good. Or you could use BorderLayout instead:

public MyPanel(String title, String[] listcontents) {
setLayout(new BorderLayout());
label = new JLabel(title);
list = new MyList2(listcontents);
add(label, BorderLayout.NORTH);
add(list, BorderLayout.CENTER);
}


bitguru blog
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Actually, in my code super(new BorderLayout())
should be slightly preferred to setLayout(new BorderLayout()).

In your code I might have done

super(null); // layout manager will be set next line
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

but this is hardly a big deal, and irrelevant to your query.
Tony Carolla
Greenhorn

Joined: Dec 05, 2005
Posts: 13
Thanks Michael and Brian. I decided to use the BorderLayout, and it places things at the left.

I saw the setAlignmentX(float) method, but I was thinking, alignment as in Left, Right, etc. I had no idea what the float arg was. I'll bet it's the pixel or percentage from the Left to start(?).

For some reason, using BoxLayout seems to ignore the setAlignmentX arg. But the BoxLayout is the ideal setup for such a simple component.

Thanks again!

final code (cleaned up a bit):

import javax.swing.*;
import java.awt.*;

public class PanelTest {
public static void main(String[] args) {
Dimension winsize = new Dimension(250, 400);
String frametitle = new String("Panel Test");
String listtitle = new String("Item List");
String[] listcontents = new String[] {"List Item 1", "List Item 2", "List Item 3", "List Item 4"};

MyFrame frame = new MyFrame(winsize, frametitle, listtitle, listcontents);
}
}

class MyFrame extends JFrame {
public MyFrame(Dimension winsize, String frametitle, String listtitle, String[] listcontents) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle(frametitle);
setSize(winsize);
add(new ListPanel(listtitle, listcontents));
setVisible(true);
}
}

class ListPanel extends JPanel {
public ListPanel(String title, String[] listcontents) {
setLayout(new BorderLayout());
add(new JLabel(title), BorderLayout.NORTH);
add(new JScrollPane(new JList(listcontents)), BorderLayout.CENTER);
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using JLabel to title a JScroller
 
Similar Threads
Scrolling JPanel Squeezing All The UI Components
Why are my JFrames closing?
NullPointerException
JLabel ..does it accept HTML ?
GridBagLayout resize issue