0
I made a method whose goal is to add a number of checkboxes according to the value of a variable that I will receive. I wanted him to stay inside a scrollPane so he wouldn’t take up more space than I set.
What I couldn’t get was to make the components all the same JScrollPane
. I wanted to make them stand next to each other, with at most 4 checks on each line, so I set after the is a small size for the JScrollPane
, but it creates a Jscrollpane for each check.
Follow an example below:
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class TesteAdd extends JFrame {
JScrollPane jsp = new JScrollPane();
public TesteAdd() {
add(addComp());
setSize(500, 500);
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private JComponent addComp() {
JPanel painel = new JPanel();
painel.setLayout(new FlowLayout());
painel.setBorder(BorderFactory.createTitledBorder("Borda"));
int controle = 7;
for (int i = 0; i < controle; i++) {
String nome = "Check " + Integer.toString(i);
JCheckBox box = new JCheckBox(nome);
painel.add(jsp = new JScrollPane(box));
}
jsp.setPreferredSize(new Dimension(150, 150));
painel.setPreferredSize(new Dimension(300, 300));
return painel;
}
public static void main(String[] args) {
TesteAdd a = new TesteAdd();
}
}
painel.add(jsp = new JScrollPane(box));
haven’t noticed anything wrong here?– user28595
@diegofm without being like this, I couldn’t make him "pick it up", he’s adding a jsp to each ne? :(
– Java