0

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!

lucian
  • 36
  • 3
  • 4
    So, when I wrap a workable framework around your out of context code, it works fine for me, so the problem isn't with what you've presented. Consider providing a [mcve] which will help remove any ambiguity – MadProgrammer Mar 06 '22 at 23:48
  • @MadProgrammer Thank you for the suggestion! I will try to do so – lucian Mar 06 '22 at 23:51
  • 1
    Side note: your program should not be throwing multiple JFrames at the user. Instead, the secondary window should be a dialog window, likely a modal JDialog would work best, *and* would make it easier for the main program to be able to extract the dialog's data after the user has completed entering it in, a problem that is more difficult when using non-modal windows (and which may be contributing to your current problem). – Hovercraft Full Of Eels Mar 07 '22 at 00:22
  • @HovercraftFullOfEels Hey there, I've replaced that frame with a couple JOptionPanes just before reading this haha, which seems to work okay, also what fixed the code above was just replacing ```vid.getTitle() == title``` with ```vid.getTitle().equals(title)```, I have no idea if this is good practice but it works perfectly now – lucian Mar 07 '22 at 00:26
  • 2
    Yes, check out [Java how to compare Strings](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Hovercraft Full Of Eels Mar 07 '22 at 00:33

0 Answers0