Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

AWT List component, how to set width?

843807May 19 2006
I have an AWT List component which I want to set wider to avoid scroll bars. The setSize(int, int) does not seem to do any difference. How can I do this?

Here is an example piece of code:
import java.awt.Button;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.List;
import java.awt.Panel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BoxLayout;

public class listsizeproblem {
	
	public static void main (String [] args) {
		ExampleFrame mainframe = new ExampleFrame("List example");
	}
}

class ExampleFrame extends Frame {

	public List list1, list2;
	Button delete1, delete2, up1, down1, up2, down2;

	ExampleFrame (String t) {
		this.setTitle(t); // setting up the main window
		this.setSize(1000, 600);
		this.setLocation(50, 50);
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		addWindowListener(new WindowAdapter() { // application will exit if window is closed
	        public void windowClosing(WindowEvent e) {
	        	System.exit(0);
	        }
	    });
		
		Panel control = new Panel(new FlowLayout(FlowLayout.LEFT, 0, 0));
		Panel lstpanel1 = new Panel();
		lstpanel1.setLayout(new BoxLayout(lstpanel1, BoxLayout.Y_AXIS));
		Panel lstpanel2 = new Panel();
		lstpanel2.setLayout(new BoxLayout(lstpanel2, BoxLayout.Y_AXIS));
		Panel order1 = new Panel();
		order1.setLayout(new BoxLayout(order1, BoxLayout.Y_AXIS));
		Panel order2 = new Panel();
		order2.setLayout(new BoxLayout(order2, BoxLayout.Y_AXIS));
	
		list1 = new List(15, false);
		list2 = new List(15, false);
		list2.setSize(500, 500);
		list2.setPreferredSize(new Dimension(500, 500));
		list2.setMinimumSize(new Dimension(500, 500));
		
		list1.add("this is an example of a string that does not fit the list");
		delete1 = new Button("Delete");
		delete2 = new Button("Delete");
		up1 = new Button("Up");
		down1 = new Button("Down");
		up2 = new Button("Up");
		down2 = new Button("Down");
				
		lstpanel1.add(list1);
		lstpanel1.add(delete1);
		lstpanel2.add(list2);
		lstpanel2.add(delete2);
		order1.add(up1);
		order1.add(new Label(" "));
		order1.add(new Label(" "));
		order1.add(down1);
		order2.add(up2);
		order2.add(new Label(" "));
		order2.add(new Label(" "));
		order2.add(down2);
		control.add(lstpanel1); 
		control.add(order1);
		control.add(lstpanel2);
		control.add(order2);
				
		this.add(control);
		
		this.pack();
		
		this.setVisible(true);
		
		list2.add("another example of a string that make the list scroll");

	}
}
I am suspecting that it could be because of mixing in Swings BoxLayout with AWT components, but AWT doesn't have a LayoutManager that does what I need.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 16 2006
Added on May 19 2006
0 comments
232 views