Swing GUI Builder and Java Framework using XML
  JAVA  SWING  XML  GUI  HTTP  APPLET  APPLICATION  JAVA WEB START   
JEasy
Download
License & Purchase
Demo
Development
 

Menu


MyHello World

Now you may enhance the Hello World example with a better look and bring some action to it.

Building a GUI

To get a GUI looking like this:

Please copy the lines below into a file MyHelloWorld.xml

<?xml version='1.0' encoding='ISO-8859-1' ?>
<jeasy>
<JEStart>
	<id>AC_action</id>
	<startIdApplication>FR_Main</startIdApplication>
	<global>GL_Standard</global>
</JEStart>
<JEButton>
	<id>BT_TB_Refresh</id>
	<text>Refresh</text>
	<constraints>CS_ToolBar</constraints>
	<focusPainted>false</focusPainted>
	<margin>IN_Button</margin>
</JEButton>
<JEConstraints>
	<id>CS_East</id>
	<anchor>EAST</anchor>
	<fill>NONE</fill>
	<insets>IN_Standard</insets>
</JEConstraints>
<JEConstraints>
	<id>CS_LastOfLine</id>
	<anchor>WEST</anchor>
	<gridwidth>REMAINDER</gridwidth>
	<fill>NONE</fill>
	<insets>IN_Standard</insets>
</JEConstraints>
<JEConstraints>
	<id>CS_LastOfLineWest</id>
	<anchor>WEST</anchor>
	<gridwidth>REMAINDER</gridwidth>
	<weightx>1.0</weightx>
	<fill>NONE</fill>
	<insets>IN_Standard</insets>
</JEConstraints>
<JEConstraints>
	<id>CS_SameLine</id>
	<anchor>WEST</anchor>
	<fill>NONE</fill>
	<insets>IN_Standard</insets>
</JEConstraints>
<JEConstraints>
	<id>CS_ToolBar</id>
	<insets>IN_ToolBar</insets>
	<anchor>WEST</anchor>
	<fill>NONE</fill>
</JEConstraints>
<JEDataField>
	<id>DF_Name</id>
	<label>type your name</label>
	<dataType>DT_Char35</dataType>
</JEDataField>
<JEDataType>
	<id>DT_Char35</id>
	<length>35</length>
	<primitiveDataType>CHAR</primitiveDataType>
</JEDataType>
<JEFont>
	<id>FO_Big</id>
	<fontName>Dialog</fontName>
	<style>BOLD+ITALIC</style>
	<size>48</size>
</JEFont>
<JEFont>
	<id>FO_Standard</id>
	<fontName>Dialog</fontName>
	<style>PLAIN</style>
	<size>14</size>
</JEFont>
<JEFrame>
	<id>FR_Main</id>
	<width>800</width>
	<height>500</height>
	<north>MB_Repository</north>
	<center>PA_Main</center>
	<title>MyHello World</title>
</JEFrame>
<JEGlobal>
	<id>GL_Standard</id>
	<buttonConstraints>CS_SameLine</buttonConstraints>
	<labelConstraints>CS_East</labelConstraints>
	<textConstraints>CS_LastOfLine</textConstraints>
	<buttonFont>FO_Standard</buttonFont>
	<labelFont>FO_Standard</labelFont>
	<textFont>FO_Standard</textFont>
	<tabbedPaneFont>FO_Standard</tabbedPaneFont>
	<onENTERGotoNextField>true</onENTERGotoNextField>
</JEGlobal>
<JEGradientPaint>
	<id>GP_Panel</id>
	<rgbColor1>eaeaea</rgbColor1>
	<rgbColor2>fcfafa</rgbColor2>
	<point1x>0</point1x>
	<point1y>0</point1y>
	<point2x>0</point2x>
	<point2y>300</point2y>
</JEGradientPaint>
<JEGradientPaint>
	<id>GP_ToolBar</id>
	<rgbColor1>eaeaea</rgbColor1>
	<rgbColor2>fcfafa</rgbColor2>
	<point1x>0</point1x>
	<point1y>0</point1y>
	<point2x>0</point2x>
	<point2y>30</point2y>
</JEGradientPaint>
<JEInsets>
	<id>IN_Button</id>
	<top>0</top>
	<left>2</left>
	<bottom>0</bottom>
	<right>2</right>
