For Programmers: Free Programming Magazines  


Home > Archive > Java Help > February 2006 > Changing the Title On A Border









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Changing the Title On A Border
Hal Vaughan

2006-02-26, 3:57 am

I create a border, then add it to a JPanel. There are times, later, when I
want to change the title on the border. I use TitledBorder.setTitle(), as
described in the API docs, but it doesn't change the title. Here's what I
do:

//Create the border:

Border raisedBevel = BorderFactory.createRaisedBevelBorder();
Border loweredBevel = BorderFactory.createLoweredBevelBorder();
Border doubleBorder = BorderFactory.createCompoundBorder(loweredBevel,
raisedBevel);
Border bottomBorder = BorderFactory.createTitledBorder(doubleBorder, "My
Title");

//Create the panel and ad the border:
JPanel actionPanel = new JPanel();
actionPanel.setBorder(bottomBorder);

//Then, later, I try this:
bottomBorder.setTitle("My new Title");

//When that didn't work, I tried this:
TitledBorder tb = (TitledBorder) actionPanel.getBoarder();
tb.setTitle("My new title");

When I do the last one, or try variations, I get a NullPointerException.

Is there some way to change the title on a boarder, once it has been set, to
something different? Why can't I change the title using setTitle("Title")?

Thanks for any help.

Hal
Paul Hamaker

2006-02-26, 7:57 am

You got close.
((TitledBorder)bottomBorder).setTitle("My new Title");
Border 'doesn't know' about the setTitle method.
btw:
actionPanel.getBoarder()
actionPanel.getBorder()
--------------------
Paul Hamaker, SEMM, teaching ICT since 1987
http://javalessons.com

Hal Vaughan

2006-02-26, 7:02 pm

Paul Hamaker wrote:

> You got close.
> ((TitledBorder)bottomBorder).setTitle("My new Title");
> Border 'doesn't know' about the setTitle method.


Then shouldn't I get errors from

bottomBorder.setTitle("My New Title!");

??


> btw:
> actionPanel.getBoarder()
> actionPanel.getBorder()


I guess those things happen when you have someone with mild dyslexia posting
at 3 am!

Thanks.

Hal
VisionSet

2006-02-26, 7:02 pm


"Hal Vaughan" <hal@thresholddigital.com> wrote in message
news:BOGdnT-sLplfdJzZnZ2dnUVZ_smdnZ2d@comcast.com...
> Paul Hamaker wrote:
>
>
> Then shouldn't I get errors from
>
> bottomBorder.setTitle("My New Title!");
>
> ??


Well it won't compile if that's what you mean by errors?
You do know the difference between compile and runtime errors don't you?
The only other explanation is you have a more narrowly scoped variable with
the same name 'bottomBorder' which is Typed to something that *does* have a
setTitle(String) method.
I'd avoid casting where possible for just that reason. Hold a reference to
TitledBorder instead.

--
Mike W


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com