Results 1 to 5 of 5
Thread: Java GUI revalidate() problem ~
- 09-30-2013, 05:07 PM #1
Member
- Join Date
- May 2013
- Posts
- 19
- Rep Power
- 0
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(); }
- 09-30-2013, 05:14 PM #2
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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
- 09-30-2013, 06:12 PM #3
Member
- Join Date
- May 2013
- Posts
- 19
- Rep Power
- 0
Re: Java GUI revalidate() problem ~
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; }
durationPanel = getDurationPanel("2","0"); // Inside getReservationPanel() methodLast edited by CruelSoulz; 09-30-2013 at 06:21 PM.
- 09-30-2013, 10:09 PM #4
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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
- 10-01-2013, 08:50 AM #5
Member
- Join Date
- May 2013
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
Question about revalidate();
By Sutured in forum New To JavaReplies: 10Last Post: 08-06-2012, 04:05 AM -
Problem with Java Web Applications and Java in Control Panel
By Abysinian in forum Advanced JavaReplies: 4Last Post: 03-16-2012, 12:29 PM -
repaint() and revalidate() doesn't work in my JContentPane
By bigjo in forum AWT / SwingReplies: 3Last Post: 11-20-2011, 06:50 PM -
Small problem with problem with Java, C++ parse program.
By dragstang86 in forum New To JavaReplies: 4Last Post: 10-30-2011, 04:43 AM -
new JLabels not showing in JPanel (doing revalidate&repaint)
By r00tb33r in forum AWT / SwingReplies: 6Last Post: 06-16-2010, 07:03 AM
Bookmarks