Page 2 of 2 FirstFirst 12
Results 21 to 25 of 25

Thread: Strange Error

  1. #21
    willemien is offline Member
    Join Date
    Apr 2010
    Location
    london UK
    Posts
    53
    Rep Power
    0

    Default Re: Strange Error

    I want to scroll (and keep the scroll slider at the end of the JTextArea (so that always the latest added line is visuable)
    the Jtext area is in a scrollpanel
    at the moment i do it with vbar (the vertical scrollbar of the scrollpanel )


    Java Code:
    
    	
    package graphical.swing;
     
     
    import java.awt.BorderLayout;
    import java.awt.Font;
     
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollBar;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.ScrollPaneConstants;
    import javax.swing.WindowConstants;
     
     
    public class TheoremLoggerSwing
    {
       
      private final JPanel      contentPane;
      private final JScrollPane scrollPane;
      private final JTextArea   logArea;
      private final JTextArea   theoremArea;
      private final JScrollBar  vbar;
       
      private JDialog           frame;
     
      // width of textarea 
      private int               width = 70;
         
     
      // constructor
      public TheoremLoggerSwing() {
        this.logArea = new JTextArea();
        this.logArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
        this.logArea.setColumns(this.width);
        this.logArea.setRows(20);
        this.logArea.setEditable(false);
        this.logArea.setWrapStyleWord(true);
        this.logArea.setLineWrap(true);
         
        this.theoremArea = new JTextArea();
         
        this.scrollPane = new JScrollPane(this.logArea);
        
        this.scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        this.vbar = TheoremLoggerSwing.this.scrollPane.getVerticalScrollBar();
         
        this.contentPane = new JPanel();
        this.contentPane.setLayout(new BorderLayout());
        this.contentPane.add(this.theoremArea, BorderLayout.NORTH);
        this.contentPane.add(this.scrollPane, BorderLayout.CENTER);
      }
       
       
      // methods
       
       
       
      public void print(final String log) {
        // add log to logarea
        final int nWidth = 2 + log.length();
        if (this.width < nWidth) {
          this.width = nWidth;
          this.logArea.setColumns(this.width);
        }
        this.logArea.append(log);
        this.adjustBar();
      }
       
       
      private void adjustBar() {
        this.vbar.setValue(this.vbar.getMaximum());
      }
       
       
      public void printNewList(final String nextString) {
        this.logArea.setText(nextString);
        this.adjustBar();
      }
       
       
      protected JPanel getPanel() {
        return this.contentPane;
      }
       
       
      public void setParentloopInfo(final String thInfo) {
        this.theoremArea.setText(thInfo);
      }
       
    }

  2. #22
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: Strange Error

    Quote Originally Posted by willemien View Post
    I want to scroll (and keep the scroll slider at the end of the JTextArea (so that always the latest added line is visuable)
    the Jtext area is in a scrollpanel
    at the moment i do it with vbar (the vertical scrollbar of the scrollpanel )
    Does that code work? Sorry, I'm not sure whether you still have a question or you're just keeping us updated on what you're doing.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #23
    willemien is offline Member
    Join Date
    Apr 2010
    Location
    london UK
    Posts
    53
    Rep Power
    0

    Default Re: Strange Error

    The code does work but i am not sure if it is optimal, (making a runnable each time a line is added to the logger) i would like to have code where the call to adjustBar isn't nescesary (so that i only have to add the line to the JTextArea. the thing is that a call to JTextArea.setText(String) and JTextArea.add(String) ARE threadsafe, while the call to JScrollBar.setValue is NOT threadsafe (don't understand why) , a bit defeating the efficiency of it all.

    was wondering if it is not possible to do it in another way.
    the only improvement i could come up with to this moment is to change

    Java Code:
     
      private void adjustBar() {
        this.vbar.setValue(this.vbar.getMaximum());
      }
    to

    Java Code:
     private void adjustBar() {
        this.vbar.setValue(Integer.MAX_VALUE);
      }
    reducing it to from two to one call to vbar, but I hoped that it could be possible without calling vbar at all or doing it without having to make a runnable.

    later study made me worry about Swing, The print() routine is called very often and i am thinking that the frequency that the main thread adds messages is higer than the frequency that the EventDispatchThread can execute them, wondering if Java has build in safety measures to prevent this scenario.

  4. #24
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: Strange Error

    "Premature optimization is the root of all evil" - Donald Knuth

    You're worrying about a few method calls here and there. I wouldn't stress out too much about it, as long as the code works. You can always shave a few lines of code or method calls off your program, but is it really worth the effort? Especially if it sacrifices readability or maintainability, the answer is often no.

    The best thing you can do is just keep writing code. Don't get too bogged down in making things perfect. No matter what you write, in 6 months you'll look back at the code you're writing now and shudder, and this will always be the case. Welcome to programming.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #25
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Strange Error

    Disclaimer: I haven't read the code.

    Quote Originally Posted by willemien View Post
    I want to scroll (and keep the scroll slider at the end of the JTextArea (so that always the latest added line is visuable)
    Check out camickr's Text Area Scrolling « Java Tips Weblog

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. strange error in eclipse
    By stevenpalomino in forum New To Java
    Replies: 29
    Last Post: 06-25-2011, 02:46 PM
  2. BufferedReader throwing strange error
    By DragonGhola in forum Advanced Java
    Replies: 4
    Last Post: 06-16-2011, 11:17 AM
  3. Strange unreachable statement error...
    By silafirion in forum New To Java
    Replies: 5
    Last Post: 12-11-2010, 06:05 AM
  4. Strange Error
    By AJArmstron@aol.com in forum New To Java
    Replies: 1
    Last Post: 04-18-2010, 10:31 PM
  5. strange Error message
    By little_polarbear in forum New To Java
    Replies: 4
    Last Post: 08-26-2008, 12:45 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
  •