-2

i have a JFrame, JPanel and a JButton, the button is in the panel at coords (100, 1000), the panel (with absolute layout) is in the frame and the frame has dimensions (500, 500). what i want is to be able to scroll the window in order to reach the button that is under the window bottom. here is the code:

public static void main(String...args)
{
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    JButton button = new JButton("Test");

    frame.setSize(500, 500);
    frame.getContentPane().add(panel);

    panel.setLayout(null);

    button.setBounds(100, 1000, 100, 30);
    panel.add(button);
}

how can i do to be able to scroll the window to reach the button? could you write a simple code with this button?

  • Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson May 01 '16 at 23:28
  • *"could you write a simple code"* I sure could. But SO is not your personal code generation service. – Andrew Thompson May 01 '16 at 23:31

2 Answers2

1

You can simply use JScrollPane for it. P.S: I don't why you set 1000 for Y Position of button even if your frame size 500

JScrollPane jYourScrollPane = new JScrollPane(panel);
frame.add(jYourScrollPane);

Edit: setLayout(null); makes your panel layout to absolute. check these answers answer1 answer2

Community
  • 1
  • 1
ziLk
  • 3,120
  • 21
  • 45
  • it doesn't work, it appear a window with no scrollPane JFrame wnd = new JFrame(); JPanel panel = new JPanel(); JButton button = new JButton("Test"); JScrollPane scroll = new JScrollPane(panel); button.setBounds(100, 1000, 70, 30); panel.setLayout(null); panel.add(button); wnd.setSize(600, 500); wnd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); wnd.add(scroll); wnd.setVisible(true); – Tommaso Salvetti May 01 '16 at 19:28
  • Edited my answer for you @TommasoSalvetti – ziLk May 01 '16 at 20:10
0
this.revalidate();  //for swing.

Should update your frame and get your buttons and such to show.

DarkV1
  • 1,776
  • 12
  • 17