Can someone point me in the right direction for formatting text and aligninng text in a JTextField?
Thanks...

Recommended Answers

All 20 Replies

JTextFields support a basic subset of HTML tags, so you can achieve a lot of formatting by setting the text of the JTextField to some valid HTML (start with <HTML> )

Sorry I meant JTextArea! Doh
This is my metho so far

    private void processPrint()
    {
        PrintFame pf = new PrintFame();
        pf.text.setText(gui.results.getText() + newline + newline + newline +
                         "Customer Signature  ______________________");
        pf.setVisible(true);
    } 

Where would I put HTML tags?
Thanks

Hi, just one more thing.
I have my notes stored as one long string, it may have one line, it may have ten.
So it may look like this in my gui notes text area:

Screen broke.
No power.
etc
etc
etc
When I format it with html in my JEditorPane all my notes are on a single line. How would I go about making notes as it is in my notes JTextArea? On seperate lines?
Thanks Glen

HTML <BR> tag is like a \n

Yes but how do i break up the string though?

Insert a "<br>" into your input wherever you want to start a new line.

I get that James. But I am getting the notes(String) attribute of a Repair object and putting it in a JEditorPane. My code for that looks like this:

pf.text.setText("<html><center><h2>" + cus.getName() + "<br />" + cus.toString() + "        
                <br /><br />" +  "Ref:  " + getRef() +  "</h2>" + 
                "<br />" + rep.getDateFormat() + "<br />" + rep.getDevice() + "<br />"   
                 + rep.getNotes() + "</center></html>");

pf is a class PrinterFrame and text is a JEditorPane. rep is the Repair object, so rep.getNotes() get the notes but puts it all on one line, how wouldd i seperate the notes sring.
I hope this was a clear explanation!
Thanks

That should be "<br>" not "<br />"

Does it make a difference?.

If it doesn't then why did I bother to make that post? Did you try it???

Haha.
Its still the same.
When I add notes to a Repair object I do it by typing into a JTxtArea, and get a newline simply by hitting enter and when I show the notes in another JTextArea it has the line breaks, like this

GLEN ROGERRS
PINE AVENUE, CARRICKFERGUS, BT38 8EE, 07821771116

Ref: QY0DGJ

2012/07/17

DEVICE;   XBOX

NOT POWERING UP                //notes
MAKING BUZZING SOUND           //notes

but when i try to show notes in a JEditorPane as above it puts the full sting on on line,like this:

GLEN ROGERRS 
PINE AVENUE,
CARRICKFERGUS, 
BT38 8EE, 
07821771116  
Ref: QY4UGW
2012/07/17 
DEVICE; XBOX 
NOT POWERING UP MAKING BUZZING SOUND  //notes

YOu're not giving enough info to diagnose this properly (eg what are the eaxct strings that are in the areas). It seems you have new lines in the address etc, so what's different about the two problem descriptions?

The address is all seperate variables. street is PINE AVENUE, town is CARRICKFERGUS, post is BT38 8EE and phone is 07821771115. These all have getters which i have used.
notes is one string.

OK, so let's play a guessing game. Does the notes string have a \n in it?

No, like I said it's text that is typed into a JTextField, each new line ?I get in the JTextFiels is simply an enter/return key press. Then this is assigned to the String variable '
notes'

So how do you think your enter/return key presses are represented in the notes variable? If you're not sure, try searching for "\n" characters in the string, eg
System.out.println(notes.indexOf("\n"));

I typed a String with 6 returns
this
is
a
sting
with
6
returns
They were all put on one line as usual and the sytem.out.println you gave me said 4

That's because the new lines are coded in the string as \n characters (the first at position 4 in the string), which are new lines in an ordinary text area, but ignored in an HTML context. To display them in HTML you need to replace all "\n" by "<br>" befreo displaying it. (There's a String method for that!)

Thanks for all your help, I got there eventually......................

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.