Results 1 to 9 of 9
Thread: JList Location
- 04-06-2010, 10:10 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 33
- Rep Power
- 0
JList Location
Hello,
I was wondering if there is a way to set the location of JList on a Frame. I noticed that the JList shows up in the center for the frame everytime. Is there a way to position the frame to the left of the screen or any other position???
A sample code is listed below:
XML Code:import javax.swing.*; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import java.awt.*; import java.awt.event.*; public class JListDemo extends JFrame { JList list; String[] listColorNames = { "black", "blue", "green", "yellow", "white" }; Container contentpane; public JListDemo() { super("List Source Demo"); contentpane = getContentPane(); contentpane.setLayout(new FlowLayout()); list = new JList(listColorNames); list.setSelectedIndex(0); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); contentpane.add(new JScrollPane(list)); list.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { System.out.println("You have selected "+list.getSelectedValue()); } }); setSize(200, 200); setVisible(true); } public static void main(String[] args) { JListDemo test = new JListDemo(); test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }Last edited by BeeGee; 04-06-2010 at 10:25 PM. Reason: Code Tags Addition
- 04-06-2010, 10:14 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,146
- Rep Power
- 5
Use the "Code" formatting tags when posting code.
Read the section from the Swing tutorial on Using Layout Managers to understand whats happening.
The FlowLayout has a parameter that allows you to control where the component is layed out.
- 04-06-2010, 11:06 PM #3
- 04-07-2010, 02:31 PM #4
Member
- Join Date
- Apr 2010
- Posts
- 33
- Rep Power
- 0
Hi, Thanks for your help. I was looking for a way to position the JList inside the frame as it's default position seems to be at the center of the frame.
Any help is much appreciated. thanks.
- 04-07-2010, 02:35 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 04-07-2010, 03:04 PM #6
Member
- Join Date
- Apr 2010
- Posts
- 33
- Rep Power
- 0
Sorry to bother you all. I looked at the other constructors of FlowLayout and used the FlowLayout(int align) constructor. However, I was unable to use the setLayout function with the FlowLayout constructor as the latter had an int type. It ended up looking similar to ListDemo in How to Use Lists (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components). I was trying to shift what I was getting when using new FlowLayout() to the left and not change the way it looked.
I apologize if I am annoying but I really need your help. Thanks.
- 04-07-2010, 03:08 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 04-07-2010, 03:09 PM #8
my suggestion is changing two lines in
contentpane.setLayout(new BorderLayout());
contentpane.add(new JScrollPane(list), BorderLayout.WEST);
use WEST to position your jscrollpane on the left side, EAST for the right side and CENTER for center positionl. for all details look at BorderLayout (Java Platform SE 6)
- 04-07-2010, 03:09 PM #9
Member
- Join Date
- Apr 2010
- Posts
- 33
- Rep Power
- 0
Similar Threads
-
need to copy file from one location to another location
By naveen.dpt2007 in forum New To JavaReplies: 4Last Post: 02-28-2010, 03:53 PM -
location of click
By saima in forum AWT / SwingReplies: 3Last Post: 11-22-2009, 12:06 PM -
how to set the location of a panel?
By jboy in forum New To JavaReplies: 5Last Post: 10-27-2009, 07:24 PM -
location provider returns no location
By sandeeprao.techno in forum CLDC and MIDPReplies: 0Last Post: 09-24-2009, 09:54 AM -
how to get the location of some button
By mary in forum Java 2DReplies: 2Last Post: 08-05-2007, 04:02 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks