I have a panel, and I will have just fields and buttons on it. I thought about using JScrollPane
to not make the screen so large. However, I'm not able to add this scrollbar.
How do I add a scroll bar inside the p
panel, in the sample code? I just put a 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;
}
}