These are features of JBuilder Professional and Enterprise.
For information on documentation conventions used in this tutorial, see "Documentation conventions."
For the complete source code, see the "HelloWorld source code" at the end of the tutorial.
For additional suggestions on improving this tutorial, send email to jpgpubs@inprise.com.
First, you need a project in which to store your application. The Project wizard creates one for you:
The Project wizard opens first. After you complete this wizard, the Application wizard opens.
Note: Projects in JBuilder are saved by default in the /[home]/jbproject
directory. It is recommended that only advanced users change this default path.
Two files, HelloWorld.jpr
and HelloWorld.html
, appear in the project pane of the AppBrowser. The Application wizard is open on top of it.
.java
files that are added to the project you just created.
To generate source files for your application, follow these steps:
helloworld.
By default, the wizard takes the package name from the project file name, HelloWorld.jpr.
Step 1 of the Application wizard should look like this:
Step 2 of the Application wizard should look like this:
The new .java
class files and toolbar images are added to your project, and the source code for HelloWorldFrame.java
is open in the content pane as shown in the image below.
Note: The source files are saved to: /[home]/jbproject/HelloWorld/src/helloworld
The class files are saved to: /[home]/jbproject/HelloWorld/classes/helloworld
HelloWorldClass.java
in the project pane, right-click, and select Run.
Your application is displayed and should look like this:
HelloWorldFrame.java
in the project pane if it's not already open.
UI
, Menu
, and Other
.
JPanel
component to add a panel to your design.
Note: The constraints
property in the Inspector should be Center. If not, click the column to the right of the constraints
property, and choose Center from the drop-down list.
jPanel1
is now selected in your application design and in the component tree.
jPanel1
to White.
background
property in the Inspector.
jPanel1
and change the border color to Gray.
border
property in the Inspector.
jPanel1
to null.
layout
property in the Inspector.
null
from the drop-down list.
null
layout to prototype your design when laying out multiple components. Because null
layout does not use a layout manager, you can place components exactly where you want them. Later, you can switch to an appropriate portable layout for your design. We recommend never leaving a container in null
for deployment, because components do not adjust when you resize the parent container.
JLabel
component to the JPanel
component.
JLabel
component.
JPanel
. Use one of the following two methods:
jPanel1
in the component tree. This places it in the upper-left corner of the panel.
jPanel1
in the
UI designer. The upper-left corner of the component is placed where you
click.
jLabel1
is added below jPanel1
in the component tree. If you drop the control in the wrong place, you can select jLabel1
in the component tree or in the designer and press the Del key. Then re-add it.
jLabel1
in the component tree, and complete the following steps:
text
property in the Inspector and type Hello World! Press Enter. "Hello World!" now appears in the label.
font
property to set the font. Click the ellipsis button jLabel1
by dragging the black handles until "Hello World" is visible.
foreground
property in the Inspector to set the
color of the "Hello World!" text. Click the Down arrow and select Blue from
the drop-down list of colors.
Your design now looks similar to this:
HelloWorldFrame_AboutBox.java
in the project pane to open the file. The content pane changes to the source view where you can edit the code in the editor.
String version = "1.0";
The editor finds the selected text.
1.0
and enter 2.0 inside the quotes.
The "Hello World" application is displayed:
You can do this using any of these methods:
JDK/bin
directory in your path.
JDK/bin
directory.
To run the application,
java -classpath /[home]/jbproject/HelloWorld/classes helloworld.HelloWorldClass
This command should be in the following form:
java -classpath package-name.main-class-nameIn this example,
/[home]/jbproject/HelloWorld/classes
helloworld
HelloWorldClass
HelloWorld.jpr
(File|Reopen). Open HelloWorldFrame.java
and click the Design tab to switch to the UI designer.
JButton
component jPanel1
in the component tree or in the panel in your design. jButton4
is added below jPanel1
in the component tree.
jButton4
in the design and drag it to the top center of the design as shown in the image below.
Text
property in the Inspector from jButton4
to Push Me
. Press Enter. Enlarge the button by dragging the black handles until "Push Me" shows completely.
The Inspector should look like this:
jButton4
is pressed.
ActionPerformed
event.
JBuilder switches to the editor where the following skeleton code has been added for the ActionPerformed
event.
You can now enter code that defines the event.void jButton4_actionPerformed(ActionEvent e) { }
void jButton4_actionPerformed(ActionEvent e) { jLabel1.setForeground(new Color(255,0,0)); }
setForeground(Color)
from the pop-up window or use the arrow keys. Press Enter. You can configure CodeInsight in the IDE Options dialog box (Tools|IDE Options|CodeInsight).
Now, when you run the application and push the "Push Me" button, "Hello World!" should turn red.
Steps 10 and 11 use features found in JBuilder Professional and Enterprise. If you are using JBuilder Foundation, you have completed the tutorial.
Continue with the tutorial if you have JBuilder Professional or Enterprise.
To deploy your application:
HelloWorld.jar
will be saved to the HelloWorld
directory.
Application
appears in the project pane. You can modify this file by right-clicking and selecting Properties.
Application
archive node to see the HelloWorld.jar
archive file. Double-click the JAR file in the project pane. The manifest file appears in the content pane and the contents of the JAR file appear in the structure pane.
Continue with the tutorial if you have JBuilder Professional or Enterprise.
To test the deployed application, you can run the JAR file from the command line.
jdk/bin
directory must be on your path or you must run the application from within the jdk/bin
directory.
java -classpath /[home]/jbproject/HelloWorld/HelloWorld.jar helloworld.HelloWorldClass
The command must have the following form:
java -classpath package-name.main-class-name
Congratulations, you've created your first application with JBuilder. Now that you're familiar with JBuilder's development environment, you'll find its many time-saving features make your programming easier.
HelloWorldFrame.java
HelloWorldClass.java
/** * Title: HelloWorld * Description: This is the "Hello World" tutorial. * Copyright: Copyright (c) 2000 * Company: MyCompany * @author MyName * @version 1.0 */ package helloworld; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class HelloWorldFrame extends JFrame { JPanel contentPane; JMenuBar jMenuBar1 = new JMenuBar(); JMenu jMenuFile = new JMenu(); JMenuItem jMenuFileExit = new JMenuItem(); JMenu jMenuHelp = new JMenu(); JMenuItem jMenuHelpAbout = new JMenuItem(); JToolBar jToolBar = new JToolBar(); JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); JButton jButton3 = new JButton(); ImageIcon image1; ImageIcon image2; ImageIcon image3; JLabel statusBar = new JLabel(); BorderLayout borderLayout1 = new BorderLayout(); JPanel jPanel1 = new JPanel(); Border border1; JLabel jLabel1 = new JLabel(); JButton jButton4 = new JButton(); /**Construct the frame*/ public HelloWorldFrame() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } /**Component initialization*/ private void jbInit() throws Exception { image1 = new ImageIcon(helloworld.HelloWorldFrame.class.getResource("openFile.gif")); image2 = new ImageIcon(helloworld.HelloWorldFrame.class.getResource("closeFile.gif")); image3 = new ImageIcon(helloworld.HelloWorldFrame.class.getResource("help.gif")); //setIconImage(Toolkit.getDefaultToolkit().createImage(HelloWorldFrame.class.getResource("[Your Icon]"))); contentPane = (JPanel) this.getContentPane(); border1 = BorderFactory.createLineBorder(Color.gray,1); contentPane.setLayout(borderLayout1); this.setSize(new Dimension(400, 300)); this.setTitle("Hello World"); statusBar.setText(" "); jMenuFile.setText("File"); jMenuFileExit.setText("Exit"); jMenuFileExit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jMenuFileExit_actionPerformed(e); } }); jMenuHelp.setText("Help"); jMenuHelpAbout.setText("About"); jMenuHelpAbout.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jMenuHelpAbout_actionPerformed(e); } }); jButton1.setIcon(image1); jButton1.setToolTipText("Open File"); jButton2.setIcon(image2); jButton2.setToolTipText("Close File"); jButton3.setIcon(image3); jButton3.setToolTipText("Help"); jPanel1.setBackground(Color.white); jPanel1.setBorder(border1); jPanel1.setLayout(null); jLabel1.setFont(new java.awt.Font("Serif", 3, 28)); jLabel1.setForeground(Color.blue); jLabel1.setText("Hello World!"); jLabel1.setBounds(new Rectangle(135, 111, 184, 56)); jButton4.setText("Push Me"); jButton4.setBounds(new Rectangle(157, 21, 117, 36)); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton4_actionPerformed(e); } }); jToolBar.add(jButton1); jToolBar.add(jButton2); jToolBar.add(jButton3); jMenuFile.add(jMenuFileExit); jMenuHelp.add(jMenuHelpAbout); jMenuBar1.add(jMenuFile); jMenuBar1.add(jMenuHelp); this.setJMenuBar(jMenuBar1); contentPane.add(jToolBar, BorderLayout.NORTH); contentPane.add(statusBar, BorderLayout.SOUTH); contentPane.add(jPanel1, BorderLayout.CENTER); jPanel1.add(jLabel1, null); jPanel1.add(jButton4, null); } /**File | Exit action performed*/ public void jMenuFileExit_actionPerformed(ActionEvent e) { System.exit(0); } jLabel1.setf /**Help | About action performed*/ public void jMenuHelpAbout_actionPerformed(ActionEvent e) { HelloWorldFrame_AboutBox dlg = new HelloWorldFrame_AboutBox(this); Dimension dlgSize = dlg.getPreferredSize(); Dimension frmSize = getSize(); Point loc = getLocation(); dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y); dlg.setModal(true); dlg.show(); } /**Overridden so we can exit when window is closed*/ protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { jMenuFileExit_actionPerformed(null); } } void jButton4_actionPerformed(ActionEvent e) { jLabel1.setForeground(new Color(255,0,0)); } }
/** * Title: HelloWorld * Description: This is the "Hello World" tutorial. * Copyright: Copyright (c) 2000 * Company: MyCompany * @author MyName * @version 1.0 */ package helloworld; import javax.swing.UIManager; import java.awt.*; public class HelloWorldClass { boolean packFrame = false; /**Construct the application*/ public HelloWorldClass() { HelloWorldFrame frame = new HelloWorldFrame(); //Validate frames that have preset sizes //Pack frames that have useful preferred size info, e.g. from their layout if (packFrame) { frame.pack(); } else { frame.validate(); } //Center the window Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = frame.getSize(); if (frameSize.height > screenSize.height) { frameSize.height = screenSize.height; } if (frameSize.width > screenSize.width) { frameSize.width = screenSize.width; } frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); frame.setVisible(true); } /**Main method*/ public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) { e.printStackTrace(); } new HelloWorldClass(); } }