How can I react to JEditorPane text changes? - java

I'm working to build an editor. I'd like to know whether the text in JEditorPane is modified or not. How can I react to editor pane text changes?
The following code seems not work.
JEditorPane editorConfig = new JEditorPane();
editorConfig.getDocument().addDocumentListener(new DocumentListener(){
#Override
public void insertUpdate(DocumentEvent e) {
System.out.println("insertUpdate called");
Dialogc.this.refreshTitle();
}
#Override
public void removeUpdate(DocumentEvent e) {
System.out.println("removeUpdate called");
Dialogc.this.refreshTitle();
}
#Override
public void changedUpdate(DocumentEvent e) {
System.out.println("changedUpdate");
Dialogc.this.refreshTitle();
}
});

Thanks very much for your feedback; the code is right for document update. The code did not work because the document of JTextArea are changed in the coming logic.
Thanks
Klaus

Related

Java - How to "unhide" a JFrame

I am quite new to Java, but familiar with native Android dev so bear with me xD. I created an application that creates a JFrame. Then I set the closeOperation to: setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);.
It performs as expected, the frame is hidden and this is what I want (when I close). I need the application to keep on running (only once instance), because I am running a thread in the background that is performing an operation.
My actionListener on my button in my JFrame currently does this: setVisible(false);
My question is this, how can I maximize the JFrame again after it has been hidden? Would it be possible to display the frame when the user clicks on the minimized application in the task bar? Is there some type of listener that I need to implement?
Thanks in advance, any advice will be appreciated
UPDATE
For this solution to work correctly you need to do the following. Also have a look at XtremeBaumer's answer for this to make sense.
On JFrame creation setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);.
When you want to minimize the app (on click possibly) frame.setState(Frame.ICONIFIED);.
When you want to maximize the app again frame.setState(Frame.NORMAL); in windowDeiconified event.
One last thing, if you want to also minimize your app when the user clicks on the exit button (red x) add this to the windowClosing event frame.setState(Frame.ICONIFIED);.
this.addWindowListener(new WindowListener(){
#Override
public void windowActivated(WindowEvent e) {
}
#Override
public void windowClosed(WindowEvent e) {
}
#Override
public void windowClosing(WindowEvent e) {
setState(Frame.ICONIFIED)
}
#Override
public void windowDeactivated(WindowEvent e) {
}
#Override
public void windowDeiconified(WindowEvent e) {
this.setVisible(true);
//this should be what you want
}
#Override
public void windowIconified(WindowEvent e) {
}
#Override
public void windowOpened(WindowEvent e) {
}
});
i hope this solves your question. add it to your JFrame

Unable to update TextArea while typing in Text Field at same time

Hello guyz i am new to java swing and working on a project, I am unable to update the text which i was typed in text field to text area in java swing , I am using this Example as a reference , but i am making my GUI using Drag and Drop in Netbeans by using JFrame Form
Here is my code
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String s = this.jTextField1.getText();
jTextArea1.setEditable(false);
jTextField1.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent de) {
jTextArea1.setText(s);
}
#Override
public void removeUpdate(DocumentEvent de) {
jTextArea1.setText(s);
}
#Override
public void changedUpdate(DocumentEvent de) {
//Plain text components don't fire these events.
}
});
}
I am unable to do this by using Drag and drop method , whereas its working fine for me as like example which i have posted above.
Any help would be highly appreciate
Normally, we don't put a Listener inside another Listener, which makes the inner Listener to be called multiple times.
Clear out the following code.
jTextArea1.setEditable(false);
jTextField1.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent de) {
jTextArea1.setText(s);
}
#Override
public void removeUpdate(DocumentEvent de) {
jTextArea1.setText(s);
}
#Override
public void changedUpdate(DocumentEvent de) {
//Plain text components don't fire these events.
}
});
Then use your Netbeans GUI Builder to make the jTextArea1 non-editable ( in the Properties ) and also add a DocumentListener to the jTextField1 like what you did with its ActionListener.
Then update the text in the newly added methods created by the GUI Builder, which I believed to be:
public void jTextField1RemoveUpdate(DocumentEvent de)
public void jTextField1InsertUpdate(DocumentEvent de)
in each method, you make a call to jTextArea1.setText(jTextField1.getText());

Text Changed event in JTextArea? How to?

I have been trying to make a text changed event handling mechanism for my JTextArea. For my purposes an event has to be fired whenever there is a change in the text of the JTextArea. I tried using the KeyListener interface and here is my code for that.
txtArea.addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent arg0) {
}
#Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void keyPressed(KeyEvent arg0) {
currentText = text.getText();
if (currentText == textString)
JOptionPane.showMessageDialog(null, "Correct");
}
});
Nothing happened when the textarea's text matched the hardcoded text. How can an event changed event be made for this.
Can this objective be achieved with a PropertyChangedListener? If it can, then how?
I would get the JTextArea's Document via getDocument() (a PlainDocument actually) and use a DocumentListener to listen for changes. This way you'd capture changes from key strokes and also from copy/paste/cut events.
Not the JTextArea, but the contained document receives updates, so you need:
jTextArea.getDocument().addDocumentListener(new DocumentListener() {
#Override
public void removeUpdate(DocumentEvent e) {
}
#Override
public void insertUpdate(DocumentEvent e) {
}
#Override
public void changedUpdate(DocumentEvent arg0) {
}
});
You are comparing strings with ==
if (currentText == textString)
This will never be true. This compares if the strings are the same String object. You should use equals.
if (currentText.equals( textString) )
You might also want to check out DocumentListeners.
See also this How do I compare strings in Java?

JXDatePicker and selectAll()

I want a listener that autoselects the entry on the JXDatePickers editor cell when same gains focus.
DatePicker.getEditor().selectAll();
doesn´t work. So i tried this :
DatePicker.getEditor().addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
DatePicker. getEditor().selectAll();
}
});
}
public void focusLost(FocusEvent e) {
}
});
Any suggestions ?
Edit
just realized that you probably have stand-alone datepicker, and run your snippet: worksforme. So we'll need to dig for differences - what's your swingx/jdk version and OS?
Original
typically, the JFormattedTextField is tricky to convince being selected ;-) See
Combining JXTable with RXTable
and adapt the solution to handle JXDatePicker as well - by adding
if (editor instanceof JXDatePicker) {
LOG.info("got picker: " + editor);
invokeSelectAll(((JXDatePicker) editor).getEditor());
return;
}

Problem with SWT Combobox addSelectionListener()

cmbCategory.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
//Do things
}
});
The above code works just fine when I make selection using the control itself, but does not fire when I try changing the index programmatically.
Can anyone help me?
Maybe this could work:
cmbCategory.addModifyListener(new ModifyListener(){
#Override
public void modifyText(ModifyEvent event) {
//Do things
}
});

Categories