I'm quite new to using Java and its built-in GUI options, I'm building a small application where I need to have 2 inputs that have their contents passed to a function that exists inside a different class, one represents a name of a customer and another the title of a video, so I have my GUI set up like this:
public class UI {
public UI(VideoStore v) {
//main panel and frame for the GUI
JPanel panel = new JPanel();
JFrame frame = new JFrame();
//creating buttons
JButton list = new JButton("List movies");
JButton borrow = new JButton("Borrow");
JButton quit = new JButton("Quit");
//click listeners for each of the buttons
list.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
v.listAllMovies();
}
});
borrow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//creating another window for the customer name and video title inputs !
JFrame newFrame = new JFrame();
JPanel newpanel = new JPanel();
//2 fields, one for the customer, one for the video title and then a submit button + the labels
JTextField customer = new JTextField(32);
JTextField video = new JTextField(32);
JButton submit = new JButton("Submit");
JLabel customer_label = new JLabel("Customer name");
JLabel video_label = new JLabel("Video title");
newpanel.add(customer_label);
newpanel.add(customer);
newpanel.add(video_label);
newpanel.add(video);
newpanel.add(submit);
//optioms and adding panel to frame, same process as main window
newpanel.setBorder(BorderFactory.createEmptyBorder(50, 50, 10, 50));
newpanel.setLayout(new GridLayout(0, 1));
newFrame.add(newpanel, BorderLayout.CENTER);
newFrame.setTitle("Borrow video");
newFrame.pack();
newFrame.setVisible(true);
//event listener for the submit button
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String customer_string = customer.getText();
String video_string = video.getText();
v.borrow(customer_string, video_string); //we call the borrow function with the values from our inputs
newFrame.setVisible(false); //after we call the borrow function we just made this invisible
}
});
}
});
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//dispatching event for the frame to close, this is the same as pressing X on it,
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
}
});
//a label with instructions
JLabel instructions = new JLabel("Choose from the options below.");
//adding elements to the panel
panel.add(instructions);
panel.add(list);
panel.add(borrow);
panel.add(quit);
panel.setBorder(BorderFactory.createEmptyBorder(50, 50, 10, 50));
panel.setLayout(new GridLayout(0, 1));
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Video store");
frame.pack();
frame.setVisible(true);
}
}
The borrow
function exists inside an object called VideoStore and it looks like this:
//setting up a simple GUI, we dont need a loop for this.
public void setupGUI(VideoStore v) {
//a new UI constructor for our videostore
new UI(v);
}
//borrow method with a customer and video
public void borrow(String name, String title){ // <- here
for(Customer cust: customers){
//in loop, is the current array element name equal to String name?
if(name == cust.getName()) {
for(Video vid: videos){
if(vid.getTitle() == title) {
cust.borrowvideo(vid);
System.out.println("Finished!");
}
}
}
}
}
And the UI is created in the main() function during:
VideoStore videostore = new VideoStore();
//adding the GUI for the videostore
videostore.setupGUI(videostore);
Now, when I pass say some arbitrary values like v.borrow("Bob", "Batman");
the function works properly, but when I use the .getText();
for the JTextField inputs it fails to work.
You guys think theres something I'm doing wrong? (Extremely new to Java)
Cheers!