Results 1 to 11 of 11
- 10-29-2009, 02:54 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 7
- Rep Power
- 0
-
Have you gone through the Sun Swing tutorial on use of scrollbars? Without seeing your code (please use code-tags -- see my signature below), we have no idea what you're doing wrong.
best of luckLast edited by Fubarable; 10-29-2009 at 03:00 PM.
- 10-29-2009, 02:57 PM #3
Post a code and ask for the solution.
Ramya:cool:
- 10-29-2009, 03:16 PM #4
Member
- Join Date
- Oct 2009
- Posts
- 7
- Rep Power
- 0
Java Code:JPanel jpanel = new DrawPane(); JScrollPane scrollPane = new JScrollPane(jpanel); add(scrollPane); //************************** class DrawPane extends JPanel { public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; double leftX = 100.; double topY = 100.; double width = 200.; double height = 150.; Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height); g2.draw(rect); } }
- 10-29-2009, 03:31 PM #5
Member
- Join Date
- Oct 2009
- Posts
- 7
- Rep Power
- 0
I have looked at Sun Swing tutorial, but it seems that all I do is right.
-
I'm not seeing the problem in the code you've posted, so likely the problem is elsewhere (unless I'm missing it, which is not uncommon). I suggest you have a look at this link on SSCCE, and then create and post an SSCCE. This will allow us to work with your problem directly and come up with a solution more quickly.
Much luck!
- 10-29-2009, 03:57 PM #7
Member
- Join Date
- Oct 2009
- Posts
- 7
- Rep Power
- 0
Full code of application below:
Java Code:import java.awt.*; import java.awt.geom.Rectangle2D; import javax.swing.*; public class Main { public static void main(String[] args) { JFrame frame = new JFrame(); JPanel jpanel = new DrawPane(); JScrollPane scrollPane = new JScrollPane(jpanel); frame.add(scrollPane); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(true); frame.setLocationByPlatform(true); frame.setSize(200,200); frame.setVisible(true); } } class DrawPane extends JPanel { @Override public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; double leftX = 100.; double topY = 100.; double width = 200.; double height = 150.; Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height); g2.draw(rect); } }
-
See, SSCCEs work!
Don't forget to set the preferredSize of your drawing panel (and everything else):
Java Code:public static void main(String[] args) { JFrame frame = new JFrame(); JPanel jpanel = new DrawPane(); jpanel.setPreferredSize(new Dimension(400, 400)); //!! added JScrollPane scrollPane = new JScrollPane(jpanel); frame.add(scrollPane); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(true); frame.setPreferredSize(new Dimension(200, 200)); //!! added frame.pack(); //!! added frame.setLocationByPlatform(true); //frame.setSize(200,200); //!! removed frame.setVisible(true); }
- 10-30-2009, 06:57 AM #9
Member
- Join Date
- Oct 2009
- Posts
- 7
- Rep Power
- 0
Thank you very much!!!
It works!
- 11-18-2009, 03:02 AM #10
Member
- Join Date
- Aug 2008
- Posts
- 2
- Rep Power
- 0
Hey,I am have the same problem,My code draw a line with a ladge leng on a jpanel,Then I add this jpanel to a frame which smaller leng.I can scroll,but the picture of line has some problem.can You help me?
this is my main code:
Java Code:/* * class vi du tao scroll cho ung dung ve mui ten. Khi panel chua mui ten thay doi ve be rong thi scroll se duoc tao ra de co the keo de xem * mui ten * */ package vd; import java.awt.Color; import javax.swing.*; import java.awt.*; import java.awt.event.*; class Ve extends JPanel{ // jpanel se duoc add vao scrollpane int w,h; public Ve(){} public Ve(int w,int h){ this.w = w; this.h = h; setSize(w,h); /* * In Swing a scrollpane asks its viewport child (the view, here, obj of ve) for its preferred size as it gathers information to layout its viewports view. If you want your component to be shown at a size other than the scrollpanes viewport view size you must provide the size information to the scrollpane. You can do this by calling setPreferredSize on the component or by overriding getPreferredSize in the component and returning the desired size */ //setPreferredSize(new Dimension(w,h)); } public void paintComponent(Graphics g) { Graphics2D g1 = (Graphics2D) g; g1.setColor(Color.red); // draw a line g1.drawLine(getWidth()-20,50); } } public class Main { public JFrame fr; public JScrollPane scr; public Ve ve; public Main() { int wframe = 600; int hframe = 400; ve = new Ve(); //wframe*2,hframe); //*** dieu chinh wpanel se thay duoc su thay doi cua scroll ve.setPreferredSize(new Dimension(wframe*2,hframe)); fr = new JFrame("Thu nghiem"); // tao scroll de add jpanel vao scr = new JScrollPane(ve);//,JScrollPane.VERTICAL_SCROLLBAR_NEVER,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scr.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER); //Container contentPane = fr.getContentPane(); //contentPane.add(scr); fr.add(scr); fr.setDefaultCloseOperation(fr.EXIT_ON_CLOSE); fr.setLocation(100, 100); //fr.setSize(wframe,hframe); fr.setPreferredSize(new Dimension(wframe,hframe)); fr.pack(); fr.setLocationByPlatform(true); fr.setVisible(true); } public static void main(String[] args) { Main v = new Main(); } }
thank you very much
- 11-18-2009, 03:26 AM #11
Member
- Join Date
- Aug 2008
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
need help with JPanel Images
By Thebigchalupa in forum New To JavaReplies: 5Last Post: 04-23-2009, 07:15 PM -
Images in JPanel
By Thebigchalupa in forum Advanced JavaReplies: 1Last Post: 04-21-2009, 05:11 PM -
Resizing Images in JPanel??
By NoNickName in forum New To JavaReplies: 1Last Post: 04-09-2009, 10:19 PM -
Not another Jpanel Scrollbar problem!
By jiexx in forum AWT / SwingReplies: 3Last Post: 03-18-2009, 02:09 AM -
Moving Images in JPanel
By killpoppop in forum AWT / SwingReplies: 7Last Post: 03-08-2009, 02:54 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks