Skip to main content

6u14b06: setting the title of a JTabbedPane in HTML does not work anymore

3 replies [Last post]
whaid
Offline
Joined: 2008-09-04
Points: 0

Setting the title of a JTabbedPane using HTML code does not work with version 6u14b05 and 6u14bu06. Works well with previous versions of Java 6.

Try to set some title with HTML code,for example setting the text style to bold, using JTabbedPane.setTitleAt(int index, String title). Then try to switch from one tab to another, and you will see strange things happening: sometimes the titles are refreshed with the title of another tab, sometimes the title shows the raw HTML code...

Has anyone else encountered this behavior ? is this a bug ?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
stephane_gotfryd
Offline
Joined: 2009-11-16
Points: 0

Awayting SUN's correction ... (since two years) ...

Here is a working workaround :

final JTabbedPane t = new JTabbedPane()
{
@Override
public void setTitleAt(int index, String title)
{
try
{
Field df = getField(getUI().getClass(), "htmlViews");
df.set(ui, null);
}catch(Exception e){e.printStackTrace();}
super.setTitleAt(index, title);
}
Field getField(Class c, String name) throws SecurityException, NoSuchFieldException
{
Field f=null;
try
{
f = c.getDeclaredField(name);
f.setAccessible(true);
return f;
} catch (NoSuchFieldException e)
{
if(f instanceof Object)
throw e;
return getField(c.getSuperclass(), name);
}
}
};

Use this inner-overloaded class instead of the JTabbedPane to obtain a working class.

whaid
Offline
Joined: 2008-09-04
Points: 0

[b]here a short code example to reproduce this issue:[/b]

[i]import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class JTabbedPaneTest {

private JTabbedPane _tabbedPane = null;

private String[] _tabTitles = {"One", "Two", "Three", "Four"};
private int _lastSelectedTab = 0;

private final String HTML_PLAIN_START = "";
private final String HTML_BOLD_START = "";
private final String HTML_END = "";

public static void main(String[] args){
new JTabbedPaneTest();
}

public JTabbedPaneTest(){
JFrame frame = new JFrame();

_tabbedPane = new JTabbedPane();

for (int i = 0; i < _tabTitles.length; i++) {
_tabbedPane.addTab(HTML_PLAIN_START+_tabTitles[i]+HTML_END, new JPanel());
}

_tabbedPane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
int index = _tabbedPane.getSelectedIndex();
highlightTabAt(index);
_lastSelectedTab = index;
}
});

frame.setContentPane(_tabbedPane);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

highlightTabAt(0);
}

private void highlightTabAt(int index) {
if (_lastSelectedTab >= 0) {
String title = _tabTitles[_lastSelectedTab];
_tabbedPane.setTitleAt(_lastSelectedTab, HTML_PLAIN_START + title + HTML_END);
}
String title = _tabTitles[index];
_tabbedPane.setTitleAt(index, HTML_BOLD_START + title + HTML_END);
}

}[/i]

doubletrouble
Offline
Joined: 2007-11-15
Points: 0

Yes, I too noticed that in 6u14.

While web searching for an answer, I stumbled across this old bug
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6670274
which is STILL unfixed. Not sure if it is related to what we are seeing in 614 or not, but I decided that html formatting is simply flaky at this point, so I gave up on it entirely and have had a happy program ever since...