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>
}
Related
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.
I have a JPanel in which I need to add bunch of JLabels at a required coordinates. These JLabels will have key Listeners assigned to them that will determine new position using arrow keys.
To be more specific I know how to do it when there is only one JLabel but whenever I put a more of them the things mess up. while I use arrow key the first JLabel moves but all other JLabel disappears.
Can Anyone give me some hints to write a method to put a JLabel in a specific coordinate and also move them using arrow key later without making other JLabels dissapear?
Huge Thanks in Advance
You can try using JDesktopPane or JLayeredPane, it works the same as the JPanels, but you won't use layouts, with these you will use Bounds, you always have to set the bound of a jlabel like this.
JLabel label = new JLabel("Hello");
label.setBounds(0, 0, 100, 20);
//label.setBounds(x, y, width, height);
pane.add(label)
if you need to move that label, then you can use something like
int xx = label.getBounds().getX();
int yy = label.getBounds().getY();
int ww = label.getBounds().getWidth();
int hh = label.getBounds().getHeight();
//to the right 10 units
xx+=10;
label.setBounds( xx, yy, ww, hh );
I assume you are using repaint() to update the UI. Btw, upon which component you are calling repaint()?
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));
How do I get a JLabel displaying a HTML string to appear greyed out (which is the behaviour of JLabels that don't display HTML text)? Is there another way than actually changing the colour myself by modifying the foreground property?
JLabel label1 = new JLabel("Normal text");
JLabel label2 = new JLabel("<html>HTML <b>text</b>");
// Both labels are now black in colour
label1.setEnabled(false);
label2.setEnabled(false);
// label1 is greyed out, label2 is still black in colour
Thank you very much for all of your responses. From what I gather, it seems that Java doesn't support automatic greying out of JLabels when they use HTML text. Suraj's solution has come closest to the fix considering the limitations.
I have however, tried a different out-of-the box approach, where I have put the HTML text JLabels inside of an inner JPanel and did this:
mInnerPanel.setEnabled(shouldShow); //shouldShow is a boolean value
Which hasn't worked. Any suggestions for this way?
EDIT: Added implemented solution.
If text is HTML, the text wont be grayed out because of the following code in BasicLabelUI#paint()
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
v.paint(g, paintTextR);
}
As you can see if the text is html, then the View is used to paint and it is not checked wheter the label is enabled or not.
Hence we need to do it explictly as shown below:
label2.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if (!evt.getPropertyName().equals("enabled"))
return;
if (evt.getNewValue().equals(Boolean.FALSE))
label2.setText("<html><font color=gray>HTML <b>text</b></html>");
else
label2.setText("<html><font color=black>HTML <b>text</b></html>");
}
});
Implemented solution:
Color foreground = (shouldShow) ? SystemColor.textText : SystemColor.textInactiveText;
for (Component comp : mInnerPanel.getComponents())
{
comp.setForeground(foreground);
}
Caved in and used setForeground in the end, as it appears that Java seems to explicitly ignore the enabled property when painting JLabels so long as it contains HTML text. See also #Suraj's answer, for "pure" solution.
I would suggest the following, which is combination of two solutions provided here:
public class HtmlLabel extends JLabel{
public void setEnabled(boolean enabled){
if(getClientProperty(BasicHTML.propertyKey) != null ){
Color foreground = (enabled) ? SystemColor.textText : SystemColor.textInactiveText;
setForeground(foreground);
}
super.setEnabled(enabled);
}
}
You can specify the font color in the HTML.
Override the paint method in the UI, set the client property BasicHTML.propertyKey to null if it is disabled and call super...
I am developing a Java Desktop Application. In it I have 4 JButtons on a JPanel. Now I want that whenever a button is clicked its background color changes to some other color (say orange) to represent that it has been clicked and the background color of all other 3 buttons reset to their default color (in case any of them had Orange background color).
So, at one time only one button can have the orange color.
The current approach that I have applied is that I have implemented the following code in the xxxActionPerformed() method of JButton button1
button1.setBackground(Color.Orange);
button2.setBackground(Color.Gray);
button3.setBackground(Color.Gray);
button4.setBackground(Color.Gray);
and similarly for the rest three buttons.
Now in actual, I don't want the backgroud color as Gray (for unclicked button). Instead, I want the default background color so that the backgroud color will adjust itself to the look-and-feel of the GUI according to the end-user's platform's look-and-feel.
Q1. How can I get the default background color?
Q2. Is this the correct approach to do this or Is there any other mechanism through which I can group all the four buttons in a button group so that only one can have the specified property at one time (like radio buttons)?
just use null to use the default color:
button1.setBackground(Color.ORANGE);
button2.setBackground(null);
...
consider using JToggleButtons with a ButtonGroup, set the Icon and PressedIcon of the buttons. No need to change the background color.
button1 = new JToggleButton(new ImageIcon("0.jpg"));
button1.setSelectedIcon(new ImageIcon("1.jpg"));
button2 = new JToggleButton(new ImageIcon("0.jpg"));
button2.setSelectedIcon(new ImageIcon("2.jpg"));
...
ButtonGroup group = new ButtonGroup();
group.add(button1);
group.add(button2);
...
You can get the standard background color for buttons from the UIManager:
button1.setBackground(UIManager.getColor("Button.background"));
As far as I know, the keys could change in different look & feels. Here is a nice webstart app that shows all available keys:
http://tips4java.wordpress.com/2008/10/09/uimanager-defaults/
Here's a cross-section of "Button.background" based on this example:
*** Metal javax.swing.plaf.metal.MetalLookAndFeel 636 entries
Button.background: javax.swing.plaf.ColorUIResource[r=238,g=238,b=238]
*** Nimbus com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel 1052 entries
Button.background: com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel$NimbusProperty#60961dff
*** CDE/Motif com.sun.java.swing.plaf.motif.MotifLookAndFeel 550 entries
Button.background: javax.swing.plaf.ColorUIResource[r=174,g=178,b=195]
*** Mac OS X com.apple.laf.AquaLookAndFeel 705 entries
Button.background: com.apple.laf.AquaNativeResources$CColorPaintUIResource[r=238,g=238,b=238]
Q1.: To get the GUI color of the button, just do this
button1.setBackground(Color.Orange);
button2.setBackground(java.awt.SystemColor.control);
button3.setBackground(java.awt.SystemColor.control);
button4.setBackground(java.awt.SystemColor.control);
With this class (java.awt.SystemColor.*), you can get the color of all elements of your user interface.
Q2.: I've never heard about grouping buttons. Then, I canĀ“t answer you this one.
Hope it helps.
If you wish you can redesign your whole button UI
public class buttonUI extends javax.swing.plaf.basic.BasicButtonUI{
buttonUI(JButton b){
b.setContentAreaFilled(false);
}
#Override
public void paint(Graphics g,JComponent c){
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
AbstractButton b = (AbstractButton) c;
g2d.setFont(font.deriveFont(11f));
Dimension d = b.getSize();
FontMetrics fm = g2d.getFontMetrics();
g2d.setColor(new Color(100,100,100));
String caption = b.getText();
int x = (d.width - fm.stringWidth(caption)) / 2;
int y = (d.height + fm.getAscent()) / 2 - 2;
g2d.drawString(caption, x, y);
} }
and in your main piece of code use like
jButton1.setUI(new buttonUI(jButton1));
This how I use it .. you can have your own way too
have you looked into the decorator pattern in java you pass a button into a method which decorates it depending on whether the button is the current active one, for example if it hovered over.
public Jbutton decorateButton(JButton b, boolean isHoveredOver){
if(isHoveredOver)
b.setBackground(getContentPane().getBackground().GREEN);
return b;
}
you call this method from the MouseEvent or ActionEvent methods and just issue a repaint() after. put all your buttons into an array or vector and loop through it passing each one in to the decorateButton method and give the initail boolean value of false then on event set it to true.
This way the initial value is default and the button is then decorated upon action the buttons will appear to be acting as part of a group