2

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);
    }
}
6
  • you need to revalidate() and repaint() the contentPane if you want to add components to it after the frame is already visible. Commented Aug 11, 2014 at 16:03
  • @peeskillet So should I add these two lines of code after the If Statement? "contentPane.revalidate();" "contentPane.repaint();" Commented Aug 11, 2014 at 16:06
  • .. at the end of the listener actionPerformed... see what happens Commented Aug 11, 2014 at 16:06
  • That worked, thanks! Sorry if this was a really noob question. Commented Aug 11, 2014 at 16:07
  • It's ok, as long as you learned something new ;-) Commented Aug 11, 2014 at 16:08

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

You can fix this by adding

contentPane.repaint();

after the code for said button.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.