Hi i am trying to make a desktop application in swing here i am using jscrollpane. i want to add multipul button in jscrollpane. i am able to add only single button there how can i do this
My code is given below
public class AddingToJScrollPane {
public static void main(String args[]) {
JFrame frame = new JFrame("Tabbed Pane Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Label");
label.setPreferredSize(new Dimension(1000, 1000));
JScrollPane jScrollPane = new JScrollPane(label);
JButton jButton1 = new JButton("Hello");
JButton jButton2 = new JButton("Hello");
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane.setViewportBorder(new LineBorder(Color.RED));
jScrollPane.getViewport().add(jButton1,jButton2);
frame.add(jScrollPane, BorderLayout.NORTH);
frame.setSize(400, 150);
frame.setVisible(true);
}
}
Updated code
public class AddingToJScrollPane {
public static void main(String args[]) {
JFrame frame = new JFrame("Tabbed Pane Sample");
JPanel panel = new JPanel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setLayout( new GridLayout() );
JLabel label = new JLabel("Label");
label.setPreferredSize(new Dimension(1000, 1000));
JScrollPane jScrollPane = new JScrollPane(panel);
JButton jButton1 = new JButton("Hello");
JButton jButton2 = new JButton("He");
// jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane.setViewportBorder(new LineBorder(Color.RED));
// jScrollPane.getViewport().add(panel);
frame.add(jScrollPane, BorderLayout.NORTH);
panel.add(jButton1);
panel.add(jButton2);
frame.setSize(400, 150);
frame.setVisible(true);
}
}
How can i achieve my desired output Thanks in advance