Possible race between an ActionListener & a DocumentListener











up vote
0
down vote

favorite












I have a frame that has a pair of JEditorPanes components. One is editable and users can type into it. A DocumentListener attached to that editor pane the synchs the contents of this one with the other when the text is changed. That seemed to work OK.



Then I added an ActionListener attached to a button to load content from a file. I assumed that the document listener would be triggered by this updating the text and synch the data from the input editor pane but unfortunately it only partially works. It appears that not all of the content loaded into the input pane is loaded into the output pane.



I am suspecting there is some kind of race between the action listener updating the input pane and the document listener reading the input pane and updating the output pane. I am not sure if that is the case and if so how to fix it.



public class EditorFrame extends JFrame {

private static final long serialVersionUID = 7119768788946124936L;

private JFrame _parentframe;

private JEditorPane _inputpane;
private JScrollPane _inputscrollpane;
private JButton _inputloadbutton;
private JPanel _inputbuttonpanel;
private JPanel _inputpanel;

private JEditorPane _outputpane;
private JScrollPane _outputscrollpane;
private JPanel _outputpanel;

private JPanel _editorpanel;

private JButton _savebutton;
private JButton _discardbutton;

private JPanel _buttonpanel;

private JPanel _contentpanel;

public EditorFrame(JFrame pframe) {
super("Editor");

this._parentframe = pframe;

this.init();
}

public EditorFrame(JFrame pframe) {
super("Editor");

this._parentframe = pframe;

this.init();
}

private boolean init() {
Dimension editorsize = new Dimension(750,500);

this._inputpane = new JEditorPane();
this._inputpane.setContentType("text/plain");
this._inputpane.setPreferredSize(editorsize);
this._inputpane.setBorder(BorderFactory.createLineBorder(Color.black));
this._inputpane.setEditable(true);
this._inputpane.setVisible(true);
this._inputscrollpane = new JScrollPane(this._inputpane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

this._inputloadbutton = new JButton("Load Content");
EditorFrameLoadButtonActionListener loadbutton_actionlistener = new EditorFrameLoadButtonActionListener(this);
this._inputloadbutton.addActionListener(loadbutton_actionlistener);

this._inputbuttonpanel = new JPanel();
this._inputbuttonpanel.add(this._inputloadbutton);

this._inputpanel = new JPanel();
this._inputpanel.setLayout(new BoxLayout(this._inputpanel, BoxLayout.PAGE_AXIS));
this._inputpanel.add(this._inputscrollpane);
this._inputpanel.add(this._inputbuttonpanel);

this._outputpane = new JEditorPane();
this._outputpane.setContentType("text/html");
this._outputpane.setPreferredSize(editorsize);
this._outputpane.setBorder(BorderFactory.createLineBorder(Color.black));
this._outputpane.setEditable(false);
this._outputpane.setVisible(true);
HTMLEditorKit output_htmleditorkit = new HTMLEditorKit();
this._outputpane.setEditorKit(output_htmleditorkit);
this._outputscrollpane = new JScrollPane(this._outputpane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

this._outputpanel = new JPanel();
this._outputpanel.add(this._outputscrollpane);

this._editorpanel = new JPanel();
this._editorpanel.add(this._inputpanel);
this._editorpanel.add(this._outputpanel);

this._savebutton = new JButton("Save");
this._savebutton.setEnabled(false);

this._discardbutton = new JButton("Discard");
CloseButtonActionListener discardbuttonlistener = new CloseButtonActionListener(this);
this._discardbutton.addActionListener(discardbuttonlistener);

this._buttonpanel = new JPanel();
this._buttonpanel.add(this._savebutton);
this._buttonpanel.add(this._discardbutton);

this._contentpanel = new JPanel();
this._contentpanel.setLayout(new BorderLayout());
this._contentpanel.add(this._titlepanel, BorderLayout.PAGE_START);
this._contentpanel.add(this._editorpanel, BorderLayout.CENTER);
this._contentpanel.add(this._buttonpanel, BorderLayout.PAGE_END);

this.setContentPane(this._contentpanel);

EditorFrameInputPaneDocumetListener doclistener = new EditorFrameInputPaneDocumetListener(this);
this._inputpane.getDocument().addDocumentListener(doclistener);

EditorFrameWindowListener windowlistener = new EditorFrameWindowListener(this._parentframe);
this.addWindowListener(windowlistener);

this.pack();
this.setVisible(true);

return true;
}

public void setInputPane(String instr) {
this._inputpane.setText(instr);
}

public void setOutputPane(String outstr) {
this._outputpane.setText(outstr);
}

public String getInputPane() {
return this._inputpane.getText();
}

public String getOutputPane() {
return this._outputpane.getText();
}

public void syncPanes() {
String data = this._inputpane.getText();
this._outputpane.setText(data);
}

public void enableSaveButton() {
this._savebutton.setEnabled(true);
}

}




public class EditorFrameInputPaneDocumetListener implements DocumentListener{

private EditorFrame _frame;

public EditorFrameInputPaneDocumetListener(EditorFrame f) {
this._frame = f;
}

@Override
public void changedUpdate(DocumentEvent arg0) {
this.synchAction();
}

@Override
public void insertUpdate(DocumentEvent arg0) {
this.synchAction();
}

@Override
public void removeUpdate(DocumentEvent arg0) {
this.synchAction();
}

private void synchAction() {
this._frame.syncPanes();
this._frame.enableSaveButton();
}
}




public class EditorFrameLoadButtonActionListener implements ActionListener{

private EditorFrame _frame;

public EditorFrameLoadButtonActionListener(EditorFrame frame) {
this._frame = frame;
}

@Override
public void actionPerformed(ActionEvent arg0) {
JFileChooser fc;
String path;
String ext;
fc = new JFileChooser();
fc.setAcceptAllFileFilterUsed(false);
if (fc.showOpenDialog(this._frame) == JFileChooser.APPROVE_OPTION) {
path = fc.getSelectedFile().getPath();
ext = AbsConverter.getExtension(path);
switch(ext) {
case "png" :
this.loadImage(path);
break;
default :
String msg = String.format("File *.%s cannot be loaded", ext);
JOptionPane.showMessageDialog(this._frame, msg, "Load Error", JOptionPane.ERROR_MESSAGE);
}
}
}

private void loadImage(String file) {
String imageStr;
String inputStr;
imageStr = AbsConverter.imageString(file);
if (imageStr.equals("")) {
String msg = String.format("Could not load image");
JOptionPane.showMessageDialog(this._frame, msg, "Load Error", JOptionPane.ERROR_MESSAGE);
}
else {
inputStr = "";
inputStr = inputStr + "<html>n";
inputStr = inputStr + "<body>n";
inputStr = inputStr + "<img src="";
inputStr = inputStr + imageStr;
inputStr = inputStr + "" >n";
inputStr = inputStr + "</body>n";
inputStr = inputStr + "</html>n";
this._frame.setInputPane(inputStr);
//this._frame.setOutputPane(inputStr);
}
}
}









share|improve this question
























  • 1) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) Please use code formatting for code and code snippets, structured documents like HTML/XML or input/output. To do that, select the text and click the {} button at the top of the message posting/editing form.
    – Andrew Thompson
    18 hours ago















