This is the snippit im having a problem with. I've tried playing with the preferred size of both the JList and the JScrollPane with no luck/mixed results.

I want to be able to size it to exactly 1 size so it never changes.

import java.awt.BorderLayout;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;

public class plat extends JFrame {
	
	JList users;
	DefaultListModel listModel;

	public static void main(String[] args){
		SwingUtilities.invokeLater(new Runnable() {
			public void run(){
				new plat();
			}
		});
	}
		
	public plat() {
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
				
		JPanel topPanel = new JPanel();
			JTextArea chatArea = new JTextArea(10, 20);
			chatArea.setEditable(false);
		
			listModel = new DefaultListModel();
			users = new JList(listModel);
			users.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			users.setLayoutOrientation(JList.VERTICAL);
		
		topPanel.add(new JScrollPane(chatArea));
		topPanel.add(new JScrollPane(users));
		
		this.add(topPanel, BorderLayout.NORTH);
				
		//listModel.addElement("HI");
		//listModel.addElement("MY");
		//listModel.addElement("TRY");
		
		this.pack();
		this.setVisible(true);
	}
}

The JTextArea on the left I'm able to size using rows/cols options in the constructor. I want the JScrollPane for the JList to be a fixed width and match the height of the JTextArea.

Any suggestions?

Use the setSize method. The preferred size is just that - preferred. The layout manager is probably ignoring your preference, whereas if you use setSize, you might have better luck. Oh, and set the size of the scroll pane, which contains the other object. If that doesn't work try setting both.

Use setPreferredSize with Jscrollpane,

JScrollPane j1=new JScrollPane(chatArea);
JScrollPane j2=new JScrollPane(users);
j1.setPreferredSize(new Dimension(100,100));
j2.setPreferredSize(new Dimension(100,100));

topPanel.add(j1);
topPanel.add(j2);

Like I said, both didn't work..........

Since pack() would size everything to its preferred size and then size the frame to fit those preferred sizes, i thought setPreferredSize would work, but it doesnt...

Same with setSize...

whoops, my mistake... setPreferredSize was working... i was using the cols/rows of the jtextarea as a guide to set up the size of the scrollpane, but thats not the same as pixels.. :)

thanks

No, you did not say that. You said, "I've tried playing with the preferred size . ." As you know, setPreferredSize and setSize are two completely different methods. But anyway, I'm glad you got it working. Cheers

:)

Sounds the same to me :)

thanks though

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.