Results 1 to 4 of 4
- 08-18-2011, 03:41 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 4
- Rep Power
- 0
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:
Maybe some of you know this issue and the fitting solution.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); } }); } }
Flo
- 08-18-2011, 04:20 AM #2
Member
- Join Date
- Aug 2011
- Posts
- 4
- Rep Power
- 0
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:
It works but it looks not nice. The scrollbar is flickering and the edited line is shown in the very top of the viewport.Java Code:float prozent = getVerticalScrollBar().getValue() / (float)getVerticalScrollBar().getMaximum(); lineBar.setText(numbers); this.getVerticalScrollBar().setValue( (int)(getVerticalScrollBar().getMaximum()*prozent) );
Has anybody an idee?
- 08-18-2011, 05:06 AM #3
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
- 08-18-2011, 01:17 PM #4
Member
- Join Date
- Aug 2011
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
How to autoscroll a JScrollPane
By Junky in forum AWT / SwingReplies: 2Last Post: 03-10-2011, 09:32 PM -
Autoscroll website
By Eliteoomph in forum New To JavaReplies: 2Last Post: 02-09-2011, 09:33 PM -
Chat window autoscroll lockes scrollbar at buttom
By ZacK.YoveL in forum AWT / SwingReplies: 2Last Post: 06-25-2010, 11:19 PM -
dragging the row header autoscroll
By Allgorythm in forum New To JavaReplies: 6Last Post: 02-15-2010, 05:10 AM -
jscrollpane scroll issues
By kumar_gemi in forum AWT / SwingReplies: 11Last Post: 09-30-2009, 08:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks