Results 1 to 4 of 4
  1. #1
    RavexENG is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default JList set location and size

    Hi!

    I would like to add JList for displaying list in my program.
    I am not using any layout manager (setLayout(null);).
    When i try to add the JList it doesn't appear when i run the program.
    For other elements (JButton etc.) i was able to add the button after setting the location and size but it doesn't work with JList.
    It works if i use FlowLayout but i would prefer to set the size and location by myself.

    This is my code:

    Java Code:
    package javacourseworkpractise;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    
    public class JBPlaylist extends JFrame implements ActionListener {
        //JFrame items setup
        JTextField playlistName = new JTextField(10);
        JButton done = new JButton("Save");
        JLabel songSelectLabel = new JLabel("Choose songs to add to new playlist:");
        
       //JLIST 
        private JList list; 
        private static String[] colornames = {"Song 1", "Song 2", "Song 3", "Song 4"};
        private static Color[] colors = {Color.BLACK, Color.BLUE, Color.RED, Color.WHITE};
        
        
        public static void main(String[] args) {
            JBPlaylist jf = new JBPlaylist();
            
            
        }//end of main method  
        
        
        public JBPlaylist(){
            //layout setup
            setLayout(null);
            setResizable(false);
            setSize(300, 600);
            setTitle("JB- Create new playlist");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            
            
            
            //playlistName setup
            playlistName.setLocation(10,10);
            playlistName.setSize(200, 30);
            playlistName.setText("Enter playlist name");
            add(playlistName);
            
            // done button setup
            done.setSize(70,30);
            done.setLocation(220,10);
            add(done);
            done.addActionListener(this); //not in use
            
            //songSelectLabelLabelLabel setup
            songSelectLabel.setSize(250,15);
            songSelectLabel.setLocation(15, 60);
            add(songSelectLabel);
            
            
            //JLIST
            list = new JList(colornames); 
            list.setVisibleRowCount(3); 
            list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            list.setLocation(10, 400);
            list.setSize(100, 400);
            
           
            add(new JScrollPane(list));
            
            
            
            setVisible(true);
            
            
        }
        
        
         public void actionPerformed(ActionEvent e){
            
        }//end of actionPerformer 
        
        
        
    }//end of class
    thanks for help

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,723
    Rep Power
    5

    Default Re: JList set location and size

    Quote Originally Posted by RavexENG View Post
    It works if i use FlowLayout but i would prefer to set the size and location by myself.
    Because you like to make your life as difficult as possible? Use a layout manager, they exist to help you deal with layout problems, not to hinder you. Using a combination of layout managers you can position components anywhere that you want WITHOUT having to micro-manage their positioning and sizing.

    In any case: you likely need to give the list a preferred size to properly work together with the scrollpane.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    RavexENG is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default Re: JList set location and size

    Quote Originally Posted by gimbal2 View Post

    In any case: you likely need to give the list a preferred size to properly work together with the scrollpane.
    I've tried working with layout managers and i feel much more comfortable when im not using them.
    I am working on a very simply music player for university assignment so positioning everything without layout manager won't be hard.

    Can you please give me some example code for setting the preferred size? Currently I am using this code and it doesn't work.

    Java Code:
    list.setLocation(10, 400);
    list.setSize(100, 400);

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: JList set location and size

    Try positioning the scrollpane
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: 09-27-2010, 07:07 AM
  2. JList Location
    By BeeGee in forum Advanced Java
    Replies: 8
    Last Post: 04-07-2010, 03:09 PM
  3. 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
  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. JFrame 's components size and location problem
    By petrosgraf in forum Threads and Synchronization
    Replies: 5
    Last Post: 04-18-2009, 02:24 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
  •