I'm trying to open a "FileOpenDialog"
I'm trying to open a "FileOpenDialog" but am getting two err msgs:
1) openFileButton cannot be resolved or is not a field RTTCPSortSimMain.java
2) RTTCPSortSimMain cannot be resolved to a variable
Code Java:
public class OpenFileButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == this.openFileButton) { // 1
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(RTTCPSortSimMain); //2
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
// This is where a real application would open the file.
// log.append("Opening: " + file.getName() + "." + newline);
} else {
// log.append("Open command cancelled by user." + newline);
}
}
}
}
1) "openFileButton" is the name of the JButton
2) "RTTCPSortSimMain" is the name of the class this all lives in
What should I be using in these places instead?
Re: I'm trying to open a "FileOpenDialog"
Re: I'm trying to open a "FileOpenDialog"
ok, well you are saying this.openFileButton. I believe your problem is you are misinterpretting what this is referencing to. The keyword this references to the class that contains the keyword. So, when you have a nested class (such as the one I assume you have above), this is referring to the nested class, not the outer class.
Look at the code below:
Code java:
public class OuterClass
{
int var = 0;
public OuterClass()
{
System.out.println(this.var); //References OuterClass's var variable
}
public class InnerClass
{
int var = 0;
public InnerClass()
{
System.out.println(this.var); //References InnerClass's var variable
}
public class InnerInnerClass
{
int var = 0;
public InnerInnerClass()
{
System.out.println(this.var); //References InnerInnerClass's var variable
}
}
}
}
Now, if you wanted to reference OuterClass's var variable from inside the InnerClass (by using the keyword this), you would have to say:
Code java:
public class OuterClass
{
int var = 0;
public OuterClass()
{
System.out.println(this.var); //References OuterClass's var variable
}
public class InnerClass
{
int var = 0;
public InnerClass()
{
System.out.println(OuterClass.this.var); //References OuterClass's var variable
}
}
}
Does that make sense? And could that be your problem?