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 7643093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:21:35+00:00 2026-05-31T09:21:35+00:00

In the code, I am reading an xml file and painting the contents accordingly

  • 0

In the code, I am reading an xml file and painting the contents accordingly on a JPanel.
If the file contains a tag <LineBreak/> I am supposed to insert a Line Break before the next word.
I thought I could use a

g.drawString(..)

but that requires that I know what follows the break, which in this case, I do not.
Is there any other way to do this by overriding the draw method?

Really appreciate the help. Thanks

  • 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-05-31T09:21:37+00:00Added an answer on May 31, 2026 at 9:21 am

    I think you will need to use FontMetrics (g.getFontMetrics()) to determine the number of vertical pixels needed to display a line of text. When you call drawString to paint the second line of text, you must pass a y value large enough that the second line of text is drawn below the first line of text.

    String[] lines = myStringFromXmlDoc.split("\r\n|\r|\n");
    int verticalOffset = 0; // your starting offset would probably be more dynamic
    foreach(String line : lines)
    {
        g.drawString(line, 0, verticalOffset);
        // use FontMetrics to implement calcLineHeight for yourself
        int currentLineHeight = calcLineHeight(line, g);
        verticalOffset += currentLineHeight;
    }
    

    Edit:

    Also see java.awt.font.LineBreakMeasurer for a possibly useful utility class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading XML file using Etree module. Im using following code to print the
With the following file reading code: using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read,
what is the best way of reading xml file using linq and the below
I've been having some issues reading an XML file in my 'res' folder and
I am trying to create a dynamic menu by reading an XML file using
Is it possible to create a class dynamically by reading an xml file (
I'm reading an XML file from a REST web service, parsing it, and displaying
I am reading an xml file and want to perform string operations on the
I am newbie in XML parsing world. I found solution for reading xml file
I am reading an xml file using php, after reading those files i am

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.