Results 1 to 9 of 9

Thread: JList Location

  1. #1
    BeeGee is offline Member
    Join Date
    Apr 2010
    Posts
    33
    Rep Power
    0

    Default 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

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    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.

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by BeeGee View Post
    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???

    what are you looking for, positioning the frame on the screen or the jlist inside the frame?

  4. #4
    BeeGee is offline Member
    Join Date
    Apr 2010
    Posts
    33
    Rep Power
    0

    Default

    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.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by BeeGee View Post
    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.
    Camickr already gave you the answer: now you're using a FlowLayout which centers its components by default. Read the API documentation for its other constructors.

    kind regards,

    Jos

  6. #6
    BeeGee is offline Member
    Join Date
    Apr 2010
    Posts
    33
    Rep Power
    0

    Default

    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.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by BeeGee View Post
    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.
    This is what the API documentation has to say about that other constructor:

    Quote Originally Posted by API docs
    Constructs a new FlowLayout with the specified alignment and a default 5-unit horizontal and vertical gap. The value of the alignment argument must be one of FlowLayout.LEFT, FlowLayout.RIGHT, FlowLayout.CENTER, FlowLayout.LEADING, or FlowLayout.TRAILING
    so:

    Java Code:
    new FlowLayout(FlowLayout.LEFT);
    does the job.

    kind regards,

    Jos

  8. #8
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by BeeGee View Post
    Any help is much appreciated. thanks.

    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)

  9. #9
    BeeGee is offline Member
    Join Date
    Apr 2010
    Posts
    33
    Rep Power
    0

    Default

    Thanks. Can't believe I didn't think of that. I feel so stupid now. Thank you for your help.

Similar Threads

  1. need to copy file from one location to another location
    By naveen.dpt2007 in forum New To Java
    Replies: 4
    Last Post: 02-28-2010, 03:53 PM
  2. location of click
    By saima in forum AWT / Swing
    Replies: 3
    Last Post: 11-22-2009, 12:06 PM
  3. how to set the location of a panel?
    By jboy in forum New To Java
    Replies: 5
    Last Post: 10-27-2009, 07:24 PM
  4. location provider returns no location
    By sandeeprao.techno in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-24-2009, 09:54 AM
  5. how to get the location of some button
    By mary in forum Java 2D
    Replies: 2
    Last Post: 08-05-2007, 04:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •