Results 1 to 9 of 9
  1. #1
    peterhabe is offline Member
    Join Date
    Jul 2010
    Posts
    43
    Rep Power
    0

    Default Changing JScollPane view

    What i want to happen when the text the user typed onto the JTextArea gets too large for the viewport, is for the view in the viewport to be on the right hand side of the JTextArea so the user can see what they just typed instead of it staying on the left hand side of the JTextArea.

    Here's a little exacutable program which demonstates the problem (as well as a few others which don't seem to be present in my full program):

    Java Code:
    import java.awt.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.util.LinkedList;
    import javax.swing.*;
    
    public class OutputDisplayFrame extends JFrame{
    	//class objects
    	private KeyListenerClass keyListener;
    	//variables
    	private JPanel mainPanel;
    	private JTextArea display;
    	private JScrollPane displayPanelScrollPane;
    	private LinkedList<String> displayText = new LinkedList<String>();
    	private int lineCounter = 0;
    	private final int DIGIT_WIDTH = 30, MARGIN_WIDTH = 30;
    	private JViewport viewport;
    	private Component view;
    	private String displayString = "";
    	
    	//main method
    	public static void main(String args[]){
    		new OutputDisplayFrame();
    	}
    	
    	//constructor
    	public OutputDisplayFrame(){
    		//setting frame
    		this.setTitle("Calculator");
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setSize(800, 600);
    		this.setVisible(true);
    		
    		//setting panel
    		mainPanel = new JPanel();
    		mainPanel.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
    		
    		//setting JScrollPane
    		displayPanelScrollPane = new JScrollPane(mainPanel);
    		this.add(displayPanelScrollPane);
    		
    		//setting mainPanel
    		mainPanel.setBackground(Color.WHITE);
    		mainPanel.setLayout(new BorderLayout());
    		
    		//setting display
    		display = new JTextArea();
    		display.setEditable(false);
    		display.setOpaque(false);
    		display.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 40));
    		display.setTabSize(2);
    		mainPanel.add(display, BorderLayout.CENTER);
    		
    
    		//////////////////////////////////setting default scrollPane position///////////////////////////////////////////
    		viewport = displayPanelScrollPane.getViewport();
    		view = viewport.getView();
    		viewport.setViewPosition(new Point(0, view.getHeight()));
    
    	    
    		//adding object to linked list
    		displayText.add("");
    		
    		this.setFocusable(true);
    		
    		//add keyListener
    		keyListener = new KeyListenerClass();
    		this.addKeyListener(keyListener);
    	}
    	
    	//OutputDisplayInterface methods
    	public void addText(String displayString){
    		//saving new string
    		displayText.remove(lineCounter);
    		displayText.add(displayString);
    		
    		//formatting test
    		String formatedText = "";
    		
    		for(int i = 0; i <= lineCounter; i++){
    			formatedText += displayText.get(i);
    			formatedText += "\n";
    		}
    		
    		display.setText(formatedText);
    		
    		//changing focus position of scrollPane if the current line of working is wider than the frame
    		if(displayText.peekLast().length() * DIGIT_WIDTH - MARGIN_WIDTH * 2 > this.getWidth()){
    			
    			
    			/////////////////////////////////////changing the view of the viewport////////////////////////////////////////////////
    			
    			System.out.println(new Point(view.getWidth(), view.getHeight()));
    			
    			viewport.setViewPosition(new Point(view.getWidth(), view.getHeight()));
    			display.revalidate();
    			
    			System.out.println(viewport.getViewPosition());
    			
    			
    		}
    			
    	}
    	
    	public void clear(){
    		//clearing linked list
    		displayText.clear();
    		lineCounter = 0;
    		displayText.add("");
    		
    		//resetting display
    		display.setText("");
    		
    		//setting default scrollPane position
    		viewport.setViewPosition(new Point(0, view.getHeight()));
    	}
    	
    	public void newLine(){
    		displayText.add("");
    		
    		//setting default scrollPane position
    		viewport.setViewPosition(new Point(0, view.getHeight()));
    		
    		lineCounter++;
    	}
    	
    	private void setTopDisplayString(String s){
    		displayString += s;
    		addText(displayString);
    	}
    	
    	//private class
    	
    	private class KeyListenerClass implements KeyListener{
    		public void keyPressed(KeyEvent event) {
    			int keyCode = event.getKeyCode();
    			
    			if(keyCode == KeyEvent.VK_DELETE)
    				clear();
    			else if(keyCode == KeyEvent.VK_A)
    				setTopDisplayString("Ans");
    			else if(keyCode == KeyEvent.VK_DIVIDE)
    				setTopDisplayString("/");
    			else if(keyCode == KeyEvent.VK_1 || keyCode == KeyEvent.VK_NUMPAD1)
    				setTopDisplayString("1");
    			else if(keyCode == KeyEvent.VK_2 || keyCode == KeyEvent.VK_NUMPAD2)
    				setTopDisplayString("2");
    			else if(keyCode == KeyEvent.VK_3 || keyCode == KeyEvent.VK_NUMPAD3)
    				setTopDisplayString("3");
    			else if(keyCode == KeyEvent.VK_MULTIPLY)
    				setTopDisplayString("*");
    			else if(keyCode == KeyEvent.VK_4 || keyCode == KeyEvent.VK_NUMPAD4)
    				setTopDisplayString("4");
    			else if(keyCode == KeyEvent.VK_5 || keyCode == KeyEvent.VK_NUMPAD5)
    				setTopDisplayString("5");
    			else if(keyCode == KeyEvent.VK_6 || keyCode == KeyEvent.VK_NUMPAD6)
    				setTopDisplayString("6");
    			else if(keyCode == KeyEvent.VK_MINUS || keyCode == KeyEvent.VK_SUBTRACT)
    				setTopDisplayString("-");
    			else if(keyCode == KeyEvent.VK_7 || keyCode == KeyEvent.VK_NUMPAD7)
    				setTopDisplayString("7");
    			else if(keyCode == KeyEvent.VK_8 || keyCode == KeyEvent.VK_NUMPAD8)
    				setTopDisplayString("8");
    			else if(keyCode == KeyEvent.VK_9 || keyCode == KeyEvent.VK_NUMPAD9)
    				setTopDisplayString("9");
    			else if(keyCode == KeyEvent.VK_ADD)
    				setTopDisplayString("+");
    			else if(keyCode == KeyEvent.VK_0 || keyCode == KeyEvent.VK_NUMPAD0)
    				setTopDisplayString("0");
    			else if(keyCode == KeyEvent.VK_EQUALS || keyCode == KeyEvent.VK_ENTER){
    				newLine();
    				displayString = "";
    			}
    		}
    		
    		public void keyReleased(KeyEvent event) {}
    		public void keyTyped(KeyEvent event) {}
    	}
    }
    Any help would be appresiated.
    Last edited by peterhabe; 09-17-2011 at 09:12 PM. Reason: Said i was using a JLabel when i was accually using a JTextArea

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: Changing JScollPane view

    Disclaimer: I haven't gone through your code since it obviously contains a whole lot of stuff that's unrelated to the problem. To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example.
    Java Code:
    JScrollBar scrollBar = scrollPane.getHorizontalScrollBar();
    scrollBar.setValue(scrollBar.getMaximum());
    Or you may want to use the approach in camickr's Left Dot Renderer Java Tips Weblog

    db

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: Changing JScollPane view

    Incidentally, why are you using a JLabel and not a JTextField with setEditable(false)? With the latter, you could set the caret position.

    db

  4. #4
    peterhabe is offline Member
    Join Date
    Jul 2010
    Posts
    43
    Rep Power
    0

    Default Re: Changing JScollPane view

    I'm accually using a JTextArea in the program, sorry for cofussing you. Setting the scrollBar value didn't make any difference. I'll see where i can get with caret position but i haven't used any caret methods before.
    Last edited by peterhabe; 09-17-2011 at 09:24 PM.

  5. #5
    peterhabe is offline Member
    Join Date
    Jul 2010
    Posts
    43
    Rep Power
    0

    Default Re: Changing JScollPane view

    I can't seem to get anywhere with the caret idea but i don't realy know what i'm doing.

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

    Default Re: Changing JScollPane view

    Quote Originally Posted by peterhabe View Post
    I'm accually using a JTextArea in the program, sorry for cofussing you. Setting the scrollBar value didn't make any difference. I'll see where i can get with caret position but i haven't used any caret methods before.
    Darryl didn't say to use JTextArea, please re-read his post.

    I can't seem to get anywhere with the caret idea but i don't realy know what i'm doing.
    And we don't know what you're doing either since you're not showing us your code attempt and any error messages, etc...

    If you're still stuck, post an SSCCE that shows your latest attempt, and let's work with it with you.
    Last edited by Fubarable; 09-17-2011 at 09:59 PM.

  7. #7
    peterhabe is offline Member
    Join Date
    Jul 2010
    Posts
    43
    Rep Power
    0

    Default Re: Changing JScollPane view

    Quote Originally Posted by Fubarable View Post
    Darryl didn't say to use JTextArea, please re-read his post.

    In origonal first post in this thread i said i was using JLabel to display the text when i was accually using a JTextArea (as pointed out in my reason for editing). Again sorry for any confussion.

    Quote Originally Posted by Fubarable View Post
    And we don't know what you're doing either since you're not showing us your code attempt and any error messages, etc...

    If you're still stuck, post an SSCCE that shows your latest attempt, and let's work with it with you.
    I did mange to get the getCaretPosition() method running in the end but it doesn't change the view of the JViewPort. Also as my text will be covering more than one line i don't think it will be of much use.

    There is the code for the SSCCE of my latest attempt which i've stipped down a bit more from the origonal code i posted:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.LinkedList;
    import javax.swing.*;
    
    public class OutputDisplayFrame extends JFrame implements KeyListener{
    	//variables
    	private JPanel mainPanel = new JPanel();
    	private JTextArea displayTextField = new JTextArea();
    	private JScrollPane displayPanelScrollPane = new JScrollPane(mainPanel);
    	private LinkedList<String> displayText = new LinkedList<String>();
    	private int lineCounter = 0;
    	private final int DIGIT_WIDTH = 7;
    	private JViewport viewport;
    	private Component view;
    	private String displayString = "";
    	
    	//main method
    	public static void main(String args[]){
    		new OutputDisplayFrame();
    	}
    	
    	//constructor
    	public OutputDisplayFrame(){
    		//setting frame
    		this.setTitle("Calculator");
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setSize(800, 600);
    		this.setVisible(true);
    		
    		//adding JScrollPane
    		this.add(displayPanelScrollPane);
    		
    		//setting mainPanel
    		mainPanel.setLayout(new BorderLayout());
    		
    		//setting display JTextArea
    		displayTextField.setEditable(false);
    		mainPanel.add(displayTextField, BorderLayout.CENTER);
    		
    		viewport = displayPanelScrollPane.getViewport();
    		view = viewport.getView();
    	    
    		//adding new string to linked list
    		displayText.add("");
    		
    		//add keyListener
    		this.addKeyListener(this);
    		this.setFocusable(true);
    	}
    	
    	public void addText(String displayString){
    		//saving new string to displayText LinkedList
    		displayText.remove(lineCounter);
    		displayText.add(displayString);
    		
    		//formatting test
    		String formatedText = "";
    		
    		for(int i = 0; i <= lineCounter; i++){
    			formatedText += displayText.get(i);
    			formatedText += "\n";
    		}
    		
    		//setting JTextField
    		displayTextField.setText(formatedText);
    		
    		//changing view of scrollPane if the current line of working is wider than the frame
    		if(displayText.peekLast().length() * DIGIT_WIDTH > this.getWidth()){
    			
    			
    			/////////////////////////////////////trying to change the view of the JViewport////////////////////////////////////////////////
    			viewport.setViewPosition(new Point(displayTextField.getCaretPosition() * DIGIT_WIDTH, view.getHeight()));
    			//displayPanelScrollPane.getHorizontalScrollBar().setValue(displayPanelScrollPane.getHorizontalScrollBar().getMaximum());
    			viewport.revalidate();
    			
    			System.out.println("getCaretPosition() * DIGIT_WIDTH: "+displayTextField.getCaretPosition() * DIGIT_WIDTH);
    			System.out.println("Point passed into setViewPosition(): "+new Point(view.getWidth(), view.getHeight()));
    			System.out.println("Current view position: "+viewport.getViewPosition()+"\n");
    			
    			
    		}	
    	}
    	
    	//KeyListener methods
    	public void keyPressed(KeyEvent event) {
    		int keyCode = event.getKeyCode();
    		
    		if(keyCode == KeyEvent.VK_DELETE){
    			clear();
    		}else if(keyCode == KeyEvent.VK_EQUALS || keyCode == KeyEvent.VK_ENTER){
    			newLine();
    			displayString = "";
    		}else{
    			 addToDisplayString(String.valueOf(keyCode));
    		}
    	}
    		
    	public void keyReleased(KeyEvent event) {}
    	public void keyTyped(KeyEvent event) {}
    	
    	//clears displayTextField
    	public void clear(){
    		//clearing displayText LinkedList
    		displayText.clear();
    		lineCounter = 0;
    		displayText.add("");
    		
    		//resetting display
    		displayTextField.setText("");
    		
    		//setting default scrollPane view
    		viewport.setViewPosition(new Point(0, view.getHeight()));
    	}
    		
    	//starts new line in DisplayTextField
    	public void newLine(){
    		//adding new String to linked list
    		displayText.add("");
    		
    		//setting default scrollPane view
    		viewport.setViewPosition(new Point(0, view.getHeight()));
    		
    		lineCounter++;
    	}
    	
    	private void addToDisplayString(String s){
    		displayString += s;
    		addText(displayString);
    	}
    }
    And here is the printouts that are displayed in the console when the program tries to change the view of the JViewport from its default view:

    getCaretPosition() * DIGIT_WIDTH: 826
    Point passed into setViewPosition(): java.awt.Point[x=798,y=544]
    Current view position: java.awt.Point[x=17,y=544]

    getCaretPosition() * DIGIT_WIDTH: 847
    Point passed into setViewPosition(): java.awt.Point[x=819,y=544]
    Current view position: java.awt.Point[x=38,y=544]

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: Changing JScollPane view

    You don't need to setViewPosition, just use the methods of DefaultCaret with a policy of ALWAYS_UPDATE. Take a look at camickr's blog page on Text Area Scrolling Java Tips Weblog

    db

  9. #9
    peterhabe is offline Member
    Join Date
    Jul 2010
    Posts
    43
    Rep Power
    0

    Default Re: Changing JScollPane view

    I've tried both methods suggested in that post, putting
    Java Code:
    DefaultCaret caret = (DefaultCaret)displayTextArea.getCaret();
    caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
    in the constructor and putting
    Java Code:
    displayTextArea.append("");
    displayTextArea.setCaretPosition(displayTextArea.getDocument().getLength());
    every time the size of the JTextArea is enlarged, but the horizontal scroll bar just won't budge. The vertical scroll bar scrolls down without a problem.

    Thanks for the help so far DarrylBurke, its much appreciated.
    Last edited by peterhabe; 09-19-2011 at 10:56 AM.

Similar Threads

  1. Replies: 0
    Last Post: 08-14-2010, 10:09 AM
  2. How enter this mode-gui/design-view/code-view
    By lse123 in forum NetBeans
    Replies: 0
    Last Post: 02-28-2010, 09:09 PM
  3. Updating a view using actions in a seperate view
    By xcallmejudasx in forum Eclipse
    Replies: 0
    Last Post: 10-24-2008, 09:24 PM
  4. Changing color of variables view details
    By mtoledo in forum Eclipse
    Replies: 0
    Last Post: 03-25-2008, 02:35 PM
  5. changing the secondary ID of a view
    By schuetzejanett in forum SWT / JFace
    Replies: 0
    Last Post: 08-08-2007, 07:28 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
  •