Results 1 to 7 of 7
- 05-17-2012, 10:55 PM #1
Member
- Join Date
- May 2012
- Posts
- 3
- Rep Power
- 0
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 :(
I really need scrollJava 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();
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.
- 05-18-2012, 05:23 AM #3
Re: ScrollBar
Moved from Java 2D
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-18-2012, 12:00 PM #4
Member
- Join Date
- May 2012
- Posts
- 3
- Rep Power
- 0
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
Java 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.
- 05-18-2012, 08:33 PM #6
Member
- Join Date
- May 2012
- Posts
- 3
- Rep Power
- 0
Re: ScrollBar
Solved
Thank you
-
Similar Threads
-
Problem with the Scrollbar
By mike28 in forum Java AppletsReplies: 0Last Post: 10-30-2011, 09:59 PM -
How to update the scrollbar?
By Martino in forum Java 2DReplies: 3Last Post: 06-08-2011, 06:09 PM -
Custom scrollbar possible?
By FatWhale in forum AWT / SwingReplies: 2Last Post: 03-19-2011, 02:37 PM -
ScrollBar in a JList
By lupo in forum AWT / SwingReplies: 2Last Post: 05-20-2010, 10:42 PM -
Using java.awt.Scrollbar
By Java Tip in forum Java TipReplies: 0Last Post: 01-03-2008, 09:19 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks