Does anyone know how to add a ScrollBars to Panel with GridBagLayout, with vertical scroll only. The panels components need to be self contained when the user resizes horizontally until a minimal size has been reached. Vertical scroll needs to be visible. I can add a scrollbar, but need the components to stay on screen and all visible horizontally. The GridBagLayout should still move the components.
The code:
import java.awt.; import javax.swing.;
public class SwingTest {
public static void main(String[] args) {
final JScrollPane scrollPane = new JScrollPane(addCustomer());
JFrame frame = new JFrame("Swing Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(640, 480);
frame.setMinimumSize(new Dimension(300, 480));
frame.setLocation(200, 200);
frame.getContentPane().add(scrollPane);
frame.setVisible(true);
}
public static Component addCustomer(){
JTextField textField1;
/***********************Initialize Fields******************************/
JLabel labels[] = new JLabel[2];
labels[0] = new JLabel("Left side - more components to add here");
labels[1] = new JLabel("Right side - more components to add here");
textField1 = new JTextField(10);
JComboBox<String> materialTitlesCombo = new JComboBox<String>();
materialTitlesCombo.addItem("-");
materialTitlesCombo.addItem("test");
materialTitlesCombo.setPreferredSize(new Dimension(250, 500));
materialTitlesCombo.setMinimumSize(materialTitlesCombo.getPreferredSize());
/****************************Create the canvas**************************/
GridBagConstraints constraints;
constraints = new GridBagConstraints();
final JPanel panel = new JPanel(new GridBagLayout());
constraints.gridx = 0;
constraints.gridy = 3;
constraints.gridheight = 1;
constraints.gridwidth = 1;
constraints.weightx = 0.5;
constraints.weighty = 1;
constraints.anchor = GridBagConstraints.WEST;
constraints.insets = new Insets(-20,20,0,0);
panel.add(labels[0], constraints); //Left side
constraints.gridx = 1;
constraints.gridy = 3;
constraints.gridheight = 1;
constraints.gridwidth = 1;
constraints.weightx = 0.5;
constraints.weighty = 1;
constraints.anchor = GridBagConstraints.CENTER;
constraints.insets = new Insets(-10,-9,0,9);
panel.add(textField1, constraints); // TextField 1
constraints.gridx = 8;
constraints.gridy = 3;
constraints.gridheight = 1;
constraints.gridwidth = 1;
constraints.weightx = 0.5;
constraints.weighty = 1;
constraints.anchor = GridBagConstraints.WEST;
constraints.insets = new Insets(-15,15,0,0);
panel.add(labels[1], constraints);//Right side
return panel;
}
}
Aucun commentaire:
Enregistrer un commentaire