up vote
0
down vote

favorite












I have a frame that has a pair of JEditorPanes components. One is editable and users can type into it. A DocumentListener attached to that editor pane the synchs the contents of this one with the other when the text is changed. That seemed to work OK.



Then I added an ActionListener attached to a button to load content from a file. I assumed that the document listener would be triggered by this updating the text and synch the data from the input editor pane but unfortunately it only partially works. It appears that not all of the content loaded into the input pane is loaded into the output pane.



I am suspecting there is some kind of race between the action listener updating the input pane and the document listener reading the input pane and updating the output pane. I am not sure if that is the case and if so how to fix it.



public class EditorFrame extends JFrame {

private static final long serialVersionUID = 7119768788946124936L;

private JFrame _parentframe;

private JEditorPane _inputpane;
private JScrollPane _inputscrollpane;
private JButton _inputloadbutton;
private JPanel _inputbuttonpanel;
private JPanel _inputpanel;

private JEditorPane _outputpane;
private JScrollPane _outputscrollpane;
private JPanel _outputpanel;

private JPanel _editorpanel;

private JButton _savebutton;
private JButton _discardbutton;

private JPanel _buttonpanel;

private JPanel _contentpanel;

public EditorFrame(JFrame pframe) {
super("Editor");

this._parentframe = pframe;

this.init();
}

public EditorFrame(JFrame pframe) {
super("Editor");

this._parentframe = pframe;

this.init();
}

private boolean init() {
Dimension editorsize = new Dimension(750,500);

this._inputpane = new JEditorPane();
this._inputpane.setContentType("text/plain");
this._inputpane.setPreferredSize(editorsize);
this._inputpane.setBorder(BorderFactory.createLineBorder(Color.black));
this._inputpane.setEditable(true);
this._inputpane.setVisible(true);
this._inputscrollpane = new JScrollPane(this._inputpane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

this._inputloadbutton = new JButton("Load Content");
EditorFrameLoadButtonActionListener loadbutton_actionlistener = new EditorFrameLoadButtonActionListener(this);
this._inputloadbutton.addActionListener(loadbutton_actionlistener);

this._inputbuttonpanel = new JPanel();
this._inputbuttonpanel.add(this._inputloadbutton);

this._inputpanel = new JPanel();
this._inputpanel.setLayout(new BoxLayout(this._inputpanel, BoxLayout.PAGE_AXIS));
this._inputpanel.add(this._inputscrollpane);
this._inputpanel.add(this._inputbuttonpanel);

this._outputpane = new JEditorPane();
this._outputpane.setContentType("text/html");
this._outputpane.setPreferredSize(editorsize);
this._outputpane.setBorder(BorderFactory.createLineBorder(Color.black));
this._outputpane.setEditable(false);
this._outputpane.setVisible(true);
HTMLEditorKit output_htmleditorkit = new HTMLEditorKit();
this._outputpane.setEditorKit(output_htmleditorkit);
this._outputscrollpane = new JScrollPane(this._outputpane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

this._outputpanel = new JPanel();
this._outputpanel.add(this._outputscrollpane);

this._editorpanel = new JPanel();
this._editorpanel.add(this._inputpanel);
this._editorpanel.add(this._outputpanel);

this._savebutton = new JButton("Save");
this._savebutton.setEnabled(false);

this._discardbutton = new JButton("Discard");
CloseButtonActionListener discardbuttonlistener = new CloseButtonActionListener(this);
this._discardbutton.addActionListener(discardbuttonlistener);

this._buttonpanel = new JPanel();
this._buttonpanel.add(this._savebutton);
this._buttonpanel.add(this._discardbutton);

this._contentpanel = new JPanel();
this._contentpanel.setLayout(new BorderLayout());
this._contentpanel.add(this._titlepanel, BorderLayout.PAGE_START);
this._contentpanel.add(this._editorpanel, BorderLayout.CENTER);
this._contentpanel.add(this._buttonpanel, BorderLayout.PAGE_END);

this.setContentPane(this._contentpanel);

EditorFrameInputPaneDocumetListener doclistener = new EditorFrameInputPaneDocumetListener(this);
this._inputpane.getDocument().addDocumentListener(doclistener);

EditorFrameWindowListener windowlistener = new EditorFrameWindowListener(this._parentframe);
this.addWindowListener(windowlistener);

this.pack();
this.setVisible(true);

return true;
}

public void setInputPane(String instr) {
this._inputpane.setText(instr);
}

public void setOutputPane(String outstr) {
this._outputpane.setText(outstr);
}

public String getInputPane() {
return this._inputpane.getText();
}

public String getOutputPane() {
return this._outputpane.getText();
}

public void syncPanes() {
String data = this._inputpane.getText();
this._outputpane.setText(data);
}

public void enableSaveButton() {
this._savebutton.setEnabled(true);
}

}




public class EditorFrameInputPaneDocumetListener implements DocumentListener{

private EditorFrame _frame;

public EditorFrameInputPaneDocumetListener(EditorFrame f) {
this._frame = f;
}

@Override
public void changedUpdate(DocumentEvent arg0) {
this.synchAction();
}

@Override
public void insertUpdate(DocumentEvent arg0) {
this.synchAction();
}

@Override
public void removeUpdate(DocumentEvent arg0) {
this.synchAction();
}

private void synchAction() {
this._frame.syncPanes();
this._frame.enableSaveButton();
}
}




public class EditorFrameLoadButtonActionListener implements ActionListener{

private EditorFrame _frame;

public EditorFrameLoadButtonActionListener(EditorFrame frame) {
this._frame = frame;
}

@Override
public void actionPerformed(ActionEvent arg0) {
JFileChooser fc;
String path;
String ext;
fc = new JFileChooser();
fc.setAcceptAllFileFilterUsed(false);
if (fc.showOpenDialog(this._frame) == JFileChooser.APPROVE_OPTION) {
path = fc.getSelectedFile().getPath();
ext = AbsConverter.getExtension(path);
switch(ext) {
case "png" :
this.loadImage(path);
break;
default :
String msg = String.format("File *.%s cannot be loaded", ext);
JOptionPane.showMessageDialog(this._frame, msg, "Load Error", JOptionPane.ERROR_MESSAGE);
}
}
}

private void loadImage(String file) {
String imageStr;
String inputStr;
imageStr = AbsConverter.imageString(file);
if (imageStr.equals("")) {
String msg = String.format("Could not load image");
JOptionPane.showMessageDialog(this._frame, msg, "Load Error", JOptionPane.ERROR_MESSAGE);
}
else {
inputStr = "";
inputStr = inputStr + "<html>n";
inputStr = inputStr + "<body>n";
inputStr = inputStr + "<img src="";
inputStr = inputStr + imageStr;
inputStr = inputStr + "" >n";
inputStr = inputStr + "</body>n";
inputStr = inputStr + "</html>n";
this._frame.setInputPane(inputStr);
//this._frame.setOutputPane(inputStr);
}
}
}









share|improve this question
























  • 1) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) Please use code formatting for code and code snippets, structured documents like HTML/XML or input/output. To do that, select the text and click the {} button at the top of the message posting/editing form.
    – Andrew Thompson
    18 hours ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a frame that has a pair of JEditorPanes components. One is editable and users can type into it. A DocumentListener attached to that editor pane the synchs the contents of this one with the other when the text is changed. That seemed to work OK.



