I have made a program with a scroll pane, but it is not working. Please look at the source code:
JInfoView.java
package view;
import javax.swing.*;
import java.util.*;
import java.awt.*;
public class JInfoView extends JPanel {
private JButton button = new JButton("ADD");
private JButton buttonDelete = new JButton("DEL");
private JTextField input = new JTextField("Text", 5);
private JLabel label = new JLabel("Test");
public JInfoView() {
this.setLayout(new FlowLayout());
this.add(button);
this.add(buttonDelete);
this.add(input);
this.add(label);
}
}
JMainView.java
package view;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import view.JInfoView;
public class JMainView extends JFrame {
private JPanel mypanel = new JPanel(new GridLayout(0, 1, 30, 50));
private JScrollPane scrollPane = new JScrollPane(mypanel);
public JMainView() {
super("Simple Example");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(new FlowLayout());
container.add(scrollPane);
scrollPane.setVisible(true);
scrollPane.setAutoscrolls(true);
mypanel.add(new JInfoView());
mypanel.add(new JInfoView());
mypanel.add(new JInfoView());
mypanel.add(new JInfoView());
mypanel.add(new JInfoView());
mypanel.add(new JInfoView());
mypanel.add(new JInfoView());
mypanel.add(new JInfoView());
mypanel.add(new JInfoView());
}
public static void main(String[] args) {
JMainView app = new JMainView();
app.setVisible(true);
}
}
I’ve read a tutorial which says:
//In a container that uses a BorderLayout:
textArea = new JTextArea(5, 30);
...
JScrollPane scrollPane = new JScrollPane(textArea);
...
setPreferredSize(new Dimension(450, 110));
...
add(scrollPane, BorderLayout.CENTER);
I’ve done the same steps,
private JPanel mypanel = new JPanel(new GridLayout(0, 1, 30, 50));
private JScrollPane scrollPane = new JScrollPane(mypanel);
And then added scrollpane:
container.add(scrollPane);
Where is a mistake?
EDIT:
The problem is that the scroll pane is not working. I add many JInfoView to the mypanel,
but scroll is not working..
You have forgotten to add
scrollPane.setPreferredSizecall like this: