• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

using JLabel to title a JScroller

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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));
}
}
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
list = new MyList(listcontents);
list.setAlignmentX(0.0f); //<-----------add this line
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
 
Brian Cole
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
reply
    Bookmark Topic Watch Topic
  • New Topic