textAreaLeft.select(10,20);
The text in the text box does not get highlighted.
We tried various options including:
1. setting the Selection color, e. g.
textAreaRight.setSelectionColor(Color.blue);
2. setting Selected text Color
textAreaRight.setSelectedTextColor(Color.green);
3. using the caret
c.setDot(beginAlreayExistsAt);
c.moveDot(endAlreayExistsAt);
4. repainting
textAreaRight.repaint();
5. using validate
textAreaRight.validate();
We also got the getSelectionStart and getSelectionEnd methods after setting
the position. They returned the positions that we set--so the Text area is
recording the program's attempt at selection. We just can't get the user to see
what is selected.
How can one, under program control, highlight text in a text area
as if the user selected it with the mouse
Here is the full program:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.lang.*;
import java.util.*;
import javax.swing.text.*;
public class test1{
static JTextArea textAreaLeft;
public static void main (String[] args) {
JFrame frame = new JFrame("This is a test file");
textAreaLeft = new JTextArea(30,30);
try {
FileReader reader = new FileReader("d:\\project\\contract1.txt");
BufferedReader inFile = new BufferedReader(reader);
StringBuffer inputLine = new StringBuffer(inFile.readLine());
textAreaLeft.setLineWrap(true);
while (inputLine != null){
inputLine.append((char)10);
textAreaLeft.append(inputLine.toString());
String temp = inFile.readLine();
if (temp!=null) {
inputLine = new StringBuffer(temp);
}
else {
inputLine = null;
}
}
reader.close();
}
catch (IOException e) {
System.out.println(e);
}
JScrollPane scrollPaneLeft = new JScrollPane(textAreaLeft);
scrollPaneLeft.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPaneLeft.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JPanel paneLeft = new JPanel();
paneLeft.add(scrollPaneLeft);
frame.getContentPane().add(paneLeft, BorderLayout.WEST);
JPanel paneCenter = new JPanel();
JButton butt_CoNprod_info = new JButton("Co and Prod");
paneCenter.add(butt_CoNprod_info);
frame.getContentPane().add(paneCenter, BorderLayout.CENTER);
class CoAndProdListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// textAreaLeft.setSelectionColor(Color.blue);
// textAreaLeft.setSelectionColor(Color.red);
// textAreaLeft.setSelectedTextColor(Color.green);
textAreaLeft.select(10,20);
}
}
CoAndProdListener CAPL = new CoAndProdListener();
butt_CoNprod_info.addActionListener(CAPL);
Dimension size = new Dimension(800,600);
frame.setSize(size);
frame.setVisible(true);
}
}
Imtinan Ahmad
Computer Science Department
Western Illinois University
Macomb IL
mu...@wiu.edu (Ahmad IMTINAN) wrote in news:ahmlft$qho$1...@ecom1.wiu.edu:
> We are having a minor problem with a java swing program. Our program
> is selecting some text in a text area (JTextArea textAreaLeft). For
> this purpose we are using the select method of JTextArea as shown
> below:
[explanation and code snipped]
Your JTextArea will need to be focused in order to show selected text. In
your Button's ActionListener this is not the case.
Use a HighLighter instead.
André