The border type is not createTitledBorder
. That is just the name of the static factory method you are using. It returns a TitledBorder
. Calling getBorder()
on the JTextField only returns a border of type Border
, which does not have the getTitle()
method, since not all borders have titles.
You need to have a reference to the border that is of type TitledBorder
so that Java knows that the border has a title:
TitledBorder b = null;
...
b = BorderFactory.createTitledBorder(new LineBorder(BLACK, 2),title);
c.setBorder(b);
b = (TitledBorder)c.getBorder();
b.getTitle();
or, if you don't want to keep a reference of type TitledBorder
, you could do this:
Border b = null;
...
b = BorderFactory.createTitledBorder(new LineBorder(BLACK, 2),title);
c.setBorder(b);
b = c.getBorder();
( (TitledBorder)b ).getTitle();
EDIT
Here is a less verbose way of doing what you have done in your posted answer:
Border b;
String title;
b = c.getBorder();
if(b instanceof TitledBorder)
{
title = ( (TitledBorder)b ).getTitle();
}