Passing a value from a JFrame to a JPanel
I have an annoying problem using JFrames and JPanels. I have a class extending a JFrame and in the contructor I have a string. I want to pass this value into the JPanel also in the contructor. I cant think how to do it. This is what I did:
public class NewFileMaker extends JFrame{
private String name;
public NewFileMaker(JPanel j, String newfilename){
setTitle("New File");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.ad开发者_C百科d(j);
this.pack();
this.name = newfilename;
}
Is there a way of passing the value "name"? I could extend JPanel and create a new class and a new method, but it would require a lot of reworking a lot of other classes.
MORE INFO: I took the advice and extended JPanel, which was actually really painless.
The NewFileMaker class is called in another class like this
new NewFileMaker(new GeneratePanel(getFileName()));
where getfileName() gets the name I wanted. Actually the solution is so simple I have to apologize to everybody. Sorry for wasting your time!
i think that the best solution would be extending jpanel. Anyway if you can't do that maybe you can add to jframe a PropertyChangeListener.
JPanel doesn't have a string constructor, so you can't pass it in that way.
You can try calling panel.setName(name)
instead.
精彩评论