Skip to main content

How to update JTabbedPanes different tabs title dynamically?

No replies
umesh_kacha
Offline
Joined: 2008-09-21

I have designed one customized tabbedpane from JTabbedPane.This customized JTabbedPane has different look and feel and support for close by means of close icon.

Now our users is using this customized tabbed pane by puting it into some MDI frame or simple frame.

Now when user is resizing/reducing size of mainframe then in this case different tabbed panes title is not changing according to width and hence title is coming outside tab width means if title is long and tab width is less then long title is painting outside tab width and on mainframe.

I have overridden below Java's BasicTabbedPaneUI method

protected void paintText(Graphics g, int tabPlacement,
Font font, FontMetrics metrics, int tabIndex,
String title, Rectangle textRect,
boolean isSelected)
{

}

Inside above method I am calculating size of title according to tab width by using below Java method

String calculatedTextwhichNeedToBeDisplayedinTab = SwingUtilities.layoutCompoundLabel(metrics ,
actualTitleofTab, null , SwingUtilities.CENTER , SwingUtilities.CENTER ,
SwingUtilities.CENTER , SwingUtilities.TRAILING , rectAccordingToTabWidth ,
new Rectangle() , rectAccordingToTabWidth, textIconGap);

Above layoutCompoundLabel() method is not working in JDK 1.4 but it is working in JDK 1.5. Means above method is giving title according to tab width in JDK 1.5 but above method is not giving title based on tab width in 1.4 everytime it is giving same title so title is going outside tab width.

Please provide some work around if any of you have got this type of problem or aware of this problem.

Please provide me some work around or some other way to update and calculate title of tabbed pane's tab according to tab width.

Demo code is shown below:

Below is a demo program to display TabbedPane behaviour change between JDK 1.4 and JDK 1.5.

Steps to reproduce :

Compile below file in TabbedPaneDemo.java in JDK 1.5

Try to reduce size of frame by mouse you will find that title is changing according to tab width due to below method call.It will return required title to show in tab according to tab width.

String clippedText = SwingUtilities.layoutCompoundLabel(
metrics , title , null , SwingUtilities.CENTER ,
SwingUtilities.CENTER , SwingUtilities.CENTER ,
SwingUtilities.TRAILING , rect , new Rectangle() ,
rect , 0);

But above method call in not working in when you compile below java file with JDK 1.4.Above method is not returning clipped text to show in tab according to tab width.Everytime it is giving same title so that title is crossing range of tab width.

Please check below JAVA file in both versions JDK 1.4 and JDK 1.5 and see the difference in output.

Please if any of you has got this problem or if any one knows some work around then please let me know ASAP.

Thanking you in advance.

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class TabbedPaneDemo
{
public static void main(String[] args)
{
JFrame f = new JFrame("Umesh");
JTabbedPane tp = new JTabbedPane();

JPanel p = new JPanel(new FlowLayout());
JButton b = new JButton();
p.add(b , FlowLayout.LEFT);

tp.setUI(new javax.swing.plaf.basic.BasicTabbedPaneUI()
{
final Insets tabInsets = UIManager
.getInsets("TabbedPane.tabInsets");

protected int calculateTabWidth(int tabPlacement , int tabIndex ,
FontMetrics metrics)
{
Insets insets = tabPane.getInsets();
Insets tabAreaInsets = getTabAreaInsets(tabPlacement);
int width = tabPane.getWidth() - insets.left - insets.right
- tabAreaInsets.left - tabAreaInsets.right;
switch (tabPlacement)
{
case LEFT:
case RIGHT:
return (int) (width / 4);
case BOTTOM:
case TOP:
default:
return (int) (width / tabPane.getTabCount());
}
}

protected void paintText(Graphics g , int tabPlacement , Font font ,
FontMetrics metrics , int tabIndex , String title ,
Rectangle textRect , boolean isSelected)
{
Rectangle tabRect = rects[tabIndex];
Rectangle rect = new Rectangle(textRect.x + tabInsets.left ,
textRect.y , tabRect.width - tabInsets.left
- tabInsets.right , textRect.height);
String clippedText = SwingUtilities.layoutCompoundLabel(
metrics , title , null , SwingUtilities.CENTER ,
SwingUtilities.CENTER , SwingUtilities.CENTER ,
SwingUtilities.TRAILING , rect , new Rectangle() ,
rect , 0);
if (title.equals(clippedText))
{
super.paintText(g , tabPlacement , font , metrics ,
tabIndex , title , textRect , isSelected);
}
else
{
rect = new Rectangle(textRect.x + tabInsets.left ,
textRect.y , tabRect.width - tabInsets.left
- tabInsets.right , textRect.height);
super.paintText(g , tabPlacement , font , metrics ,
tabIndex , clippedText , rect , isSelected);
}
}
});

tp.addTab("Give some lonnnnnnngggggggg title here",new JPanel());

f.getContentPane().add(tp);
f.setSize(461 , 380);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}