Go Back   Java Programming Forums > Java Standard Edition Programming Help > AWT / Java Swing


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-06-2009, 09:37 AM
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
  #2 (permalink)  
Old 05-06-2009, 08:53 AM
Freaky Chris's Avatar
Senile Half-Wit
 

Join Date: Mar 2009
Location: UK
Posts: 621
Thanks: 2
Thanked 64 Times in 60 Posts
Freaky Chris will become famous soon enoughFreaky Chris will become famous soon enoughFreaky Chris will become famous soon enough

I'm feeling Cheerful
Default Re: How to set the Listbox size.

How about using the following.
Java Code
JList.setVisibleRowCount(n);
That could be what you are after.

Chris
Reply With Quote
  #3 (permalink)  
Old 06-06-2009, 08:57 AM
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
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.
Reply With Quote
  #4 (permalink)  
Old 06-06-2009, 09:36 PM
Freaky Chris's Avatar
Senile Half-Wit
 

Join Date: Mar 2009
Location: UK
Posts: 621
Thanks: 2
Thanked 64 Times in 60 Posts
Freaky Chris will become famous soon enoughFreaky Chris will become famous soon enoughFreaky Chris will become famous soon enough

I'm feeling Cheerful
Default Re: How to set the Listbox size.

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

Chris
__________________
chris[at]javaprogrammingforums[dot]com

University of Wales, Bangor
Reply With Quote
The Following User Says Thank You to Freaky Chris For This Useful Post:
jacinto (22-06-2009)
  #5 (permalink)  
Old 22-06-2009, 12:39 PM
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
Default Re: How to set the Listbox size.

THANK YOU. It worked.
Reply With Quote
Reply

Tags
jlist

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Get the size of a file in bytes JavaPF Java Code Snippets and Tutorials 1 08-06-2009 03:19 PM
How to Change JTextArea font, font size and color Flash Java Code Snippets and Tutorials 4 08-07-2008 06:45 PM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 02:05 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.