3

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

  • _"How can i achieve my desired output "_ What exactly _is_ your desired output? – Paul Samsotha Mar 08 '14 at 11:55
  • Edit your post to show what you tried. _Don't_ remove the current code. Just write **EDIT** below what you have, then post your new code below it. – Paul Samsotha Mar 08 '14 at 12:20
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/49301/discussion-between-user3377703-and-peeskillet) –  Mar 08 '14 at 12:42
  • *"How can i achieve my desired output"* Copy/paste the part of your question where you ***describe*** the desired output. – Andrew Thompson Mar 09 '14 at 00:46

1 Answers1

8

Add the buttons to a panel with suitable layout (e.g. GridLayout or FlowLayout). Add the panel to the scroll pane.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • i did this and able to get multiple button but they are not coming in appropriate manner –  Mar 08 '14 at 12:05
  • 2
    1) I'm not your 'dear'. 2) The stated problem is 'get more than one button into a scroll pane', so it **is** solved. 3) If you expect me to be able to solve the layout problem not knowing what layout was used, or what the intended affect is, you must think I'm some sort of magical wizard. I'm not. – Andrew Thompson Mar 08 '14 at 12:22
  • for natural scrolling to change intereg value in setUnitIncrement for derived JScrollBars from JScrollPane – mKorbel Mar 08 '14 at 12:47