Cursor missing in JTextpane

hi,

this may sound silly, but when i minimize my application (with the cursor focused on the JTextpane) and reActivate it, i loose the cusor though the focus stays on the TextPane. I can type the keys and c them on the Textpane, bte the blinking caret/cursor is missing.

I have tried to recreate it when window is reactivated, but it doesn't help.

Any ideas ?

Thanx and regards,

Sriram.

[431 byte] By [vsriram6] at [2007-9-27 2:15:55]
# 1

Try to find demo app. here on the site. It's called TextComponentDemo. You should be able to find it very quick, it's somewhere in tutorials. This very simple app., but has many key points about JTextPane. I have it handy. I tried to minimize and do other things. The caret stays put.

There is even CaretListener in it reporting the caret moves.

Probably you are missing something ?

SSvetlana at 2007-7-4 21:27:02 > top of Java-index,Archived Forums,Swing...
# 2

lemme put my source here -

public class XMLFormatter extends javax.swing.JFrame {

/** Creates new form XMLFormatter */

public XMLFormatter() {

initComponents();

jtaText.setCaretPosition(0);

}

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

private void initComponents() {

jspText = new javax.swing.JScrollPane();

jtaText = new javax.swing.JTextArea();

jpnSouth = new javax.swing.JPanel();

jbtnFont = new javax.swing.JButton();

jbtnFormat = new javax.swing.JButton();

jtbnClose = new javax.swing.JButton();

setTitle("XMLFormatter");

setFont(new java.awt.Font("Dialog", 0, 11));

setIconImage(java.awt.Toolkit.getDefaultToolkit().createImage(XMLFormatter.class.getResource("xmlformatter.gif")));

addWindowListener(new java.awt.event.WindowAdapter() {

public void windowClosing(java.awt.event.WindowEvent evt) {

exitForm(evt);

}

public void windowActivated(java.awt.event.WindowEvent evt) {

formWindowActivated(evt);

}

});

/* prepare Horizontal ScrollBar */

com.eibus.tools.CustomScrollBar hScrollBar = new com.eibus.tools.CustomScrollBar();

hScrollBar.setScrollBarUI(new com.eibus.tools.CustomScrollBarUI());

hScrollBar.setUnitIncrement(10);

hScrollBar.setOrientation(javax.swing.JScrollBar.HORIZONTAL);

/* prepare Vertical ScrollBar */

com.eibus.tools.CustomScrollBar vScrollBar = new com.eibus.tools.CustomScrollBar();

vScrollBar.setScrollBarUI(new com.eibus.tools.CustomScrollBarUI());

vScrollBar.setUnitIncrement(10);

vScrollBar.setOrientation(javax.swing.JScrollBar.VERTICAL);

jspText.setHorizontalScrollBar(hScrollBar);

jspText.setVerticalScrollBar(vScrollBar);

jtaText.setFont(new java.awt.Font("Dialog", 0, 11));

jspText.setViewportView(jtaText);

getContentPane().add(jspText, java.awt.BorderLayout.CENTER);

jbtnFont.setFont(new java.awt.Font("Dialog", 0, 11));

jbtnFont.setText("Font");

jbtnFont.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jbtnFontActionPerformed(evt);

}

});

jpnSouth.add(jbtnFont);

jbtnFormat.setFont(new java.awt.Font("Dialog", 0, 11));

jbtnFormat.setText("Format");

jbtnFormat.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jbtnFormatActionPerformed(evt);

}

});

jpnSouth.add(jbtnFormat);

jtbnClose.setFont(new java.awt.Font("Dialog", 0, 11));

jtbnClose.setText("Exit");

jtbnClose.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jtbnCloseActionPerformed(evt);

}

});

jpnSouth.add(jtbnClose);

getContentPane().add(jpnSouth, java.awt.BorderLayout.SOUTH);

pack();

java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();

setSize(new java.awt.Dimension(550, 400));

setLocation((screenSize.width-550)/2,(screenSize.height-400)/2);

}

