0

i've searched the interwebs for info regarding adding swing GUI to an existing java program, but i'm still a little confused as to how i should make it happen.

I've created a separate class for the form layouts, and can call/open the form window from the main class. I got the base code for the GUI off the internet...

GUI class:

package gui;

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class MainWindow extends JFrame implements ActionListener {

    private Container c; 
    private JLabel title; 
    private JLabel masterFilePath; 
    private JTextField masterFilePathInput; 
    private JLabel dataFilePath; 
    private JTextField dataFilePathInput; 
    private JButton reset;

    String def = ""; 

    public void mainWindow() {

        setTitle("Count Survey Comparison"); 
        setBounds(300, 90, 900, 600); 
        setDefaultCloseOperation(EXIT_ON_CLOSE); 
        setResizable(false); 

        c = getContentPane(); 
        c.setLayout(null); 

        title = new JLabel("Count Survey Comparison"); 
        title.setFont(new Font("Arial", Font.PLAIN, 30)); 
        title.setSize(500, 30); 
        title.setLocation(300, 30); 
        c.add(title); 

        masterFilePath = new JLabel("Master Data File Location"); 
        masterFilePath.setFont(new Font("Arial", Font.PLAIN, 20)); 
        masterFilePath.setSize(500, 20); 
        masterFilePath.setLocation(80, 100); 
        c.add(masterFilePath); 

        masterFilePathInput = new JTextField(); 
        masterFilePathInput.setFont(new Font("Arial", Font.PLAIN, 15)); 
        masterFilePathInput.setSize(200, 20); 
        masterFilePathInput.setLocation(400, 100); 
        c.add(masterFilePathInput); 

        dataFilePath = new JLabel("Count Survey Data File Location"); 
        dataFilePath.setFont(new Font("Arial", Font.PLAIN, 20)); 
        dataFilePath.setSize(500, 20); 
        dataFilePath.setLocation(80, 150); 
        c.add(dataFilePath); 

        dataFilePathInput = new JTextField(); 
        dataFilePathInput.setFont(new Font("Arial", Font.PLAIN, 15)); 
        dataFilePathInput.setSize(200, 20); 
        dataFilePathInput.setLocation(400, 150); 
        c.add(dataFilePathInput); 

        reset = new JButton("Reset"); 
        reset.setFont(new Font("Arial", Font.PLAIN, 15)); 
        reset.setSize(100, 20); 
        reset.setLocation(270, 450); 
        reset.addActionListener(this); 
        c.add(reset); 

        setVisible(true); 
    } 

    public void actionPerformed(ActionEvent e) { 


    }
}

What should be the actions that go into actionPerformed?

I can call the class from my main method, and open the form, but i'm stuck as to how to get data from the form itself, and enter them into my existing code (namely the readExcel(...) method)

Main Method:

MainWindow window = new MainWindow();
            window.mainWindow();

            ReadExcel readExcel = new ReadExcel();
            CompareAndCleanData cleanData = new CompareAndCleanData();
            WriteExcel writeExcel = new WriteExcel();

            /* Change file paths here */

            /* Based on BSTVN Data */
            System.out.println("Read Input Excel Data: In Progress...");
            List<VehicleData> vehicleDataList = readExcel.readExcel(
                    "C:\\Users\\ray.tong\\Desktop\\Count Survey Digitalisation Things\\BSTVN\\Count survey data (PSR19, TBR18).xlsx");
            System.out.println("Read Input Excel Data: Complete");

            /* Read MasterData */
            System.out.println("Read Brand Master Data: In Progress...");
            List<BrandMasterData> brandMasterDataList = readExcel
                    .readBrandMaster("C:\\Users\\ray.tong\\Desktop\\Count Survey Digitalisation Things\\TireMasterData.xlsx");
            System.out.println("Read Brand Master Data: Complete");

            System.out.println("Read Size Master Data: In Progress...");
            List<SizeMasterData> sizeMasterDataList = readExcel
                    .readSizeMaster("C:\\Users\\ray.tong\\Desktop\\Count Survey Digitalisation Things\\TireMasterData.xlsx");
            System.out.println("Read Size Master Data: Complete");

            System.out.println("Read Pattern Master Data: In Progress...");
            List<PatternMasterData> patternMasterDataList = readExcel
                    .readPatternMaster("C:\\Users\\ray.tong\\Desktop\\Count Survey Digitalisation Things\\TireMasterData.xlsx");
            System.out.println("Read Pattern Master Data: Complete");

            System.out.println("Read Vehicle Make/Model Master Data: In Progress...");
            List<VehicleMasterData> vehicleMasterDataList = readExcel
                    .readVehicleMaster("C:\\Users\\ray.tong\\Desktop\\Count Survey Digitalisation Things\\TireMasterData.xlsx");
            System.out.println("Read Vehicle Master Data: Complete");

