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
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:
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
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With