private void formWindowActivated(java.awt.event.WindowEvent evt) {

/* Sometimes the caret is not visible after minimizing the window, hence this is used */

javax.swing.text.DefaultCaret defaultCaret = new javax.swing.text.DefaultCaret();

defaultCaret.setVisible(true);

defaultCaret.setBlinkRate(400);

jtaText.setCaret(defaultCaret);

jtaText.setCaretPosition(jtaText.getText().length());

}

private void jbtnFontActionPerformed(java.awt.event.ActionEvent evt) {

FormatterFontDialog fntDialog = new FormatterFontDialog(this, true, jtaText.getFont());

fntDialog.show();

if (FormatterFontDialog.blnOk) {

java.awt.Font f = fntDialog.getFont();

jtaText.setFont(f);

FormatterFontDialog.blnOk = false;

fntDialog.dispose();

}

}

private void jtbnCloseActionPerformed(java.awt.event.ActionEvent evt) {

System.exit(0);

}

private void jbtnFormatActionPerformed(java.awt.event.ActionEvent evt) {

try{

SAXDocument saxDocument = new SAXDocument(jtaText.getText().getBytes());

jtaText.setText((new NiceDOMWriter(saxDocument)).toString());

}catch(Exception ex){

javax.swing.JOptionPane.showMessageDialog(this, ex.getMessage(), "Invalid XML", javax.swing.JOptionPane.ERROR_MESSAGE);

}

}

/** Exit the Application */

private void exitForm(java.awt.event.WindowEvent evt) {

System.exit(0);

}

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

try{

javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());

}catch(Exception ex) {}

new XMLFormatter().show();

}

// Variables declaration - do not modify

private javax.swing.JScrollPane jspText;

private javax.swing.JTextArea jtaText;

private javax.swing.JPanel jpnSouth;

private javax.swing.JButton jbtnFont;

private javax.swing.JButton jbtnFormat;

private javax.swing.JButton jtbnClose;

// End of variables declaration

}

Pls ignore the functionality of "Format/Font", but i don't c the reason for the Caret disappearing here .....

Thanx.

vsriram6 at 2007-7-4 21:27:02 > top of Java-index,Archived Forums,Swing...
# 3

Hi Sriram

Try placing a CaretListener for you JTextPane

Here is a sample

CaretListener clst=new CaretListener() {

public void caretUpdate(CaretEvent e){

showAttributes(e.getDot());

}

};

ep.addCaretListener(clst);

// where ep is the JTextPane instance.

This worked for me.

Thanks

Swaraj

kswaraj at 2007-7-4 21:27:02 > top of Java-index,Archived Forums,Swing...
# 4

Add ComponentListener to your Frame/Dialog and just mention this public void componentShown(java.awt.event.ComponentEvent p1) {

yourTextPane.grabFocus()

}

sasivarnan at 2007-7-4 21:27:02 > top of Java-index,Archived Forums,Swing...
# 5

Thanx Guys for ur responses,

Sorry for mentioning it as TextPane - infact it's a TextArea (hope you have fished it out from the source though ;-))

Sasi,

using grabFocus(), wouldn't work always. Though the focus stays intact in the TextArea, the caret is invisible.

Swaraj,

could you pls clarify me about :

showAttributes(e.getDot());

This event fires on every change in caret position. Do you want me to move my code in windowActivated to here ? i.e. create a DefaultCaret each time ... i hope you don't mean so.

What am i supposed to do in the caretUpdate() ?

Thanx for your time.

vsriram6 at 2007-7-4 21:27:02 > top of Java-index,Archived Forums,Swing...
# 6

hi,

i have found the solution for Cursor/Caret.

all i need to do is to put the defaultCaret.setVisible(true) in formActivated() at the end of method.

But now i face a new problem - when i do a select of text in TextArea, the selectionBackground/selectionForeground doesn't change.

?

Thanx.

vsriram6 at 2007-7-4 21:27:02 > top of Java-index,Archived Forums,Swing...
# 7
hi, FYI.I have found the solution - just set setSelectionVisible(true) to the new Caret created.:-))
vsriram6 at 2007-7-4 21:27:02 > top of Java-index,Archived Forums,Swing...
# 8
also set it at the last.for the caret do -newCaret.setSelection(true);
vsriram_6 at 2007-7-4 21:27:02 > top of Java-index,Archived Forums,Swing...