Question : Size JScrollPane correctly

In this example, the JScrollPane is too large. I've tried a number of ways to sizeit, but can't get it right. See the "size???" comment in MyOptionsPanel class.

To run this application, run the main in MyTabbedPane
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
package tableTest;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SpringLayout;
import javax.swing.table.DefaultTableModel;

public class MyOptionsPanel extends JPanel {

	private JLabel plantLabel;
	private JComboBox plantComboBox;

	private JLabel animalLabel;
	private JComboBox animalComboBox;

	private JPanel listPanel;
	private JTable myTable;
	
	private String[] columnHeader = { "Plant/Animal" };
	
	/**
	 * constructor
	 */
	public MyOptionsPanel() {

		layoutComponents();

	}

	/**
	 * layout components
	 */
	private void layoutComponents() {

		// plant
		plantLabel = new  JLabel("Plant");
		this.add(plantLabel);

		String[] plantTypes = {"grass", "forb", "flower", "shrub", "tree"};
		plantComboBox = new JComboBox(plantTypes);
		plantComboBox.setSelectedItem("grass");
		this.add(plantComboBox);

		// animal
		animalLabel = new  JLabel("Animal");
		this.add(animalLabel);

		String[] animalTypes = {"dog", "cat", "horse", "sheep", "cow"};
		animalComboBox = new JComboBox(animalTypes);
		animalComboBox.setSelectedItem("dog");
		this.add(animalComboBox);


		//  list
		listPanel = new JPanel(new BorderLayout());

		myTable = new JTable();
		DefaultTableModel defaultTableModel = new DefaultTableModel();
		String[][] stringArray = composeList();

		defaultTableModel.setDataVector(stringArray, columnHeader);
		myTable.setModel(defaultTableModel);

		//myTable.setPreferredScrollableViewportSize(myTable.getPreferredSize());
		JScrollPane scrollPane = new JScrollPane(myTable);
		listPanel.add(scrollPane);

		this.add(listPanel);

		// size???

		Dimension plantLabelDimension = plantLabel.getPreferredSize();
		Dimension animalDimension = animalLabel.getPreferredSize();
		Dimension myListDimension = myTable.getPreferredSize();
		Dimension scrollPaneDimension = scrollPane.getViewport().getPreferredSize();
		Dimension listPanellDimension = listPanel.getPreferredSize();
		Dimension thisDimension = this.getPreferredSize();

		//System.out.println ("plantLabelDimension width:height: " + plantLabelDimension.width + " : " + plantLabelDimension.height);
		//System.out.println ("animalabelDimension width:height: " + animalDimension.width + " : " + animalDimension.height);
		//System.out.println ("myList width:height: " + myListDimension.width + " : " + myListDimension.height);
		//System.out.println ("scrollPaneDimension width:height: " + scrollPaneDimension.width + " : " + scrollPaneDimension.height);
		//System.out.println ("listPanellDimension width:height: " + listPanellDimension.width + " : " + listPanellDimension.height);

		int width = listPanellDimension.width;
		myTable.setPreferredScrollableViewportSize(myTable.getSize());
		//myList.setFillsViewportHeight(true);
		scrollPane.getViewport().setPreferredSize(myTable.getPreferredSize());
		scrollPane.getViewport().setMinimumSize(myTable.getPreferredSize());
		scrollPane.getViewport().setMaximumSize(myTable.getPreferredSize());

		int height = plantLabelDimension.height +  animalDimension.height +  scrollPaneDimension.height;
		thisDimension.setSize(width, height);
		//System.out.println ("thisDimension width:height: " + thisDimension.width + " : " + thisDimension.height);

		this.setPreferredSize(thisDimension);

		SpringLayout springLayout = new SpringLayout();
		this.setLayout(springLayout);

		// adjust constraints (ie position components)

		// plant
		springLayout.putConstraint(SpringLayout.WEST, plantLabel, 10, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.NORTH, plantLabel, 15, SpringLayout.NORTH, this);

		springLayout.putConstraint(SpringLayout.WEST, plantComboBox, 110, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.NORTH, plantComboBox, 0, SpringLayout.NORTH, plantLabel);

		// animal
		springLayout.putConstraint(SpringLayout.WEST, animalLabel, 0, SpringLayout.WEST,plantLabel);
		springLayout.putConstraint(SpringLayout.NORTH, animalLabel, 15, SpringLayout.SOUTH, plantLabel);

		springLayout.putConstraint(SpringLayout.WEST, animalComboBox, 110, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.NORTH, animalComboBox, 0, SpringLayout.NORTH, animalLabel);

		// list
		springLayout.putConstraint(SpringLayout.WEST, listPanel, 0, SpringLayout.WEST,animalLabel);
		springLayout.putConstraint(SpringLayout.EAST, listPanel, -10, SpringLayout.EAST, this); // a little room on the right side
		springLayout.putConstraint(SpringLayout.NORTH, listPanel, 15, SpringLayout.SOUTH, animalLabel);
		springLayout.putConstraint(SpringLayout.SOUTH, listPanel, -10, SpringLayout.SOUTH, this);	 // a little room at the bottom
	}


