Results 1 to 11 of 11
- 03-21-2012, 11:57 AM #1
create scroll bars inside JScrollPane
my (J)Panel consists of the (J)ScrollPane
my (J)ScrollPane consists of a (J)Panel
the codings ran, and the (J)Panel was inside the (J)ScrollPane which was inside thje (J)Frame,Java Code:public classname extends JPanel { public void paintComponent(Graphics g) { //my panel with a graph which really really needs a scroll bar to view the full graph } public static void main(String[] args) { JScrollPane scrollpane=new JScrollPane(); scrollpane.setViewPortView(classname); //my panel is inside the scrollpane JFrame frame = new JFrame("title"); frame.add(scrollpane); //my scrollpane is inside the frame frame.setVisible(true); } }
and there were no scrollbars. hence it showed only a beginning part of the image, and the rest was hidden to the right side of the monitor.
How do we add scrollbars?
thx
dhilip
-
Re: create scroll bars inside JScrollPane
The scrollbars will automatically appear if the component held by the view port is larger than the viewport. For more specific help, you'll need to post some code.
- 03-21-2012, 01:43 PM #3
Re: create scroll bars inside JScrollPane
this is my code,Java Code:import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.event.MouseWheelListener; import java.awt.font.FontRenderContext; import java.awt.font.LineMetrics; import java.awt.geom.Line2D; import java.io.*; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollBar; import javax.swing.JScrollPane; public class ReadPlat extends JPanel { /** * */ private static final long serialVersionUID = 4157203125948260492L; final int PAD = 20; double[] xArray ; double[] yArray ; int Size; ////////////////////////////////////////////////////////////////////////////////////////// public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); int w = getWidth(); int h = getHeight(); // Draw ordinate. g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD)); // Draw abscissa. g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD)); // Draw labels. Font font = g2.getFont(); FontRenderContext frc = g2.getFontRenderContext(); LineMetrics lm = font.getLineMetrics("0", frc); float sh = lm.getAscent() + lm.getDescent(); // Ordinate label. String s = "amplitude"; float sy = PAD + ((h - 2*PAD) - s.length()*sh)/2 + lm.getAscent(); for(int i = 0; i < s.length(); i++) { String letter = String.valueOf(s.charAt(i)); float sw = (float)font.getStringBounds(letter, frc).getWidth(); float sx = (PAD - sw)/2; g2.drawString(letter, sx, sy); sy += sh; } s = "time"; sy = h - PAD + (PAD - sh)/2 + lm.getAscent(); float sw = (float)font.getStringBounds(s, frc).getWidth(); float sx = (w - sw)/2; g2.drawString(s, sx, sy); double xInc = (double)(w - 2*PAD)/(this.yArray.length-1); double scale = (double)(h - 2*PAD)/getMax(); for(int i=PAD;i<w-PAD;i=i+50) { g2.drawString(/*Integer.toString*/Double.toString(/*i-20*/this.xArray[i]), i, h-PAD+10); } g2.setPaint(Color.green.darker()); for(int i = 0; i < this.yArray.length-1; i++) { double x1 = PAD + i/**xInc*/; double y1 = h - PAD - scale*((this.yArray[i])); double x2 = PAD + (i+1)/**xInc*/; double y2 = h - PAD - scale*this.yArray[i+1]; g2.draw(new Line2D.Double(x1, 2*y1, x2, 2*y2)); } // Mark data points. g2.setPaint(Color.red); } ////////////////////////////////////////////////////////////////////////////////////////// private double getMax() { double max = -Integer.MAX_VALUE; for(int i = 0; i < this.yArray.length; i++) { if(this.yArray[i] > max) max = this.yArray[i]; } return max; } public ReadPlat(int size) { this.Size=size; xArray= new double[this.Size]; yArray= new double[this.Size]; } public void setData(int i,double x,double y) { if(i>this.Size) return; xArray[i]=x; yArray[i]=y; } public static void main(String[] args) throws Exception { ReadPlat ReadPlatInstance; String filename; FileReader file_in; BufferedReader in; String input_line; String[]input_split; int TotalLineCount=0; filename = args[0]; file_in = new FileReader(filename); in = new BufferedReader(file_in); input_line ="Starting"; //Reading total lines while((input_line=in.readLine())!=null) { //input_line =in.readLine(); TotalLineCount++; } System.out.println(TotalLineCount); ReadPlatInstance = new ReadPlat(TotalLineCount); //Closing the File and reopening TotalLineCount=0; input_line ="ReStarting"; in.close(); file_in.close(); file_in = new FileReader(filename); in = new BufferedReader(file_in); while((input_line=in.readLine())!=null) { System.out.println("---------------"); System.out.println("Line No:"+TotalLineCount); input_line=input_line.replaceAll("\"",""); input_split = input_line.split(" "); System.out.println("Input Line:"+input_line); System.out.print(input_split.length); System.out.print("x"+input_split[0]); System.out.print("y"+input_split[2]); ReadPlatInstance.setData(TotalLineCount, Double.valueOf(input_split[0].trim()).doubleValue(),Double.valueOf(input_split[2].trim()).doubleValue() ) ; System.out.println("\n"+"instant x "+ReadPlatInstance.xArray[TotalLineCount] + "\t instant y" + ReadPlatInstance.yArray[TotalLineCount]); TotalLineCount++; } JFrame f=new JFrame("ReadPlatInstance");////// JScrollPane spp=new JScrollPane(); spp.setViewportView(ReadPlatInstance); spp.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(spp); f.pack(); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); in.close(); file_in.close(); } }
this is my input file added to the argument for my class (ReadPlat) input.txt
the original file has 63375 lines, but i couldn't upload it in this forum, hence, I've deleted so many lines and this file has only 849 lines, and scrollbar doesn't appear yet.
thx
dhilipLast edited by noobplus; 03-21-2012 at 02:12 PM.
-
Re: create scroll bars inside JScrollPane
You need to give your ReadPlat class an override for the getPreferredSize method. Then you also need to set the preferredSize of your JScrollPane's viewport (making sure that the two heights match. Do not set the size of the JFrame but rather let pack() take care of its size.
Also the logic in your paintComponent is wrong as you change how many data points are displayed depending on the size of the JPanel. Don't do that.
- 03-22-2012, 01:50 PM #5
Re: create scroll bars inside JScrollPane
thx, I've added the line
(after initializing the JFrame),Java Code:ReadPlatInstance.setPreferredSize(new Dimension(TotalLineCount,718));//TotalLineCount is the size of the array of y-axis values
and yay the scroll bar has appeared!!! and the graph was alright.Java Code:spp.setPreferredSize(new Dimension(1357,718));

but when I scroll the scrollbar, the incoming graph gets drawn below, and appears as kind of the lines are overlapped,

that's what I can't get in mind.. I've removed "xInc" so it got dependency-free(depending on the size of the panel), and got that error while scrolling
regards
dhilipLast edited by noobplus; 03-22-2012 at 02:41 PM.
- 03-22-2012, 06:32 PM #6
Re: create scroll bars inside JScrollPane
I'll go with this question..
where do we create the actionListener and which listener? because I need the scrollPane get refreshed or repainted every scrolling action
regards
dhilip
- 03-26-2012, 06:31 PM #7
Re: create scroll bars inside JScrollPane
hi again,
is it possible to get rid of the error by using repaint() ? I tried that in vain anyway, but
is there any other way?
or listener is needed?
thanks
dhilip
- 03-31-2012, 01:24 PM #8
Re: create scroll bars inside JScrollPane
Why do they call it rush hour when nothing moves? - Robin Williams
- 03-31-2012, 03:44 PM #9
Re: create scroll bars inside JScrollPane
-
- 03-31-2012, 04:35 PM #11
Re: create scroll bars inside JScrollPane
Similar Threads
-
Shell with scroll bars
By M54 in forum SWT / JFaceReplies: 0Last Post: 10-27-2011, 11:17 AM -
How can i make this JScrollPane scroll horizontally?
By ozzyman in forum AWT / SwingReplies: 1Last Post: 04-05-2011, 12:12 PM -
Moving Scroll in JScrollPane
By ayesh85 in forum AWT / SwingReplies: 0Last Post: 03-02-2011, 08:11 AM -
Help with Scroll Bars
By Abion47 in forum AWT / SwingReplies: 5Last Post: 03-02-2011, 04:44 AM -
jscrollpane scroll issues
By kumar_gemi in forum AWT / SwingReplies: 11Last Post: 09-30-2009, 08:33 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote
-.gif)

Bookmarks