Then I added an ActionListener attached to a button to load content from a file. I assumed that the document listener would be triggered by this updating the text and synch the data from the input editor pane but unfortunately it only partially works. It appears that not all of the content loaded into the input pane is loaded into the output pane.



I am suspecting there is some kind of race between the action listener updating the input pane and the document listener reading the input pane and updating the output pane. I am not sure if that is the case and if so how to fix it.



public class EditorFrame extends JFrame {

private static final long serialVersionUID = 7119768788946124936L;

private JFrame _parentframe;

private JEditorPane _inputpane;
private JScrollPane _inputscrollpane;
private JButton _inputloadbutton;
private JPanel _inputbuttonpanel;
private JPanel _inputpanel;

private JEditorPane _outputpane;
private JScrollPane _outputscrollpane;
private JPanel _outputpanel;

private JPanel _editorpanel;

private JButton _savebutton;
private JButton _discardbutton;

private JPanel _buttonpanel;

private JPanel _contentpanel;

public EditorFrame(JFrame pframe) {
super("Editor");

this._parentframe = pframe;

this.init();
}

public EditorFrame(JFrame pframe) {
super("Editor");

this._parentframe = pframe;

this.init();
}

private boolean init() {
Dimension editorsize = new Dimension(750,500);

this._inputpane = new JEditorPane();
this._inputpane.setContentType("text/plain");
this._inputpane.setPreferredSize(editorsize);
this._inputpane.setBorder(BorderFactory.createLineBorder(Color.black));
this._inputpane.setEditable(true);
this._inputpane.setVisible(true);
this._inputscrollpane = new JScrollPane(this._inputpane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

this._inputloadbutton = new JButton("Load Content");
EditorFrameLoadButtonActionListener loadbutton_actionlistener = new EditorFrameLoadButtonActionListener(this);
this._inputloadbutton.addActionListener(loadbutton_actionlistener);

this._inputbuttonpanel = new JPanel();
this._inputbuttonpanel.add(this._inputloadbutton);

this._inputpanel = new JPanel();
this._inputpanel.setLayout(new BoxLayout(this._inputpanel, BoxLayout.PAGE_AXIS));
this._inputpanel.add(this._inputscrollpane);
this._inputpanel.add(this._inputbuttonpanel);

this._outputpane = new JEditorPane();
this._outputpane.setContentType("text/html");
this._outputpane.setPreferredSize(editorsize);
this._outputpane.setBorder(BorderFactory.createLineBorder(Color.black));
this._outputpane.setEditable(false);
this._outputpane.setVisible(true);
HTMLEditorKit output_htmleditorkit = new HTMLEditorKit();
this._outputpane.setEditorKit(output_htmleditorkit);
this._outputscrollpane = new JScrollPane(this._outputpane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

this._outputpanel = new JPanel();
this._outputpanel.add(this._outputscrollpane);

this._editorpanel = new JPanel();
this._editorpanel.add(this._inputpanel);
this._editorpanel.add(this._outputpanel);

this._savebutton = new JButton("Save");
this._savebutton.setEnabled(false);

this._discardbutton = new JButton("Discard");
CloseButtonActionListener discardbuttonlistener = new CloseButtonActionListener(this);
this._discardbutton.addActionListener(discardbuttonlistener);

this._buttonpanel = new JPanel();
this._buttonpanel.add(this._savebutton);
this._buttonpanel.add(this._discardbutton);

this._contentpanel = new JPanel();
this._contentpanel.setLayout(new BorderLayout());
this._contentpanel.add(this._titlepanel, BorderLayout.PAGE_START);
this._contentpanel.add(this._editorpanel, BorderLayout.CENTER);
this._contentpanel.add(this._buttonpanel, BorderLayout.PAGE_END);

this.setContentPane(this._contentpanel);

EditorFrameInputPaneDocumetListener doclistener = new EditorFrameInputPaneDocumetListener(this);
this._inputpane.getDocument().addDocumentListener(doclistener);

EditorFrameWindowListener windowlistener = new EditorFrameWindowListener(this._parentframe);
this.addWindowListener(windowlistener);

this.pack();
this.setVisible(true);

return true;
}

public void setInputPane(String instr) {
this._inputpane.setText(instr);
}

public void setOutputPane(String outstr) {
this._outputpane.setText(outstr);
}

public String getInputPane() {
return this._inputpane.getText();
}

public String getOutputPane() {
return this._outputpane.getText();
}

public void syncPanes() {
String data = this._inputpane.getText();
this._outputpane.setText(data);
}

public void enableSaveButton() {
this._savebutton.setEnabled(true);
}

}




public class EditorFrameInputPaneDocumetListener implements DocumentListener{

private EditorFrame _frame;

public EditorFrameInputPaneDocumetListener(EditorFrame f) {
this._frame = f;
}

@Override
public void changedUpdate(DocumentEvent arg0) {
this.synchAction();
}

@Override
public void insertUpdate(DocumentEvent arg0) {
this.synchAction();
}

@Override
public void removeUpdate(DocumentEvent arg0) {
this.synchAction();
}

private void synchAction() {
this._frame.syncPanes();
this._frame.enableSaveButton();
}
}




public class EditorFrameLoadButtonActionListener implements ActionListener{

private EditorFrame _frame;

public EditorFrameLoadButtonActionListener(EditorFrame frame) {
this._frame = frame;
}

@Override
public void actionPerformed(ActionEvent arg0) {
JFileChooser fc;
String path;
String ext;
fc = new JFileChooser();
fc.setAcceptAllFileFilterUsed(false);
if (fc.showOpenDialog(this._frame) == JFileChooser.APPROVE_OPTION) {
path = fc.getSelectedFile().getPath();
ext = AbsConverter.getExtension(path);
switch(ext) {
case "png" :
this.loadImage(path);
break;
default :
String msg = String.format("File *.%s cannot be loaded", ext);
JOptionPane.showMessageDialog(this._frame, msg, "Load Error", JOptionPane.ERROR_MESSAGE);
}
}
}

private void loadImage(String file) {
String imageStr;
String inputStr;
imageStr = AbsConverter.imageString(file);
if (imageStr.equals("")) {
String msg = String.format("Could not load image");
JOptionPane.showMessageDialog(this._frame, msg, "Load Error", JOptionPane.ERROR_MESSAGE);
}
else {
inputStr = "";
inputStr = inputStr + "<html>n";
inputStr = inputStr + "<body>n";
inputStr = inputStr + "<img src="";
inputStr = inputStr + imageStr;
inputStr = inputStr + "" >n";
inputStr = inputStr + "</body>n";
inputStr = inputStr + "</html>n";
this._frame.setInputPane(inputStr);
//this._frame.setOutputPane(inputStr);
}
}
}









share|improve this question















I have a frame that has a pair of JEditorPanes components. One is editable and users can type into it. A DocumentListener attached to that editor pane the synchs the contents of this one with the other when the text is changed. That seemed to work OK.



Then I added an ActionListener attached to a button to load content from a file. I assumed that the document listener would be triggered by this updating the text and synch the data from the input editor pane but unfortunately it only partially works. It appears that not all of the content loaded into the input pane is loaded into the output pane.



I am suspecting there is some kind of race between the action listener updating the input pane and the document listener reading the input pane and updating the output pane. I am not sure if that is the case and if so how to fix it.



public class EditorFrame extends JFrame {

private static final long serialVersionUID = 7119768788946124936L;

private JFrame _parentframe;

private JEditorPane _inputpane;
private JScrollPane _inputscrollpane;
private JButton _inputloadbutton;
private JPanel _inputbuttonpanel;
private JPanel _inputpanel;

private JEditorPane _outputpane;
private JScrollPane _outputscrollpane;
private JPanel _outputpanel;

private JPanel _editorpanel;

private JButton _savebutton;
private JButton _discardbutton;

private JPanel _buttonpanel;

private JPanel _contentpanel;

public EditorFrame(JFrame pframe) {
super("Editor");

this._parentframe = pframe;

this.init();
}

public EditorFrame(JFrame pframe) {
super("Editor");

this._parentframe = pframe;

this.init();
}

private boolean init() {
Dimension editorsize = new Dimension(750,500);

this._inputpane = new JEditorPane();
this._inputpane.setContentType("text/plain");
this._inputpane.setPreferredSize(editorsize);
this._inputpane.setBorder(BorderFactory.createLineBorder(Color.black));
this._inputpane.setEditable(true);
this._inputpane.setVisible(true);
this._inputscrollpane = new JScrollPane(this._inputpane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

this._inputloadbutton = new JButton("Load Content");
EditorFrameLoadButtonActionListener loadbutton_actionlistener = new EditorFrameLoadButtonActionListener(this);
this._inputloadbutton.addActionListener(loadbutton_actionlistener);

this._inputbuttonpanel = new JPanel();
this._inputbuttonpanel.add(this._inputloadbutton);

this._inputpanel = new JPanel();
this._inputpanel.setLayout(new BoxLayout(this._inputpanel, BoxLayout.PAGE_AXIS));
this._inputpanel.add(this._inputscrollpane);
this._inputpanel.add(this._inputbuttonpanel);

this._outputpane = new JEditorPane();
this._outputpane.setContentType("text/html");
this._outputpane.setPreferredSize(editorsize);
this._outputpane.setBorder(BorderFactory.createLineBorder(Color.black));
this._outputpane.setEditable(false);
this._outputpane.setVisible(true);
HTMLEditorKit output_htmleditorkit = new HTMLEditorKit();
this._outputpane.setEditorKit(output_htmleditorkit);
this._outputscrollpane = new JScrollPane(this._outputpane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

this._outputpanel = new JPanel();
this._outputpanel.add(this._outputscrollpane);

this._editorpanel = new JPanel();
this._editorpanel.add(this._inputpanel);
this._editorpanel.add(this._outputpanel);

this._savebutton = new JButton("Save");
this._savebutton.setEnabled(false);

this._discardbutton = new JButton("Discard");
CloseButtonActionListener discardbuttonlistener = new CloseButtonActionListener(this);
this._discardbutton.addActionListener(discardbuttonlistener);

this._buttonpanel = new JPanel();
this._buttonpanel.add(this._savebutton);
this._buttonpanel.add(this._discardbutton);

this._contentpanel = new JPanel();
this._contentpanel.setLayout(new BorderLayout());
this._contentpanel.add(this._titlepanel, BorderLayout.PAGE_START);
this._contentpanel.add(this._editorpanel, BorderLayout.CENTER);
this._contentpanel.add(this._buttonpanel, BorderLayout.PAGE_END);

this.setContentPane(this._contentpanel);

EditorFrameInputPaneDocumetListener doclistener = new EditorFrameInputPaneDocumetListener(this);
this._inputpane.getDocument().addDocumentListener(doclistener);

EditorFrameWindowListener windowlistener = new EditorFrameWindowListener(this._parentframe);
this.addWindowListener(windowlistener);

this.pack();
this.setVisible(true);

return true;
}

public void setInputPane(String instr) {
this._inputpane.setText(instr);
}

public void setOutputPane(String outstr) {
this._outputpane.setText(outstr);
}

public String getInputPane() {
return this._inputpane.getText();
}

public String getOutputPane() {
return this._outputpane.getText();
}

public void syncPanes() {
String data = this._inputpane.getText();
this._outputpane.setText(data);
}

public void enableSaveButton() {
this._savebutton.setEnabled(true);
}

}




public class EditorFrameInputPaneDocumetListener implements DocumentListener{

private EditorFrame _frame;

public EditorFrameInputPaneDocumetListener(EditorFrame f) {
this._frame = f;
}

@Override
public void changedUpdate(DocumentEvent arg0) {
this.synchAction();
}

@Override
public void insertUpdate(DocumentEvent arg0) {
this.synchAction();
}

@Override
public void removeUpdate(DocumentEvent arg0) {
this.synchAction();
}

private void synchAction() {
this._frame.syncPanes();
this._frame.enableSaveButton();
}
}




public class EditorFrameLoadButtonActionListener implements ActionListener{

private EditorFrame _frame;

public EditorFrameLoadButtonActionListener(EditorFrame frame) {
this._frame = frame;
}

@Override
public void actionPerformed(ActionEvent arg0) {
JFileChooser fc;
String path;
String ext;
fc = new JFileChooser();
fc.setAcceptAllFileFilterUsed(false);
if (fc.showOpenDialog(this._frame) == JFileChooser.APPROVE_OPTION) {
path = fc.getSelectedFile().getPath();
ext = AbsConverter.getExtension(path);
switch(ext) {
case "png" :
this.loadImage(path);
break;
default :
String msg = String.format("File *.%s cannot be loaded", ext);
JOptionPane.showMessageDialog(this._frame, msg, "Load Error", JOptionPane.ERROR_MESSAGE);
}
}
}

private void loadImage(String file) {
String imageStr;
String inputStr;
imageStr = AbsConverter.imageString(file);
if (imageStr.equals("")) {
String msg = String.format("Could not load image");
JOptionPane.showMessageDialog(this._frame, msg, "Load Error", JOptionPane.ERROR_MESSAGE);
}
else {
inputStr = "";
inputStr = inputStr + "<html>n";
inputStr = inputStr + "<body>n";
inputStr = inputStr + "<img src="";
inputStr = inputStr + imageStr;
inputStr = inputStr + "" >n";
inputStr = inputStr + "</body>n";
inputStr = inputStr + "</html>n";
this._frame.setInputPane(inputStr);
//this._frame.setOutputPane(inputStr);
}
}
}






java swing actionlistener jeditorpane documentlistener






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 18 hours ago









Andrew Thompson

152k27159333




152k27159333










asked 21 hours ago









Neil Pittman

129119




129119












  • 1) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) Please use code formatting for code and code snippets, structured documents like HTML/XML or input/output. To do that, select the text and click the {} button at the top of the message posting/editing form.
    – Andrew Thompson
    18 hours ago


















  • 1) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) Please use code formatting for code and code snippets, structured documents like HTML/XML or input/output. To do that, select the text and click the {} button at the top of the message posting/editing form.
    – Andrew Thompson
    18 hours ago
















1) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) Please use code formatting for code and code snippets, structured documents like HTML/XML or input/output. To do that, select the text and click the {} button at the top of the message posting/editing form.
– Andrew Thompson
18 hours ago




1) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) Please use code formatting for code and code snippets, structured documents like HTML/XML or input/output. To do that, select the text and click the {} button at the top of the message posting/editing form.
– Andrew Thompson
18 hours ago

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53368878%2fpossible-race-between-an-actionlistener-a-documentlistener%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53368878%2fpossible-race-between-an-actionlistener-a-documentlistener%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Saint-Aignan (Tarn-et-Garonne)

Volksrepublik China

How to test boost logger output in unit testing?