	protected void resetList() {

		String[][] stringArray = composeList();
		DefaultTableModel dtm = (DefaultTableModel)myTable.getModel();
		dtm.setDataVector(stringArray, new Object[] { "Plant/Animal" });

	}

	private String[][] composeList() {

		String[][] returnArray = new String[2][1];
		returnArray[0][0] = (String) plantComboBox.getSelectedItem();
		returnArray[1][0] = (String) animalComboBox.getSelectedItem();

		return returnArray;

	}

	public String getPlant() 	{
		return (String)plantComboBox.getSelectedItem();
	}

	public String getAnimal() {
		return (String)animalComboBox.getSelectedItem();
	}

	public String getTitle() {
		return "Plant Animal List";
	}

}

--------

package tableTest;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;

public class MyTabbedPane extends JTabbedPane implements PropertyChangeListener {

	private MyOptionsPanel myOptionsPanel;
	private JPanel miscPanel;

	/**
	 * constructor
	 */
	MyTabbedPane() {

		setLayout();

	}

	/** 
	 * set layout of tabs
	 */
	private void setLayout() {

		myOptionsPanel = new MyOptionsPanel();
		this.addTab(myOptionsPanel.getTitle(), myOptionsPanel);

		miscPanel = new JPanel();
		this.addTab("Misc", miscPanel);

	}

	@Override
	public void propertyChange(PropertyChangeEvent event) {
		System.out.println ("MyTabbedPane.propertyChange(): event: " + event.getPropertyName());
	}

	 /**
	 * @return
	 */
	public MyOptionsPanel getMyOptionsPanel() {
		return myOptionsPanel;
	}

	/**
	 * @return
	 */
	public JPanel getMiscPanel() {
		return miscPanel;
	}


	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		try{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch (Exception e){
			e.printStackTrace();
		}

		MyTabbedPane myTabbedPane = new MyTabbedPane();

		final JFrame frame = new JFrame();

		JScrollPane scroll = new JScrollPane(myTabbedPane);
		scroll.getViewport().setPreferredSize(myTabbedPane.getPreferredSize());
		frame.getContentPane().add(scroll);

		frame.setTitle("MyTabbedPane");
		frame.addWindowListener(new WindowAdapter(){
			@Override
			public void windowClosing(WindowEvent e) {
				int width = frame.getWidth();
				int height = frame.getHeight();
				System.out.println ("width x height:: " + width + " x " + height);
			}
		});

		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setLocation(300, 200);
		frame.pack();
		frame.setVisible(true);
	}

}

--------

package tableTest;

import java.awt.BorderLayout;

import javax.swing.JPanel;

/**
 * panel containing tabbed pane
 */
public class MyTabbedPanePanel extends JPanel {

	protected MyTabbedPane myTabbedPane;


	/**
	 * constructor
	 */
	public MyTabbedPanePanel() {

		myTabbedPane = new MyTabbedPane();

		layoutComponents();

	}

	private void layoutComponents() {

		setLayout(new BorderLayout());

		add(myTabbedPane, BorderLayout.CENTER);

	}

	public String getPlant() {
		return myTabbedPane.getMyOptionsPanel().getPlant();
	}

	public String getAnimal() {
		return myTabbedPane.getMyOptionsPanel().getAnimal();
	}

	public void resetList() {
		myTabbedPane.getMyOptionsPanel().resetList();
	}

}

Answer : Size JScrollPane correctly

you've got it in the centre of a BorderLayout which will result in it being resized to fill all the available space in the parent panel.

If you want to control how much space it takes then add a JPanel to listPanel, and add your scrollpane to that panel. You can then use the layout manager of the panel to control the size of the scroll pane
Random Solutions  
 
programming4us programming4us