Results 1 to 4 of 4
  1. #1
    Flo
    Flo is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Question JScrollPane issues - no autoscroll wished

    Hello.
    Currently I'm working on a simple IDE. I came up with a pretty good codeeditor but unfortunately there is a tiny problem with the JScrollPane.
    A JTextPane is embedded in a JPanel which is the ViewPortView of the JScrollPane.
    And with that there come major scrolling problems. When you enter a lot of rows then scroll to the top and enter letters there, the JScrollPane just scrolls to the very end of the JTextPane.

    Here is my code:
    Java Code:
    /**
     * Editorfeld mit Zeilennummerierung und Syntax-Highlighting
     */
    class JEditorNumbered extends JScrollPane implements DocumentListener
    {    
        // Private Datenfelder
        JTextPane textPane = new JTextPane();    
        JTextPane lineBar = new JTextPane();
        
        String[] commands = {"mov ", "add ", "sub ", "not ", "xor ", "or ", "and ", "jmp ", "jmpg ", "jmpe ", "jmpl ", "swap ", "load ", "store ", "cmp ", "nop", "reset", "addo ", "db ", "var ", "set ", "#include "};
        String[] keywords = {"PortA", "PortB", "PortC", "Akku", "HL"};
        
        
        /**
         * Text bekommen
         * @return der Text
         */
        public String getText(){return textPane.getText();}
        
        
        /**
         * Text setzen
         * @param text 
         */
        public void setText(String text){textPane.setText(text);}
        
        /**
         * Aktivieren/Deaktivieren
         * @param b 
         */
        @Override
        public void setEnabled(boolean b){super.setEnabled(b); textPane.setEnabled(b);}
        
        
        /**
         * Ein Dokumentlistener hinzufügen
         * @param listener 
         */
        public void addDocumentListener(DocumentListener listener){textPane.getDocument().addDocumentListener(listener); }
        
        
        
        /**
         * Konstruktor
         */
        public JEditorNumbered()
        {
            super();
            
            JPanel panel = new JPanel();
            panel.setLayout(new BorderLayout());
            panel.add(textPane,BorderLayout.CENTER);
            panel.add(lineBar,BorderLayout.WEST); 	
            
            lineBar.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Color.LIGHT_GRAY));
            lineBar.setEnabled(false);
            lineBar.setText(" 0 ");
            
            textPane.getDocument().addDocumentListener(this); 
            textPane.setMargin(new Insets(0,10,0,0));
            
             this.setViewportView(panel);
            setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
            
            Style style = textPane.addStyle("Normal", null);
            StyleConstants.setForeground(style, Color.BLACK);
            StyleConstants.setItalic(style, false);
            StyleConstants.setBold(style, false);
            
            style = textPane.addStyle("Befehl", null);
            StyleConstants.setForeground(style, Color.BLUE);
            StyleConstants.setBold(style, true);
            
            style = textPane.addStyle("Schlusselworter", null);
            StyleConstants.setForeground(style, Color.darkGray);
            StyleConstants.setBold(style, true);
            
            style = textPane.addStyle("Labels", null);
            StyleConstants.setForeground(style, Color.RED);
            StyleConstants.setBold(style, true);
            
            style = textPane.addStyle("Address", null);
            StyleConstants.setForeground(style, Color.RED);
            
            style = textPane.addStyle("String", null);
            StyleConstants.setForeground(style, new Color(0,0.5f,0));
            StyleConstants.setBold(style, true);
            
            style = textPane.addStyle("Kommentar", null);
            StyleConstants.setForeground(style, new Color(0.5f,0.5f,0.5f));
        }
        
        
        public void saveToFile(String path) throws IOException
        {
            BufferedWriter  out = new BufferedWriter(new FileWriter(path));
            out.write(textPane.getText());
            out.close();
        }
    
        public void readFromFile(String path) throws IOException
        {
            Scanner sc = new Scanner(new FileInputStream(path)); 
            String text = "";
            while(sc.hasNextLine())
            {
                text += sc.nextLine()+"\r\n";
            }
            
            sc.close();
            
            setText(text);
        }
        
        
        @Override
        public void changedUpdate(DocumentEvent e) {
            
        }
    
        @Override
        public void removeUpdate(DocumentEvent e) {
            insertUpdate(e);
        }
    
        @Override
        public void insertUpdate(DocumentEvent e) {
                   
            
            SwingUtilities.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    StyledDocument doc = textPane.getStyledDocument();
                    doc.setCharacterAttributes(0, textPane.getText().length(), textPane.getStyle("Normal"), true);
                    
                    Scanner sc = new Scanner(textPane.getText());
                    String numbers = "";		
                    int lineNr = 0;
                    int offsetToRow = 0;
                    
                    while(sc.hasNextLine())
                    {
                        numbers = numbers + " " + (lineNr++) + " \n";
                        String line = sc.nextLine();
                        
                        
                        // Highlighting
                        int start;
                        int pos;
                        int end;
    
                        // COMMANDS
                        for(int i=0;i<commands.length;i++)
                        {
                            start = 0;
                
                            while(true)
                            {
                                pos = line.indexOf(commands[i], start);
    
                                if(pos == -1)
                                    break;
    
                                int len = commands[i].length();
                                doc.setCharacterAttributes(offsetToRow + pos, len, textPane.getStyle("Befehl"), true);  
                                start += len;
                            }
                        }
                        
                        // <editor-fold defaultstate="collapsed" desc="Filter">
                        // KEYWORDS
                        for(int i=0;i<keywords.length;i++)
                        {
                            start = 0;
                
                            while(true)
                            {
                                pos = line.indexOf(keywords[i], start);
    
                                if(pos == -1)
                                    break;
    
                                int len = keywords[i].length();
                                doc.setCharacterAttributes(offsetToRow + pos, len, textPane.getStyle("Schlusselworter"), true);  
                                start += len;
                            }
                        }
                        
                        // LABELS
                        start = line.indexOf(":");
                        if(start != -1)
                        {
                            doc.setCharacterAttributes(offsetToRow, start+1, textPane.getStyle("Labels"), true);  
                        }
                        
                        
                        // ADRESSES
                        start = 0;
                        end = 0;
                        while(true)
                        {
                            start = line.indexOf("[", start);
                            end = line.indexOf("]", end);
                            if(start != -1 && end != -1)
                            {
                                doc.setCharacterAttributes(offsetToRow+start, end-start+1, textPane.getStyle("Address"), true);
                            }else{
                                break;
                            } 
                            start++; end++;
                        }
                        
                        
                        // STRING
                        start = 0;
                        end = 0;
                        while(true)
                        {
                            start = line.indexOf("\"", end);
                            end = line.indexOf("\"", start+1);
                            if(start != -1 && end != -1)
                            {
                                doc.setCharacterAttributes(offsetToRow+start, end-start+1, textPane.getStyle("String"), true);
                            }else{
                                break;
                            } 
                            start++; end++;
                        }
                        
                        
                        
                        // COMMENT
                        start = line.indexOf("//");
                        if(start != -1)
                        {                     
                            doc.setCharacterAttributes(offsetToRow+start, line.length()-start+1, textPane.getStyle("Kommentar"), true);
                        }
                        
                        //</editor-fold>
                                            
                        offsetToRow += line.length()+1;
                    }
                    
                    sc.close();
    
                    
                    if(textPane.getText().length()>0)
                        if(textPane.getText().charAt(textPane.getText().length()-1) == '\n')
                            numbers = numbers + " " + (lineNr) + " \n";
    
                    lineBar.setText(numbers);
                }
            });
            
        }
        
    }
    Maybe some of you know this issue and the fitting solution.

    Flo

  2. #2
    Flo
    Flo is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default

    Ok I found the Problem.
    When you refresh the line numbers then the scrollbar automatically goes down.
    How can I change these without interfering with the scrollbar?
    I tried:
    Java Code:
            float prozent = getVerticalScrollBar().getValue() / (float)getVerticalScrollBar().getMaximum();
            
            lineBar.setText(numbers);
            
            this.getVerticalScrollBar().setValue( (int)(getVerticalScrollBar().getMaximum()*prozent) );
    It works but it looks not nice. The scrollbar is flickering and the edited line is shown in the very top of the viewport.

    Has anybody an idee?

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default

    Instead of using a separate text pane for the line numbers you should create a custom component that is used as the row header view of the scroll pane.

    Text Component Line Number « Java Tips Weblog is a component that was designed for this purpose.

  4. #4
    Flo
    Flo is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

Similar Threads

  1. How to autoscroll a JScrollPane
    By Junky in forum AWT / Swing
    Replies: 2
    Last Post: 03-10-2011, 09:32 PM
  2. Autoscroll website
    By Eliteoomph in forum New To Java
    Replies: 2
    Last Post: 02-09-2011, 09:33 PM
  3. Chat window autoscroll lockes scrollbar at buttom
    By ZacK.YoveL in forum AWT / Swing
    Replies: 2
    Last Post: 06-25-2010, 11:19 PM
  4. dragging the row header autoscroll
    By Allgorythm in forum New To Java
    Replies: 6
    Last Post: 02-15-2010, 05:10 AM
  5. jscrollpane scroll issues
    By kumar_gemi in forum AWT / Swing
    Replies: 11
    Last Post: 09-30-2009, 08:33 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •