(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
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.