Invisible JScrollPane?

I have a JList matched up with a JTextArea. Inside the JList I want it to list numbers corresponding to the line in the JTextArea. I have the JList's and JTextArea's JScrollPanes synched up. But, I only want the JTextArea's JScrollPane to show up. Is it possible to have the JList's JScrollPane be invisible but because it is synched up with the JTextArea's JScrollPane still scroll through the JList? If you do not know what I mean or want more of my code, let me know.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.event.CaretEvent;

import javax.swing.event.CaretListener;

publicclass cfmCodeCounter1_1extends JFrame

{

private JLabel lblEnter;

private JTextArea input, output;

private JList row;

private JButton parse, clear, exit;

private JScrollPane scroller, scroller2, scroller3;

private Container c;

private Vector <String> rowVec;

privateboolean threadflag =false;// Used so only one thread for the CaretListener runs at one time

private cfmCodeCounter1_1()

{

try

{

this.setTitle("ColdFusion Parser - NGIT");

// Vector

rowVec =new Vector<String>();

// Display Label

lblEnter =new JLabel("Enter Code Below: ");

lblEnter.setVisible(true);

// JTextAreas

input =new JTextArea(10, 30);// (rows, columns)

input.setEditable(true);

scroller =new JScrollPane(input);

input.requestFocus();

output =new JTextArea(5, 19);// (rows, columns)

output.setEditable(false);

scroller2 =new JScrollPane(output);

output.requestFocus();

// Selectable List

row =new JList(rowVec);

row.setVisibleRowCount(10);

row.setFixedCellWidth(50);

row.setFixedCellHeight((input.getPreferredSize().height/10));

scroller3 =new JScrollPane(row);

// Set up scroller and scroller3

scroller.getVerticalScrollBar().setModel(scroller3.getVerticalScrollBar().getModel());

// Set up listener for input and row

TextHandler txthandle =new TextHandler();

input.addCaretListener(txthandle);

// Button

parse =new JButton("Parse Code");

clear =new JButton("Clear");

exit =new JButton("Exit");

// Set Layout

c = getContentPane();

SpringLayout layout =new SpringLayout();

c.setLayout(layout);

c.add(lblEnter);

c.add(scroller3);

c.add(scroller);

c.add(scroller2);

c.add(parse);

c.add(clear);

c.add(exit);

// Place lblEnter

layout.putConstraint(SpringLayout.WEST, lblEnter,

10,

SpringLayout.WEST, c);

layout.putConstraint(SpringLayout.NORTH, lblEnter,

10,

SpringLayout.NORTH, c);

// Place scroller3 aka row

layout.putConstraint(SpringLayout.WEST, scroller3,

10,

SpringLayout.WEST, c);

layout.putConstraint(SpringLayout.NORTH, scroller3,

5,

SpringLayout.SOUTH, lblEnter);

// Place scroller aka input

layout.putConstraint(SpringLayout.WEST, scroller,

-1,

SpringLayout.EAST, scroller3);

layout.putConstraint(SpringLayout.NORTH, scroller,

0,

SpringLayout.NORTH, scroller3);

// Place scroller2 aka output

layout.putConstraint(SpringLayout.WEST, scroller2,

5,

SpringLayout.EAST, scroller);

layout.putConstraint(SpringLayout.NORTH, scroller2,

0,

SpringLayout.NORTH, scroller);

// Place parse

layout.putConstraint(SpringLayout.WEST, parse,

10,

SpringLayout.WEST, c);

layout.putConstraint(SpringLayout.NORTH, parse,

5,

SpringLayout.SOUTH, scroller);

// Place clear

layout.putConstraint(SpringLayout.WEST, clear,

5,

SpringLayout.EAST, parse);

layout.putConstraint(SpringLayout.NORTH, clear,

0,

SpringLayout.NORTH, parse);

// Place exit

layout.putConstraint(SpringLayout.WEST, exit,

5,

SpringLayout.EAST, clear);

layout.putConstraint(SpringLayout.NORTH, exit,

0,

SpringLayout.NORTH, clear);

// Button Event Handler

ButtonHandler handler =new ButtonHandler();

parse.addActionListener(handler);

clear.addActionListener(handler);

exit.addActionListener(handler);

this.addWindowListener(new WindowAdapter()

{

publicvoid windowClosing(WindowEvent e)

{

System.exit(0);

}

}

);

setSize(650, 300);

setVisible(true);

}

catch(Exception e)

{

e.printStackTrace();

JOptionPane.showMessageDialog(null,"General Error: See Command Line.","ERROR - NGIT", JOptionPane.ERROR_MESSAGE);

System.exit(0);

}

}

privateclass TextHandlerimplements Runnable, CaretListener

{

publicvoid caretUpdate(CaretEvent e)

{

if (!threadflag)

{

threadflag =true;

Thread txtThread =new Thread(this);

txtThread.start();

}

}

publicvoid run()

{

rowVec.removeAllElements();

for (int i = 1; i <= input.getLineCount(); i++)

{

rowVec.add("" + i);

}

row.setListData(rowVec);

threadflag =false;

// Find current row of cursor

int numrows = 1;

char[] chararr = input.getText().toCharArray();

int caretPos = input.getCaretPosition();

for (int i = (caretPos-1); i >= 0; i--)

{

if (chararr[i] =='\n')

{

numrows++;

}

}

row.setSelectedIndex((numrows-1));

row.ensureIndexIsVisible((numrows-1));

}

}

//Code Continues

[9332 byte] By [Scuba_Stevea] at [2007-11-27 11:35:39]
# 1

Also, when I paste something into the JTextArea, it is not auto-scrolling to the bottom. Anyone know why?

Scuba_Stevea at 2007-7-29 17:04:06 > top of Java-index,Desktop,Core GUI APIs...
# 2

> Is it possible to have the JList's JScrollPane be invisible

//scroller3 = new JScrollPane(row);

scroller3 = new JScrollPane(row,ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,

ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

> output.requestFocus();

won't work unless called after frame's visibility is true and should be

output.requestFocusInWindow();

> it is not auto-scrolling to the bottom.

set the caret position to the length of the doc

Michael_Dunna at 2007-7-29 17:04:06 > top of Java-index,Desktop,Core GUI APIs...
# 3

Thank you, you did catch an error that I had. I did not want output to have requestFocus(), as you can see just above it I actually wanted input to have it. Also, I put "input.requestFocus()" after I set the visible to true and it works. Thank you for that.

But, I have two main questions. One is, can you do what I described above and have one jscrollpane be invisible, yet still work since it is synched up with another jscrollpane that is visible?

The other question goes with this code below. I do not necessarily want it to scroll to the bottom of the page, just to where the cursor ends. Let's say I already have code in the input JTextArea, and I paste more code above it, I do not want it to scroll to the bottom of the JTextArea, just to where the cursor ends up. I cannot figure out why it is not autoscrolling.

private class TextHandler implements Runnable, CaretListener

{

public void caretUpdate(CaretEvent e)

{

if (!threadflag)

{

threadflag = true;

Thread txtThread = new Thread(this);

txtThread.start();

}

}

public void run()

{

rowVec.removeAllElements();

for (int i = 1; i <= input.getLineCount(); i++)

{

rowVec.add("" + i);

}

row.setListData(rowVec);

// Find current row of cursor

int numrows = 1;

char[] chararr = input.getText().toCharArray();

int caretPos = input.getCaretPosition();

for (int i = (caretPos-1); i >= 0; i--)

{

if (chararr[i] == '\n')

{

numrows++;

}

}

row.setSelectedIndex(numrows-1);

row.ensureIndexIsVisible(numrows-1);

System.out.println("ACTUAL: " + row.getSelectedIndex());

threadflag = false;

}

}

Scuba_Stevea at 2007-7-29 17:04:06 > top of Java-index,Desktop,Core GUI APIs...
# 4

> I have a JList matched up with a JTextArea. Inside the JList I want it to

> list numbers corresponding to the line in the JTextArea

You don't need two scroll pane for this. You use a single scroll pane and then add your line number component to the scroll pane using the setRowHeaderView(...) method.

I've posted several examples using this approach in the forum which you can find by searching for them.

camickra at 2007-7-29 17:04:06 > top of Java-index,Desktop,Core GUI APIs...
# 5

I could not find any good examples from searching the forums. Any threads that I came across all basically said, "Search the forums". So, for anyone who comes across this thread and wonders what to do, this is what I did to add the line counter. I do not have the program working perfectly yet, so if you use my code above, just use it as a base and work the kinks out. BTW, row (JList) is the component used as the row counter.

// JTextAreas

input = new JTextArea(10, 30); // (rows, columns)

input.setEditable(true);

scroller = new JScrollPane(input)

// Selectable List

row = new JList(rowVec);

row.setVisibleRowCount(10);

row.setFixedCellWidth(50);

row.setFixedCellHeight((input.getPreferredSize().height/10));

scroller.setRowHeaderView(row);

Scuba_Stevea at 2007-7-29 17:04:06 > top of Java-index,Desktop,Core GUI APIs...
# 6

I have another issue with this same program. I would add more Duke Stars, but I cannot and I did not think you guys wanted me to create a whole new thread since it deals with the same program. The new problem/question is, that the JScrollPane scrolls too much. I would like to have it only scroll one or two lines at a time. Can you do that?

Scuba_Stevea at 2007-7-29 17:04:06 > top of Java-index,Desktop,Core GUI APIs...
# 7

> I could not find any good examples from searching the forums

Then you need to improve your searching skills. Clues for picking keywords

a) I said I have posted examples (so use my id in the search)

b) I gave your the key method to use (so use the method in the search)

