Saturday, 23 July 2011

JForm: Fill the form with style

After understanding and using swing toolkit, I understood that there is a allot of scope to develop Custom components easily.
I have developed a component called JForm which basically a button and its selection pops up a window if it is not visible.

Technically the JForm component is a combination of 2 components they are:

1. JButton
2. Window

JButton is a API of swing toolkit where as Window is a API of AWT.

Behaviour of JForm:

JForm is a usual button like JButton. but it is glued to a Window component. this Window takes a JPanel class as an arguments.

In the given example has 2 components they are 1. JTextArea  2. JButton [Submit] which are appended to a JPanel..

The below image shows the effect before selecting the JForm:






This image shown is the effect after selecting the JForm:

                                     



the below code is an example which includes code for JForm custom component and main() includes usage of JForm.


import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class JForm extends JButton {

Point location;
int X = 0;
int Y = 0;
int W = 0;
int H = 0;
JFrame frame;
MyWindow window;

public JForm(JFrame frame, JPanel panel) {

this.frame = frame;
this.setBackground(Color.gray);
this.setText("click me!...");
JForm.this.window = new MyWindow();
JForm.this.window.add(panel);
this.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {

if (window.isVisible() == false) {
JForm.this.window.setSize(10, 20);
location = getLocationOnScreen();
X = location.x;
Y = location.y;
JForm.this.window.setLocation(X - 5, Y + JForm.this.getHeight());
window.pack();
window.setVisible(true);
}else{
window.setVisible(false);
}
}
});
}

class MyWindow extends Window {
public MyWindow() {
super(JForm.this.frame);
frame.addComponentListener(new ComponentListener() {
public void componentResized(ComponentEvent evt) {
if (JForm.this.isShowing() == true) {
location = JForm.this.getLocationOnScreen();
X = location.x;
Y = location.y;
JForm.this.window.setLocation(X - 5, Y + JForm.this.getHeight());
}
}

@Override
public void componentHidden(ComponentEvent arg0) {
if (JForm.this.isShowing() == true) {
location = JForm.this.getLocationOnScreen();
X = location.x;
Y = location.y;
JForm.this.window.setLocation(X - 5, Y + JForm.this.getHeight());
}
}

@Override
public void componentMoved(ComponentEvent arg0) {
if (JForm.this.isShowing() == true) {
location = JForm.this.getLocationOnScreen();
X = location.x;
Y = location.y;
JForm.this.window.setLocation(X - 5, Y + JForm.this.getHeight());
}
}

@Override
public void componentShown(ComponentEvent arg0) {
if (JForm.this.isShowing() == true) {
location = JForm.this.getLocationOnScreen();
X = location.x;
Y = location.y;
JForm.this.window.setLocation(X - 5, Y + JForm.this.getHeight());
}
}
});
}

}

public static void main(String[] args) {
JFrame frame = new JFrame();

GridBagLayout l = new GridBagLayout();

GridBagConstraints c = new GridBagConstraints();
c.gridwidth = 40;
c.gridheight = 20;
frame.setLayout(l);
JPanel panel = new JPanel();
GridBagLayout pl = new GridBagLayout();
GridBagConstraints pC = new GridBagConstraints();
JTextArea ta = new JTextArea(5, 10);
panel.add(ta, pC);
JButton button = new JButton("Submit");
pC.gridx = 50;
pC.gridy = 40;
panel.add(button, pC);

panel.setBackground(Color.gray);
JForm form = new JForm(frame, panel);
l.setConstraints(form, c);
frame.getContentPane().add(form, c);
frame.setSize(200, 200);
frame.pack();
frame.setVisible(true);
}
}

Next time I will come up with a JForm with different shapes like rounded, rounded corners etc..  

Friday, 22 July 2011

JColorComboBox: JComboBox as Color Chooser

 
Swing toolkit provides a component called JColorChooser to choose colors. It allows users to select color from multiple color combinations. Some times our application may need simple component with an option to select only basic colors/ less number of color options unlike sepearate dialog with too may color options[JColorChooser]. This JColorComboBox may serve the need.