Any help is appreciated,

Thanks in advance guys.

Ray
  • 43
  • 1
  • 8
  • 1
    "What should be the actions that go into actionPerformed?" - Don't just use code you've found on the internet but work through it to understand what it does or is meant to do. Otherwise you're on your way to a lot of trouble. That being said, have a look at `reset.addActionListener(this);`. That's where the frame being also an ActionListener is used (I'd not do it that way but that's a different story). So whatever needs to go into `actionPerformed(...)` is whatever you need the reset button to do. – Thomas Dec 10 '19 at 09:05
  • `JTextFile` has method `getText()` that should be used to get user's input (in your case the file name). But it would be better, when you don't reinvent the wheel, and use [JFileChooser](https://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html). – Sergiy Medvynskyy Dec 10 '19 at 09:06
  • @thomas, thanks for the advice. So, if i add another button to submit data, i'd have to create another method to handle that? i.e. actionSubmit()? – Ray Dec 10 '19 at 09:24
  • @SergiyMedvynskyy, ok thanks, will have a look at JFileChooser – Ray Dec 10 '19 at 09:24
  • No, you'd set a different ActionListener or Action for the submit button - that's one reason why I'd not make the frame itself the ActionListener. – Thomas Dec 10 '19 at 14:22
  • I recommend working through [Creating a GUI With JFC/Swing](https://docs.oracle.com/javase/tutorial/uiswing/index.html) – Abra Dec 13 '19 at 08:59

1 Answers1

1

I would recommend separating the gui from its control by implementing the MVC Pattern.
Have a Model class that holds all the information that the view (gui) needs.
Have a View class that uses the model to displays gui.
Have a Controller class that controls the model and view.

The following mre demonstrates the use of MVC Pattern with very simple functionality: it allows the user to select a file. Once file is selected the file name is displayed. It can be expanded to include the functionality you need.
For convenience and simplicity, the following code can be copy-pasted into one file called MVCDemo.java and run:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class MVCDemo {

    public static void main(String[] args) {
        Model model = new Model();
        View view = new View();
        new Controller(view, model);
    }
}

/*Model contains the information for the view and information from the view
 * as well as the logic.
 * The model is independent of the user interface.
 */
class Model {

    private String fileName;

    String getFileName() {
        return fileName;
    }

    void setFileName(String fileName) {
        this.fileName = fileName;
    }
}

/*View only contains the user interface part*/
class View {

    private Model model;
    private JFrame frame;
    private JButton selectFileButton;
    private JTextField textField;

    void createAndShowGui(Model model){

        this.model = model;

        frame = new JFrame("MVC Model Demo");
        textField = new JTextField(25);
        //do not use null layout
        frame.add(textField, BorderLayout.NORTH); //JFrame uses BorderLayout by default
        selectFileButton = new JButton("Select File");
        frame.add(selectFileButton, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
    }

    //may return null
    File getFile(){
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);  
        // pop up  file chooser dialog
        fileChooser.showOpenDialog(frame);
        return fileChooser.getSelectedFile();
    }

    void update() {
        textField.setText(model.getFileName());
    }

    JButton getSelectFileButton() {
        return selectFileButton;
    }
}

/* The controller controls the view and model.
 * Based on the user action, the Controller calls methods in the View and Model
 * to accomplish the requested action.
 */
class Controller implements ActionListener{

    private final Model model;
    private final View view;

    Controller(View view,Model model) {
        this.model=model;
        this.view=view;
        view.createAndShowGui(model);
        view.getSelectFileButton().addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        File file = view.getFile();
        model.setFileName(file == null  ? "No file selected" : file.getName());
        view.update(); //alternatively use have View listen to Model changes 
    }
}
c0der
  • 18,467
  • 6
  • 33
  • 65