Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8911115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:51:27+00:00 2026-06-15T03:51:27+00:00

After thinking I was on course to solving a problem with making text (read

  • 0

After thinking I was on course to solving a problem with making text (read from a file) appear in a JPanel, I`m frustratingly back to square one.

The code is below. The result is just a blank screen 400×500 screen. Some combinations of using nextLine() + nextLine() as displayText commands result in one word coming up from the file (the word has been different multiple times). This makes me wonder: do I need code that deals with text wrapping? The textfile itself is in paragraphs, and as such, I thought that sf.displayText should say sf.displayText(reader.next() + reader.nextline() + reader.nextline(), and have tried other combinations, but this may be confusing the while parameters. Have also tried a different textfile with simple sentences, no paragraphs, but again, nothing comes up.

Having looked online, I have found that layouts may be an issue, and that alternative options may be BufferedReader or using JTextArea. Browsing through Big Java didn`t provide anything I felt I could take, as all discussion on the scanner went towards integers, whereas the file I want read is prose. I also tried putting a small piece of text in the code itself and cancelling out everything else below it to see if I could transfer text from the code to the JPanel:

StoryFrame sf = new StoryFrame();
sf.displayText("Life is beautiful"); 

but still nothing came up. Ultimately, I want to put text from a file into a JPanel, and have each paragraph come up 5 seconds after the one before. So ultimately, my questions are:

  • Why does my text fail to show up, or only display one word? Is it because I don`t specify a layout?
  • Do I need to think about text wrapping?
  • Should I look into JTextArea instead of JPanel, and BufferedReader instead of Scanner?
  • Have I been using the nextLine method from the Scanner correctly?
  • Can I put a command to read a file and display that file`s text in the display
    method of StoryFrame (I think this would make things a lot easier)?

I know it`s a lot, so any answers to any of the questions would be greatly appreciated, thank you.
Tom

import javax.swing.JFrame;
import javax.swing.JLabel;

public class StoryFrame extends JFrame {

private JLabel mylabel;
public StoryFrame() {

    setTitle("見張ってしながら...");
    setSize(400,500);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    mylabel = new JLabel();
    this.add(mylabel);
    setVisible(true);
}


public void displayText(String text) {
    JLabel storyText = new JLabel();
    add(storyText);
}

}

ShowIntro

import java.util.Scanner;
import java.io.File;
import java.io.IOException;

class ShowIntro {
 public static void main(String args[]) 
        throws IOException {

StoryFrame sf = new StoryFrame();
Scanner reader = new Scanner(new File("Try.txt"));

while (reader.hasNextLine()) {
    //String line = in.nextLine() Not sure whether this would contribute, I doubt it does though
    sf.displayText(reader.next()); 
            //sf.displayText(reader.next() + reader.nextLine() + reader.nextLine()); was also attempted.

    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {          }
}

 }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:51:28+00:00Added an answer on June 15, 2026 at 3:51 am

    it fails because you never call a method to use the text in your displaytext method

    public void displayText(String text) {
        mylabel.setText(text);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: This post was originally specific to ASP.NET, but after thinking about it I'm
After working with ASP.Net MVC, it has me thinking about Rails. I worked with
I asked this question earlier, and after thinking about the topic some more, I
I'm learning about linear congruential generator in an algorithms and data structures course. After
I am thinking of going to the pragmatic studio iphone course, but am a
Hey guys, I'm at a deadlock here after thinking about this for too long.
I just had a problem with a tree conflict in subversion/tortoisesvn. after a lot
With preferably using jquery id want to update written text from input field into
I've written a simple library file with a function for reading lines from a
After thinking about the the whole concept of shared memory , a question came

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.