I have a class that extends JComponent called Canvas. Even when it's bigger than my JScrollPane's JViewport, the knobs will not appear on the scroll bars and I cannot scroll down on the Canvas. The code for my scroll pane is here:
final JFrame frame = new JFrame("SketchPad");
frame.setLayout(new BorderLayout());
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
JScrollPane scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
JViewport vp = scrollPane.getViewport();
vp.setLayout(null);
vp.setBackground(Color.BLUE);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
canvas = new Canvas();
canvas.setBounds(0, 0, w, h);
vp.add(canvas);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Am I doing something wrong with the JScrollPane, or is it something else?
vp.setLayout(null);
Don't set a null layout on the viewport. The scrollbars will appear automatically when the preferred size of the component added to the viewport of the scrollpane is greater than the size of the scrollpane.
The layout manager us used to determine the preferred size (as a general rule you should never use a null layout).
Also, don't use a Canvas, that is an AWT components. Use a JPanel when using Swing. Or, if it is a custom class then it should have a more descriptive name to avoid confusion.
Related
I have a problem with scroling JPanel,
I have a lot of labels and fields which are generated dynamicly, but my frame can't show it all.
My code:
JPanel showPanel = new JPanel();
JScrollPane scrollPane = new JScrollPane(showPanel);
add(scrollPane);
scrollPane.setBounds(0, 0, 400, 400);
scrollPane.setVisible(true);
scrollPane.add(Jbuttons);
And Im adding a lot of these buttons but my scrollPane won't show it.
I don't have any scrollBar, with text area I didn't have any problems.
Do you have any idea?
scrollPane.setBounds(0, 0, 400, 400); looks like you're using a null layout, don't this
scrollPane.add(Jbuttons); isn't how you should be adding content to the scroll pane, instead, add it to the showPanel which is already inside the JScrollPane. JScrollPane contains a single component, the JViewport, you can not "add" components to the JScrollPane, you must set the JViewports view to what you want shown and the manipulate this view
Take a look at How to Use Scroll Panes for more details
I want to set the size of the JTextPane according to the size of the panel so that when i add other panels, it changes accordingly. But it just gives a small text pane in the center and when i add some text, it's size changes accordingly.
JPanel panel = new JPanel();
JTextPane txt = new JTextPane();
JScrollPane pane = new JScrollPane();
pane.add(txt);
panel.add(pane,BorderLayout.CENTER);
add(pane);
now the jtextpane just appears at the center of the screen like a small box. I want it to appear according to the size of the panel
JPanel uses FlowLayout by default which sizes components according to their preferred sizes. You can use BorderLayout which will use the maximum area possible.
Also using constraints such as BorderLayout.CENTER has no effect unless the container is actually using BorderLayout. Dont add components to the JScrollPane. This will replaces all components within the view of the component. Instead set the JTextPane as the ViewPortView, for example
JPanel panel = new JPanel(new BorderLayout());
JTextPane txt = new JTextPane();
JScrollPane pane = new JScrollPane(txt);
// pane.add(txt); remove
panel.add(pane, BorderLayout.CENTER);
Read:
How to Use BorderLayout
How to Use Scroll Panes
You added pane twice. Add panel to your base (a JFrame?) instead and remember to actually set your JPanel to use BorderLayout.
I've created a JPanel and JScrollPane like so:
JPanel panel = new JPanel();
panel.setPreferredSize(5000, 5000);
JScrollPane scrollPane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPanel.getViewport().setBackground(Color.GRAY); //panel is white
I will be using event handlers to dynamically resize the panel. The problem is, sometimes the panel is smaller than the viewport. I'd set the panel's size like this:
panel.setPreferredSize(10, 10); //Just an example
but the panel will never be smaller than the viewport. I tried using
panel.setSize(10, 10);
and i saw a gray flicker (the viewport's background showing through), which indicated that the panel was being sized to what i wanted it to be, but then grew to the viewport's size. How can i stop this?
To control the sizing the view inside a JViewport, let it implement the Scrollable interface. Without, it's always forced to the size of the viewport.
So I am trying to have a JPanel inside a JScrollPane, which is located directly inside a JFrame, resize so that it is sometimes smaller than the JScrollPane. Problem is that when I try this it, the lesser size seems to be resized to the original size of the JScrollPane. It works fine when I leave it at a larger size. How do I get this to work? Code will be provided if needed.
Wrap you panel in another panel that uses a FlowLayout. This way the outer panel will be resized to fill the viewport of the scroll pane:
JPanel red = new JPanel();
red.setBackground(Color.RED);
red.setPreferredSize( new Dimension(200, 200) );
JPanel outer = new JPanel();
outer.add( red );
add( new JScrollPane(outer) );
I am failing to display a JComponent inside a JPanel on a JFrame.
The following does not work.
JComponent component = ...
panel.add(component, BorderLayout.CENTER);
frame.add(panel, BorderLayout.CENTER);
But if I add the JComponent to the JFrame[like frame.add(component, BorderLayout.CENTER);], it displays the contents.
Any ideas
A JPanel's default layout is a FlowLayout so you don't have to specify the center of the panel.
Simply do:
panel.add(component);
Alternately, do:
panel.setLayout(new BorderLayout());
panel.add(component, BorderLayout.CENTER);
By default a JComponent doesn't have a preferred size.
By default a JPanel uses a FlowLayout. When you add a component to the panel it will respect the preferred size of the component, which is 0, so you don't see it.
By default the panel which is used as the content pane of a JFrame uses a BorderLayout. So when you add a component to the content pane of the frame the component is automatically resized to fill the space available to the frame,
The solution is to give your component a preferred size, then it can be used on any panel with any layout manager.