-1

setVisible currently use to restart the methods, but the transition does a flashing, is there any instruction other than setVisible? leave it in its initial state. reset button on the last Frame_1 only to return to the initial state.

   public class frame_1 extends javax.swing.JFrame {

   private void btnfinishActionPerformed(java.awt.event.ActionEvent evt) {                                         
        Period total = new Period(0, totalDuration, PeriodType.yearMonthDay());
        txta.append("________________________________________________" + "\n");
        txta.append("Duracion total: " + total.getYears() + " years " + total.getMonths() + " Months " + total.getDays() + " Days " + "\n");
        txtini.setText(null);
        txtfin.setText(null);
}                                        

private void btnaddActionPerformed(java.awt.event.ActionEvent evt) {                                        
        period();
        txtini.setText(null);
        String fu = principio.txtingresa.getText();
        txtfin.setText(fu);
}                                       

public void period(){
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

        String startDate = txtini.getText();
        String endDate = txtfin.getText();

        Date start = null;
        Date end = null;
    try {
        start = sdf.parse(startDate);
        end = sdf.parse(endDate);
    } catch (ParseException ex) {
        Logger.getLogger(fecha_1.class.getName()).log(Level.SEVERE, null, ex);
    }

        if(end.getTime() < start.getTime()){
            JOptionPane.showMessageDialog(null, "the date finish y higher.!");
            txtini.setText(null);
            txtfin.setText(null);
        }else{
        long duration = end.getTime() - start.getTime();
        Period period = new Period(0, duration, PeriodType.yearMonthDay());

        txta.append(startDate + " - "+ endDate + "     =     " + period.getYears()+ "  years  " + period.getMonths() + "  Months  " + period.getDays() + "  Days  " + "\n");
        totalDuration += duration;
        }
        txtini.requestFocusInWindow();
}

   private void btnrestartActionPerformed(java.awt.event.ActionEvent evt) {                                        
        frame_1 obj1 = new frame_1();
        obj1.setVisible(false);
        obj1.setVisible(true);
        }

     }
danielbmz
  • 31
  • 1
  • 1
  • 8
  • 2
    What are you trying to achieve? Restart the application? – aioobe Sep 19 '14 at 14:27
  • restart a JFrame, contains a textarea, button that calculates a period of time, I show this code, I work, there are some other code to restart the frame with his methods? – danielbmz Sep 19 '14 at 14:30
  • Transition into what? A new frame? Perhaps you can just display items on a JPanel and swap those around? – MarGar Sep 19 '14 at 14:33
  • 1
    And define "restart". If all you want to do is to get your components in their initial state, then perhaps all you need to is to clear all text components. Again, clarity of question is key. – Hovercraft Full Of Eels Sep 19 '14 at 14:33
  • yes, show the same frame with reset methods (initial state) – danielbmz Sep 19 '14 at 14:35
  • 1
    Please edit your post and include some application code. More than just part of your listener. – MarGar Sep 19 '14 at 14:36
  • `"yes, show the same frame with reset methods (initial state)"` -- yes, then you're correct in coming here with your question, because your way of doing this is most definitely wrong, but in order to help you better, we should know more about your GUI, including more code and perhaps an image or images of what you're trying to achieve. If you upload your images somewhere and give us the direct link to the image, we can help put it into your question. – Hovercraft Full Of Eels Sep 19 '14 at 14:44

2 Answers2

1

Based on your presented code, your reset button could be as simple as...

private void btnrestartActionPerformed(java.awt.event.ActionEvent evt) {
   txta.setText("");
   txtini.setText("");
   txtfin.setText("");

   // here you have to initialize any other GUI fields and
   // any non-gui fields that need it.
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
-1

I think what you want is

JFrame.getContentPane().invalidate();
JFrame.getContentPane().validate();
JFrame.getContentPane().repaint();
Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80