Wtalk2
Wtalk2

Reputation: 43

Java JButton not creating when in an If Statement

I am working on a Java GUI program that is just a practice/example program that checks what rooms are empty in a hotel and reserves them for a group of people. My problem is I need a JButton to be show after the "Check Room Availability" is pressed and if there is a index in the RoomsArray that has a value of 0. I was able to get a JTextPane to be shown after that button is pressed but my "Yes" JButton won't appear if it is in an If Statement, it works fine if it is outside of the If Statement but I need it to only appear after the "Check Room Availability" button is pressed. Any help is appreciated!

package text;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Toolkit;

import javax.swing.JTextPane;

import java.awt.Color;

import javax.swing.UIManager;

import java.awt.Font;
import java.awt.Choice;
import javax.swing.JButton;

import org.apache.commons.lang3.ArrayUtils;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;

@SuppressWarnings("serial")
public class HotelReservationGUI extends JFrame {
    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    HotelReservationGUI frame = new HotelReservationGUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     * @return 
     */ 
    public HotelReservationGUI() {

        contentPane = new JPanel();

        final int[] RoomsArray = new int[10];

        RoomsArray[0] = (int) ((10 * Math.random()) - 1);
        RoomsArray[1] = (int) ((10 * Math.random()) - 1);
        RoomsArray[2] = (int) ((10 * Math.random()) - 1);
        RoomsArray[3] = (int) ((10 * Math.random()) - 1);
        RoomsArray[4] = (int) ((10 * Math.random()) - 1);
        RoomsArray[5] = (int) ((10 * Math.random()) - 1);
        RoomsArray[6] = (int) ((10 * Math.random()) - 1);
        RoomsArray[7] = (int) ((10 * Math.random()) - 1);
        RoomsArray[8] = (int) ((10 * Math.random()) - 1);
        RoomsArray[9] = (int) ((10 * Math.random()) - 1);

        setResizable(false);
        setIconImage(Toolkit.getDefaultToolkit().getImage(HotelReservationGUI.class.getResource("/text/Hotel Icon.png")));
        setTitle("Hotel Reservation");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 500, 500);
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu MenuFile = new JMenu("File");
        menuBar.add(MenuFile);

        JMenuItem MenuItemExit = new JMenuItem("Exit");
        MenuFile.add(MenuItemExit);
        MenuItemExit.setToolTipText("Exit application");
        contentPane.setBackground(UIManager.getColor("Button.light"));
        contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JTextPane WelcomeToHotel = new JTextPane();
            WelcomeToHotel.setBackground(UIManager.getColor("Button.light"));
            WelcomeToHotel.setFont(new Font("Tahoma", Font.BOLD, 13));
            WelcomeToHotel.setText("Welcome to Hotel Reservation!");
            WelcomeToHotel.setBounds(10, 10, 208, 20);
            contentPane.add(WelcomeToHotel);
            MenuItemExit.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent event) {
                    System.exit(0);
                }
            });

        JTextPane EnterYourNumber = new JTextPane();
            EnterYourNumber.setFont(new Font("Tahoma", Font.BOLD, 13));
            EnterYourNumber.setBounds(10, 40, 198, 44);
            EnterYourNumber.setBackground(UIManager.getColor("Button.light"));
            EnterYourNumber.setForeground(Color.BLACK);
            EnterYourNumber.setEditable(false);
            EnterYourNumber.setText("Enter your number of guests:");
            contentPane.add(EnterYourNumber);

        Choice Guests = new Choice();
            Guests.setBounds(214, 40, 64, 20);
            contentPane.add(Guests);
            Guests.add("1");
            Guests.add("2");
            Guests.add("3");
            Guests.add("4");
            Guests.add("5");
            Guests.add("6");
            Guests.add("7");
            Guests.add("8");

        Object GuestsNumber = ((Choice) Guests).getSelectedItem();

        JButton CheckRoomAvailability = new JButton("Check Room Availability");
            CheckRoomAvailability.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) 
                {
                if (ArrayUtils.contains(RoomsArray, 0)) 
                {
                    //This button won't appear while in the If Statement.
                    JButton ReserveRoomYes = new JButton("Yes");
                    ReserveRoomYes.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent arg0) 
                        {

                        }
                    });
                    ReserveRoomYes.setBounds(288, 300, 55, 20);
                    contentPane.add(ReserveRoomYes);

                    JTextPane RoomAvailableTXT = new JTextPane();
                        RoomAvailableTXT.setFont(new Font("Tahoma", Font.BOLD, 13));
                        RoomAvailableTXT.setText("A room is available! \nWould you like to reserve this room?");
                        RoomAvailableTXT.setBounds(10, 70, 300, 200);
                        contentPane.add(RoomAvailableTXT);
                }
                else 
                {
                    JTextPane RoomNotAvailableTXT = new JTextPane();
                        RoomNotAvailableTXT.setFont(new Font("Tahoma", Font.BOLD, 13));
                        RoomNotAvailableTXT.setText("No rooms are available, please try again later.");
                        RoomNotAvailableTXT.setBounds(10, 70, 300, 200);
                        contentPane.add(RoomNotAvailableTXT);
                }
                }
            });
            CheckRoomAvailability.setBounds(288, 40, 170, 20);
            contentPane.add(CheckRoomAvailability);
    }
}

Upvotes: 2

Views: 232

Answers (2)

Wtalk2
Wtalk2

Reputation: 43

You can fix this by adding

contentPane.repaint();

after the code for said button.

Upvotes: 0

mbw
mbw

Reputation: 366

Another way of doing this would be to initially create the button on your page, then immediately after,

jButton.setVisible(false);

Your GUI will be displayed like the button is not there. Whenever the button should be shown, you would just call

jButton.setVisible(true);

After you're done with the button, you can just hide it again.

Upvotes: 1

Related Questions