-1

I have this simple GUI that ask for a string and then write it to a text file but if no input is given the JLabel shows an error message and i want that error message to stay for 5 seconds

    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import javax.swing.*;    
    import javax.swing.JFrame;    
    import net.miginfocom.swing.MigLayout;

    public class Q1 extends JFrame {

    private JLabel lblString, lblMessage;
    private JTextField txtString;
    private JButton btnStore;
    private JPanel thePanel;

    public static void main(String[] args) {    
        new Q1();    
    }// End of main

    public Q1() {

        super("Store your text");
        this.setSize(600, 100);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
        thePanel = new JPanel(new MigLayout());    
        lblString = new JLabel("Enter Your Text :");    
        txtString = new JTextField(50);    
        btnStore = new JButton("Store");
        // Listener for Store Button
        ListenerForButton lForButton = new ListenerForButton();    
        btnStore.addActionListener(lForButton);    
        lblMessage = new JLabel();    
        thePanel.add(lblString);
        thePanel.add(txtString, "wrap");
        thePanel.add(btnStore, "skip,split2");
        thePanel.add(lblMessage, "gapleft 200");    
        this.add(thePanel);    
        this.setResizable(false);
        this.setVisible(true);    
    }// End of constructor

    // Listener implement    
    public class ListenerForButton implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            if (e.getSource() == btnStore) {    
                if (txtString.getText().equals("")) {    
                    lblMessage.setText("ERROR-NO Text is given !");
                    lblMessage.setForeground(Color.red);    
                } else {
                    try {
                        String str = txtString.getText();    
                        File file = new File("appending-Text-File.txt");    
                        // if file doesnt exists, then create it
                        if (!file.exists()) {    
                            file.createNewFile();
                        }    
                        FileWriter fileWritter = new FileWriter(file.getName(),
                                true);    
                        BufferedWriter bufferWritter = new BufferedWriter(
                                fileWritter);    
                        bufferWritter.newLine();// Write a new line
                        bufferWritter.write(str);
                        bufferWritter.close();    
                        lblMessage.setText("String is successfully stored !");    
                        txtString.setText("");    
                    } catch (IOException ex) {    
                        ex.printStackTrace();
                    }
                }
            }
        }// End actionPerfomred
    }    
}// End of Class
mKorbel
  • 109,525
  • 20
  • 134
  • 319
CYBERSIX
  • 347
  • 1
  • 7
  • 19
  • 1
    What's the specific question? How to do all of the stuff you said in your description? – Dave Newton May 13 '13 at 16:51
  • 1
    I think you need to use `Timer Class`. Look at answer for [Swing timer not stopping](http://stackoverflow.com/questions/14409868/swing-timer-not-stopping/14410163#14410163). Look at pseudo code given. – Smit May 13 '13 at 16:52
  • 1
    Or you could use a JDialog box. What if the user went away those 5 seconds and didn't see the error? Or what if five seconds is too long and annoying for the user. Why not just let them read it their own pace and click okay once they understand? – Faahmed May 13 '13 at 17:10
  • `MigLayout` is irrelevant; please edit your question to include an [sscce](http://sscce.org/) that focuses on the problem. – trashgod May 13 '13 at 21:06

1 Answers1

0

Try updating your if block as:

  if (txtString.getText().equals("")) {    
     lblMessage.setText("ERROR-NO Text is given !");
     lblMessage.setForeground(Color.red);
     Thread.currentThread().sleep(5000);
     lblMessage.setText("");
  }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • 2
    not, never, bad answer, wrong direction, please why `Thread.currentThread().sleep(5000);`, not to use `SwingWorker`, otherwise `lblMessage.setText("");` must be wrapped inside `invokeLater()` – mKorbel May 13 '13 at 18:15