Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a absolute layout inside a JScrollPane

I need to use a JScrollPane with absolute layoute. I know that the use of setLayout(null) is not recommendable at all. I've been reading that if you want to use the absolute layout with a JScrollPane it is necessary to set the preferred size property of the elements inside in order to JScrollPane can calculate its size.

I've been trying the next code changing the order and sizes of elemnts but I can't work out where I've been wrong.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;


public class frame extends JFrame {

private JPanel contentPane;
private JScrollPane scrollPane;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                frame frame = new frame();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public frame() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setLayout(null);
    setContentPane(contentPane);

    JPanel panel = new JPanel();
    panel.setBackground(Color.red);
    panel.setBounds(0,0,600,600);
    panel.setPreferredSize(new Dimension(420,280));

    scrollPane = new JScrollPane(panel);
    scrollPane.setBounds(0, 0, 600, 600);
    scrollPane.setLayout(null);
    scrollPane.setBackground(Color.green);

    scrollPane.add(panel);
    scrollPane.setPreferredSize(new Dimension(450,300));
    contentPane.add(scrollPane);
}
}

Why setPreferredSize is not doing the right work? Thanks in advance.

like image 473
Jonás Avatar asked Aug 16 '12 21:08

Jonás


People also ask

When might you need to use the JScrollPane control?

A JScrollPane provides a scrollable view of a component. When screen real estate is limited, use a scroll pane to display a component that is large or one whose size can change dynamically. Other containers used to save screen space include split panes and tabbed panes.

How do I make JScrollPane transparent?

You need to use setOpaque(false) to make it transparent. Call that both on the JScrollPane, and on it's ViewPort. sp. setOpaque(false); sp.

What is the difference between JScrollPane and scrollbar?

What are the differences between a JScrollBar and a JScrollPane in Java? A JScrollBar is a component and it doesn't handle its own events whereas a JScrollPane is a Container and it handles its own events and performs its own scrolling.


2 Answers

First of all, don't change the layout manager of the scroll pane, it's not required for what you want to achieve

Second, scroll pane works though a JViewport. You have to add your container to it instead (scrollPane.add(panel) is the wrong thing to do), instead use scrollPane.setViewportView(panel)

Thirdly, you want to take a look at the Scrollable interface as scroll pane doesn't just rely on the preferred size of its contents

like image 52
MadProgrammer Avatar answered Sep 29 '22 06:09

MadProgrammer


Changing the layout manager of the actual JScrollPane does not make sense; if you wish to use absolute layout within your application you would typically call setLayout(null) on a container class (such as JPanel) in order to position child components within it using absolute positioning.

I suggest that you should try:

  • Call setLayout(null) on the JPanel contained within the JScrollPane (as I think this is what you're trying to do).
  • Rather than use JPanel directly, subclass it and have your subclass implement the Scrollable interface, which provides additional information to the JScrollPane regarding the optimal scrollable viewport size.
like image 21
Adamski Avatar answered Sep 29 '22 05:09

Adamski