JLabel with separate colors for text and icon icons

I have a simple Jlabel element with text and icon

alt text

background setting changes the full color of the label.

I want to be able to display only the background color in the text section of the label, i.e. have separate backgrounds / front frames for the icon and text. Selecting / deselecting the label will turn the color behind the icon and text. Can this be done simply by expanding JLabel, and if so, what methods should I look for to configure?

alt text

My alternative idea is to create a panel with two separate label elements, one with an icon, the other with text. This seems a bit messy, and before I start, I wonder if there is a smarter way to achieve this with Swing.

+3
source share
3 answers

I like the style of what you are doing, but it looks like you are overriding JToggleButton.

Here is an example of a toggle button, with the left selected and not selected on the right:

alt text http://downloads.padlocksoftware.net/toggle.png

It does not have a curved background above the text, but it is a solution that does not require you to implement your own component.

Alignment is configured as:

jToggleButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jToggleButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
+1
source

I assume that the icon you are using has a transparent background? If so, you can use the icon with the appropriate background color. Doing this should allow you to set Background () on the entire shortcut, but only see it in the text.

0
source

JButton . , . , IMO , , .

Your alternative idea is a much simpler solution. There you can use BorderLayoutor MigLayoutto achieve your goal.

0
source

Source: https://habr.com/ru/post/1738476/


All Articles