Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's wrong with my Dialog box and how should I change arraylist from different class and on separate .java file?
    primarykey
    data
    text
    <p>I am trying to write a GUI program that will import a text file, read its content and put them into ArrayList. </p> <p>I write the panel in a separate .java file that contain the import button and perform the import function, while the Arraylist is on the different .java file (main method ones). </p> <p>How should I make ActionListener to read and import file contents into ArrayList from different class/.java file?</p> <p>My import dialog box alos causes error, what's wrong with it? The code works fine in CLI but not in GUI program.</p> <p><strong>This is the main method of GUI Frame, it will add the panel from different .java file:</strong></p> <pre><code>public class FrameGUI { public static void main(String[] args) throws Exception { //Creates ArrayList and initiating JFrame ArrayList&lt;String&gt; stringFile = new ArrayList&lt;String&gt;(); JFrame frame = new JFrame("Title"); *//Rest of the following codes here* } //---End of Main Method--- } ---End of the First .java file </code></pre> <p><strong>This is the second .java file, which contains panel with import button and performing the action, read the contents of .txt file and import them into arraylist:</strong></p> <pre><code>public class ImportPanel extends JPanel { private JButton importButton; public ImportPanel(ArrayList&lt;String&gt; StringFile) { //Setting up JButton and link it to inner class of //ActionListener importButton = new JButton("Import Data"); importButton.addActionListener(new importListener()); *//***Adding the button into panel, set background color and panel size* } //---End of main method--- //Private inner class: private class importListener implements ActionListener { public void actionPerformed (ActionEvent event) { //---Import Function--- //The following statement will ask the user for input first //BEFORE it displays title and JFrame //Setting the file type, showing the dialog box: JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("TEXT FILES", "txt","text"); //only allows user to import .txt files chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(null); //Execute the following and import data //if the file type match: Scanner fileScan; String stringAdd; if (returnVal == JFileChooser.APPROVE_OPTION) { File inputFile = chooser.getSelectedFile(); fileScan = new Scanner(inputFile); //Scan the first line as title: //-disable-frame = new JFrame (fileScan.nextLine()); //Parsing the data into Arraylist while (fileScan.hasNext()) { stringAdd = fileScan.nextLine(); StringFile.add(new String(stringAdd)); } } //End of the if statement- } } //---End of private inner class--- } //End of the second .java file </code></pre> <p>I have tried to search before asking for question, I know that in order to change variable on different class, we need to add something called reference. But I don't know how to add and where to add in this case, when the classes are on two different .java file, rather than one.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload