SWT Java: how to change colour of text in Label control? - java

I know how to change size, style but how can I set colour of text in Label control? Here is my code so far:
Label myLabel = new Label(shell, SWT.NONE);
myLabel.setText("some text that needs to be for example green");
FontData[] fD = myLabel.getFont().getFontData();
fD[0].setHeight(16);
fD[0].setStyle(SWT.BOLD);
myLabel.setFont( new Font(display,fD[0]));
I see there is no colour property in FontData class.

Make sure you don't mix SWT and AWT colors, and if you build a Color object, make sure you dispose it. You want something like:
final Color myColor = new Color(getDisplay(), 102, 255, 102);
myLabel.setForeground(color);
myLabel.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e)
{
myColor.dispose();
}
});
Or you can just use the built-in system colors:
myLabel.setForeground(getDisplay().getSystemColor(SWT.COLOR_GREEN));
(Do not dispose the system colors.)

myLabel.setForeground(Color fg).
color : The Color class is used to encapsulate colors in the default sRGB color space or colors in arbitrary color spaces identified by a ColorSpace.
For more information : see this
For green it'd be something like : myLabel.setForeground(new org.eclipse.swt.graphics.Color(getDisplay(), 102, 255, 102));

Related

Java color the head of a table

I am trying to color the header of a jtable and I have no idea what my fault is, I appreciate the help
I leave part of the code:
// style jtable
jTable_registry.getTableHeader().setFont(new Font("Segoe UI", Font.BOLD, 12));
jTable_registry.getTableHeader().setOpaque(false);
jTable_registry.getTableHeader().setBackground(new Color(32, 136, 203));
jTable_registry.getTableHeader().setForeground(new Color(255, 255, 255));
jTable_registry.setRowHeight(25);
I'm doing the test to try to get what I want and in that simple way I can't change the background color of the header, it is still the default color ...
Thanks in advance
The code works and makes the change of background color to the table headers, the problem I found with the Nimbus visual theme which uses Painter to render the headers and make them noticeable with effects and such for which it will ignore the instruction in a certain way: setBackground, this does not happen with Metal, GTK or the one that comes with the operating system.
private void Theme() {
try {
if(UIManager.getLookAndFeel().toString().contains("Nimbus"))
UIManager.put("nimbusBlueGrey",new Color(32,136,203));
} catch(Exception e) {
e.printStackTrace();
}
}
The above modifies pre-established values within this Look And Feel through: putusing a form of Key - Value pairs.

How to change BackGround of JMonthChooser in Java

Here I am using JMonthChooser and JYearChooser. So how to Change BackGround of JMonthChooser and JYearChooser is there any Idea. how to do it.
I am using Netbeans.
I assume that you use toedter's JCalendar, that you can add to NetBeans'palette.
In this case you have to make it in 3 times for a WHITE background, 2 for other background's colors(3rd point of the belowed list is not useful in this case).
get the JCombobox (Java Component). You have to cast it into a JComboBox because the method getComboBox() returns a java.awt.Component.
javax.swing.JComboBox box = (javax.swing.JComboBox) monthChooser.getComboBox();
Modify the JComboBox's Renderer to change list's background (more examples here).
box.setRenderer(new javax.swing.DefaultListCellRenderer() {
#Override
public void paint(java.awt.Graphics g) {
setBackground(new java.awt.Color(255, 255, 255));
setForeground(java.awt.Color.BLACK);
super.paint(g);
}
});
Set the "collapsed list" (selected) background (WHITE only)
box.setOpaque(false);
Hope that help.
Actually JCalender is made of multiple components. So, if you want to change background or foreground of it, then first you have to traverse from all different subcomponents of it and then change each's background color.
In my case:
JDateChooser jdatechooser = new JDateChooser();
//to change background color : <br>
for( Component c : jDateChooser1.getComponents()){<br>
((JComponent)c).setBackground(Color.YELLOW); // whatever color you want to choose<br>
}

JXdatepicker : Edit the red cross Color of unselectable Dates

