title: Java GUI refresh results after opening file
body: <p>I am trying to make an Income Tax Calculator and i have a <code>MainWindow</code> with a menu bar to open a file. After i open with <code>JFileChooser</code> and parse the file i put the results in a <code>Taxpayer</code> class. The problem is that is read the file but i can't make the results appear to the <code>MainWindow</code>. I somehow need to get back to the <code>MainWindow</code> and refresh the results after opening the file. So, basically what i need to do is to refresh the already opened <code>MainWindow</code>(which has empty JFormattedTextFields) with the appropriate infos to the <code>JFormattedTextField</code>. </p>
  
  <p>Here's the code for the <code>MainWindow</code>: </p>
  
  <pre><code>package gui;
  
  import java.awt.EventQueue;
  
  import javax.swing.JFrame;
  
  import javax.swing.JMenuBar;
  import javax.swing.JMenuItem;
  import javax.swing.JMenu;
  
  import javax.swing.JLabel;
  
  import java.awt.Font;
  
  import javax.swing.JList;
  import javax.swing.JButton;
  import javax.swing.JSeparator;
  import javax.swing.JComboBox;
  import javax.swing.JTextPane;
  import javax.swing.JFormattedTextField;
  
  import java.awt.event.ActionListener;
  import java.awt.event.ActionEvent;
  import java.util.ArrayList;
  
  import javax.swing.JCheckBox;
  import javax.swing.text.NumberFormatter;
  
  import datamodel.Taxpayer;
  
  public class MainWindow extends JFrame{
  
      private JFrame frame;
      private Taxpayer taxpayer;
  
  
      /**
       * Launch the application.
       */
      public static void main(String[] args) {
          EventQueue.invokeLater(new Runnable() {
              public void run() {
                  try {
                      MainWindow window = new MainWindow();
                      window.frame.setVisible(true);                  
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
          });
      }
  
      /**
       * Create the application.
       */
      public MainWindow() {
          initialize();
      }
  
      /**
       * Initialize the contents of the frame.
       */
      private void initialize() {
          frame = new JFrame();
          frame.setBounds(100, 100, 590, 524);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setTitle("Income Tax Calculator");
  
          JMenuBar menuBar = new JMenuBar();
          frame.setJMenuBar(menuBar);
  
          JMenu menuFile = new JMenu("File");
          menuBar.add(menuFile);
  
          JMenuItem menuOpenFile = new JMenuItem("Open File");
          menuOpenFile.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent arg0) {
                  FileChooser chooser = new FileChooser();
                  chooser.setVisible(true);       
  
              }
          });
          menuFile.add(menuOpenFile);
  
          JMenuItem menuExit = new JMenuItem("Exit");
          menuExit.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent arg0) {
                  frame.dispose();
              }
          });
          menuFile.add(menuExit);
  
          JMenu menuOptions = new JMenu("Options");
          menuBar.add(menuOptions);
  
          JMenu menuTaxpayer = new JMenu("Taxpayer");
          menuOptions.add(menuTaxpayer);
  
          JMenuItem menuItemDeleteTaxpayer = new JMenuItem("Delete");
          menuItemDeleteTaxpayer.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent arg0) {
                  TaxpayerDeletion deletedTaxpayer = new TaxpayerDeletion();
                  deletedTaxpayer.setVisible(true);
              }
          });
          menuTaxpayer.add(menuItemDeleteTaxpayer);
  
          JMenu menuReceipt = new JMenu("Receipt");
          menuOptions.add(menuReceipt);
  
          JMenuItem menuItemAdd = new JMenuItem("Add");
          menuItemAdd.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent arg0) {
                  ReceiptAdding receipt = new ReceiptAdding();
                  receipt.setVisible(true);
              }
          });
          menuReceipt.add(menuItemAdd);
  
          JMenuItem menuItemDeleteReceipt = new JMenuItem("Delete");
          menuItemDeleteReceipt.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent arg0) {
                  ReceiptDeletion deletedReceipt = new ReceiptDeletion();
                  deletedReceipt.setVisible(true);
              }
          });
          menuReceipt.add(menuItemDeleteReceipt);
  
          JMenu menuHelp = new JMenu("Help");
          menuBar.add(menuHelp);
  
          JMenuItem menuItemHelp = new JMenuItem("Help");
          menuHelp.add(menuItemHelp);
  
          JMenuItem menuItemAbout = new JMenuItem("About");
          menuHelp.add(menuItemAbout);
          frame.getContentPane().setLayout(null);
  
          JLabel labelSelectTaxpayer = new JLabel("Please select a taxpayer from the list:");
          labelSelectTaxpayer.setFont(new Font("Tahoma", Font.PLAIN, 12));
          labelSelectTaxpayer.setBounds(25, 21, 211, 23);
          frame.getContentPane().add(labelSelectTaxpayer);
  
          JLabel labelPersonalDetails = new JLabel("Personal Details");
          labelPersonalDetails.setFont(new Font("Tahoma", Font.BOLD, 14));
          labelPersonalDetails.setBounds(238, 69, 211, 14);
          frame.getContentPane().add(labelPersonalDetails);
  
          JLabel labelName = new JLabel("Name:");
          labelName.setFont(new Font("Tahoma", Font.PLAIN, 12));
          labelName.setBounds(56, 105, 46, 14);
          frame.getContentPane().add(labelName);
  
          JLabel labelAfm = new JLabel("AFM:");
          labelAfm.setFont(new Font("Tahoma", Font.PLAIN, 12));
          labelAfm.setBounds(56, 130, 46, 14);
          frame.getContentPane().add(labelAfm);
  
          JLabel labelStatus = new JLabel("Status:");
          labelStatus.setFont(new Font("Tahoma", Font.PLAIN, 12));
          labelStatus.setBounds(56, 155, 46, 14);
          frame.getContentPane().add(labelStatus);
  
          JLabel labelIncome = new JLabel("Income:");
          labelIncome.setFont(new Font("Tahoma", Font.PLAIN, 12));
          labelIncome.setBounds(56, 180, 59, 14);
          frame.getContentPane().add(labelIncome);
  
          JLabel labelReceipts = new JLabel("Receipts:");
          labelReceipts.setFont(new Font("Tahoma", Font.PLAIN, 12));
          labelReceipts.setBounds(56, 205, 59, 23);
          frame.getContentPane().add(labelReceipts);
  
          JButton buttonCalculate = new JButton("Calculate");
          buttonCalculate.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent arg0) {
                  Report report = new Report();
                  report.setVisible(true);
              }
          });
          buttonCalculate.setFont(new Font("Tahoma", Font.BOLD, 14));
          buttonCalculate.setBounds(429, 425, 135, 31);
          frame.getContentPane().add(buttonCalculate);
  
          JSeparator separatorLeftDetails = new JSeparator();
          separatorLeftDetails.setBounds(10, 81, 218, 2);
          frame.getContentPane().add(separatorLeftDetails);
  
          JSeparator separatorRightDetails = new JSeparator();
          separatorRightDetails.setBounds(346, 81, 219, 2);
          frame.getContentPane().add(separatorRightDetails);
  
          JComboBox comboBoxTaxpayerList = new JComboBox();
          comboBoxTaxpayerList.setBounds(246, 23, 319, 20);
          frame.getContentPane().add(comboBoxTaxpayerList);
  
          JComboBox comboBoxReceiptsList = new JComboBox();
          comboBoxReceiptsList.setBounds(143, 207, 357, 20);
          frame.getContentPane().add(comboBoxReceiptsList);
  
  
          JFormattedTextField formattedTextFieldAfm = new JFormattedTextField();
          formattedTextFieldAfm.setEditable(false);
          formattedTextFieldAfm.setEnabled(true);
          if(taxpayer != null){
              System.out.println("TAXPAYER NOT NULL");
              formattedTextFieldAfm.setText(String.valueOf(taxpayer.getTaxRegistrationNumber()));
              formattedTextFieldAfm.setVisible(true);
          }
  
          formattedTextFieldAfm.setBounds(143, 128, 357, 20);
          frame.getContentPane().add(formattedTextFieldAfm);
  
          JFormattedTextField formattedTextFieldStatus = new JFormattedTextField();
          formattedTextFieldStatus.setEditable(false);
          formattedTextFieldStatus.setEnabled(true);
          if(taxpayer != null){
              formattedTextFieldStatus.setText(taxpayer.getStatus());
          }
  
          formattedTextFieldStatus.setBounds(143, 153, 357, 20);
          frame.getContentPane().add(formattedTextFieldStatus);
  
          JFormattedTextField formattedTextFieldIncome = new JFormattedTextField();
          formattedTextFieldIncome.setEditable(false);
          formattedTextFieldIncome.setEnabled(true);
          //formattedTextFieldIncome.setText(String.valueOf(taxpayer.getIncome()));
          formattedTextFieldIncome.setBounds(143, 178, 357, 20);
          frame.getContentPane().add(formattedTextFieldIncome);
  
          JFormattedTextField formattedTextFieldName = new JFormattedTextField();
          formattedTextFieldName.setEditable(false);
          formattedTextFieldName.setEnabled(true);
          //formattedTextFieldName.setText(taxpayer.getName());
          formattedTextFieldName.setBounds(143, 103, 357, 20);
          frame.getContentPane().add(formattedTextFieldName);
  
          JSeparator separatorLeftCalculation = new JSeparator();
          separatorLeftCalculation.setBounds(10, 253, 218, 2);
          frame.getContentPane().add(separatorLeftCalculation);
  
          JLabel lblTaxCalculation = new JLabel("Tax Calculation");
          lblTaxCalculation.setFont(new Font("Tahoma", Font.BOLD, 14));
          lblTaxCalculation.setBounds(233, 227, 200, 50);
          frame.getContentPane().add(lblTaxCalculation);
  
          JSeparator separatorReightCalculation = new JSeparator();
          separatorReightCalculation.setBounds(340, 253, 225, 2);
          frame.getContentPane().add(separatorReightCalculation);
  
          JLabel lblPleaseSelectOne = new JLabel("Please select one or more options for tax calculation");
          lblPleaseSelectOne.setFont(new Font("Tahoma", Font.PLAIN, 12));
          lblPleaseSelectOne.setBounds(155, 266, 505, 23);
          frame.getContentPane().add(lblPleaseSelectOne);
  
          JCheckBox checkBoxTotalTax = new JCheckBox("Total Tax");
          checkBoxTotalTax.setBounds(93, 306, 150, 23);
          frame.getContentPane().add(checkBoxTotalTax);
  
          JCheckBox chechBoxTaxAnalysis = new JCheckBox("Tax analysis(Basic tax, tax increase or decrease based on receipts)");
          chechBoxTaxAnalysis.setBounds(92, 332, 357, 23);
          frame.getContentPane().add(chechBoxTaxAnalysis);
  
          JCheckBox checkBoxReceiptsTotalValue = new JCheckBox("Total value of the gathered receipts");
          checkBoxReceiptsTotalValue.setBounds(93, 358, 271, 23);
          frame.getContentPane().add(checkBoxReceiptsTotalValue);
  
          JCheckBox checkBoxAmountDifferentCategories = new JCheckBox("Analysis of the amounts spent for different categories of expenditure");
          checkBoxAmountDifferentCategories.setBounds(93, 384, 421, 23);
          frame.getContentPane().add(checkBoxAmountDifferentCategories);
  
  
      }
  
      public void setTaxpayerInfo(Taxpayer taxpayer){
          this.taxpayer = taxpayer;
          System.out.println("Test message :Inside");
      }
  }
  </code></pre>
  
  <p>And this is the code for <code>FileChooser</code>:</p>
  
  <pre><code>package gui;
  
  
  import input.TxtParser;
  
  import java.awt.EventQueue;
  
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.border.EmptyBorder;
  
  
  
  
  
  
  
  
  import datamodel.Taxpayer;
  
  import java.awt.event.ActionListener;
  import java.awt.event.ActionEvent;
  import java.io.File;
  import java.io.FileNotFoundException;
  
  public class FileChooser extends JFrame {
  
      /**
       * 
       */
      private static final long serialVersionUID = 1L;
      private JPanel contentPane;
      private Taxpayer taxpayer;
  
      /**
       * Launch the application.
       */
      public static void main(String[] args) {
          EventQueue.invokeLater(new Runnable() {
              public void run() {
                  try {
                      FileChooser frame = new FileChooser();
                      frame.setVisible(true);
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
          });
      }
  
      /**
       * Create the frame.
       */
      public FileChooser() {
          setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          setBounds(100, 100, 593, 436);
          contentPane = new JPanel();
          contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
          setContentPane(contentPane);
          contentPane.setLayout(null);
  
          javax.swing.JFileChooser fileChooser = new javax.swing.JFileChooser();
          fileChooser.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  if(e.getActionCommand()!= null){
                      File file = fileChooser.getSelectedFile();
                      TxtParser txtParser = new TxtParser();
  
                          try {
                              taxpayer = txtParser.parse(file);
                              if(taxpayer == null){
                                  System.out.println("NULL TAXPAYER");
                              }
                              System.out.println(taxpayer);
                          } catch (FileNotFoundException e1) {
                              // TODO Auto-generated catch block
                              e1.printStackTrace();
                          }
  
                      MainWindow mainWindow = new MainWindow();
                      mainWindow.setTaxpayerInfo(taxpayer);
                      mainWindow.setEnabled(true);
                      mainWindow.setVisible(true);
  
                      close();
                  }
              }
          });
          fileChooser.setBounds(0, 0, 582, 397);
          contentPane.add(fileChooser);
          this.setTitle("Income Tax Calculator");
      }
  
  
  
      private void close(){
          this.dispose();
      }
  }
  </code></pre>