-1

in netbeans I've got a JFrame and a JavaClass. In my JFrame I have a combobox to select a file that will be used in the operations within the Java class.

Java class:

public class WekaTest {
    public static BufferedReader readDataFile(String filename) {
        BufferedReader inputReader = null;

        try {
            inputReader = new BufferedReader(new FileReader(filename));
        } catch (FileNotFoundException ex) {
            System.err.println("Ficheiro " + filename + " não encontrado");
        }

        return inputReader;
    }

(...)

    public static void main(String[] args) throws Exception {

        JFrame1 form = new JFrame1();
        form.setVisible(true);

        BufferedReader datafile = readDataFile("weather.nominal.arff");

        Instances data = new Instances(datafile);
        data.setClassIndex(data.numAttributes() - 1);

        (...)

    }

}

What I need is, from the JFrame's combobox, to select a different datafile to read from. So, as I change the selected item in my combobox, I want to set my datafile as that value.

Here's the JFrame code:

public class JFrame1 extends javax.swing.JFrame {

    public JFrame1() {
        initComponents();
    }

   (...)                       

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        jTextField1.setText(arffComboBox.getSelectedItem().toString());;

    }                                        

    private void arffComboBoxActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
    }                                            

(...)               
}

How can I do this?

chiapa
  • 4,362
  • 11
  • 66
  • 106
  • What class has the BufferedReader? Pass the file name from your Swing code to this class using the constructor or a setter method. – Gilbert Le Blanc Jul 10 '13 at 16:48
  • I didn't get it. Check the post, I edited it and added the BufferedReader definition – chiapa Jul 10 '13 at 17:06
  • 2
    1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) 3) *"Thanks, Chiapa"* Don't include sigs. in questions. They are noise. – Andrew Thompson Jul 10 '13 at 17:06

1 Answers1

1

Make the following a private (or public) member:

private BufferedReader datafile = null;

Then do the read within the action listener you've assigned to the combobox:

private void arffComboBoxActionPerformed(java.awt.event.ActionEvent evt) {                                             
    String pth = arffComboBox.getSelectedItem();
    datafile = readDataFile(pth);
}

Then you can use datafile either in the listener or elsewhere as necessary.

Something like that should do what you're after.

EDIT

Given the new information, you're probably going to do best with a PropertyChangeListener that subscribes to the JFrame1 (form.addPropertyChangeListener) object and listens to PropertyChangeEvents that you fire from within your arffComboBoxActionPerformed method.

In the arffComboBoxActionPerformed:

private void arffComboBoxActionPerformed(java.awt.event.ActionEvent evt) {                                             
    String pth = arffComboBox.getSelectedItem();
    firePropertyChange('combo_changed', null, pth);
}

Then in the main:

JFrame1 form = new JFrame1();
form.setVisible(true);
form.addPropertyChangeListener(new PropertyChangeListener() {

    @Override
    public void propertyChange(PropertyChangeEvent pce) {
        // Handle the change here

        String pth = (String) pce.getNewValue();
        BufferedReader datafile = readDataFile(pth);

        Instances data = new Instances(datafile);
        data.setClassIndex(data.numAttributes() - 1);

        (...)
    }

});
blazetopher
  • 1,050
  • 9
  • 13
  • Perhaps I'm missing something - even if `readDataFile` is defined in another class, the above should still work, you'd just do `datafile = myclass.readDataFile(pth);`? – blazetopher Jul 10 '13 at 17:08
  • It doesn't work. `datafile = myclass.` and there are no options available. – chiapa Jul 10 '13 at 17:16
  • See #1 from @Andrew Thompson's comment. I'm stabbing in the dark without more information. – blazetopher Jul 10 '13 at 17:33
  • *"all the important code"* I suggested you post an ***SSCCE*** for good reasons. One of the (many) good reasons is that people with broken code often have *no idea* which parts of the code are relevant. For your own sake, please actually *read* the document before making random guesses about the meaning of SSCCE. – Andrew Thompson Jul 10 '13 at 18:37
  • In the SSCCE page you provided and I actually read, it clearly suggests people to insert only the relevant parts without code parts that they are SURE that have no influence in the problem. I followed the suggestions in what I read the best I could: I'm sure you are far more experienced than me and are trying to help but pointing fingers isn't a good motivation for learners. Furthermore, what I posted helped @blazetopher find the solution for my specific problem (and he actually helped me out instead of making me feel dumb) so, I guess my indications weren't that bad – chiapa Jul 11 '13 at 10:23