I'm working on JXdatepicker and i can't modify the red color of unselectable dates,
I found a subject about it, but it doesn't working "setting the following UIDefaults property JXMonthView.unselectableDayForeground
any help please,
since i am in hurry to answer this
but you can use the swingx source code
from here
https://github.com/cuba-platform/swingx-core/tree/master/src/main/java/org/jdesktop/swingx
then go to the function initMonthview in JXDatepicker.java
overwrite it with this one
Color.Black you can use your own color.
This fix the problem when you have a plaf with the jlabel text color as white and it hides with the background color of JXdatepicker.
private void initMonthView() {
_monthView = new JXMonthView();
// _monthView.setSelectionModel(new SingleDaySelectionModel());
_monthView.setTraversable(true);
_monthView.addPropertyChangeListener(getMonthViewListener());
_monthView.setDayForeground(Calendar.SUNDAY, Color.black);
_monthView.setDayForeground(Calendar.MONDAY, Color.black);
_monthView.setDayForeground(Calendar.TUESDAY, Color.black);
_monthView.setDayForeground(Calendar.WEDNESDAY, Color.black);
_monthView.setDayForeground(Calendar.THURSDAY, Color.black);
_monthView.setDayForeground(Calendar.FRIDAY, Color.black);
_monthView.setDayForeground(Calendar.SATURDAY, Color.black);
}
and then set the color you like then export it as a jar have fun.
or rather you can do a hard job of pluggable Look and Feel.
alternatively you can create a new constructor in the JXDatepicker.java
which takes the object of JXMonthview with the particular set of colors
means create a object of
_monthView = new JXMonthView();
// _monthView.setSelectionModel(new SingleDaySelectionModel());
_monthView.setTraversable(true);
_monthView.addPropertyChangeListener(getMonthViewListener());
_monthView.setDayForeground(Calendar.SUNDAY, Color.black);
_monthView.setDayForeground(Calendar.MONDAY, Color.black);
_monthView.setDayForeground(Calendar.TUESDAY, Color.black);
_monthView.setDayForeground(Calendar.WEDNESDAY, Color.black);
_monthView.setDayForeground(Calendar.THURSDAY, Color.black);
_monthView.setDayForeground(Calendar.FRIDAY, Color.black);
_monthView.setDayForeground(Calendar.SATURDAY, Color.black);
now add
create a new constructor like
JXDatepicker(JXMonthView temp){
_monthview = temp;
JXDatepicker();
}
export it as jar use it.

Nimbus look-and-feel changes color of JButtons