</JEInsets>
<JEInsets>
	<id>IN_Standard</id>
	<top>1</top>
	<left>2</left>
	<right>8</right>
	<bottom>1</bottom>
</JEInsets>
<JEInsets>
	<id>IN_ToolBar</id>
	<top>2</top>
	<left>5</left>
	<bottom>2</bottom>
	<right>0</right>
</JEInsets>
<JELabel>
	<id>LA_Hello</id>
	<font>FO_Big</font>
	<antialiasing>true</antialiasing>
	<text>Hello World!</text>
</JELabel>
<JEMenuBar>
	<id>MB_Repository</id>
	<menus>ME_File</menus>
	<menus>ME_Help</menus>
</JEMenuBar>
<JEMenu>
	<id>ME_File</id>
	<text>File</text>
	<menuItems>MI_Exit</menuItems>
	<text lc="LC_DE">Datei</text>
</JEMenu>
<JEMenu>
	<id>ME_Help</id>
	<text>Help</text>
	<menuItems>MI_About</menuItems>
	<text lc="LC_DE">Hilfe</text>
</JEMenu>
<JEMenuItem>
	<id>MI_About</id>
	<text>About JEasy Repository</text>
	<mnemonic>A</mnemonic>
	<text lc="LC_DE">Ueber</text>
</JEMenuItem>
<JEMenuItem>
	<id>MI_Exit</id>
	<text>Exit</text>
</JEMenuItem>
<JEPanel>
	<id>PA_Main</id>
	<scrollPane>true</scrollPane>
	<layout>BorderLayout</layout>
	<north>PA_Main_ToolBar</north>
	<center>PA_Main_Center</center>
</JEPanel>
<JEPanel>
	<id>PA_Main_Center</id>
	<layout>GridBagLayout</layout>
	<gradientPaint>GP_Panel</gradientPaint>
	<elements>LA_Hello</elements>
</JEPanel>
<JEPanel>
	<id>PA_Main_ToolBar</id>
	<layout>FlowLayout</layout>
	<gradientPaint>GP_ToolBar</gradientPaint>
	<elements>DF_Name</elements>
	<elements>BT_TB_Refresh</elements>
</JEPanel>
</jeasy>

You will see some more jeasy objects defined inside the xml file. They are in alphabetically order. To give a button a more web like look, the focus painting and the content area filling are disabled. The margin references to an insets that defines the number of pixels until the border of the button. Setting the top and bottom to zero prevents from getting to height like it is in the standard margin.

JEContraints implements GridBagConstraints which are to use when a component is added to a panel using the LayoutManager GridBagConstraints. This LayoutManager is the preferred manager for all forms. The CS_LastOfLine for example is used for jeasy datafields. They are a combination out of a label and a textfield. The property <gridwidth>REMAINDER</gridwidth> let the textfields be the last of the line which means that the next component will start at a new line. A JEGradientPaint fills a panel beginning with color one and ending with color two. You will see this effect in many web pages. The label uses a bigger font and its antialiasing is true to prevent drawing edges. This is an enhancement of jeasy. You should use it for large font sizes.

The menu structure declares itself. In the textfield DF_Name you may input your name. The button behind will refresh the label.

Writing the program

Copy the lines below and paste them into your MyHelloWorld.java.

import java.awt.AWTEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentEvent;
import java.awt.event.ItemEvent;
import java.awt.event.MouseEvent;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import javax.swing.ListSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.DocumentEvent;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import de.cbse.jeasy.JECallBack;
import de.cbse.jeasy.JEFrame;
import de.cbse.jeasy.JELabel;
import de.cbse.jeasy.JEStart;
import de.cbse.jeasy.JEUtil;

public class MyHelloWorld implements JECallBack {
	static MyHelloWorld myHelloWorld;

	public static void main(String args[]) {
		myHelloWorld = new MyHelloWorld();
	}

	public MyHelloWorld() {
		InputStream is = null;
		try {
			is = this.getClass().getResourceAsStream("MyHelloWorld.xml");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.exit(-1);
		}
		JEUtil.readJEObj(is);
		JEStart jestart = JEStart.getInstance();
		JEFrame frame = (JEFrame) JEUtil.getJO(
		 jestart.getStartIdApplication());
		frame.setupComponent();
		frame.setVisible(true);
		JEUtil.registerJECallBack(this, "FR_Main");
	}

