Java cant connect buttonto another java file :'(

(we are having this project as self study, our professor literally just said make a ordering system interface hardcoded without the help of gui builder || our last lesson is method and encapsulation)

So we had a group project that is to create an ordering system without using the jframe gui builder to just drag and drop and we are required to have it hard coded and I created the main code where in my classmates will give me their codes/java files with their respective assigned parts (example: i do the login page and he will do the register page)

The thing is it just doesnt connect I added the actionListener that will create a new Main file

it goes like this

JButton iconButton4 = new JButton();

ImageIcon imageIcon4 = new ImageIcon("C:\Users\PC\Desktop\icons\report.jpg");

iconButton4.setIcon(imageIcon4);

iconButton4.setContentAreaFilled(false);

iconButton4.setBorderPainted(false);

iconButton4.setBounds(620, 310, 140, 140);

p2.add(iconButton4);

p2.setComponentZOrder(iconButton4, 0);

iconButton4.ActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

new Main();

}

});

and the main java file that i was trying to connect to is

package login;


import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.text.MaskFormatter;
import java.awt.print.*;

public class Main extends JFrame implements ActionListener {
    JLabel lb, lb1, lb2, lb3, lb5;
    JTextField tf1, tf2, tf5;
    JTextArea tf3;
    JButton btn, submitButton, printButton;

    Main() {
        super("Report");

        lb5 = new JLabel("Select Date (yyyy-MM-dd):");
        lb5.setBounds(20, 20, 150, 20);

        tf5 = new JTextField(10);
        tf5.setBounds(170, 20, 100, 20);

        btn = new JButton("Choose Date");
        btn.setBounds(280, 20, 120, 20);
        btn.addActionListener(this);

        submitButton = new JButton("Submit");
        submitButton.setBounds(170, 50, 100, 30); 
        submitButton.addActionListener(this); 

        printButton = new JButton("Print");
        printButton.setBounds(280, 300, 100, 50);
        printButton.addActionListener(this);

        lb = new JLabel("Report Transaction:");
        lb.setBounds(30, 80, 450, 30);
        lb.setForeground(Color.red);
        lb.setFont(new Font("Serif", Font.BOLD, 20));

        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 500);

        lb1 = new JLabel("Date");
        lb1.setBounds(20, 120, 100, 20);
        tf1 = new JTextField(50);
        tf1.setBounds(130, 120, 200, 20);

        lb2 = new JLabel("Name");
        lb2.setBounds(20, 150, 100, 20);
        tf2 = new JTextField(100);
        tf2.setBounds(130, 150, 200, 20);

        lb3 = new JLabel("Transaction");
        lb3.setBounds(20, 180, 100, 20);
        tf3 = new JTextArea();
        tf3.setBounds(130, 180, 250, 100); 

        setLayout(null);

        // components to the JFrame
        add(lb5);
        add(tf5);
        add(btn);
        add(submitButton);
        add(printButton);
        add(lb);
        add(lb1);
        add(tf1);
        add(lb2);
        add(tf2);
        add(lb3);
        add(tf3);

        tf1.setEditable(false);
        tf2.setEditable(false);
        tf3.setEditable(true); //can edit by the user;editable
    }

    public void actionPerformed(ActionEvent e) {

        //to open the date picker
        if (e.getSource() == btn) {
            String selectedDate = showDatePickerDialog();
            if (selectedDate != null) {
                tf5.setText(selectedDate);
            }
        } else if (e.getSource() == submitButton) {
            try {
                String s = tf5.getText();

                //dummy data 
                String s1 = "Ivan";
                String s2 = "13:20- User processed an order of 2 Patty's Twist Nuggets Meal and 2 Caramel Drift with a total of 320Php.";
                tf1.setText(s);
                tf2.setText(s1);
                tf3.setText(s2);
            } catch (Exception ex) {
                System.out.println(ex);
            }
        } else if (e.getSource() == printButton) {
            printReport();
        }
    }

    private String showDatePickerDialog() {
        JDialog dialog = new JDialog(this, "Date of Transaction2", true);
        dialog.setLayout(new BorderLayout());
        dialog.setSize(300, 200);
        dialog.setLocationRelativeTo(this);

        MaskFormatter maskFormatter;
        try {
            maskFormatter = new MaskFormatter("####-##-##");
            maskFormatter.setPlaceholderCharacter('_');
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
        JFormattedTextField formattedTextField = new JFormattedTextField(maskFormatter);
        formattedTextField.setColumns(10);

        JButton selectButton = new JButton("Select");
        selectButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dialog.dispose();
            }
        });

        JPanel panel = new JPanel();
        panel.add(new JLabel("Select Date (yyyy-MM-dd):"));
        panel.add(formattedTextField);
        panel.add(selectButton);
        dialog.add(panel, BorderLayout.CENTER);

        dialog.setVisible(true);

        return formattedTextField.getText();
    }

    private void printReport() {
        String date = tf1.getText();
        String name = tf2.getText();
        String transaction = tf3.getText();


        //report content
        StringBuilder reportContent = new StringBuilder();
        reportContent.append("Report Transaction:n");
        reportContent.append("Date: ").append(date).append("n");
        reportContent.append("Name: ").append(name).append("n");
        reportContent.append("Transaction: ").append(transaction);

        PrinterJob printerJob = PrinterJob.getPrinterJob();
        printerJob.setPrintable(new Printable() {
            public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
                if (pageIndex > 0) {
                    return NO_SUCH_PAGE;
                }
                Graphics2D g2d = (Graphics2D) graphics;
                g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
                reportContent.toString();
                return PAGE_EXISTS;
            }
        });

        if (printerJob.printDialog()) {
            try {
                printerJob.print();
            } catch (PrinterException ex) {
                ex.printStackTrace();
            }
        }
    }

    public static void main(String args[]) {
        new Main();
    }
}

and I was asking chatgpt for the same problem and he was saying the same solution I was doing, a friend also said that I should try to instatiate a constructor thing with the main class. So is the problem because the file that was sent to me has a Main thingy even tho my file is already the main or its like a code thing

i want to connect the button to another java file but it shows bunch of errors when I do so saying stuffs about the mainframe thing or just the main

New contributor

IVAN ABA-A is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật