Results 1 to 5 of 5
  1. #1
    morris4019 is offline Member
    Join Date
    Nov 2010
    Location
    Federal Way, WA
    Posts
    18
    Rep Power
    0

    Default Custom Component Scrolling

    Hi,

    I am attempting to create a custom component that works like a JTextPane. I am extending JComponent to do so.

    Everything is going find but I run in to a little problem. I would like my component to work inside a JScrollPane. At the moment it scrolls just fine, but when text reaches the bottom I would like it to automatically adjust the viewport of the JScrollPane so as to keep up with the text. Basically just like the JTextPane does... When the text reaches out of the bounds of the current viewport the pane is expanded and the JScrollPane automatically adjusts.

    I thought there would be a method to maybe override by implementing the scrollable interface but no luck. Does anyone know how I might go about making my Text automatically scroll when the text reaches the bottom?

    -Morris4019

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Consider creating and posting an SSCCE so we can better understand your problem and possibly help you to find a solution.

  3. #3
    morris4019 is offline Member
    Join Date
    Nov 2010
    Location
    Federal Way, WA
    Posts
    18
    Rep Power
    0

    Default

    So below is a very basic version of what I'm talking about. I took out the code which actually adds the texts, filters things i need filtering, etc. Also, the drawing to the screen is pretty much gone so I wouldn't take up a whole lot of space, but I do not think it should be a problem.

    I thought, and aparently I was wrong, that the JScrollPane called some sort of method (like getViewPortRect, or something of the sort) on the component. I am just having a hard time figuring out how to update the scrollpane when it needs to scroll due to text entry.

    By calling the setPreferredSize method when needed I have no problem resizing the component inside the JScrollPane, but it is not viewing where i want it to view.

    Java Code:
    public class NodeTextBox extends JComponent
            implements KeyListener, MouseListener, FocusListener {
    
        private final Color caretColor = Color.red;
    
        private String myText="";
        private int caretPos = 0; // Starts at pos 0
        private int insets = 10; // px
    
        public NodeTextBox() {
            setFocusable(true);
            this.setFocusTraversalKeysEnabled(false);
            addMouseListener(this);
            addFocusListener(this);
            addKeyListener(this);
        }
    
        @Override
        public void paintComponent(Graphics g) {
            Graphics2D g2;
    
            if (g instanceof Graphics2D)
                g2 = (Graphics2D)g;
            else {
                g2 = (Graphics2D)g.create();
            }
    
            // Paint a square for our textbox.
            g2.setColor(Color.WHITE);
            g2.fillRect(getX(), getY(), getWidth()-1, getHeight()-1);
            g2.setColor(Color.black);
            g2.drawRect(getX(), getY(), getWidth()-1, getHeight()-1);
            
            ... { draw the text and caret } ...
    
            // Resize the window if we need to
            if ( #oflines * presetHeight > getHeight() {
                this.setPreferredSize(new Dimension(getWidth(), (lines.size() * 20) + insets + 30));
            }
    
            // Dispose of our new reference to the graphics object.
            g2.dispose();
        }
    
        public void keyTyped(KeyEvent e) {
            e.consume();
        }
    
        public void keyPressed(KeyEvent e) {
    
          .... { code to add text to the string } ....
          repaint();
    
        }
    
        public void keyReleased(KeyEvent e) {
            e.consume();
        }
    
        public void mouseClicked(MouseEvent e) {
            requestFocusInWindow();
        }
    
        public void focusGained(FocusEvent e) {
            System.out.println("GOT FOCUS");
            repaint();
        }
    
        public void focusLost(FocusEvent e) {
            repaint();
        }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    We can't compile nor run this code. Again, I suggest you create and post an SSCCE if you are serious about needing help. We don't want the whole code either. Again, the link should explain all.

  5. #5
    morris4019 is offline Member
    Join Date
    Nov 2010
    Location
    Federal Way, WA
    Posts
    18
    Rep Power
    0

    Default

    Ok, sorry about that. This is a dumbed down version (which will compile) that simulates exactly what I am asking about. The paintComponent method simulates adding the text to the screen line by line.

    You will notice that when run the scrollbars have appeared and function properly when clicked and dragged.

    But it is not quite how I would like it. If, say instead of my custom component, there was a standard JTextPane inserted instead, when the user entered the text (which mine simulates just by drawing straight to the screen), each newline that went beyond the bottom of the textpane would automatically scroll down.

    My question is how could I design my Component (not the client program) to activate the scrolling. I would like the Component to change the viewport somehow of the jscrollpane. (The final "Blah" should be the last line visible in the window) Is this possible?


    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 javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.Scrollable;
    
    public class NodeTextBoxSample extends JComponent {
    
        public static void main(String[] argv) {
    
            NodeTextBoxSample newSample = new NodeTextBoxSample();
    
            JFrame frame = new JFrame("Test NodeTextBoxSample");
                frame.setLayout(new BorderLayout());
                JScrollPane textScroll = new JScrollPane(newSample);
                    textScroll.setPreferredSize(new Dimension(200, 100));
                frame.add(textScroll, BorderLayout.CENTER);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setBounds(50, 50, 350, 100);
                frame.setVisible(true);
        }
    
        private String myText="THIS IS MY SAMPLE TEXT LINE BY LINE BLAH BLAH BLAH";
    
        public NodeTextBoxSample() {
            setFocusable(true);
            this.setFocusTraversalKeysEnabled(false);
        }
    
        @Override
        public void paintComponent(Graphics g) {
            Graphics2D g2;
    
            // Take the correct steps to get a Graphics2D object to work with
            if (g instanceof Graphics2D)
                g2 = (Graphics2D)g;
            else {
                g2 = (Graphics2D)g.create();
            }
    
            // Paint a rounded square for our textbox.
            g2.setColor(Color.WHITE);
            g2.fillRect(getX(), getY(), getWidth()-1, getHeight()-1);
            g2.setColor(Color.black);
            g2.drawRect(getX(), getY(), getWidth()-1, getHeight()-1);
    
            // Set the font to momospaced
            g2.setFont(new Font("Monospaced", Font.PLAIN, 12));
    
            // Split up my test text by spaces
            String[] ary = myText.split(" ");
            for (int i = 0; i < ary.length; i++) {
                g2.drawString(ary[i], 10, 10 + (i * 20));
    
                // Check the size and see if we need to resize the components height
                if ((10 + (i * 20)) > getHeight())
                    this.setPreferredSize(new Dimension(getWidth(), 10 + (i * 20)));
            }
    
            // Dispose of our new reference to the graphics object.
            g2.dispose();
        }
    
    
    
    }

Similar Threads

  1. Scrolling to focused component
    By berkeleybross in forum AWT / Swing
    Replies: 1
    Last Post: 07-21-2010, 02:03 PM
  2. Custom component and paint outside of bounds
    By happy_hippie in forum AWT / Swing
    Replies: 2
    Last Post: 06-30-2010, 05:41 PM
  3. Custom painter for standard component
    By spike in forum AWT / Swing
    Replies: 1
    Last Post: 10-04-2008, 05:06 PM
  4. Help with custom component
    By Falcon1 in forum AWT / Swing
    Replies: 8
    Last Post: 07-21-2007, 12:39 PM
  5. JScrollPane not scrolling
    By Riftwalker in forum Advanced Java
    Replies: 2
    Last Post: 07-17-2007, 08:16 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
  •