let’s play in the look and feel!

It – Hardware & Software | | 4 views

Looking and feeling will be changed!

There is “free look and feeling” in one of the major feature of Swing.Looking and feeling, GUI design “of the eye which was seen”.For example, with Windows and Mac OS, design of the button and the menu being different, now the shank.This, is looking and feeling.

With Swing, it offers this look and feeling, in the form which becomes independent, it is designed in such a way that it can set that.So, whether really some kind of look and feeling being prepared, how doing, whether if it should have modified looking and feeling, making the sample program, it will keep explaining.

package jp.allabout;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;

public class SampleApp extends JFrame implements ActionListener {

public SampleApp () {
this.setSize(new Dimension(200,200));
JButton button = new JButton(“click”);
this.add(button,BorderLayout.SOUTH);
JCheckBox check = new JCheckBox(“dummy check box”,true);
this.add(check,BorderLayout.NORTH);
this.add(this.getRadioPanel(),BorderLayout.CENTER);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private JPanel getRadioPanel(){
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
ButtonGroup group = new ButtonGroup();
UIManager.LookAndFeelInfo[] arr = UIManager.getInstalledLookAndFeels();
for(UIManager.LookAndFeelInfo info:arr){
JRadioButton radio = new JRadioButton(info.getName());
group.add(radio);
radio.setActionCommand(info.getClassName());
radio.addActionListener(this);
panel.add(radio);
}
group.setSelected(group.getElements().nextElement().getModel(), true);
return panel;
}

public static void main(String[] args) {
new SampleApp().setVisible(true);
}

@Override
public void actionPerformed(ActionEvent ex) {
JRadioButton radio = (JRadioButton)ex.getSource();
String laf = radio.getActionCommand();
System.out.println(laf);
try {
UIManager.setLookAndFeel(laf);
SwingUtilities.updateComponentTreeUI(this);
} catch (ClassNotFoundException ex2) {
ex2.printStackTrace();
} catch (InstantiationException ex2) {
ex2.printStackTrace();
} catch (IllegalAccessException ex2) {
ex2.printStackTrace();
} catch (UnsupportedLookAndFeelException ex2) {
ex2.printStackTrace();
}

}
}

this is, is a simple example to switch the look and feel. look and feel that is provided will be displayed as radio buttons. click the radio button when you switch this, window display will change on the fly. it should be noted, there is a check box to the other and push buttons, these are the dummy was prepared to make it easier to see the difference in the look and feel. there is no functional anything.

let’s describe the source code in

operation of the look and feel


Windowslook and feel that are provided by the.

WindowsofJava 5results when you run the program in the figure above is later. in this environment,, the following standard4provides you with the look and feel of the type.

-Metal——Javaoriginal look and feel of the. Java 5in the following, Ocean becomes the standard theme called, fairly clean, Windowsmaking it the look and feel less discomfort even if the use is.

-CDE/Motif——Linux/UNIXlook and feel has been used since ancient times in the world of.

-Windows——Windows XPare used in later, is a new look and feel. Vistain, Vistasuch a design will appear in almost the same.

-Windows Classic——Windows 2000look and feel has been used to.

in, these look and field what is set by how. a look at the source code, ationPerformedprovides you with the processing of change look and feel to the method.

UIManager.setLookAndFeel( look & feel );

first, UIManagerof classsetLookAndFeelthe method, change the look and feel to use. the argument, classes or look and feel, the class nameStringspecify the. changing the look and feel, this is the only only. but, since this is only a configuration change is made only, already appears inGUIsuch as the display is intact.

SwingUtilities.updateComponentTreeUI(this);

there, already appears in the currentGUIyou do have to refresh the look and feel of. this isSwingUtilitiesofupdateComponentTreeUIdone in the method. if you pass the component to the argument, to update all the components that are incorporated into its components and component. herethis (window that displays) if you pass the, all kind of components that are incorporated in the window that is not called to be updated.

determine the look and feel

in, i look and feel that this, why that is stored where and how. what look and feel there is any, what should i by examining how. here, i have examined as follows the look and feel available.

UIManager.LookAndFeelInfo[] arr = UIManager.getInstalledLookAndFeels();

UIManagerof class getInstalledLookAndFeels is, is intended to get all the look and feel available. this is, the look and feel UIManager.LookAndFeelInfo returns an array of a class called. this class is, is intended to manage information about the look and feel.

thisUIManager.LookAndFeelInfofrom, getNameorgetClassNameby calling a method such as, you can get the look and feel or class names. obtained with the class namesetLookAndFeelif you call the, can be switched to the look and feel, so is.

the followingMetallet’s change the theme.

Metalworking with themes

also in the look and feel, that you have a special function Metal is. this is, Javawas created as a new look and feel of, Javais unique. so to speak Javathe basic look and feel so, it is also incorporated features not found in other look and feel. ——in, modify the class of the previous, Metallet’s make a sample to change the look and feel of the.

package jp.allabout;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.metal.*;

public class SampleApp extends JFrame implements ActionListener {
private HashMap themes;

public SampleApp(){
try {
UIManager.setLookAndFeel(“javax.swing.plaf.metal.MetalLookAndFeel”);
SwingUtilities.updateComponentTreeUI(this);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
themes = new HashMap ();
themes.put(“steel”,(MetalTheme)(new DefaultMetalTheme()));
themes.put(“ocean”,(MetalTheme)(new OceanTheme()));

this.setSize(new Dimension(200,200));
JButton button = new JButton(“click”);
this.add(button,BorderLayout.SOUTH);
JCheckBox check = new JCheckBox(“dummy check box”,true);
this.add(check,BorderLayout.NORTH);
this.add(this.getRadioPanel(),BorderLayout.CENTER);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private JPanel getRadioPanel(){
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
ButtonGroup group = new ButtonGroup();
Iterator iterator = themes.keySet().iterator();
while(iterator.hasNext()){
String name = (String)iterator.next();
JRadioButton radio = new JRadioButton(name);
group.add(radio);
radio.setActionCommand(name);
radio.addActionListener(this);
panel.add(radio);
}
return panel;
}

public static void main(String[] args) {
new SampleApp().setVisible(true);
}

@Override
public void actionPerformed(ActionEvent ex) {
JRadioButton radio = (JRadioButton)ex.getSource();
String theme = radio.getActionCommand();
System.out.println(theme);
MetalLookAndFeel.setCurrentTheme(themes.get(theme));
try {
UIManager.setLookAndFeel(new MetalLookAndFeel());
SwingUtilities.updateComponentTreeUI(this);
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
}

Metalthe theme of steel ocean switch.

Metalis a description of the theme.

Metalchange the theme

Metaltheme of the, Java 5had been used previously steel and, Java 5has been used since the ocean of2has been provided by the standard one, but. these, to change it as follows:.

MetalLookAndFeel.setCurrentTheme( MetalTheme );
SwingUtilities.updateComponentTreeUI( component );

Metaltheme of the, MetalThemewill be provided as a class that inherits from the class. steelis DefaultMetalTheme , oceanis OceanTheme has been prepared as a class called. MetalLookAndFeelofsetCurrentThemeif you set the theme of these classes is, Metaltheme will be changed. but, as it is in those that are already displayed on the screen will not be changed, as usualSwingUtilities.updateComponentTreeUIinthisyou have to update the display of the.

Metalcreate a theme

to tell the truth, this theme is, it can be surprisingly easy to make. DefaultMetalThemecreate a class that inherits from the class, if you can have as a property value of each color to be used there, it would be the original theme it just.

class MyTheme extends DefaultMetalTheme {
public String getName() { return “MyTheme”; }
private final ColorUIResource primary1 = new ColorUIResource(100, 200, 200);
private final ColorUIResource primary2 = new ColorUIResource(100, 200, 200);
private final ColorUIResource primary3 = new ColorUIResource(200, 200, 200);
protected ColorUIResource getPrimary1() { return primary1; }
protected ColorUIResource getPrimary2 () {return primary2; }
protected ColorUIResource getPrimary3 () {return primary3; }
private final ColorUIResource secondary1 = new ColorUIResource (100, 255 and 255);
private final ColorUIResource secondary2 = new ColorUIResource (100, 255 and 255);
private final ColorUIResource secondary3 = new ColorUIResource (225, 255 and 255);
protected ColorUIResource getSecondary1 () {return secondary1; }
protected ColorUIResource getSecondary2 () {return secondary2; }
protected ColorUIResource getSecondary3() { return secondary3; }
}

original theme MyTheme when displayed in this way become.

for example, i tried to make this sample. this, in the manner just describedsetCurrentThemelook set to, in the original themeGUIyou can view the. MyThemea look at the class, primary1, primary2, primary3, secondary1, secondary2, secondary3that6you can see, that you have the two properties. and, to each ColorUIResource instance of a class that has been set. this is, with the value of the colors used in the theme, Coloras with classes, etc.RGBallows you to create to specify the brightness of each color.

XMLlet’s define a theme in the file.

SynthinXMLmake a theme of the definition!

Java 5from, more1one as a new theme Synth those that have been added. this is, XMLto define the theme is theme is interesting that. that is, styles, such as buttonXMLprepared as a file, i read this to use the theme. thisSynththe theme is, and will use the following form:.

SynthLookAndFeel synth = new SynthLookAndFeel();
Class aClass = SampleApp.class;
java.io.InputStream is = aClass.getResourceAsStream(“file1.xml”);
synth.load(is, aClass);
UIManager.setLookAndFeel(synth);
SwingUtilities.updateComponentTreeUI(this);

here, SampleAppin the class file1.xml thatXMLput the sample code was written in the form that use the file. in the same location as the class filefile1.xmlif you have a definition of the theme in a file named, will be used as the theme reads it.

Synthto take advantage of the, you need to take steps such as:.

1. SynthLookAndFeelto create an instance of.
2. Synthclass to which to apply (here, SampleApp) ofClassto provide a.
3. ClassofgetResourceAsStreamcall the, specified in the argumentXMLfor reading the resource from a fileInputStreamto create a.
4. SynthLookAndFeelfor instanceloadin, InputStreamandClassspecified in the argument, respectively, XMLload a theme from the.
5. UIManager.setLookAndFeelinSynthLookAndFeelto set the look and feel.
6. SwingUtilities.updateComponentTreeUIto update the display is.

the difference is to work with other themes and much, it is not so difficult once you know the procedure.

thisSynththe most important feature of the, completely separated from the source code would a theme is that. XMLbecause we simply defined in, even non-programmers, you can easily create a theme anyone who knows even how to define.

current, newJDKversion is being developed as, newly Nimbus i have been look and feel that is provided, this isSynthusing theXMLhas defined the display in the file. Synthwith the, i would look and feel can also create full-fledged. in, let’s make a really simple example. this theme.

XMLlet’s create a file.

XMLto define the file


Synthyou create your own look and feel by using the.

SynthofXMLfile, the most commonXMLmake as. here, push button as a sample, check box, that defines the display of radio buttonsXMLi tried to make a file.

<?xml version=”1.0″ encoding=”UTF-8″?>
<synth>
<style id=”button”>
<opaque value=”TRUE” />
<font name=”Monospaced” size=”18″ style=”BOLD” />
<state value=”PRESSED”>
<color value=”RED” type=”BACKGROUND” />
<font name=”Monospaced” size=”18″ style=”PLAIN” />
</state>
</style>
<bind style=”button” type=”region” key=”Button” />
<style id=”checkbox”>
<imageIcon id=”check_off” path=”check_off.png” />
<imageIcon id=”check_on” path=”check_on.png” />
<property key=”CheckBox.icon” value=”check_off” />
<state value=”SELECTED”>
<property key=”CheckBox.icon” value=”check_on” />
</state>
</style>
<bind style=”checkbox” type=”region” key=”Checkbox” />
<style id=”radiobutton”>
<imageIcon id=”radio_off” path=”radio_off.png” />
<imageIcon id=”radio_on” path=”radio_on.png” />
<property key=”RadioButton.icon” value=”radio_off” />
<state value=”SELECTED”>
<property key=”RadioButton.icon” value=”radio_on” />
</state>
</style>
<bind style=”radiobutton” type=”region” key=”RadioButton” />
</synth>

here, of check boxes and radio buttonsON/OFFfor display check_on.png check_off.png radio_on.png radio_off.png you are using the files, such as. these are alsoXMLas well as, please prepare in the same location as the class file to be used.

Synththeme of the, <synth>the description in the tag. in this, following2the types of tags are available.

<style>tag——tag that defines the style.
<bind>tag——is the tag to bind to a component style that you created.

first<style>to define the style in, it<bind>to bind in, description will be in the form of. <style>changes that are described in the, has been prepared over a very wide range for each component of each. referring to the sample listed here, please try to rewrite the value and variety. 1one1i know it should come little by little the work of the two tag.

Incoming search terms:

  • create your own synth look and feel xml (1)
  • java synth look and feel <style id=checkbox> properties (1)
  • radio button synth laf (1)