Hello people,

How can i display the JLabel text using external file.. When user click a button, a file is read and retrieve and will be displayed at the label.. what I did was:

1) Declare all the necessary file IO
2) use the useDelimiter() method to read the file contents (word is separated by coma)
3) display the text
4) while more taken, read next

been trying w/o success.... i tried several tutorial too.. but :-(

can anyone give me suggestion??

Recommended Answers

All 7 Replies

Show the actual code. The above "process" doesn't really tell us much. For example, what, exactly, is the "necessary file IO"? A FileInputStream, a FileReader, a Scanner? Is that wrapped in a BufferedWhatever? etc, etc.

Show the actual code. The above "process" doesn't really tell us much. For example, what, exactly, is the "necessary file IO"? A FileInputStream, a FileReader, a Scanner? Is that wrapped in a BufferedWhatever? etc, etc.

Thanks for the asap reply. Here is the code.

File questionFile =  new File ("question.txt");
Scanner questionInput = new Scanner (questionFile).useDelimiter("//");


String questions =  questionInput.nextLine();
lblQuestion.setText(questions); //set label text according token

This works well but the scenario is like this... once user answer the question, to proceed next questions, user must click Next button. So for this i added the actionListener. I coded the following..:

public void actionPerformed(ActionEvent nextbtn)
{
 Object source =  nextbtn.getSource();
    if(source==btnNext)
        {
            try
                {
                    File questionFile =  new File ("question.txt");
                    Scanner questionInput = new Scanner (questionFile).useDelimiter("//").next();

                    String questions = questionInput.nextLine();
                                    lblQuestion.setText(questions);
                    }


                    catch(IOException ioe)
                    {
                        System.out.print("IO Errors");
                    }

                }
}

I tried several methods already but still w/o success..what else did i miss?

Declare the scanner at the class level and do not re-open it in the listener. Let it use the one you have already opened.

Declare the scanner at the class level and do not re-open it in the listener. Let it use the one you have already opened.

Im not sure what do u mean by 'at class level'. Im totally new to Java...here is what I did:

lass MCQquestion extends JFrame implements ActionListener
{

    private static final int WIDTH=550;
    private static final int HEIGHT=200;
    private static final int HLOC=340;
    private static final int VLOC=290;
    private JPanel pnlQ, pnlA,pnlButton;
    private JLabel lblQuestion;
    private JFormattedTextField txtAnswer;
    private JButton btnNext;

    //Not Sure what u meant... Im totally new to Java..
    //but declaring it here, at class level generate 'unreported Exception' during compilation
    //where do i insert throw IOException
    //Try n catch dint work at class level..."invalid declaration.."

    File questionFile =  new File ("question.txt");

    Scanner questionInput = new Scanner (questionFile).useDelimiter("//");


    public MCQquestion()
    {

        lblQuestion =  new JLabel();


        try{
                        MaskFormatter ans = new MaskFormatter("U");
                        txtAnswer = new JFormattedTextField(ans);
                        txtAnswer.setPreferredSize(new Dimension(40,30));
                    }

         catch(ParseException pe)
         {
              System.out.println("Error: " + pe.getMessage());
         }

    .... the code continues...

Thanks for the asap reply. Here is the code.

            File questionFile =  new File ("question.txt");
            Scanner questionInput = new Scanner (questionFile).useDelimiter("//");


            String questions =  questionInput.nextLine();
            lblQuestion.setText(questions); //set label text according token

This works well but the scenario is like this... once user answer the question, to proceed next questions, user must click Next button. So for this i added the actionListener. I coded the following..:

public void actionPerformed(ActionEvent nextbtn)
{
 Object source =  nextbtn.getSource();
    if(source==btnNext)
        {
            try
                {
                    File questionFile =  new File ("question.txt");
                    Scanner questionInput = new Scanner (questionFile).useDelimiter("//").next();

                    String questions = questionInput.nextLine();
                                    lblQuestion.setText(questions);
                    }


                    catch(IOException ioe)
                    {
                        System.out.print("IO Errors");
                    }

                }
}

----------------------end of code----

I tried several methods already but still w/o success..what else did i miss?

in the actionPerformed method you declare some objects that you have already declared above, that's your mistake; just use the function to scan the text

and next time, use the CODE tags for your code

You just need to declare them at class level. You can initialize in the constructor as shown or in whichever method reads the first question. Subsequent calls to get the next question just use the same scanner.

class MCQquestion extends JFrame implements ActionListener
{

    private static final int WIDTH=550;
    private static final int HEIGHT=200;
    private static final int HLOC=340;
    private static final int VLOC=290;
    private JPanel pnlQ, pnlA,pnlButton;
    private JLabel lblQuestion;
    private JFormattedTextField txtAnswer;
    private JButton btnNext;

    //Not Sure what u meant... Im totally new to Java..
    //but declaring it here, at class level generate 'unreported Exception' during compilation
    //where do i insert throw IOException
    //Try n catch dint work at class level..."invalid declaration.."

    File questionFile = [B]null[/B];

    Scanner questionInput = [B]null[/B];
    

    public MCQquestion()
    {
        [B]try {[/B]
[B]           questionFile =  new File ("question.txt");
           questionInput = new Scanner (questionFile).useDelimiter("//");
        } catch (Exception e){
            // deal with exceptions here
        }
[/B] 
        lblQuestion =  new JLabel();


        try{
                        MaskFormatter ans = new MaskFormatter("U");
                          txtAnswer = new JFormattedTextField(ans);
                          txtAnswer.setPreferredSize(new Dimension(40,30));
                    }

         catch(ParseException pe)
         {
              System.out.println("Error: " + pe.getMessage());
         }

    .... the code continues...

Thanks Ezzaral, thanks a lot. It worked perfectly...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.