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

    Default How to change the scrollable block increment of a JTestArea?

    I basically want to change the scrollable block increment of a JTextField so that it exposes one character every time the user clicks on the arrows on the JScrollBar and every time a mouseWheelMoved method is called. I've been looking all day on oracle.com on how i could do it but came up with nothing.

    I've tried using objects created from a class which extends JTextField and overrides the getScrollableBlockIncrement and getScrollableUnitIncrement methods but it makes no difference to the behaviour of the JTextField.

    Also if anyone can be of any help for this problem, it would be much appreciated: Changing JScollPane view
    Last edited by peterhabe; 09-22-2011 at 08:33 PM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: How to change the scrollable block increment of a JTestArea?

    Overriding the getScrollableUnitIncrement works for me, using a JTextArea within a JScrollPane. Post an SSCCE that demonstrates the problem you are having

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

    Default Re: How to change the scrollable block increment of a JTestArea?

    You have a scrollbar on a JTextField?

    If so then I guess your code would be something like:

    Java Code:
    JScrollBar horizontal = scrollPane.getHorizontalScrollBar();
    horizontal.setBlockIncrement(...);

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

    Default Re: How to change the scrollable block increment of a JTestArea?

    Thanks for the replies.

    The method in my class overriding the getScrollableUnitIncrement() in the JTextField class is never executed for some reason.

    And i've tried setBlockIncrement(...) method on the scrollbars and it makes no to the block incrementation.

    Here's a little program which demonsrates the problem:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.LinkedList;
    import javax.swing.*;
    import javax.swing.text.DefaultCaret;
    
    public class OutputDisplayFrame extends JFrame implements KeyListener{
    	//variables
    	private JPanel mainPanel = new JPanel();
    	private DisplayTextArea displayTextArea = new DisplayTextArea();
    	private JScrollPane displayScrollPane = new JScrollPane(mainPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    	private LinkedList<String> displayText = new LinkedList<String>();
    	private int lineCounter = 0;
    	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);
    		
    		//adding JScrollPane
    		this.add(displayScrollPane);
    		
    		//trying to set block increment
    		JScrollBar horizontal = displayScrollPane.getHorizontalScrollBar();
    		horizontal.setBlockIncrement(40);
    		
    		JScrollBar vertical = displayScrollPane.getVerticalScrollBar();
    		vertical.setBlockIncrement(40);
    		
    		//setting mainPanel
    		mainPanel.setLayout(new BorderLayout());
    		
    		//setting display JTextArea
    		displayTextArea.setEditable(false);
    		mainPanel.add(displayTextArea, BorderLayout.CENTER);
    		
    		viewport = displayScrollPane.getViewport();
    		view = viewport.getView();
    	    
    		//adding new string to linked list
    		displayText.add("");
    		
    		//add keyListener
    		this.addKeyListener(this);
    		this.setFocusable(true);
    		
    		this.setVisible(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
    		displayTextArea.setText(formatedText);
    	}
    	
    	//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
    		displayTextArea.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);
    	}
    }
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class DisplayTextArea extends JTextArea{
    	
    	public DisplayTextArea(){
    		System.out.println("start");
    	}
    	
    	 public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
    		 System.out.println("1");
    		 return 40;
    	 }	 
    }
    The println in the constructor of the DisplayTextArea class prints but the println in the getScrollableBlockIncrement() method never does.

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: How to change the scrollable block increment of a JTestArea?

    For the record, JTextArea != JTextField.

    Perhaps I'm missing something, but I don not see where the JTextArea is added to a JScrollPane

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

    Default Re: How to change the scrollable block increment of a JTestArea?

    Perhaps I'm missing something, but I don not see where the JTextArea is added to a JScrollPane
    private DisplayTextArea displayTextArea = new DisplayTextArea();, where DisplayTextArea extends JTextArea. I probably should have mede that clearer. Or is that where i'm going wrong?

    For the record, JTextArea != JTextField.
    What do you mean? You talking about testing objects?

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

    Default Re: How to change the scrollable block increment of a JTestArea?

    Quote Originally Posted by peterhabe View Post
    private DisplayTextArea displayTextArea = new DisplayTextArea();, where DisplayTextArea extends JTextArea. I probably should have mede that clearer. Or is that where i'm going wrong?
    But where do you add this to a JScrollPane? And why are you extending JTextArea for that matter?


    What do you mean? You talking about testing objects?
    Please re-read your first post in this thread -- you're taking about nothing but JTextField and your question has nothing to do with JTextField. In programming precision is paramount, and by extension when asking programming questions, you must be precise, lest you cause nothing but confusion.

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

    Default Re: How to change the scrollable block increment of a JTestArea?

    And why are you extending JTextArea for that matter?
    I'm trying to override the getScrollableBlockIncrement() method, unless theres a better way of changing the block increment on a JTextArea.
    But where do you add this to a JScrollPane?
    For some reason i had a random JPanel in the mix. Taking it out has changed the default block increment to something more resonable, but the getScrollableBlockIncrement() in the DisplayTextArea class still isn't been executed and the setBlockIncrement() still isn't doing what it was designed to do.

    Here's the program without the JPanel to confuse things:
    Java Code:
    import java.awt.event.*;
    import java.util.LinkedList;
    import javax.swing.*;
    
    public class OutputDisplayFrame extends JFrame implements KeyListener{
    	//variables
    	//creating add setting JTextArea
    	private JTextArea displayTextArea = new DisplayTextArea();
    	//creating JScrollPane and adding the JTextArea to it
    	private JScrollPane displayScrollPane = new JScrollPane(displayTextArea);
    	private LinkedList<String> displayText = new LinkedList<String>();
    	private int lineCounter = 0;
    	private String displayString = "";
    	
    	//main method
    	public static void main(String args[]){
    		new OutputDisplayFrame();
    	}
    	
    	//constructor
    	public OutputDisplayFrame(){
    		//setting JFrame
    		this.setTitle("Calculator");
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setSize(800, 600);
    		this.setFocusable(true);
    		
    		//trying to set block increment
    		JScrollBar horizontal = displayScrollPane.getHorizontalScrollBar();
    		horizontal.setBlockIncrement(20);
    		
    		JScrollBar vertical = displayScrollPane.getVerticalScrollBar();
    		vertical.setBlockIncrement(20);
    	    
    		//adding new String to LinkedList
    		displayText.add("");
    		
    		//add keyListener
    		this.addKeyListener(this);
    		
    		//adding JScrollPane to JFrame
    		this.add(displayScrollPane);
    		
    		this.setVisible(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
    		displayTextArea.setText(formatedText);
    	}
    	
    	//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
    		displayTextArea.setText("");
    	}
    		
    	//starts new line in DisplayTextField
    	public void newLine(){
    		//adding new String to linked list
    		displayText.add("");
    		
    		lineCounter++;
    	}
    	
    	private void addToDisplayString(String s){
    		displayString += s;
    		addText(displayString);
    	}
    }
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class DisplayTextArea extends JTextArea{
    	
    	public DisplayTextArea(){
    		System.out.println("start");
    	}
    	
    	public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
    		System.out.println("1");
    		return 20;
    	}	 
    }
    Last edited by peterhabe; 09-23-2011 at 08:08 PM.

Similar Threads

  1. Increment Counter for ID
    By blkshrk81 in forum New To Java
    Replies: 6
    Last Post: 12-01-2010, 01:25 AM
  2. Increment Operator Example
    By abimaran in forum New To Java
    Replies: 10
    Last Post: 11-03-2009, 04:45 PM
  3. scrollable applets
    By kaemonsaionji in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 08:47 AM
  4. Increment a Variable
    By rhm54 in forum New To Java
    Replies: 2
    Last Post: 06-14-2008, 02:57 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
  •