Java Gui Grok
Java Gui Grok
Basics to Advanced
June 2025
Contents
1 Introduction to Java GUI with Apache NetBeans 2
1.1 Setting Up Apache NetBeans . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 AWT vs Swing vs JavaFX . . . . . . . . . . . . . . . . . . . . . . . . . . 2
9 JavaFX Introduction 13
1
1 Introduction to Java GUI with Apache NetBeans
Graphical User Interface (GUI) programming enables developers to create interactive
applications with visual components like windows, buttons, and text fields. In Java,
GUI development is supported by libraries like AWT, Swing, and JavaFX. **Apache
NetBeans** is an IDE that simplifies GUI development, especially for Swing, with its
GUI Builder (formerly Matisse). This document uses NetBeans to demonstrate GUI
concepts, providing step-by-step instructions for setting up and running projects.
2
8 Button button = new Button (" Submit ");
9 Checkbox checkbox = new Checkbox ("Agree ");
10 Choice choice = new Choice ();
11 choice .add(" Option 1");
12 choice .add(" Option 2");
13
14 frame.add( label );
15 frame.add( textField );
16 frame.add( button );
17 frame.add( checkbox );
18 frame.add( choice );
19 frame. setLayout (new FlowLayout ());
20 frame. setSize (400 , 300);
21 frame. setVisible (true);
22 }
23 }
NetBeans Instructions:
1. Create a new Java class (File > New File > Java > Java Class).
2. Copy the code into the class.
3. Run the project (Run > Run Main Project or F6).
3
Diagram: A figure showing FlowLayout (row of buttons), BorderLayout (five labeled
regions), and GridLayout (2x2 button grid).
NetBeans Instructions: Create a new Java class, paste the code, and run. Use the
debugger (Debug > Debug Project) to inspect event flow.
4
Key components: JFrame, JPanel, JLabel, JButton, JTextField, JCheckBox, JRadioButton,
JComboBox.
1 // Generated by NetBeans GUI Builder, modified for clarity
2 import javax. swing .*;
3 import java.awt .*;
4
15 add( label );
16 add( textField );
17 add( button );
18 add( checkBox );
19 add( comboBox );
20 setSize (400 , 300);
21 setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE );
22 }
23
NetBeans Instructions:
1. Create a JFrame Form (File > New File > Swing GUI Forms > JFrame Form).
2. Use the GUI Builder to drag-and-drop components (e.g., JLabel, JTextField).
3. Modify the generated code in the initComponents() method or add logic manually.
4. Run the form (right-click form > Run File).
5
6 setTitle (" BoxLayout Example ");
7 JPanel panel = new JPanel ();
8 panel. setLayout (new BoxLayout (panel , BoxLayout . Y_AXIS ));
9 panel.add(new JButton (" Button 1"));
10 panel.add(new JButton (" Button 2"));
11 panel.add(new JButton (" Button 3"));
12 add(panel);
13 setSize (300 , 200);
14 setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE );
15 }
16
6
26 public static void main( String [] args) {
27 SwingUtilities . invokeLater (() -> new
SwingEventExample (). setVisible (true));
28 }
29 }
23 add( tabbedPane );
24 setSize (500 , 400);
25 setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE );
26 }
27
7
29 SwingUtilities . invokeLater (() -> new
IntermediateSwingExample (). setVisible (true));
30 }
31 }
NetBeans Instructions: Use the GUI Builder to add JTabbedPane and JMenuBar from
the Palette. Configure properties (e.g., tab titles) in the Properties window.
Diagram: A figure showing a JTabbedPane with two tabs and a JMenuBar with a ”File”
menu.
8
11 root.add(new DefaultMutableTreeNode (" Folder 2"));
12 JTree tree = new JTree (root);
13 add(new JScrollPane (tree));
14 setSize (300 , 400);
15 setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE );
16 }
17
NetBeans Instructions: Add JTable or JTree via GUI Builder, then customize the
model in code.
Diagram: A figure with a JTable (2x3 grid) and a JTree (root with two folders, one
with a file).
8 public GraphicsExample () {
9 addMouseListener (new MouseAdapter () {
10 public void mouseClicked ( MouseEvent e) {
11 x = e.getX ();
12 y = e.getY ();
13 repaint ();
14 }
15 });
16 }
17
18 @Override
19 protected void paintComponent ( Graphics g) {
20 super . paintComponent (g);
21 Graphics2D g2d = ( Graphics2D ) g;
22 g2d. setColor ( Color .BLUE);
23 g2d. fillOval (x - 25, y - 25, 50, 50);
24 g2d. setColor ( Color .RED);
25 g2d. drawRect (100 , 100 , 100 , 100);
26 }
27
9
29 SwingUtilities . invokeLater (() -> {
30 JFrame frame = new JFrame (" Graphics Example ");
31 frame.add(new GraphicsExample ());
32 frame. setSize (400 , 400);
33 frame. setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE );
34 frame. setVisible (true);
35 });
36 }
37 }
10
33 setSize (300 , 200);
34 setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE );
35 }
36
8 public AnimationExample () {
9 Timer timer = new Timer (50 , e -> {
10 x += 5;
11 if (x > getWidth ()) x = 0;
12 repaint ();
13 });
14 timer. start ();
15 }
16
17 @Override
18 protected void paintComponent ( Graphics g) {
19 super . paintComponent (g);
20 g. setColor ( Color . GREEN );
21 g. fillRect (x, 50, 50, 50);
22 }
23
11
5 try {
6 UIManager . setLookAndFeel ("javax .swing .plaf. nimbus . NimbusLookAndFeel "
7 } catch ( Exception e) {
8 e. printStackTrace ();
9 }
10 setTitle ("Look and Feel Example ");
11 add(new JButton (" Styled Button "));
12 setSize (300 , 200);
13 setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE );
14 }
15
NetBeans Instructions: Use the GUI Builder for layout, add SwingWorker or Timer
in the source code.
12
25 if ( chooser . showSaveDialog (this) ==
JFileChooser . APPROVE_OPTION ) {
26 try ( BufferedWriter writer = new BufferedWriter (new
FileWriter ( chooser . getSelectedFile ()))) {
27 textArea . write ( writer );
28 } catch ( IOException ex) {
29 JOptionPane . showMessageDialog (this , "Error
saving file");
30 }
31 }
32 });
33
NetBeans Instructions: Add JFileChooser via GUI Builder, implement I/O logic in
the source.
9 JavaFX Introduction
JavaFX uses a scene graph, supporting CSS and FXML. NetBeans integrates with Scene
Builder for visual design.
1 import javafx . application . Application ;
2 import javafx .scene. Scene ;
3 import javafx .scene. control . Button ;
4 import javafx .scene. control . TextField ;
5 import javafx .scene. layout .VBox;
6 import javafx .stage. Stage ;
7
13
15 Scene scene = new Scene (vbox , 300 , 200);
16 stage. setTitle (" JavaFX Example ");
17 stage. setScene ( scene );
18 stage.show ();
19 }
20
NetBeans Instructions:
1. Create a JavaFX project (File > New Project > JavaFX > JavaFX Application).
2. Add JavaFX SDK to libraries.
3. Optionally, use Scene Builder to design the layout, then load the FXML in code.
Diagram: A figure showing VBox, HBox, BorderPane, and GridPane layouts.
14
ELSE:
APPEND button text to display
ELSE (operator or =):
IF not start:
CALCULATE result using lastCommand and display value
SET start to true
SET lastCommand to button text
IF button is =:
DISPLAY result
CALCULATE function:
PARSE display value to number
APPLY lastCommand (+, -, *, /) to result and number
UPDATE display with result
1 import javax. swing .*;
2 import java.awt .*;
3 import java.awt.event .*;
4
11 public Calculator () {
12 setTitle (" Calculator ");
13 display = new JTextField ("0");
14 display . setEditable (false );
15 add(display , BorderLayout . NORTH );
16
25 add(panel);
26 setSize (300 , 400);
27 setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE );
28 }
29
15
37 display . setText ( display . getText () + input );
38 }
39 } else {
40 if (! start ) {
41 calculate ( Double . parseDouble ( display . getText ()));
42 start = true;
43 }
44 lastCommand = input ;
45 if (input . equals ("=")) {
46 display . setText ("" + result );
47 }
48 }
49 }
50
NetBeans Instructions: Create a JFrame Form, use GUI Builder to add JTextField
and JButtons in a GridLayout, implement logic in the source.
Diagram: A figure showing a display at the top and a 4x4 button grid.
16
GET selected list item
IF item is selected:
REMOVE item from list model
1 import javax. swing .*;
2 import java.awt .*;
3 import java.awt.event .*;
4
8 public TodoList () {
9 setTitle ("Todo List");
10 listModel = new DefaultListModel < >();
11 JList <String > taskList = new JList <>( listModel );
12 JTextField input = new JTextField (20);
13 JButton addButton = new JButton ("Add Task");
14 JButton removeButton = new JButton (" Remove Selected ");
15
17
NetBeans Instructions: Use GUI Builder to add JTextField, JList, and JButtons,
configure BorderLayout.
Diagram: A figure with a text field (top), task list (center), and two buttons (bottom).
18
31
32 add(panel);
33 setSize (300 , 150);
34 setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE );
35 }
36
NetBeans Instructions: Create a JFrame Form, use GUI Builder to add components
in a GridLayout.
Diagram: A figure showing a 3x2 grid with username/password fields and a login button.
10 public DrawingApp () {
11 addMouseListener (new MouseAdapter () {
12 public void mousePressed ( MouseEvent e) {
13 startPoint = e. getPoint ();
14 }
15 });
16 addMouseMotionListener (new MouseMotionAdapter () {
17 public void mouseDragged ( MouseEvent e) {
19
18 points .add( startPoint );
19 points .add(e. getPoint ());
20 startPoint = e. getPoint ();
21 repaint ();
22 }
23 });
24 }
25
26 @Override
27 protected void paintComponent ( Graphics g) {
28 super . paintComponent (g);
29 g. setColor ( Color . BLACK );
30 for (int i = 0; i < points .size () - 1; i += 2) {
31 Point p1 = points .get(i);
32 Point p2 = points .get(i + 1);
33 g. drawLine (p1.x, p1.y, p2.x, p2.y);
34 }
35 }
36
11.5 Notepad
Pseudocode:
CREATE window with title "Notepad"
ADD menu bar with "File" menu ("Open", "Save")
ADD scrollable text area
ON "Open" click:
SHOW file chooser dialog
IF file selected:
READ file content into text area
HANDLE errors
ON "Save" click:
SHOW save dialog
IF file selected:
WRITE text area content to file
20
HANDLE errors
1 import javax. swing .*;
2 import java.awt .*;
3 import java.io .*;
4
21
45 public static void main( String [] args) {
46 SwingUtilities . invokeLater (() -> new
Notepad (). setVisible (true));
47 }
48 }
NetBeans Instructions: Create a JFrame Form, add JMenuBar and JTextArea via
GUI Builder, implement file I/O in the source.
Diagram: A figure showing a text area with a ”File” menu containing ”Open” and
”Save”.
22