LSH94
LSH94

Reputation: 109

Link GUI to Main Class

I've created a java program and created the GUI for it. But I'm unable to link them together since this is my very first java program. my codes are as follows.

Main

public class FinalN {

    private static void setRoundedCorners(XSSFChart chart, boolean setVal) {
        if (chart.getCTChartSpace().getRoundedCorners() == null) chart.getCTChartSpace().addNewRoundedCorners();
        chart.getCTChartSpace().getRoundedCorners().setVal(setVal);
    }
    public static void main (String[]args) throws Exception {
        File src= new File("C:\\Users\\File.xlsx");
        FileInputStream fis= new FileInputStream (src);

        XSSFWorkbook wb = new XSSFWorkbook(fis);
        XSSFSheet sheet1= wb.getSheetAt(0);
// Continues

GUI

public class UserInterface {

    public class java {

    }

    private JFrame frame;
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {

I've tried to get an idea from this post How to load a Java GUI class from a main class? but I'm unable to complete the task. Please help

Upvotes: 2

Views: 1807

Answers (1)

Hari Kiran
Hari Kiran

Reputation: 208

You just need to instantiate the GUI class in the FinalN class and add the code inside the constructor of GUI.

Code for main() function:

public static void main (String[]args) throws Exception {
File src= new File("C:\\Users\\File.xlsx");
FileInputStream fis= new FileInputStream (src);
UserInterface gui = new UserInterface();
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet1= wb.getSheetAt(0);
}

Code for Constructor to create a basic GUI using JFrame

public class UserInterface {
private JFrame frame;
private JTextField textField;
private JButton button;
public UserInterface() {
    frame = new JFrame("Name of the Frame");
    textField = new JTextField(20); //width of the textfield
    button = new JButton("Click"); //text on the button
    frame.add(textField); //adding the component to the frame, all components must be explicitly added like this
    frame.setVisible(true); //to make the frame visible
    frame.setSize(500,500); //width and height of frame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //to make the frame close on pressing x button on the frame
}

This should help you create a basic frame GUI from your main method in the implementing class. Please ask if you need more clarification

Upvotes: 3

Related Questions