How to add a component to a Jcheckbox

I have a requirement like..
i will have a JCheckbox having a component. That means i have some text next to my checkbox, which have some part in normal text and some part in bold.
can i achive this requirement. Please reply me if anyone knows about this problem.

I checked with all constructors, but all of them are taking string, not a component. I think if it takes a component, then we can achieve the requirement.

You can set the text of the JCheckBox to show the text. Like many Swing components it has limited HTML support in its text. For example:
As soon as your text starts with <html> it will be treated as HTML.

Thanks for your answer. The solution worked. Can i achieve it without html tags. Because in my requirement, i am displaying all these check boxes with names in a fixed width columns. If the checkbox label exceeds the width limit, It will show the label with ellipses and mouse hovering on the label will show the entire label as a tooltip. If i use html tags for displaying some of the text in bold, its not displaying the ellipses, instead it displaying the whole label in multiple rows. So can i have solution without html tags.

if you already have a component that displays "which have some part in normal text and some part in bold"
just create a JCheckBox (no text), then add that checkbox to a JPanel, and your component to the panel,
using an appropriate layoutManager to display them side-by-side

Sri Palem
Ranch Hand

Joined: Sep 12, 2009
Posts: 32

posted Mar 12, 2010 02:03:32

0

i tried doing this , but it is loosing the feature of showing ellipses if the text size is more than the column width in which it is displaying. Please suggest me solution for this.

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632

posted Mar 12, 2010 03:15:00

0

> Please suggest me solution for this.

I can't - I do not know what component you are using to display your bold/normal text.

post the code you have tried

Sri Palem
Ranch Hand

Joined: Sep 12, 2009
Posts: 32

posted Mar 12, 2010 03:35:43

0

in the above code if we comment adding one of the label, it will show the label in ellipses, if it is exceeding the width of the pane. but i wanted to display these 2 labels side by side and if it exceeds the length of the pane, it should display in ellipses. Please let me know if you understand the problem correctly.

Thanks for your answer, but here first label is also showing ellipses. Since we are showing these 2 labels as part of Checkbox, it should appear like a single label. we should not show ellipses in the middle of 2 labels. and if combination of the both the labels size increased, we have to show ellipses only at the end.

Thanks a lot
Sravanthi

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632

posted Mar 13, 2010 03:03:04

0

> but here first label is also showing ellipses

only because of the layoutManger I used.

I've shown you the basics, now experiment with the various other layoutManagers to get the effect you're after

Sri Palem
Ranch Hand

Joined: Sep 12, 2009
Posts: 32

posted Mar 15, 2010 00:10:27

0

Sorry for asking again..
i tried with GridBagLayout by using GridBagLayoutConstaints. But still could not achive the requirement. Please help me.