Graphical Representation of Text Input - Multiple JPanels in a JScrollPane


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Graphical Representation of Text Input - Multiple JPanels in a JScrollPane

  1. #1
    Join Date
    Aug 2006
    Posts
    2

    Exclamation Graphical Representation of Text Input - Multiple JPanels in a JScrollPane


    Have put in a lot of hours trying to get this work including a lot of searching for the solution on the Internet but have been unable to find a solution....today was my deadline..... oh well!

    I have a Java program that generates graphics on a JPanel for every line of input data from a text file. I am running into two problems. 1) The graphics are not fully displayed because they overlay each other with this BoxLayout that I am using.... 2) I cannot get the ScrollBar on the JScrollPane to work - it shows up, but the slider thingy doesn't appear. Wish I could show you a picture of this somehow. (SEE ATTACHMENT)

    Once again the intention is to produce one JPanel (or whatever other class I would need) of graphics for every data input record from a text file. This is so they can just grab the scroll bar and review each one of the graphical representations from the first to the last. Therefore, I should be able to make this Frame, or ScrollPane or whatever as big as is needed in order to display all these graphics. And the trick is to be able to append one JPanel after another and then resize accordingly. This isn't happening with BoxLayout because it only gives me one screen size full of scrunched up JPanels. Also, I have no idea why my scrollbar does not show up correctly.....

    Code: Although this isn't the exact code...(that would be too lengthy) it will gave you an idea of what I am trying to do.... I have everything in basically two classes... (time constraints)

    Here is the whole first class since it is rather short.......and shows
    what I am trying to do. Please ignore class names. They are meaningless.


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.util.StringTokenizer;


    public class ScrollBarExampleCustom {

    JLabel label;
    JPanel mainPanel, subPanel1, subPanel2, subPanel3;
    String token1, token2, token3, token4, token5;
    JScrollPane scrollableGraphics;

    static int H = 0;
    static int W = 600;


    public ScrollBarExampleCustom() {

    mainPanel = new JPanel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
    mainPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

    File inputFile = new File("C:\\INPUT\\InputData.txt");

    try {
    FileReader in = new FileReader(inputFile);
    BufferedReader buf_reader = new BufferedReader (in);
    do {

    String line = buf_reader.readLine();
    if (line == null) break;

    StringTokenizer st = new StringTokenizer(line);
    token1 = st.nextToken();
    token2 = st.nextToken();
    token3 = st.nextToken();
    token4 = st.nextToken();
    token5 = st.nextToken();

    subPanel1 = new Java2DExampleCustom(token1, token2, token3);

    mainPanel.add(Box.createRigidArea(new Dimension(0,5)));
    mainPanel.add(subPanel1);

    H += 500;

    } while (true);
    buf_reader.close();
    } catch (IOException e) {
    System.out.println ("IO exception = " + e );
    }

    scrollableGraphics = new JScrollPane(mainPanel);
    scrollableGraphics.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

    }


    public static void main(String s[]) {
    JFrame frame = new JFrame("Graphics Generator");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ScrollBarExampleCustom sbecust = new ScrollBarExampleCustom();
    sbecust.mainPanel.setOpaque(true); // content panes must be opaque
    frame.setContentPane(sbecust.scrollableGraphics);
    frame.setSize(W,H);
    frame.setVisible(true);
    }
    }


    This is the other class that is used Perhaps this could just be considered an instantiation of a JPanel?

    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.util.StringTokenizer;

    public class Java2DExampleCustom extends JPanel implements Scrollable {

    String shape;
    BasicStroke stroke = new BasicStroke(1.0f);
    BasicStroke wideStroke = new BasicStroke(4.0f);
    String s1,s2,s3,s4,s5;
    String c1,c2,c3,c4,c5;
    String p1,p2,p3,p4,p5;
    private int maxUnitIncrement = 1;

    /**
    * Our Java2DExample constructor sets the frame's size, adds the
    * visual components, and then makes them visible to the user.
    * It uses an adapter class to deal with the user closing
    * the frame.
    **/
    public Java2DExampleCustom(String token1, String token2, String token3) {

    ETC, ETC , ETC


    public boolean getScrollableTracksViewportWidth() {
    return false;
    }

    public boolean getScrollableTracksViewportHeight() {
    return false;
    }

    public Dimension getPreferredScrollableViewportSize() {
    return getPreferredSize();
    }

    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {

    int currentPosition = 0;
    if (orientation == SwingConstants.HORIZONTAL)
    currentPosition = visibleRect.x;
    else
    currentPosition = visibleRect.y;

    if (direction < 0) {
    int newPosition = currentPosition - (currentPosition / maxUnitIncrement) * maxUnitIncrement;
    return (newPosition == 0) ? maxUnitIncrement : newPosition;
    } else {
    return ((currentPosition / maxUnitIncrement) + 1) * maxUnitIncrement - currentPosition;
    }
    }

    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
    if (orientation == SwingConstants.HORIZONTAL)
    return visibleRect.width - maxUnitIncrement;
    else
    return visibleRect.height - maxUnitIncrement;
    }



    /**
    * The paint method provides the real magic. Here we
    * cast the Graphics object to Graphics2D to illustrate
    * that we may use the same old graphics capabilities with
    * Graphics2D that we are used to using with Graphics.
    **/
    public void paint(Graphics g) {

    Graphics2D g2d = (Graphics2D)g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setColor(Color.black);

    ETC, ETC, ETC

    Any help would be appreciated!
    Attached Images Attached Images

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Your code does not show how you set up the JScrollPane. You must add the JPanel to the JScrollPane's viewPort, not to the JScrollPane, like:
    Code:
    theScrollPanel.getViewPort().add(thePanel);
    I've done the same thing myself, and I used a FlowLayout for the scrollable JPanel:
    Code:
    thePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
    eschew obfuscation

  3. #3
    Join Date
    Aug 2006
    Posts
    2

    Multiple JPanels in a JScrollPane

    This has been resolved. By the end of the day I will post how it was done.



    AxWielder

Similar Threads

  1. Replies: 4
    Last Post: 11-28-2005, 03:14 PM
  2. Updating on screen text fields with multiple table data
    By William Ed Carden in forum VB Classic
    Replies: 2
    Last Post: 10-17-2005, 06:09 PM
  3. Getting a GUI to function
    By Eric in forum Java
    Replies: 1
    Last Post: 11-27-2001, 07:53 AM
  4. Script for scrolling
    By Mark in forum Web
    Replies: 3
    Last Post: 08-30-2001, 12:45 PM
  5. Double Text 1.0
    By George Gilbert in forum vb.announcements
    Replies: 0
    Last Post: 08-19-2001, 12:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center