-
ScrollBar
hi
I really need your help :(
I have a JFrame : fr
and a JPanle : this
fr size is 600*800 and is empty. no background , no object , ...
the Jpanel size is for example 1400*1400 ( bigger than jframe)
so I want to add scroll !
I use this code but it dosent help me :(
Code:
this.setSize(1400, 1400);
this.setLocation(50, 50);
this.setVisible(true);
JScrollPane jsp = new JScrollPane(this);
jsp.setPreferredSize(new Dimension(1300, 1300));
jsp.setAutoscrolls(true);
jsp.setVisible(true);
jsp.setLayout(null);
jsp.setLocation(40, 40);
jsp.setSize(1400, 1400);
jsp.add(this);
fr.add(jsp);
fr.repaint();
I really need scroll
please help me
Thanks in advance :)
-
Re: ScrollBar
Don't set the JScrollPane's layout, and for gosh's sake don't set it to null!
Also, don't add the JPanel to the JScrollPane itself (which replaces the JScrollPane's viewport -- and you need that) but rather to its viewport via the JScrollPane method setViewPortView(myJPanel). Either that or pass the JPanel into the JScrollPane in its constructor which does the same thing.
Also, don't set sizes of anything. If you must, set the JScrollPane's preferredSize and the JPanel that is added to it. Be sure to call pack() on your JFrame.
-
Re: ScrollBar
-
Re: ScrollBar
sorry for posting in a wrong place db .
Thank you fubarable
I use some of your advices but they don't help me
could you please run this code and check it
Code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class Tst {
public static void main(String[] args) {
MyFrame2 mainFrame = new MyFrame2();
}
}
class MyFrame2 extends JFrame {
private static final long serialVersionUID = 1L;
public int WIDTH = 600;
public int HEIGHT = 800;
public MyLines2 ml;
MyFrame2() {
this.setVisible(true);
this.setSize(this.HEIGHT, this.WIDTH);
this.setBackground(Color.gray);
this.setTitle("Graph");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.setResizable(false);
ml = new MyLines2(this);
}
}
class MyLines2 extends JPanel {
private static final long serialVersionUID = 1L;
private MyFrame2 fr;
private int WIDTH, HEIGHT;
MyLines2(MyFrame2 fr) {
this.fr = fr;
this.setLayout(null);
WIDTH = 1800;
HEIGHT = fr.HEIGHT - 200;
this.setSize(HEIGHT, WIDTH);
this.setLocation(50, 50);
this.setVisible(true);
////////////// START JSP ////////////////////
JScrollPane jsp = new JScrollPane(this,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jsp.setPreferredSize(new Dimension(600, 400));
jsp.setViewportView(this);
fr.pack();
fr.add(jsp);
//////////END JSP ///////////////////////
fr.repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (Graph.x == 0)
g.drawLine(0, 0, 0, WIDTH);
for (int i = 0; i < 3; i++)
g.drawLine(30 + i, 0, 30 + i, WIDTH);
for (int i = 0; i < 50; i++)
g.drawLine(30 + 30 * i, 0, 33 + 30 * i, WIDTH);
for (int i = 0; i < 32; i++)
g.drawString("R" + i, 5, 30 * i + 15);
}
}
I marked the Scroll part with comment. when I don't use scroll and write just " fr.add(this) " everything id OK but my panel is larger than usual size so i wanted to use scroll but I can't:(
I'm really sorry for putting my code here and asking you to check it, but I really need it. whole my program depends on this graphical part
-
Re: ScrollBar
- you only need to add your component to the JScrollPane once, not twice. So add it in the constructor or via setViewportView, but not both.
- Add all components to the JFrame before calling pack().
- You may need to set your JScrollPane's preferredSize
- Consider overriding MyLines2 getPreferredSize() method to return what size you want it to be.
-
Re: ScrollBar
-
Re: ScrollBar