Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JPanel Border Title how to justify ?

1 view
Skip to first unread message

Fuddzy

unread,
Mar 6, 2005, 9:06:30 PM3/6/05
to
Hello
I cannot figure out how to "center" the border title of a JPanel. I did
look in the API and went to the Sun site for beginners........ but I am
still stuck

//in my class I declared
JPanel janswer;

// then I proceeded to make a Jpanel

public void buildAnswerPanel()
{janswer = new JPanel();
janswer.setBackground(Color.white);
janswer.setLayout(new GridLayout(2,0,));

//everything works including the next line
janswer.BorderFactory.createTitledBorder( "Here is my Title");

// however I wish to center the panel title......... so when I try the
following I get an error
janswer.setBorder(BorderFactory.createTitledBorder("Here is my Title
", TitledBorder.CENTER));
}

Please give me an example of how to "justify the title of a border on a
Jpanel"

Many Thanks
Fud
.........................................................................................
here is the error
cannot resolve symbol
symbol : method createTitledBorder (java.lang.String,int)
location: class javax.swing.BorderFactory

janswer.setBorder(BorderFactory.createTitledBord
er("Here is my Answer ", TitledBorder.CENTER));
^
.................................................................................................

Roland

unread,
Mar 7, 2005, 5:10:37 AM3/7/05
to
There's no method named createTitleBorder which takes a String and an
int as parameters. Try
BorderFactory.createTitledBorder(null,"Here is my Answer ",
TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION)
--
Regards,

Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \

John McGrath

unread,
Mar 7, 2005, 6:43:33 AM3/7/05
to
On 3/6/2005 at 9:06:30 PM, Fuddzy wrote:

> BorderFactory.createTitledBorder("Here is my Title ",
> TitledBorder.CENTER )

There is no such method. If you check the JavaDocs, you will find the
following createTitledBorder methods in the BorderFactory class:

createTitledBorder(Border)
createTitledBorder(Border,String)
createTitledBorder(Border,String,int,int)
createTitledBorder(Border,String,int,int,Font)
createTitledBorder(Border,String,int,int,Font,Color)
createTitledBorder(String)

Note that createTitledBorder(String,int) is not one of them. You need to
select from the defined methods and supply the correct parameters. I
think you probably want to use createTitledBorder(Border,String,int,int);

Alternatively, you can create the border using a method that does not
specify all of the attributes, then set any additional attributes that
you want using TitledBorder's accessors. For example:

TitledBorder border = BorderFactory.createTitledBorder( "Title" );
border.setTitleJustification( TitledBorder.CENTER );

--
Regards,

John McGrath

Dirk Starke

unread,
Mar 7, 2005, 6:58:16 AM3/7/05
to
Hello Fud,

> [...]


> here is the error
> cannot resolve symbol
> symbol : method createTitledBorder (java.lang.String,int)
> location: class javax.swing.BorderFactory
>
> janswer.setBorder(BorderFactory.createTitledBord
> er("Here is my Answer ", TitledBorder.CENTER));
> ^

Indeed there is no such method in BorderFactory (see
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/BorderFactory.html).
You could try

BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),


"Here is my Answer",

TitledBorder.CENTER,
TitledBorder.TOP);

Regards,
Dirk

John McGrath

unread,
Mar 7, 2005, 7:37:27 AM3/7/05
to
On 3/7/2005 at 5:10:37 AM, Roland wrote:

> BorderFactory.createTitledBorder(null,"Here is my Answer ",
> TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION)

Checking the source code, I see that passing null for the Border argument
will work. Is the behavior documented, or are you depending on internal
implementation details?

--
Regards,

John McGrath

Fuddzy

unread,
Mar 7, 2005, 1:02:13 PM3/7/05
to
Fuddzy wrote:
> Hello
> I cannot figure out how to "center" the border title of a JPanel.

Thanking each one of You for your quick responses and accurate info :
Roland
Dirk Starke
John McGrath

This is a wonderful ng.

Thanks much
Fud

Roland

unread,
Mar 8, 2005, 4:13:52 AM3/8/05
to

You're probably right that it's not documented, at least not explicitly.
I guess, I must have interpreted the javadoc of createTitledBorder
("Adds a title to an existing border, ...") in combination with the
possibility to create compound borders (wrapping a border within another
border) that it should be possible. I've applied it in a number of
applications and it never failed. So I was relying on pragmatics rather
than wisdom ;-)

0 new messages