c) you want the row number beside a specific component (so use the component in the search)

Put the three keywords together and do a search. I found 3 postings, 2 of which had solutions.

I would list the keywords here, but then you would find this posting which doesn't help much.

camickra at 2007-7-29 17:04:06 > top of Java-index,Desktop,Core GUI APIs...
# 8

I did search using a & b search methods, I did not find what I wanted. I use Google to search this forum, it works much better than this forum's search.

Anyways, back to my actual question, does anyone know how to make the JScrollPane only scroll one line at a time?

Scuba_Stevea at 2007-7-29 17:04:06 > top of Java-index,Desktop,Core GUI APIs...
# 9

> does anyone know how to make the JScrollPane only scroll one line at a time?

It does scroll one line at a time. Use the up/down buttons scroll buttons. If you use the area between the scroll bars divider (whatever its called) then it scrolls one page at a time.

camickra at 2007-7-29 17:04:06 > top of Java-index,Desktop,Core GUI APIs...
# 10

So, there is no way to make it scroll one line at a time if you use the bar between the arrows like in an internet browser?

Scuba_Stevea at 2007-7-29 17:04:06 > top of Java-index,Desktop,Core GUI APIs...
# 11

I think you need to extend JTextArea and implement the Scrollable interface. Read the Swing tutorial on "How to Use Scroll Panes" for more information and an example:

http://java.sun.com/docs/books/tutorial/uiswing/components/scrollpane.html#scrollable

camickra at 2007-7-29 17:04:06 > top of Java-index,Desktop,Core GUI APIs...