View Single Post
  #1 (permalink)  
Old 04-06-2009, 09:37 AM
jacinto jacinto is offline
Junior Member
 

Join Date: May 2009
Location: Mumbai/ Bombay - India
Posts: 15
Thanks: 1
Thanked 1 Time in 1 Post
jacinto is on a distinguished road
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:-
Java Code
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.
Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence