DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Threaded View

  1. #1
    Join Date
    Aug 2006

    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));

    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)));

    H += 500;

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

    scrollableGraphics = new JScrollPane(mainPanel);


    public static void main(String s[]) {
    JFrame frame = new JFrame("Graphics Generator");
    ScrollBarExampleCustom sbecust = new ScrollBarExampleCustom();
    sbecust.mainPanel.setOpaque(true); // content panes must be opaque

    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;
    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;
    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);


    Any help would be appreciated!
    Attached Images Attached Images

Similar Threads

  1. Replies: 4
    Last Post: 11-28-2005, 02: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, 05:09 PM
  3. Getting a GUI to function
    By Eric in forum Java
    Replies: 1
    Last Post: 11-27-2001, 06:53 AM
  4. Script for scrolling
    By Mark in forum Web
    Replies: 3
    Last Post: 08-30-2001, 11:45 AM
  5. Double Text 1.0
    By George Gilbert in forum vb.announcements
    Replies: 0
    Last Post: 08-19-2001, 11:34 AM

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
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

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

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.