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.
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
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
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.
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.
Re: Changing JScollPane view
Quote:
Originally Posted by
peterhabe
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.
Quote:
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.
Re: Changing JScollPane view
Quote:
Originally Posted by
Fubarable
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
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:
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]
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
Re: Changing JScollPane view
I've tried both methods suggested in that post, putting
Code:
DefaultCaret caret = (DefaultCaret)displayTextArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
in the constructor and putting
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.