0

So I'm having these classes

public class Init {
    ...
    JFrame addStream = new AddStream();
    addStream.setVisible(true);
    addStream.setLocationRelativeTo(null);
    addStream.getData(); //not working

}

public class AddStream extends javax.swing.JFrame {

    private String nameData, urlData, qualityData;

    /** Creates new form AddStream */
    public AddStream() {
        initComponents();
    }
    private void initComponents() {
    ...
    }

    private void addActionPerformed(java.awt.event.ActionEvent evt) {
        nameData = name.getText();
        urlData = url.getText();
        qualityData = quality.getSelectedItem().toString();
    }

    public String[] getData() {
        return new String[]{nameData, urlData, qualityData};
    }
}

Note the classes arent complete, just snippets.

When the user clicks on the Add button(addActionPerformed) the values get saved to local variables in the AddStream class and get returned by getData().

The problem I'm having is with addStream.getData();, I get "cannot find symbol"

Is there a way to get that data from AddStream JFrame to Init class?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
alex2005
  • 31
  • 6
  • 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) 2) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson May 14 '13 at 02:06

2 Answers2

2

Your problem can be easily fixed by changing this line:

JFrame addStream = new AddStream();

To this:

AddStream addStream = new AddStream();

What's happening in your code is that you're trying to call a method on a JFrame that doesn't exist on a JFrame, it only exists in an AddStream. Even though your JFrame is-a AddStream in this case, the compiler forbids this unless you tell the compiler that it is-a AddStream. And you do that with the code I've shown you.

Another way is to cast it in your call. Imagine you were using your code from above, you could then do this on your last line:

((AddStream) addStream).getData();
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
  • now I don't get an error, but when trying this String[] data = addStream.getData(); System.out.println(data[0]); Im getting null, maybe because the code continues without waiting the window to close and get the data... Any way to do this? – alex2005 May 14 '13 at 02:00
  • A solution I found was String[] data; do { data = addStream.getData(); } while(data[0] == null); – alex2005 May 14 '13 at 02:05
  • @alex2005: So you want to call the `Init` code only when the JFrame is closed? – Daniel Kaplan May 14 '13 at 02:09
  • no I want the init code to wait for the window to close, but I think the do while code will work... unless you know some other tricks :P – alex2005 May 14 '13 at 02:11
  • I'm not really sure what the difference between those two things are. But my guess is a `do while` is a bad idea. I think you want to use a WindowListener: http://docs.oracle.com/javase/tutorial/uiswing/events/windowlistener.html – Daniel Kaplan May 14 '13 at 02:27
0

In runtime when you do

JFrame addstream = new AddStream();

the object is viewed as a simple JFrame (using the JFrame part of class AddStream). getData() is only available for AddStream type objects. You could trick the JVM into using the assigned type

if( addstream instanceof AddStream ){
    (AddStream) addstream.getData();
} else {
    //TODO
}

this is sometime useful when switching between different implementations of the same Interface. Note that the cast is only there to pass the compiler. The runtime checks instanceof only and go aheads if the condition evaluate to true.

mahery rafara
  • 368
  • 4
  • 13