0
I have a panel, and I will have enough fields and buttons on it. I thought to use a JScrollPane
not to make the screen so big. However, I’m not able to add this scroll bar.
As I add a scroll bar inside the panel p
, in the example code? I put only one field to simplify the code.
package scroll;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class TelaSroll extends JFrame {
private final JTextField vazio = new JTextField();
private JButton bt = new JButton("Exemplo");
public static void main(String[] args) {
TelaSroll tela = new TelaSroll();
}
public TelaSroll() {
setSize(450, 345);
add(telaPainel());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public JComponent telaPainel() {
JPanel painel = new JPanel();// Painel principal, nele eu adiciono outros paines que organizam a tela
JPanel p = new JPanel();
p.setPreferredSize(new Dimension(200, 200));
p.add(vazio);
vazio.setPreferredSize(new Dimension(100, 50));
JScrollPane srcPainel = new JScrollPane(p);
painel.add(srcPainel);
p.add(bt);
return painel;
}
}
Using another layout manager it uga a little, that’s why I’m not setting the scroll position (vertical/horizontal)?
– JavaTech
@G.Araujo what do you mean, "Hold on"? I tested the way the code was initially (without the boxlayout), and what happened was to create a horizontal scroll, since every Jpanel has by default Flowlayout, which organizes components in the form of a horizontal line. I used boxlayout to exemplify the vertical scroll, but it works for both.
– user28595
worked out here, thank you !
– JavaTech