Sto cercando di aggiungere componenti a un JPanel, quindi inserire questo pannello in un JScrollPane, quindi inserire JScrollPane in JOptionPane.Perché JScrollPane in JOptionPane non mostra tutto il suo contenuto?
Il problema: solo 19 linee di componenti aggiunte. Esiste un ciclo for che determina il numero di righe di componenti, se si modifica il contatore delle condizioni a 19 o meno, verranno visualizzate tutte.
Questa è una SSCCE del problema
import java.awt.Dimension;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class DynamicGroupLayout extends JPanel
{
JButton addRecordButton;
JTextField[] FieldsArray;
JLabel[] LabelsArray;
GroupLayout layout;
JScrollPane scrollPane;
public DynamicGroupLayout()
{
addRecordButton = new JButton("add record");
layout = new GroupLayout(this);
this.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
scrollPane = new JScrollPane(this);
scrollPane.getVerticalScrollBar().setUnitIncrement(16);
scrollPane.setPreferredSize(this.getPreferredSize());
setTextFields();
showDialog();
}
@Override
public Dimension getPreferredSize()
{
return new Dimension(400, 500);
}
private void setTextFields()
{
int num = 30; //If 19 or less, all components shown.
String[] labelsNames =
{
"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"
};
FieldsArray = new JTextField[num];
LabelsArray = new JLabel[num];
GroupLayout.ParallelGroup parallelGroupHoriz = layout.createParallelGroup();
layout.setHorizontalGroup(layout.createSequentialGroup().addGroup(parallelGroupHoriz));
for (int i = 0; i < num; i++)
{
System.out.println(i);
LabelsArray[i] = new JLabel(labelsNames[i]);
FieldsArray[i] = new JTextField(10);
FieldsArray[i].setMaximumSize(new Dimension(200, 3));
LabelsArray[i].setLabelFor(FieldsArray[i]);
parallelGroupHoriz.addGroup(layout.createSequentialGroup()
.addComponent(LabelsArray[i])
.addComponent(FieldsArray[i]));
}
parallelGroupHoriz.addComponent(addRecordButton, GroupLayout.Alignment.CENTER);
GroupLayout.SequentialGroup sequentialGroupVert = layout.createSequentialGroup();
layout.setVerticalGroup(sequentialGroupVert);
for (int i = 0; i < num; i++)
{
sequentialGroupVert.addGroup(layout.createParallelGroup().
addComponent(LabelsArray[i])
.addComponent(FieldsArray[i]));
}
sequentialGroupVert.addComponent(addRecordButton);
for (int i = 0; i < num; i++)
{
layout.linkSize(SwingConstants.HORIZONTAL, LabelsArray[i], LabelsArray[0]);
}
}
private void showDialog()
{
JOptionPane.showOptionDialog(null, scrollPane, "Update data",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null,
null, null);
}
public static void main(String[] args)
{
DynamicGroupLayout d = new DynamicGroupLayout();
}
}
+1 per [sscce] (http://sscce.org/); guarda anche questo [Q & A] (http://stackoverflow.com/q/10094825/230513). – trashgod
@trashgod Grazie a trashgod, ma il mio problema è "Come usare GroupLayout per aggiungere componenti in modo iterativo". Li ho già aggiunti, ma non tutti sono visualizzati. –
Oltre 19 sono stati aggiunti ma non sono visibili nel Viewport, è possibile vedere se si modifica 'return new Dimension (400, 900)' – oliholz