Reputation: 207
I am facing a problem when setting new text in JEditorPane
in a subclass Index
that extends JFrame
.
package gui;
...
public class Index extends JFrame {
JEditorPane editorPaneMR = new JEditorPane();
public static void main(String[] args) {
...
}
public Index() {
JButton SearchButton = new JButton("OK");
SearchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
parser GooBlog = new parser(url);
try {
GooBlog.hello(); // Go to subclass parser
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
And this the code of subclass called parser
package gui;
public class parser extends Index{
String url;
public parser (String urlInput){
this.url = urlInput;
}
public void hello () throws IOException{
editorPaneMR.setText("Hello World");
}
}
The problem is when I pressed OK button it doesn't show me the text "Hello world" in JEditorPane! and it doesn't show me any error, just nothing happened.
Upvotes: 0
Views: 442
Reputation: 39207
The code line
parser GooBlog = new parser(url);
instantiates not only a parser but also a new Index
/JFrame
. The JEditorPane
of this newly created JFrame
is used inside method hello
and since the frame is not visible, nothing will happen.
A solution might be to provide a reference to your JFrame
or JEditorPane
into method hello
, e.g.
public class Parser { // does no longer extend Index
String url;
public Parser(String urlInput) {
this.url = urlInput;
}
public void hello(JEditorPane editorPane) { // has argument now
editorPane.setText("Hello World");
}
}
which will then be called via
Parser gooBlog = new Parser(url);
gooBlog.hello(Index.this.editorPaneMR);
Note: Please stick to common Java coding standards and use upper case names for classes, i.e. Parser
instead of parser
, and lower case variable/field/method names, e.g. gooBlog
.
Upvotes: 1