Results 1 to 5 of 5
  1. #1
    CruelSoulz is offline Member
    Join Date
    May 2013
    Posts
    19
    Rep Power
    0

    Default Java GUI revalidate() problem ~

    I'm just included a part of my code which are related to the problem ( my point of view )

    Java Code:
           private JPanel getReservationPanel(){
                
                JPanel reservationDate = new JPanel();
                
                //Start setting for reservationDate containers
                reservationDate.setBorder(new TitledBorder("Reservation Dates"));
                reservationDate.setLayout(new GridLayout(0,2,10,0));
                //reservationDate.setPreferredSize(new Dimension(550,100));
                
                JPanel panelLeft = new JPanel();
                panelLeft.setLayout(gridBag);
                
                JLabel checkInTitle = new JLabel("Check-In");
                checkInTitle.setFont(font1);
                
                JLabel checkOutTitle = new JLabel("Check-Out");
                checkOutTitle.setFont(font1);
                
                //Check IN and OUT date
                JPanel panelRight = new JPanel(gridBag);
                
                final JTextField checkInDate = new JTextField(null,18);
                checkInDate.setFont(font2);
                checkInDate.setEditable(false);
                
                final JTextField checkOutDate = new JTextField(null,18);
                checkOutDate.setFont(font2);
                checkOutDate.setEditable(false);
                
                final JButton checkInNow = new JButton("Check In");
                final JButton checkOutNow = new JButton("Check Out");
                
                checkInNow.addActionListener(new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent e) {
                      checkInDate.setText(getCurrentTime());
                      //String time = Integer.toString(earlyCheckInCalc(getCurrentTime()));
                      Container contentPane = getContentPane();
                      //durationPanel.removeAll();
                      durationPanel = getDurationPanel("2","0");
                      //contentPane.add(durationPanel);
                      contentPane.revalidate();
                      //contentPane.repaint();
                     
                      //checkInNow.setEnabled(false);
                      
                    }
                });
                
               checkOutNow.addActionListener(new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if(checkInDate.getText().length()==0){
                            JOptionPane.showMessageDialog(null, "Please check in first.", "Warning", JOptionPane.WARNING_MESSAGE);
                        }else{
                           checkOutDate.setText(getCurrentTime());
                           checkOutNow.setEnabled(false);
                        }
                    }
                });
    
               GridBagConstraints c = new GridBagConstraints();
               
               c.gridx = 0;
               c.gridy = 0;
               c.anchor = GridBagConstraints.WEST;
               panelLeft.add(checkInTitle,c);
               
               c.insets = new Insets(0,0,15,10);//Insets(int top, int left, int bottom, int right)
               c.gridx = 0;
               c.gridy = 1;
               panelLeft.add(checkInDate,c);
               
               c.gridx = 1;
               c.gridy = 1;
               panelLeft.add(checkInNow,c);
    
               c = new GridBagConstraints(); //Reset
               c.anchor = GridBagConstraints.WEST;
               c.gridx = 0;
               c.gridy = 0;
               panelRight.add(checkOutTitle, c);
               
               c.insets = new Insets(0,0,15,10);//Insets(int top, int left, int bottom, int right)
               c.gridx = 0;
               c.gridy = 1;
               panelRight.add(checkOutDate,c);
               
               c.gridx = 1;
               c.gridy = 1;
               panelRight.add(checkOutNow,c);
               
                //Add contents to Frame
                reservationDate.add(panelLeft);
                reservationDate.add(panelRight);
                
                return reservationDate;
                
            }
    Java Code:
    //Global Variable
    
        Font font1 = new Font("Comic Sans MS", Font.BOLD, 12);
        Font font2 = new Font("Comic Sans MS", Font.PLAIN, 12);
        GridBagLayout gridBag = new GridBagLayout();
        
        private JPanel durationPanel = getDurationPanel("1","0");
    public CCIFrame(){
                 
                //Setting for frame
                super("Check-In System");
                frameSetting(800,600,true,true);
                setLayout(gridBag);
                //End of setting
                
                JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER,10,0));
                buttons.add(new JButton("Ok"));
                buttons.add(new JButton("Cancel"));
                
                JLabel checkInInfo = new JLabel("Check-In Information", JLabel.CENTER);
                checkInInfo.setForeground(Color.BLACK);
                
                JPanel title = new JPanel(new FlowLayout());
                title.setBackground(Color.YELLOW);
                title.add(checkInInfo);
                title.setBorder(new LineBorder(Color.BLACK, 2));
               
                 //Adding containers to frame
                GridBagConstraints c = new GridBagConstraints();
    
                c.fill = GridBagConstraints.HORIZONTAL;
                c.anchor = GridBagConstraints.NORTH;
                c.gridwidth = 2;
                c.gridx = 0;
                c.gridy = 0;
                add(title,c);
                
                //Reset
                c.gridwidth = 1;
                
                c.weightx = 0.5;
                c.gridx = 0;
                c.gridy = 1;
                add(getReservationPanel(), c);// Ignore it, it is a method which return reservation panel
                
                c.gridx = 0;
                c.gridy = 2;
                add(durationPanel, c);
                
                c.anchor = GridBagConstraints.SOUTH;
                c.gridx = 0;
                c.gridy = 3;
                c.gridwidth = 2;
                c.insets = new Insets(10,10,10,10);
                c.weighty = 1.0;
                add(buttons, c);
                
                pack();
                
                
                 
            }
    Now my problem is, after i click the button "final JButton checkInNow = new JButton("Check In");" it doesn't refresh the "Early Check In (Hours)" to 2. Please point me to the right direction. Thanks.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,098
    Rep Power
    6

    Default Re: Java GUI revalidate() problem ~

    well the problem is probably in this getDurationPanel() you did not post. I can make a guess that this method creates a new JPanel, but does it replace the durationPanel that is already added to the GUI?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    CruelSoulz is offline Member
    Join Date
    May 2013
    Posts
    19
    Rep Power
    0

    Default Re: Java GUI revalidate() problem ~

    Quote Originally Posted by gimbal2 View Post
    well the problem is probably in this getDurationPanel() you did not post. I can make a guess that this method creates a new JPanel, but does it replace the durationPanel that is already added to the GUI?
    getDurationPanel() method is working because when i invoke it at the constructor method it work perfectly.
    By the way, here is the code for this method.

    Java Code:
           private JPanel getDurationPanel(String earlyCI, String lateCO){
                
                JPanel getDuration = new JPanel();
                
                JPanel dayPanel = new JPanel();
                dayPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 15, 20));
                
                JPanel hourPanel = new JPanel();
                hourPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 15, 20));
                
                JPanel EOLcheckIn = new JPanel();
                EOLcheckIn.setLayout(new GridLayout(1,4));
                EOLcheckIn.setBorder(BorderFactory.createLoweredSoftBevelBorder());
                
                getDuration.setBorder(new TitledBorder("Duration Of Rental"));
                //getDuration.setPreferredSize(new Dimension(550,100));
                getDuration.setLayout(new GridBagLayout());
                
                rentalDurationVerify verifyDay = new rentalDurationVerify(0,30,"Please enter digit more than or equal to 0.","Please enter digit less than or equal to 30.");
                rentalDurationVerify verifyHours = new rentalDurationVerify(1,23, "Customer must at least check in for 1 hours.", "Please enter hour which less than 24");
                
                JLabel days = new JLabel("Day(s)");
                days.setFont(font1);
                JLabel hours = new JLabel("Hour(s)");
                hours.setFont(font1);
                JLabel earlyCheckIn = new JLabel("Early Check In (Hours)");
                earlyCheckIn.setFont(font1);
                JLabel lateCheckOut = new JLabel("Late Check Out (Hours)");
                lateCheckOut.setFont(font1);
                JLabel eciText = new JLabel(earlyCI);
                JLabel lcoText =  new JLabel(lateCO);
                
                JTextField daysText = new JTextField(18);
                daysText.setInputVerifier(verifyDay);
                daysText.setFont(font2);
                JTextField hoursText = new JTextField(18);
                hoursText.setInputVerifier(verifyHours);
                hoursText.setFont(font2);
                
                dayPanel.add(days);
                dayPanel.add(daysText);
                hourPanel.add(hours);
                hourPanel.add(hoursText);
                EOLcheckIn.add(earlyCheckIn);
                EOLcheckIn.add(eciText);
                EOLcheckIn.add(lateCheckOut);
                EOLcheckIn.add(lcoText);
                
                GridBagConstraints c = new GridBagConstraints();
                
                c.gridx = 0;
                c.gridy = 0;
                c.weightx = 0.5;
                c.anchor = GridBagConstraints.WEST;
                getDuration.add(dayPanel,c);
                
                c.gridx = 1;
                c.gridy = 0;
                getDuration.add(hourPanel,c);
                
                c.fill = GridBagConstraints.HORIZONTAL;
                c.gridwidth = 2;
                c.gridx = 0;
                c.gridy = 1;
                c.ipady = 40;
                getDuration.add(EOLcheckIn,c);
                
                return getDuration;
            }
    I declared the durationPanel as global variable so when "final JButton checkInNow = new JButton("Check In");" in "getReservationPanel() " been clicked, it contents should be changed.
    durationPanel = getDurationPanel("2","0"); // Inside getReservationPanel() method
    This line should change the "Early Check In (Hours)" to 2 if i'm not mistaken.
    Last edited by CruelSoulz; 09-30-2013 at 05:21 PM.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,098
    Rep Power
    6

    Default Re: Java GUI revalidate() problem ~

    It works the first time, yes.

    But no, it shouldn't change because you are doing nothing that CHANGES anything. You're just creating a NEW panel, not updating the existing one. Where is this newly created panel added to your frame's content pane?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    CruelSoulz is offline Member
    Join Date
    May 2013
    Posts
    19
    Rep Power
    0

    Default Re: Java GUI revalidate() problem ~

    Quote Originally Posted by gimbal2 View Post
    It works the first time, yes.

    But no, it shouldn't change because you are doing nothing that CHANGES anything. You're just creating a NEW panel, not updating the existing one. Where is this newly created panel added to your frame's content pane?
    Owh ... but how i'm going to update the old one? i tried remove it and update with new one but it make my layout mess up
    Any hint for solution of this problem ?

Similar Threads

  1. Question about revalidate();
    By Sutured in forum New To Java
    Replies: 10
    Last Post: 08-06-2012, 03:05 AM
  2. Replies: 4
    Last Post: 03-16-2012, 11:29 AM
  3. Replies: 3
    Last Post: 11-20-2011, 05:50 PM
  4. Small problem with problem with Java, C++ parse program.
    By dragstang86 in forum New To Java
    Replies: 4
    Last Post: 10-30-2011, 03:43 AM
  5. Replies: 6
    Last Post: 06-16-2010, 06:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •