Hi I am attempting to have my JTextArea Scroll to the line that the user clicks on from the JTree. However, I do not know how to have the JTextArea focus on that specific line.
For example, if the user clicks a JTree node that has information on line 118 then I want the JTextArea to scroll to that line. I have a picture below demonstrating what I mean, as well as parts of the code.
Code:
private static JFrame frame;
private static JLabel lblFileChosen;
private static JTextArea resultsTextArea;
private static JScrollPane scroll2;
private static JTextArea LogFileTA_1;
private static JTree errorTree;
private static DefaultTreeModel model;
private static ArrayList<logObject> rftdArrayList = new ArrayList<logObject>();
private static void clickErrorTree(MouseEvent me)
{
TreePath tp = errorTree.getPathForLocation(me.getX(), me.getY());
if (tp != null)
System.out.println(tp.toString());
else
System.out.println("huh");
}
private void initialize()
{
frame = new JFrame();
frame.setBounds(100, 100, frameW, frameH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout(0, 0));
LogFileTA_1 = new JTextArea(34,78);
frame.getContentPane().add(LogFileTA_1, BorderLayout.WEST);
LogFileTA_1.setWrapStyleWord(true);
LogFileTA_1.setLineWrap(true);
LogFileTA_1.setText("Log File");
LogFileTA_1.setEditable(false);
LogFileTA_1.setVisible(true);
JScrollPane scroll1 = new JScrollPane(LogFileTA_1);
frame.getContentPane().add(scroll1, BorderLayout.WEST);
lblFileChosen = new JLabel("File Chosen: ");
lblFileChosen.setVerticalAlignment(SwingConstants.BOTTOM);
lblFileChosen.setHorizontalAlignment(SwingConstants.LEFT);
frame.getContentPane().add(lblFileChosen, BorderLayout.SOUTH);
errorTree = new JTree();
errorTree.setVisibleRowCount(5);
errorTree.setModel(null);
resultsTextArea = new JTextArea();
//resultsTextArea.setRows(8);
resultsTextArea.setText("Results Area");
resultsTextArea.setColumns(10);
//frame.getContentPane().add(resultsTextArea, BorderLayout.CENTER);
resultsTextArea.setEditable(false);
scroll2 = new JScrollPane(errorTree);
scroll2.setColumnHeaderView(resultsTextArea); //errorTree
frame.getContentPane().add(scroll2, BorderLayout.CENTER);
mntmOpen.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
errorTree.setModel(null);
createFileChooser(frame);
}
});
errorTree.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
clickErrorTree(me);
}
});
}
, if the user clicks a JTree node that has information on line 118 then I want the JTextArea to scroll to that line.
Check out Text Utilities.
You can use the RXTextUtilities.gotoStartofLine(...)
method.
If you want you could also use the RXTextUtilities.centerLineInScrollPane(...)
method.