🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #8919

Re: JTextPane with Horizontal Scrolling

#8919
From: Thomas, Lionel SMETS
Date: Tue, 27 Jun 2000 00:00
375 lines
10694 bytes







Hi,
I haven't much time at the moment but here some code that does it 
(Sorry it's quite fuzzy but I'm righ in a debug session)

Thomas,

package dbg.tst_swing;

import javax.swing.*;
import javax.swing.text.*;
//import updateDB.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;

public class AdvanceViewer2
        extends startApps.ToolBox
{
  private boolean debug = true,
      WrapStyle = true,
          BackingStoreEnabled = true;
  private JFrame f = null;
  private JPanel  jPanelTop    = null,
                  jPanelCenter = null,
                  jPanelBottom = null,
                  jPanelGlobal = null;
  private TitledBorder titledBorder1 = null;                        // 
private JEditorPane jEditorPane1 = null;
  private GridBagLayout gridBagLayout = null;
  private JScrollPane jSP = null;
  private JTextArea jTextArea1 = null;
  private JLabel  taLabel = null,
                  arLabel = null,
                  label = null;
  private JTextPane ActionDisplay = null,
                      WaitImageDisplay = null;
  private JProgressBar jProgressBar1 = null;
  private StyleContext context = null;
  private StyledDocument document = null;
  private Style labelStyle = null;
  private Icon icon = null;

  private int rows = 10, cols = 50,       //  Default values for the
TextFields
    vDimension = rows, hDimension = cols,
    ProgressMin = 0, ProgressMax = 100,   //  Default values for the
ProgressBar
    p_Hgap = 20, p_Vgap = 20,
    c_Hgap = 20, c_Vgap = 20;

  private static final int _SLEEP_TIME_   = 200;
  private Color BgColor = Color.lightGray ;
  private static boolean isTextPaneEditable = false,
    LoadImage = false;
  private static String _INITIAL_TEXT_ = "Program output";
  private static String _ACTION_REPORTER_ = "Action Reporter";
  private static String _GUI_INITIALIZED_ = "GUI initialized ";
  private static String _AR_LABEL_TEXT_ = "Action proceeded : ";
  private static String _TA_LABEL_TEXT_ = "Program Output";
  private static String ImageToDisplay = "Wait.gif";

  private Component init(boolean LoadImage)
              throws Exception
  {
    jPanelTop    = new JPanel();
    jPanelCenter = new JPanel();
    jPanelBottom = new JPanel();
    jPanelGlobal = new JPanel();
    titledBorder1 = new TitledBorder(this.getClass().getName());
    setLoadImage(LoadImage);

    // Settings for the text area
    if ( LoadImage ) {
      context = new StyleContext();
      document = new DefaultStyledDocument(context);
      labelStyle = context.getStyle(StyleContext.DEFAULT_STYLE);
      icon = new ImageIcon(ImageToDisplay);
      label = new JLabel(icon);
      StyleConstants.setComponent(labelStyle, label);
      try {
        debugInformation("Document created with styles");
        document.insertString(document.getLength(), "Ignored",
labelStyle);
      } catch (BadLocationException badLocationException) {
        debugInformation("BadLocation exception "
          + badLocationException.getMessage());
      }

      WaitImageDisplay = new JTextPane(document);
      WaitImageDisplay.setEditable(isTextPaneEditable);

    } else
    {
      jTextArea1 = new JTextArea(_INITIAL_TEXT_, rows, cols);
    }

    arLabel = new JLabel();
    taLabel = new JLabel();
    ActionDisplay = new JTextPane();
    jProgressBar1 = new JProgressBar(JProgressBar.HORIZONTAL,
                        ProgressMin, ProgressMax);

    // Setting the Layouts & colors for the Panels
    jPanelGlobal.setLayout(new BorderLayout( ));
    jPanelTop.setLayout(    new BorderLayout( ));
    jPanelCenter.setLayout( new BorderLayout( ));
    jPanelBottom.setLayout( new BorderLayout( ));

    jPanelGlobal.setBackground(BgColor);
    jPanelTop.setBackground(BgColor);
    jPanelCenter.setBackground(BgColor);
    jPanelBottom.setBackground(BgColor);

    // Custom for Action reporter Label
    arLabel.setRequestFocusEnabled(false);
    arLabel.setText(_AR_LABEL_TEXT_);
    taLabel.setRequestFocusEnabled(false);
    taLabel.setText(_TA_LABEL_TEXT_);

    // Custom for Action Textfield
    ActionDisplay.setToolTipText(_ACTION_REPORTER_);
    ActionDisplay.setAutoscrolls(false);
    ActionDisplay.setText(_ACTION_REPORTER_);
    ActionDisplay.setEditable(false);
    ActionDisplay.setCursor(null);

    // Custom for ProgressBar
    jProgressBar1.setBorderPainted(true);
    jProgressBar1.setStringPainted(true);

    // Inserting ALL the elements onthe screen
    if ( LoadImage ) {
      jSP = new JScrollPane(WaitImageDisplay);
      jSP.setSize(icon.getIconWidth(), icon.getIconHeight() );
      jPanelTop.setSize(icon.getIconWidth() + 5, icon.getIconHeight() +
5 );
      debugInformation("TopPanel has of \nWidth : "
          + (icon.getIconWidth() + 5) + "\nHeight : "
              + (icon.getIconHeight() + 5 ));
      jSP.setAutoscrolls(true);
    } else
    {
      jPanelTop.add(taLabel, BorderLayout.NORTH);
      jSP = new JScrollPane(jTextArea1,
          JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
              JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
      jSP.setAutoscrolls(true);
    }

    jPanelTop.add( jSP, BorderLayout.WEST);
    jPanelCenter.add(jProgressBar1, BorderLayout.CENTER);
    jPanelBottom.add(arLabel, BorderLayout.WEST);
    jPanelBottom.add(ActionDisplay, BorderLayout.CENTER);

    ActionDisplay.setText(_GUI_INITIALIZED_);
    jPanelGlobal.add(jPanelTop,    BorderLayout.NORTH );
    jPanelGlobal.add(jPanelCenter, BorderLayout.CENTER);
    jPanelGlobal.add(jPanelBottom, BorderLayout.SOUTH );

    return jPanelGlobal;
  }

  public static void main (String[] args)
      throws InterruptedException
  {
     String[] s = {
      "aa",      "bb",      "cc",      "dd",      "ee",      "ff",
      "gg",      "ff",      "gg",      "hh",      "ii",      "jj",
      "kk",      "ll",      "mm",      "nn",      "oo",      "pp",
      "qq",      "rr",      "ss",      "tt",      "uu",      "vv",
      "ww",      "xx",      "yy",      "zz"      };

     AdvanceViewer2 av;
     System.out.println("in");
     av = new AdvanceViewer2();
     System.out.println("Instanciated");
     av.setActionToDo("Test1");
     Thread.sleep(_SLEEP_TIME_);
     av.setActionDone(10);
     Thread.sleep(_SLEEP_TIME_);
     av.setActionToDo("Test 2");
     Thread.sleep(_SLEEP_TIME_);
     av.setActionDone(10);
     try
     {
        for(int i = 0; ;i++)
        {
          av.setActionToDo("Writing : " + s[i]);
          av.programOutputReader(s[i]);
          av.setActionDone(3);
          Thread.sleep(_SLEEP_TIME_);
        }
     } catch(ArrayIndexOutOfBoundsException AAIOOBEe)
     { // Caught
     }
  }

  public AdvanceViewer2()
  {
    try
    {
     
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());

      //Create the top-level container and add contents to it.
      f = new JFrame(this.getClass().getName());
      Component contents = init(false);
      f.getContentPane().add(contents, BorderLayout.CENTER);

      //Finish setting up the frame, and show it.
      f.addWindowListener(
        new WindowAdapter()
        {
          public void windowClosing(WindowEvent e)
          {
            System.exit(0);
          }
        }
      ); // End of inner class
      f.pack();                         // Size is almost null without
this
      f.setResizable(false);
      f.setVisible(true);
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }

  public void textAreaRefresh()
  {
    jTextArea1.scrollRectToVisible(
      new Rectangle( 0, jTextArea1.getHeight()-10, 10, 10));
  }

  public void setActionDone(int inc)
  {
    String s = jProgressBar1.getString() + " Done ...";
    jProgressBar1.setString(s);
    jProgressBar1.setValue(jProgressBar1.getValue() + inc);
    generalInformation(s);
  }

  public void setActionToDo(String action)
  {
    jProgressBar1.setString(action);
    generalInformation(action);
  }

  public synchronized void debugInformation(String msg)
  {
    System.out.println("DEBUG (av) : " + msg);
  }

  public synchronized void generalInformation(String msg)
  {
    ActionDisplay.setText(msg);
  }

  public synchronized void programOutputReader(String msg)
  {
    if ( !getLoadImage() ) {
      jTextArea1.append("\n" + msg);
      textAreaRefresh();
    }
  }

  public void setRows(String r)
  {
    rows = Integer.parseInt(r);
  }

  private void setLoadImage(boolean li)
  {
      LoadImage = li;
  }

  public boolean getLoadImage()
  {
    return LoadImage;
  }

  public void setCols(String c)
  {
    cols = Integer.parseInt(c);
  }

  public void setProgressStart(String ps)
  {
    ProgressMin = Integer.parseInt(ps);
  }

  public void setProgressEnd(String pe)
  {
    ProgressMax = Integer.parseInt(pe);
  }

  public void setBackGroundColor(String bgCol)
  {
    BgColor = Color.getColor(bgCol);
  }

  public void setImageToDisplay(String itd)
  {
    ImageToDisplay = itd;
  }


}























doron@telemessage.com wrote:
> 
> Hi All!
> 
> I'm using the JTextPane and I didn't find out how to enable the Horizontal
> scroll. I know how to do it when using the JTextArea but the JTextPane has
> features I really need.
> 
> Anyone encountered this problem?
> 
> Thanks in advance,
> Doron.
> 
> Sent via Deja.com http://www.deja.com/
> Before you buy.

-- 
Thomas, Lionel SMETS
Av. de la Brabançonne 133 / 3
1030 Bruxelles
Belgium
e-mail : tsmets@altern.org
Technical Details
Message-ID: <395941A2.B3C12D68@altern.org>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <8j7fbm$509$1@nnrp1.deja.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: 111d45,909c99359b181f06
X-Google-Attributes: gid111d45,public
From: "Thomas, Lionel SMETS" <tsmets@altern.org>
Subject: Re: JTextPane with Horizontal Scrolling
Date: 2000/06/27
Message-ID: <395941A2.B3C12D68@altern.org>
X-Deja-AN: 639781253
Content-Transfer-Encoding: quoted-printable
References: <8j7fbm$509$1@nnrp1.deja.com>
X-Accept-Language: en
Content-Type: text/plain; charset=iso-8859-1
X-Complaints-To: abuse@chello.be
X-Trace: news.chello.be 962149904 213.132.133.5 (Wed, 28 Jun 2000 01:51:44 MET DST)
Organization: Chello Usenet Service
MIME-Version: 1.0
NNTP-Posting-Date: Wed, 28 Jun 2000 01:51:44 MET DST
Newsgroups: borland.public.jbuilder.java.swing