FacebookTwitter

JAVA: Using JPanel as Application Main Panel

By on Jul 17, 2005 in Featured, Programming | 0 comments

Share On GoogleShare On FacebookShare On Twitter

Most of the programmers like to have the most beautiful menu bars and components in their applications, but in Java programming language, because of limited access to system level resources; it is a little bit difficult.
Today I am going to explain how to make an attractive application frame, what I am going to say here is just the basics and a simple application that you can download and run it as a NetBeas Project.
Since I usually use NetBeans IDE, this entire document is based on that IDE, so if you want to match this information with your programming environment you might check documents of your product.
Normally when you are going to build a menu driven application in java, you make a Frame then start adding new components, this shows the normal borde for your application, but if you need more your should use JPanel instead of JFrame, working with JPanel need more manual codes but the result will be attractive.
To make your application create a JPanel form, NetBeans does not add main to your JPanel frame so you should add it like this:


public static void main(String[] args){
createAndShowGUI();
};

or you can do it in this way:


public static void main(String[] args){
javax.swing.SwingUtilities.invokeLater( new Runnable(){
public void run(){
createAndShowGUI();
}
}
};

also you need a method called createAndShowGUI(), the name is not important you can choose any name that you want but be sure that you use the same method name in main() method.

The main role of createAndShowGUI() is to instanciate the application class and a JFrame and add the application class to that instance of JFrame. This could be something like this:


private static void createAndShowGUI(){
panelAppTest myPanel = new panelAppTest();
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame appFrame = new JFrame("My Test Application");
appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// REMEMBER THIS LOCATION

myPanel.setOpaque(true);
appFrame.setContentPane(myPanel);

appFrame.pack();
appFrame.setVisible(true);

}

Now you need to add a menu bar and few menus and menu items, you can do this manually or do it through the IDE, don’t make your life difficult, you can easily add menu bar using IDE, but in a tricky way.
You just need to add menu bar and its children to a section called “Other Components” which you can find it under Form tree.The advantage of using IDE to make menu bar is that the IDE takes care of all event handling and the layout.
For menus to be appeared in their place we need two other panels, in this example I use a JPanel and a JDesktopPane, be sure that you set the layout of the main JPanel to borderLayout, then after adding JPanel you need to set its layout to borderLayout as well; Then you can add JDesktopPane to the main JPanel.

For the correct layout you should set the direction of inner JPanel to “north” and for JDesktopPane it must be “center”, otherwise you will never see either the menu bar or the JDesktopPane.
What you have now is just the layout, to add menu bar to the application you should get back to createAndShowGUI() method, find the line marked “// REMEMBER THIS LOCATION”; you should add the menu bar to the second JPanel that you have already made under the main JPanel.
Okay, you have all the things ready, run your program and see the result and compare it with original Application Frame that NetBeans provide to you.


1 /*
2 * panelAppTest.java
3 *
4 * Created on July 17, 2005, 9:56 AM
5 */
6
7 package panelAppTest;
8
9 import javax.swing.*;
10
11 /**
12 *
13 * @author Mohammad Ali
14 */
15 public class panelAppTest extends javax.swing.JPanel {
16
17 /** Creates new form panelAppTest */
18 public panelAppTest() {
19 initComponents();
20 }
21
22 public static void main(String[] args){
23
24 createAndShowGUI();
25
26 };
27 private static void createAndShowGUI(){
28
29 panelAppTest myPanel = new panelAppTest();
30
31 JFrame.setDefaultLookAndFeelDecorated(true);
32 JFrame appFrame = new JFrame("My Test Application");
33 appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
34
35 // REMEMBER THIS LOCATION
36 myPanel.jPanel1.add(myPanel.jMenuBar1);
37
38 myPanel.setOpaque(true);
39 appFrame.setContentPane(myPanel);
40
41 appFrame.pack();
42 appFrame.setVisible(true);
43
44 }
45 /** This method is called from within the constructor to
46 * initialize the form.
47 * WARNING: Do NOT modify this code. The content of this method is
48 * always regenerated by the Form Editor.
49 */
50 private void initComponents() {
51 jMenuBar1 = new javax.swing.JMenuBar();
52 jMenu1 = new javax.swing.JMenu();
53 jMenuItem1 = new javax.swing.JMenuItem();
54 jMenuItem2 = new javax.swing.JMenuItem();
55 jMenuItem3 = new javax.swing.JMenuItem();
56 jSeparator1 = new javax.swing.JSeparator();
57 jMenuItem4 = new javax.swing.JMenuItem();
58 jMenu2 = new javax.swing.JMenu();
59 jMenuItem5 = new javax.swing.JMenuItem();
60 jMenu3 = new javax.swing.JMenu();
61 jMenuItem6 = new javax.swing.JMenuItem();
62 jPanel1 = new javax.swing.JPanel();
63 jDesktopPane1 = new javax.swing.JDesktopPane();
64
65 jMenu1.setText("File");
66 jMenuItem1.setText("Open");
67 jMenu1.add(jMenuItem1);
68
69 jMenuItem2.setText("Save");
70 jMenu1.add(jMenuItem2);
71
72 jMenuItem3.setText("Save As ...");
73 jMenu1.add(jMenuItem3);
74
75 jMenu1.add(jSeparator1);
76
77 jMenuItem4.setText("Exit");
78 jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
79 public void actionPerformed(java.awt.event.ActionEvent evt) {
80 jMenuItem4ActionPerformed(evt);
81 }
82 });
83
84 jMenu1.add(jMenuItem4);
85
86 jMenuBar1.add(jMenu1);
87
88 jMenu2.setText("Settings");
89 jMenuItem5.setText("Database");
90 jMenu2.add(jMenuItem5);
91
92 jMenuBar1.add(jMenu2);
93
94 jMenu3.setText("Help");
95 jMenuItem6.setText("About");
96 jMenu3.add(jMenuItem6);
97
98 jMenuBar1.add(jMenu3);
99
100 setLayout(new java.awt.BorderLayout());
101
102 jPanel1.setLayout(new java.awt.BorderLayout());
103
104 add(jPanel1, java.awt.BorderLayout.NORTH);
105
106 jDesktopPane1.setBackground(new java.awt.Color(51, 51, 51));
107 jDesktopPane1.setPreferredSize(new java.awt.Dimension(400, 500));
108 add(jDesktopPane1, java.awt.BorderLayout.SOUTH);
109
110 }
111
112 private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {
113 // TODO add your handling code here:
114 System.exit(0);
115 }
116
117
118 // Variables declaration - do not modify
119 private javax.swing.JDesktopPane jDesktopPane1;
120 private javax.swing.JMenu jMenu1;
121 private javax.swing.JMenu jMenu2;
122 private javax.swing.JMenu jMenu3;
123 private javax.swing.JMenuBar jMenuBar1;
124 private javax.swing.JMenuItem jMenuItem1;
125 private javax.swing.JMenuItem jMenuItem2;
126 private javax.swing.JMenuItem jMenuItem3;
127 private javax.swing.JMenuItem jMenuItem4;
128 private javax.swing.JMenuItem jMenuItem5;
129 private javax.swing.JMenuItem jMenuItem6;
130 private javax.swing.JPanel jPanel1;
131 private javax.swing.JSeparator jSeparator1;
132 // End of variables declaration
133
134 }
135

Download panelAppTest NetBeans Project

Submit a Comment

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Shares