I have a JButton with an icon on it.
I want the background color of the JButton to be the same as the icon.
The following code works fine in the standard look-and-feel :
button.setBackground(new Color(212,208,199));
But when I change my look-and-feel to Nimbus, then the background color of the JButton is much lighter.
The JButton still changes its background color when I change the color in button.setBackground(), but I have no idea to what color I need in Nimbus to get the same color as the background color of the JButton.
Of course I could try to find the color by sight by trying all values, but there should be a simpler way.
I also tried changing the background color via the following code, but with the same result :
UIDefaults defaults = UIManager.getLookAndFeelDefaults();
defaults.put("Button.background",new Color(212,208,199));
How can I change the background color of my JButton in Nimbus, so that it merges with the background color of the containing icon ?
Below are some pictures of the button with default LaF, nimbus LaF (same code), and nimbus LaF (red color) :
Default LaF, using button.setBackground(new Color(212,208,199)) :
Nimbus LaF, using button.setBackground(new Color(212,208,199)) :
Nimbus LaF, using button.setBackground(Color.red) :
please why Table.background use Button.background instead
for more keys to see Nimbus Default
more about Background for JButton in Nimbus
maybe??? (no idea from your descriptions), maybe there no reason to change Background, have to look at JButtons API
methods
JButton.setBorderPainted(false);
JButton.setBorder(null);
JButton.setFocusable(false);
JButton.setMargin(new Insets(0, 0, 0, 0));
JButton.setContentAreaFilled(false);
JButton.setIcon(someIcon);
JButton.setRolloverIcon(someIcon);
JButton.setPressedIcon(someIcon);
JButton.setDisabledIcon(someIcon);
Use an image with no transparency and the 'background' will be whatever color is in the BG of the image.
The icons are jpg files (with no transparency) but only fill a part of the whole button..
See this question for tips on how to get buttons that are the exact size of the icon. The image below is formed from 4 buttons and 5 labels with the icons cut from a single image.
..the text below the icon would still show the button background color..
Oh right. Text as well.
In that case, the best strategy is to create an image with a transparent BG from the existing image, then use it in whatever button (with whatever GB color) it comes.
To do that, get a PNG of the image (PNG is not lossy like JPG), and use a dedicated image editor to save it with a transparent BG. But if we are forced to do it at run-time, consider using something like this:
This image actually demonstrates the lossy nature of JPG. We have to go more & more 'distant' from the base BG color to try and pick up all pixels that are outside the original icon. Even at its loosest interpretation of similar color, I still see some spots I would prefer removed.
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.net.URL;
class ImageBackgroundCleaner {
public static BufferedImage clearImageBackground(BufferedImage solidBG, int distance) {
int w = solidBG.getWidth();
int h = solidBG.getHeight();
BufferedImage transparentBG = new BufferedImage(
w,
h,
BufferedImage.TYPE_INT_ARGB);
int c = solidBG.getRGB(0, 0);
for (int xx=0; xx<w; xx++) {
for(int yy=0; yy<h; yy++) {
int s = solidBG.getRGB(xx, yy);
if (getColorDistance(s,c)>=distance) {
transparentBG.setRGB(xx, yy, s);
}
}
}
return transparentBG;
}
public static int getColorDistance(int color1, int color2) {
Color c1 = new Color(color1);
Color c2 = new Color(color2);
int r1 = c1.getRed();
int g1 = c1.getGreen();
int b1 = c1.getBlue();
int r2 = c2.getRed();
int g2 = c2.getGreen();
int b2 = c2.getBlue();
return Math.abs(r1-r2) + Math.abs(g1-g2) + Math.abs(b1-b2);
}
public static void main(String[] args) throws Exception {
URL url = new URL("http://i.stack.imgur.com/Yzfbk.png");
BufferedImage bi = ImageIO.read(url);
final BufferedImage img = bi.getSubimage(39, 21, 40, 40);
Runnable r = new Runnable() {
#Override
public void run() {
JPanel gui = new JPanel(new GridLayout(3,0,5,5));
gui.setBackground(Color.RED);
JLabel original = new JLabel(new ImageIcon(img));
gui.add(original);
int start = 12;
int inc = 3;
for (int ii=start; ii<start+(8*inc); ii+=inc) {
gui.add(new JLabel(new ImageIcon(clearImageBackground(img, ii))));
}
JOptionPane.showMessageDialog(null, gui);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}
The question still ponders me, as I would like to know why nimbus changes the outcome of setBackground() ... I assume nimbus applies some kind of color mask, which slightly changes the colors slightly?
But : in my project the buttons are all in a separate JPanel, and the Jpanels which require the nimbus LaF don't have any buttons, so I solved the problem by using the default LaF for the buttons JPanel, and only use the nimbus LaF in the JPanels where I need it.
Sorry, that I didn't think of this before (I somehow assumed the LaF had to apply to the whole project).
So problem solved, but still curious to an answer about the nimbus color handling ....
// you can always try this, it worked for me
button.setContentAreaFilled(false);
button.setOpaque(true);

Howto make JButton with simple flat style?

whats the most simple way to make a JButton only show the background color? I don't need any other effects like borders, 3D-look or hover-highlighting.
Thanks in advance.
I don't know if I have missed a point...
But I usually do somtehing like this:
button.setBorderPainted(false);
button.setFocusPainted(false);
button.setContentAreaFilled(false);
Just set the colours and the border:
private static JButton createSimpleButton(String text) {
JButton button = new JButton(text);
button.setForeground(Color.BLACK);
button.setBackground(Color.WHITE);
Border line = new LineBorder(Color.BLACK);
Border margin = new EmptyBorder(5, 15, 5, 15);
Border compound = new CompoundBorder(line, margin);
button.setBorder(compound);
return button;
}
Use setOpaque(false); to make the background transparent.
How about
yourButton.setBorder(null);
?
You may want to use a JLabel with a MouseListener instead... unless you're tied to using a JButton or ActionListener in some way.
First, set your look and feel to something cool, like 'metal'.
try
{
for (UIManager.LookAndFeelInfo lnf :
UIManager.getInstalledLookAndFeels()) {
if ("Metal".equals(lnf.getName())) {
UIManager.setLookAndFeel(lnf.getClassName());
break;
}
}
} catch (Exception e) { /* Lazy handling this >.> */ }
Then do something like this:
my_jbutton.setBackground(new Color(240,240,241);
If the color of your button is exactly equals to swing control color (240,240,240), windows will apply windows-like button style to the button, otherwise, the button assumes a simple flat style.
I think you could do it like this
JButton button = new JButton("Click Me!!");
button.setBorderPainted(false);
button.setBackground(new Color());// inside the brackets your rgb color value like 255,255,255
button.setFocusPainted(false);
you can use the color picker to get rgb code(just search color picker)

Categories

Resources