I wanted my color chooser to behave like JComboBox. The popup shows all 12 colors and their names, among all of them one color can be choosen. see the below image.


I created two classes
         1. JColorComboBox
         2. ColorRenderer
JColorComboBox extends the JComboBox and ColorRenderer extends JLabel and implements ListCellRenderer.


import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.swing.*;
/**
*
* @author sharath
*/
public class JColorComboBox extends JComboBox {

static Hashtable<String, Color> colors;

public JColorComboBox() {
super();
DefaultComboBoxModel model = new DefaultComboBoxModel();
Enumeration colorNames = addColors().keys();
while(colorNames.hasMoreElements()){
String temp = colorNames.nextElement().toString();
model.addElement(temp);
System.out.println("colors"+temp);
}
setModel(model);
setRenderer(new ColorRenderer());
this.setOpaque(true);
this.setSelectedIndex(0);
}
@Override
public void setSelectedItem(Object anObject) {
super.setSelectedItem(anObject);

setBackground((Color)colors.get(anObject));
setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 15));
if(anObject.toString().equals("BLACK") || anObject.toString().equals("DARK_GRAY")){
setForeground(Color.white);
}
}
public Color getSelectedColor(){

return this.getBackground();
} 

private Hashtable addColors(){

colors = new <String, Color>Hashtable();

colors.put("WHITE", Color.WHITE);
colors.put("BLUE", Color.BLUE);
colors.put("GREEN", Color.GREEN);
colors.put("YELLOW", Color.YELLOW);
colors.put("ORANGE", Color.ORANGE);
colors.put("CYAN", Color.CYAN);
colors.put("DARK_GRAY", Color.DARK_GRAY);
colors.put("GRAY", Color.GRAY);
colors.put("RED", Color.RED);
colors.put("PINK",Color.PINK);
colors.put("MAGENTA", Color.MAGENTA);
colors.put("BLACK", Color.BLACK);

return colors;
}
 
class ColorRenderer extends JLabel implements javax.swing.ListCellRenderer {
public ColorRenderer() {
this.setOpaque(true);
}
public Component getListCellRendererComponent(JList list, Object key, int index,
boolean isSelected, boolean cellHasFocus) {

Color color = colors.get(key);;
String name = key.toString();

list.setSelectionBackground(null);
list.setSelectionForeground(null);

if(isSelected){
setBorder(BorderFactory.createEtchedBorder());
} else {
setBorder(null);
}
setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 15));
setBackground(color);
setText(name);
setForeground(Color.black);
if(name.equals("BLACK") || name.equals("DARK_GRAY")){
setForeground(Color.white);
}

return this;
}
}
}

Demo Application:
The below code creates a JFrame by adding JColorComboBox to it.

 

/**
*
* @author sharath
*/
import java.awt.GridLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.*;
public class StartGUIApp {
public static void main(String[] args)throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
JFrame.setDefaultLookAndFeelDecorated(true);

JPanel panel = new JPanel();
JLabel label = new JLabel("Select Color");
JColorComboBox box = new JColorComboBox();
panel.add(box);
panel.add(label);
panel.setLayout(null);
label.setBounds(20,20,60,30);
box.setBounds(100,20,140,30);
panel.setSize(250, 100);

JFrame frame = new JFrame();
frame.getContentPane().add(panel);
frame.setSize(panel.getWidth(), panel.getHeight());
setFrameProperties(frame);
}
static private void setFrameProperties(JFrame frame) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

Saturday, 16 July 2011

Romain guy's blog

If you are interested in designing GUIs and building custom components with Swing toolkit, definitely Romain guy's blog can be good reference for you, he writes about his work on swing components, custom components, tips and tricks on designing and using components. and you can download the demos of his work.


You find more swing articles in 2005, 2006 archives.
His URL: http://www.curious-creature.org/

Popular posts

Demonstration of Java NullPointerException

NullPointerException causes and reasons Since Java is object oriented programming language, every thing is considered to be an object. and t...