Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How to set the Listbox size.

  1. #1
    Junior Member
    Join Date
    May 2009
    Location
    Mumbai/ Bombay - India
    Posts
    15
    Thanks
    1
    Thanked 1 Time in 1 Post

    Lightbulb How to set the Listbox size.

    AIM: I want to display a listbox on the left side of a pael and the labels, textboxes, and buttons on the right.

    This is the code for my panel - it is a scrollpane:-
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.*;
     
     
    public class Users extends JScrollPane{
     
    	JList list;
    	DefaultListModel data;
    	JLabel lName,lPassword,lType;
    	JTextField tName, tPassword,tType;
    	JButton clear,add,edit,delete;
     
    	Users(){
    		init();
    		add();
    	}
     
    	void init(){
    		this.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    		this.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
     
    		data = new DefaultListModel();
    		//data.addElement("a");
    		list = new JList(data);
     
    		lName = new JLabel("Name");
    		lPassword = new JLabel("Password");
    		lType  = new JLabel("Type");
     
    		tName = new JTextField(20); 
    		tPassword = new JTextField(20); 
    		tType = new JTextField(20);
     
    		clear = new JButton("Clear");
    		add = new JButton("Add");
    		edit = new JButton("Edit");
    		delete = new JButton("Delete");
     
    		Font f = new Font("courier", Font.PLAIN, 14);
    		lName.setFont(f);
    		lPassword.setFont(f);
    		lType.setFont(f);
    		tName.setFont(f);
    		tPassword.setFont(f);
    		tType.setFont(f);
    		clear.setFont(f);
    		add.setFont(f);
    		edit.setFont(f);
    		delete.setFont(f);
    		list.setFont(f);
     
    	}
     
     
    	void add(){
    		GridBagConstraints gc = new GridBagConstraints();
    		gc.insets = new Insets(5,5,5,5);
     
    		JPanel buttonPane = new JPanel();
    		TitledBorder tb = new TitledBorder("Functions");
    		buttonPane.setBorder(tb);
    		buttonPane.add(clear);
    		buttonPane.add(add);
    		buttonPane.add(edit);
    		buttonPane.add(delete);
     
    		JPanel dataPane = new JPanel();
    		tb = new TitledBorder("Data");
    		dataPane.setBorder(tb);
    		dataPane.setLayout(new GridBagLayout());
     
    		gc.gridy = 0;
    		gc.gridx=0;
    		dataPane.add(lName,gc);
    		gc.gridx=1;
    		dataPane.add(tName,gc);
     
    		gc.gridy = 1;
    		gc.gridx=0;
    		dataPane.add(lPassword,gc);
    		gc.gridx=1;
    		dataPane.add(tPassword,gc);
     
    		gc.gridy = 2;
    		gc.gridx=0;
    		dataPane.add(lType,gc);
    		gc.gridx=1;
    		dataPane.add(tType,gc);
     
    		JScrollPane listPane = new JScrollPane(list);
    		tb = new TitledBorder("Select User");
    		listPane.setBorder(tb);
     
     
     
    		JPanel top = new JPanel(new GridBagLayout());
    		gc.gridx=0;
    		gc.gridy=0;
    		gc.gridheight=2;
    		gc.fill = GridBagConstraints.BOTH;
    		gc.ipadx =100;
    		top.add(listPane,gc);
    		gc.ipadx=0;
    		gc.gridx=1;
    		gc.gridy = 0;
    		gc.gridheight=1;
    		gc.fill = GridBagConstraints.NONE;
    		top.add(dataPane,gc);
    		gc.gridy = 1;
    		top.add(buttonPane,gc);
     
    		this.setViewportView(top);
    	}
    }

    My problem is that I am not able to set a specific size for the listbox. In the above code, in the init() method, if you uncomment the comment, there is a change in the size of the list box.

    Please help.


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: How to set the Listbox size.

    How about using the following.
    JList.setVisibleRowCount(n);

    That could be what you are after.

    Chris

  3. #3
    Junior Member
    Join Date
    May 2009
    Location
    Mumbai/ Bombay - India
    Posts
    15
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: How to set the Listbox size.

    I shall try. But I think that this method may only affect the height. My concern is for the width also.

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: How to set the Listbox size.

    setFixedCellWidth(int n) & same for height can be used then

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  5. The Following User Says Thank You to Freaky Chris For This Useful Post:

    jacinto (June 22nd, 2009)

  6. #5
    Junior Member
    Join Date
    May 2009
    Location
    Mumbai/ Bombay - India
    Posts
    15
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: How to set the Listbox size.

    THANK YOU. It worked.

Similar Threads

  1. How to Change JTextArea font, font size and color
    By Flash in forum Java Swing Tutorials
    Replies: 7
    Last Post: January 14th, 2012, 10:47 PM
  2. How to Get the size of a file in bytes
    By JavaPF in forum File Input/Output Tutorials
    Replies: 1
    Last Post: June 8th, 2009, 11:19 AM

Tags for this Thread