4
votes

So, I'm making a kind of text editor, and I need a JScrollPane for vertical navigation. But I can't get it to work.

I have read every freaking tutorial on first ten pages of google results, and I can't get it to work.

Lets say I have JFrame (size 1000x800). I want to put a JPanel (1000x2000) in it so that it horizontally alignes with the JFrame. I want to stick a simple scroll bar to the right side of the JPanel so I can get to the rest of it.

I have reduced sizes, I have added JPanel to JScrollBar and vice versa, added one of them to JFrame, both, none, but nothing.

So, at this point, I wouldn't mind a couple of lines of finished code...

EDIT: Fine, here's the code...

mWindow = new JFrame(lang.getString("title"));
mWindow.setSize(1000, 800);
mWindow.setLocationRelativeTo(null);
mWindow.setResizable(false);
mWindow.setLayout(null);
mWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

mWindow.setVisible(true);

workspace = new JPanel();
workspace.setBounds(0,0, 1000, 1203);
workspace.setBackground(Color.RED);

scroll = new JScrollPane(workspace, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setBounds(0, 20, 600, 600);
//scroll.setLayout(null);
mWindow.getContentPane().add(scroll);
mWindow.repaint();
mWindow.validate();

That shows a part of JPanel (600X600, (JScrollPane size)), and shows scrollbars, but isn't scrollable

4
We can't help much if we don't know what you've got so far. - FThompson
@Vulcan Well, nothing really... It's just this most basic thing... Whatever I do, neither of the components ever show up... I think it's far more easier to think up the code (there can't be more than 10 lines), than going trough my gibberish and seeking bugs... - Karlovsky120
One approach is to set your JScrollPane's size as that of your frame's content pane, and then set its viewport as your JPanel, which has a larger size. - FThompson
DO NOT USE NULL LAYOUT. Instead, read the chapter on LayoutManagers (in the tutorial referenced in the swing tag) and try to understand the interaction between layout hints (like prefSize) and layoutManager. Then read the JScrollPane api doc which points you to another chapter of the tutorial plus explaining how what governs its own sizing plus ... - kleopatra

4 Answers

6
votes

So, I did this really quick test and it works fine for me...

public class TestPane extends JPanel {

    public TestPane() {

        setBorder(new LineBorder(Color.RED));
        // This is for demonstration purposes only
        // One should always rely on the layout manager
        // to define this value
        // Thank kleopatra 
        setPreferredSize(new Dimension(1000, 1000));

    }

    @Override
    protected void paintComponent(Graphics g) {

        super.paintComponent(g);

        FontMetrics fm = g.getFontMetrics();

        Dimension size = getPreferredSize();
        String text = "Pref: " + size.width + "x" + size.height;
        g.drawString(text, 0, fm.getAscent());

        size = getSize();
        text = "Size: " + size.width + "x" + size.height;
        g.drawString(text, 0, fm.getHeight() + fm.getAscent());

    }

}

And the test frame

public class TestFrame {

    public static void main(String args[]) {

        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());

        JScrollPane scroll = new JScrollPane(new TestPane());

        frame.add(scroll);

        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }

}

Which produces this:

Scroll away

On a side note, I don't know why people insist on using null layouts, they just cause more trouble and heart ache then they're worth. Take the time to find some simple layout managers. I hate VB for a lot of reasons, but layout management is at the top of my list, IMHO

3
votes

Try by applying

setPreferredSize(new Dimension());

method on your panel, instead of setSize() method.

Like this:

import java.awt.Dimension;

import javax.swing.*;
public class Example extends JFrame{

public static void main(String[] args) {
    Example ex = new Example();
    JPanel panel = new JPanel();
    JScrollPane sc = new JScrollPane(panel);
    panel.setPreferredSize(new Dimension(800,600));
    ex.getContentPane().add(sc);
    ex.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ex.pack();
    ex.setVisible(true);
}

}
1
votes

I would do:

  1. A border layout so the scroll pane fits in all the room of the frane's content pane;
  2. A pack at the end for layouting;
  3. setVisible last;
  4. setPreferredSize if nothing helps.

    mWindow = new JFrame(lang.getString("title"));
    mWindow.setSize(1000, 800);
    mWindow.setLocationRelativeTo(null);
    mWindow.setResizable(false);
    mWindow.setLayout(new BorderLayout());
    mWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    workspace = new JPanel();
    workspace.setBounds(0,0, 1000, 1203);
    workspace.setPreferredSize(new Dimension(1000,1203));
    
    workspace.setBackground(Color.RED);
    
    scroll = new JScrollPane(workspace, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    scroll.setBounds(0, 20, 600, 600);
    mWindow.getContentPane().add(scroll, BorderLayout.CENTER);
    mWindow.pack();
    mWindow.setVisible(true);
    
0
votes

why don't you try something like this and call it from your class that creates your frame.

public JPanel createLPanelWithScroller() {

      Dimension outter_dem = new Dimension(600, 600); //width,height
      Dimension inner_panel_dem = new Dimension(1000, 1203);  
      Dimension scroll_dem = new Dimension(600, 600); 

      JPanel outter_panel = new JPanel(new FlowLayout());
 outter_panel.setPreferredSize(outter_panel_dem);       

 JPanel inner_panel = new JPanel(new FlowLayout());
 inner_panel.setPreferredSize(inner_panel_dem);

 JScrollPane scroller = new JScrollPane(list_panel); 
 scroller.setHorizontalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
      scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
 scroller.setPreferredSize(scroll_dem);

 outer_panel.add(list_scroller);

 return outer_panel;
}