	public void callBack(AWTEvent e, String jeasyId) {
		System.out.println("MyHelloWorld.callBack() 
		 JEasyId: " + jeasyId + "\nEvent: " + e);
		int eid = e.getID();
		if (eid == ActionEvent.ACTION_PERFORMED) {
			if (jeasyId.equals("BT_TB_Refresh")) {
				JELabel jel = (JELabel)
				 JEUtil.getJO("LA_Hello");
				String text = JEUtil.getJC
				 ("DF_Name").getValue();
				jel.getLabel().setText
				 ("Hello "+text);
			}
		}
	}

	public void callBack(ChangeEvent arg0, 
		String arg1) {
	// TODO Auto-generated method stub

	}

	public void callBack(DocumentEvent arg0, 
		String arg1) {
	// TODO Auto-generated method stub

	}

	public void callBack(HyperlinkEvent arg0, 
		String arg1) {
	// TODO Auto-generated method stub

	}

	public void callBack(ItemEvent arg0, 
		String arg1) {
	// TODO Auto-generated method stub

	}

	public void callBack(TableModelEvent arg0, 
		String arg1) {
	// TODO Auto-generated method stub

	}

	public void callBack(TreeExpansionEvent arg0, 
		String arg1) {
	// TODO Auto-generated method stub

	}

	public void callBack(TreeModelEvent arg0, 
		String arg1) {
	// TODO Auto-generated method stub

	}

	public void callBack(ListSelectionEvent arg0, 
		String arg1, 
	 ListSelectionModel arg2) {
	// TODO Auto-generated method stub

	}

	public void callBack(MouseEvent arg0, 
		String arg1, 
	 DefaultMutableTreeNode arg2) {
	// TODO Auto-generated method stub

	}

	public void callBack(TreeSelectionEvent arg0, 
		String arg1, 
		TreePath arg2) {
	// TODO Auto-generated method stub

	}

	public void callBack(AWTEvent arg0, 
		String arg1, int arg2, int arg3) {
	// TODO Auto-generated method stub

	}

	public void callBackComponentEvent(
		ComponentEvent arg0, String arg1) {
	// TODO Auto-generated method stub

	}

	public void callBackContextHelp(int arg0, 
		String arg1) {
	// TODO Auto-generated method stub

	}

	public boolean callBackInputVerifier(String arg0, 
		String arg1, int arg2) {
		// TODO Auto-generated method stub
		return false;
	}

	public void callBackInvoke(String arg0) {
	// TODO Auto-generated method stub

	}

	public boolean callBackTimerTask(String arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	public void initJECallBack() {
	// TODO Auto-generated method stub

	}
	public void callBack(WindowEvent e, String id) {
		int eid = e.getID();
		if (eid == WindowEvent.WINDOW_CLOSING) {
			System.exit(0);
		}		
	}
}
After initialization of the jeasy objects and setting up the components you will see a new line:

JEUtil.registerJECallBack(this, "FR_Main");

This method will register your class for all events coming from the frame and all its children. Your class has to implement the JECallBack interface that defines all callBack for fetching any event. For your example the

public void callBack(AWTEvent e, String jeasyId) {
  System.out.println("MyHelloWorld.callBack() 
   JEasyId: " + jeasyId + "\nEvent: " + e);
  int eid = e.getID();
  if (eid == ActionEvent.ACTION_PERFORMED) {
    if (jeasyId.equals("BT_TB_Refresh")) {
      JELabel jel = (JELabel)JEUtil.getJO("LA_Hello");
      String text = JEUtil.getJC("DF_Name").getValue();
      jel.getLabel().setText("Hello "+text);
    }
  }
}

will do the work.
Pressing a button forces an ActionEvent. By checking the jeasy id you find the point where to implement the action. The method JEUtil.getJO(jeasyId) will give you the jeasy object. Another way is to use JEUtil.getJC(jeasyId). This method will give you a JEComponent that prevents from casting.

As the line jel.getLabel().setText("Hello "+text); demonstrates, it is easy to get the swing component out of the jeasy component.
You see that even though jeasy reads all out of xml, you have access to all created swing components. But, mostly you will not need them, because jeasy has a lot of own methods to work with the components in more comfortable way.

Have fun!