The document discusses graphical user interface (GUI) components in Java. It covers topics like basic GUI units like frames and panels, common Swing components like JFrame, JButton, JTextField, layout managers, event handling using listeners, and examples of creating simple GUI applications in Java. Key concepts explained include the component hierarchy in Swing, commonly used layouts like FlowLayout, BorderLayout and GridLayout, and how to add listeners for events like button clicks and window closing. Code samples are provided to demonstrate creating frames, adding components, setting layouts and handling events.
An abstract class is a class that is declared abstract —it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class.
This presentation introduces some concepts about the Java Collection framework. These slides introduce the following concepts:
- Collections and iterators
- Linked list and array list
- Hash set and tree set
- Maps
- The collection framework
The presentation is took from the Java course I run in the bachelor-level informatics curriculum at the University of Padova.
An abstract class is a class that is declared abstract —it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class.
This presentation introduces some concepts about the Java Collection framework. These slides introduce the following concepts:
- Collections and iterators
- Linked list and array list
- Hash set and tree set
- Maps
- The collection framework
The presentation is took from the Java course I run in the bachelor-level informatics curriculum at the University of Padova.
Java programming, Java Swing Programming, SWING GUI Design, desktop application, Getting Started with Swing application development in java, Introduction to GUI based programming in java, Java to standard edition, J2SE, Core Java, Swing package in java
Java programming presentations By Daroko blog
Do not just read java as a programmer, find projects and start making some Money, at DAROKO BLOG,WE Guide you through what you have learned in the classroom to a real business Environment, find java applications to a real business Environment, find also all IT Solutions and How you can apply them, find the best companies where you can get the IT jobs worldwide, Find java contract, Complete and start making some cash, find clients within your Country, refer and get paid when you complete the work.
Not Just a contact, at daroko Blog(www.professionalbloggertricks.com/),you are also being taught How you can apply all IT related field in real world.
Simply Google, Daroko Blog or visit (www.professionalbloggertricks.com/) to Know More about all these service now.
Do not just learn and god, Apply them in real world
package buttongui;
import static com.sun.deploy.config.JREInfo.clear;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class ButtonGUI extends Application implements EventHandler {
@Override
public void start(Stage primaryStage) {
btn.setText(\"Find\");
btn.setOnAction(this);
btn1.setText(\"Clear\");
btn1.setOnAction(this);
btn2.setText(\"Exit\");
btn2.setOnAction(this);
FlowPane flow = new FlowPane();
flow.getChildren().addAll(btn,btn1,btn2);
BorderPane border = new BorderPane();
border.setCenter(flow);
Scene scene = new Scene(border, 300,250);
primaryStage.setTitle(\"ButtonGUI\");
primaryStage.setScene(scene);
primaryStage.show();
}
@Override
public void handle(ActionEvent event) {
String tx =\"Hello world\";
System.out.println(tx);
if(btn2 == event.getSource()){
System.out.println(\"Exit Pressed\");
System.exit(0);
}
}
public static void main(String[] args)throws Exception {
launch(args);
}
}
Simple Exit & Clear Events) Add the Exit Event to your ButtonsGUI. Make it so that when the
user clicks on the Exit button, the application ends. Add the Clear Event to your ButtonsGUI.
Make it so that when the user clicks on the Clear button, the Text on all the Buttons is cleared.
please solve the clear button so that when the user clicks on the Clear button, the Text on all the
Buttons is cleared. thanks
Solution
Follow below steps to Add the Exit Event to your Buttons GUI:
1. Right Click on the Exit button and choose Events --> Action --> ActionPerformed.
2. The IDE will automatically add an ActionListener to the Exit button and generate a handler
method for handling the listener\'s actionPerformed method.
3. The IDE will open up the Source Code window and scroll to where you implement the action
you want the button to do when the button is pressed.
4. Add code for what we want the Exit Button to do. You will have to type System.exit(0);
5. Your final Exit button code should look like this:
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)
{
System.exit(0);
}
Follow below steps to Add the Clear Event to your Buttons GUI:
In below example the text in two of our jTextFields will clear, it is overwriting the existing Text
with a blank.
1. Click on the Design tab at the top of your work area to go back to the Form Design
2. Right Click on the Clear button (jButton1). From the pop-up menu select Events --> Action --
> ActionPerformed.
3. Your final source code will be:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
jTextField1.setText(\"\");
jTextField2.setText(\"\");
}
Note: You can add as many jTextField depending upon number of buttons y.
Java GUI PART II is the continues of JAVA GUI PART I covering and discussing the GUI components as well as the different available Layout Managers which is available in JAVA and you can find dedicated example for each Layout Managers …
Instructions for Submissions thorugh G- Classroom.pptxJheel Barad
This presentation provides a briefing on how to upload submissions and documents in Google Classroom. It was prepared as part of an orientation for new Sainik School in-service teacher trainees. As a training officer, my goal is to ensure that you are comfortable and proficient with this essential tool for managing assignments and fostering student engagement.
Acetabularia Information For Class 9 .docxvaibhavrinwa19
Acetabularia acetabulum is a single-celled green alga that in its vegetative state is morphologically differentiated into a basal rhizoid and an axially elongated stalk, which bears whorls of branching hairs. The single diploid nucleus resides in the rhizoid.
Biological screening of herbal drugs: Introduction and Need for
Phyto-Pharmacological Screening, New Strategies for evaluating
Natural Products, In vitro evaluation techniques for Antioxidants, Antimicrobial and Anticancer drugs. In vivo evaluation techniques
for Anti-inflammatory, Antiulcer, Anticancer, Wound healing, Antidiabetic, Hepatoprotective, Cardio protective, Diuretics and
Antifertility, Toxicity studies as per OECD guidelines
How to Make a Field invisible in Odoo 17Celine George
It is possible to hide or invisible some fields in odoo. Commonly using “invisible” attribute in the field definition to invisible the fields. This slide will show how to make a field invisible in odoo 17.
Introduction to AI for Nonprofits with Tapp NetworkTechSoup
Dive into the world of AI! Experts Jon Hill and Tareq Monaur will guide you through AI's role in enhancing nonprofit websites and basic marketing strategies, making it easy to understand and apply.
Synthetic Fiber Construction in lab .pptxPavel ( NSTU)
Synthetic fiber production is a fascinating and complex field that blends chemistry, engineering, and environmental science. By understanding these aspects, students can gain a comprehensive view of synthetic fiber production, its impact on society and the environment, and the potential for future innovations. Synthetic fibers play a crucial role in modern society, impacting various aspects of daily life, industry, and the environment. ynthetic fibers are integral to modern life, offering a range of benefits from cost-effectiveness and versatility to innovative applications and performance characteristics. While they pose environmental challenges, ongoing research and development aim to create more sustainable and eco-friendly alternatives. Understanding the importance of synthetic fibers helps in appreciating their role in the economy, industry, and daily life, while also emphasizing the need for sustainable practices and innovation.
Macroeconomics- Movie Location
This will be used as part of your Personal Professional Portfolio once graded.
Objective:
Prepare a presentation or a paper using research, basic comparative analysis, data organization and application of economic information. You will make an informed assessment of an economic climate outside of the United States to accomplish an entertainment industry objective.
2. GUI Basic Unit
Basic unit of Graphical
User Interface (GUI) is
a Window.
A window is called
Frame in AWT or
Swing.
Frame is the top
container of all Visual
Graphical Controls.
www.SunilOS.com
2
10. www.SunilOS.com
10
Create A Window
public static void main(String[] args) {
JFrame frame = new JFrame(“My First Window");
JPanel pan = (JPanel)frame.getContentPane();
JButton b = new JButton("Click Me");
pan.add(b);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Close window when click on ‘x’
frame.pack();
//frame.setSize(400, 200);
frame.setVisible(true);
}
11. www.SunilOS.com
11
Create a Window by Extending JFrame
public class Loginform extends JFrame{
public Loginform(){
o super(“Login Form");
o JPanel pan = (JPanel)getContentPane();
o Jlabel lab1 = new Jlabel(“User ID”);
o pan.add(lab1);
o JtextField fl1 = new JTextField();
o pan.add(fl1);
o JButton button = new JButton(“Go");
o pan.add(button);
o setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
12. www.SunilOS.com
12
Create an object of extended JFrame
public static void main(String[] args) {
o Loginform frame = new Loginform ();
o frame.pack();
o frame.setVisible(true);
}
}
18. www.SunilOS.com
18
Messenger
Rohit to Dinesh>Hi
Dinesh to Rohit>Hi, Can you tell me about
JDBC.
Rohit to Dinesh>Why?
Dinesh to Rohit>I was absent yesterday.
Nakul to Dinesh>Don’t worry I am here to
help you.
Sheetal to Nakul>I would also like to
learn.
Rohit
Dinesh
Nakul
Ajhar
Pradeep
Saveeta
Nidhi
Sheetal
Enter Message Here SendSend
Rohit
LogoutLogout
19. www.SunilOS.com
19
Messenger
Rohit to Dinesh > Hi
Dinesh to Rohit > Hi, Can tell me about
JDBC
Rohit to Dinesh > Why
Dinesh to Rohit > I was absent
Nakul to Dinesh > Don’t worry I am here to
Help you
Sheetal to Nakul > I also would like to
Learn.
Rohit
Dinesh
Nakul
Ajhar
Pradeep
Saveeta
Nidhi
Sheetal
Enter Message Here SendSend
Rohit
LogoutLogout
20. www.SunilOS.com
20
Flow Layout
JFrame frame = new JFrame(“Flow L Window");
FlowLayout layout = new FlowLayout();
//FlowLayout layout = new FlowLayout(FlowLayout.RIGHT);
//FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
frame.setLayout(layout);
JPanel pan = (JPanel)frame.getContentPane();
JButton b1 = new JButton("Button1"); pan.add(b1);
JTextField text = new JTextField("Enter Text"); pan.add(text);
JCheckBox cb = new JCheckBox("Select Here"); pan.add(cb);
JButton b2 = new JButton("Button2"); pan.add(b2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
frame.setVisible(true);
21. www.SunilOS.com
21
BorderLayout
public static void main(String[] args) {
JFrame frame = new JFrame("MyFirstWindow");
frame.setLayout(new BorderLayout());
JPanel pan = (JPanel)frame.getContentPane();
JButton b1 = new JButton("North"); pan.add(b1,BorderLayout.NORTH);
JButton b2 = new JButton("South"); pan.add(b2, BorderLayout.SOUTH);
JButton b3 = new JButton("Right"); pan.add(b3, BorderLayout.EAST);
JButton b4 = new JButton("West"); pan.add(b4, BorderLayout.WEST);
JButton b5 = new JButton("Center"); pan.add(b5);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
22. www.SunilOS.com
22
GridLayout
public static void main(String[] args) {
JFrame frame = new JFrame("Meri First Window");
JPanel pan = (JPanel)frame.getContentPane();
pan.setLayout(new GridLayout(3,2));
JButton b1 = new JButton("Button1"); pan.add(b1);
JButton b2 = new JButton("Button2"); pan.add(b2);
JButton b3 = new JButton("Button3"); pan.add(b3);
JButton b4 = new JButton("Button4"); pan.add(b4);
JButton b5 = new JButton("Button5"); pan.add(b5);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
frame.setVisible(true);
}
23. www.SunilOS.com
23
BoxLayout
JFrame frame = new JFrame("My Box Layout Window");
JPanel pan = (JPanel) frame.getContentPane();
BoxLayout layout = new BoxLayout(pan, BoxLayout.X_AXIS);
// BoxLayout layout =new BoxLayout(pan, BoxLayout.Y_AXIS);
pan.setLayout(layout);
JButton b1 = new JButton("Button1"); pan.add(b1);
JTextField text = new JTextField("Enter Text"); pan.add(text);
JButton b2 = new JButton("Button2"); pan.add(b2);
JButton b3 = new JButton("Button3"); pan.add(b3);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
24. www.SunilOS.com
24
Absolute Position
public static void main(String[] args) {
JFrame frame = new JFrame("Meri First Window");
JPanel pan = (JPanel)frame.getContentPane();
pan.setLayout(null);
JButton b1 = new JButton("Button1");
b1.setSize(100,30);
b1.setLocation(10,10);
pan.add(b1);
JButton b2 = new JButton("Button2");
b2.setSize(100,30);
b2.setLocation(10,50);
pan.add(b2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
frame.setVisible(true);
}
26. www.SunilOS.com
26
Event Listeners
Some Events and Their Associated Event Listeners
Act that Results in the Event Listener Type
User clicks a button, presses Enter key while typing
in a text field, or chooses a menu item
ActionListener
User closes a frame (main window) WindowListener
User presses a mouse button while the cursor is
over a component
MouseListener
User moves the mouse over a component MouseMotionListener
Component becomes visible ComponentListener
Component gets the keyboard focus FocusListener
Table or list selection changes ListSelectionListener
Any property in a component changes such as
the text on a label
PropertyChangeListener
28. www.SunilOS.com
28
SimpleButtonHandler
public class SimpleButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
o JButton b = (JButton) event.getSource();
o String label = b.getText();
o if (label.equals("Click Me")) {
• b.setText("Don't Click Me");
o } else if (label.equals("Don't Click Me")) {
• b.setText("Click Me");
o } else {
• System.out.println("Button is Clicked");
o }
}
}
29. www.SunilOS.com
29
Mouse Listner using Adapter
public class MouseHandler extends MouseAdapter {
o public void mouseEntered(MouseEvent event) {
o System.out.println("Mouse Enetred");
o }
o public void mouseExited(MouseEvent event) {
o System.out.println("Mouse Exit");
o }
}
30. www.SunilOS.com
30
Apply Action Listner
public class ButtonClickEvent {
public static void main(String[] args) {
JFrame frame = new JFrame("Meri First Window");
JPanel pan = (JPanel)frame.getContentPane();
pan.setLayout(new FlowLayout());
JButton button = new JButton("Click Me");
SimpleButtonHandler handler = new SimpleButtonHandler();
button.addActionListener(handler); pan.add(button);
JButton b = new JButton("Click Me & See Console");
b.addActionListener(handler); pan.add(b);
b.addMouseListener(new MouseHandler(););
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
31. www.SunilOS.com
31
WindowHandler
public class WindowHandler implements WindowListener {
public void windowActivated(WindowEvent e) { System.out.println("windowActivated"); }
public void windowClosed(WindowEvent e) { System.out.println("windowClosed"); }
public void windowClosing(WindowEvent e) { System.out.println("windowClosing"); }
public void windowDeactivated(WindowEvent e) {
System.out.println("windowDeactivated");
}
public void windowDeiconified(WindowEvent e)
{ System.out.println("windowDeiconified"); }
public void windowIconified(WindowEvent e) { System.out.println("windowIconified"); }
public void windowOpened(WindowEvent e) { System.out.println("windowOpened"); } }
32. www.SunilOS.com
32
TestWindowListner
public class TestWindowListner {
public static void main(String[] args) {
JFrame f = new JFrame("Test Window Events");
WindowHandler wh = new WindowHandler(); f.addWindowListener(wh);
JPanel p = (JPanel) f.getContentPane();
p.setLayout(new FlowLayout(FlowLayout.LEFT));
JButton b = new JButton("Send"); p.add(b);
JButton login = new JButton("Login"); p.add(login);
JTextField t = new JTextField("Enter TExt Here"); p.add(t);
f.setSize(300, 200);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
1. windowActivated
2. windowOpened
3. windowIconified
4. windowDeactivated
5. windowDeiconified
6. windowActivated
7. windowClosing
34. www.SunilOS.com
34
MyWindow.java
public class MyWindow
extends JFrame {
…
}
ButtonHandler.java
public class ButtonHandler
implements ActionListener{
…
}
MyWindow.class
ButtonHandler.class
MyWindow.java
public class MyWindow
extends JFrame {
…
}
class ButtonHandler
implements ActionListener{
…
}
MyWindow.class
ButtonHandler.class
1 Class = 1 File 1 File – n Class
ButtonHandler handler = new ButtonHandler()
35. www.SunilOS.com
35
MyWindow.java
public class MyWindow
extends JFrame {
…
public class ButtonHandler
implements ActionListener{
…
}//ButtonHandler
}//MyWindow
MyWindow.class
MyWindow$ButtonHandler
.class
Inner Class Private Inner Class
MyWindow w = new MyWindow();
w.ButtonHandler h = w.new ButtonHandler();
MyWindow.java
public class MyWindow
extends JFrame {
…
private class ButtonHandler
implements
ActionListener{
…
}//ButtonHandler
}//MyWindow
MyWindow.class
MyWindow$ButtonHandler
.class
36. www.SunilOS.com
36
MyWindow.java
public class MyWindow extends
JFrame {
…
public static class
ButtonHandler implements
ActionListener{
…
}
}
MyWindow.class
MyWindow$ButtonHandler.cla
ss
Inner class can be
public
protected (Default)
private
static
Mainly used in Event
handling
Static Inner Class
MyWindow.ButtonHandler h = new MyWindow.ButtonHandler();
37. www.SunilOS.com
37
Inner Class - TestFocusListner
public class TestFocusListner {
o private class InnFocusHandler implements FocusListner {
• public void focusGained(FocusEvent e) {
+ JButton b = (JButton) e.getSource();
+ System.out.println("Focus Gained " + b.getText());
• }
• public void focusLost(FocusEvent e) {
+ JButton b = (JButton) e.getSource();
+ System.out.println("Focus lost " + b.getText());}
o }
38. www.SunilOS.com
38
Inner Class – TestFocusListner (Cont..)
public static void main(String[] args) {
JFrame f = new JFrame("Test Focus List Win");
JPanel p = (JPanel) f.getContentPane();
p.setLayout(new FlowLayout(FlowLayout.LEFT));
TestFocusListner tOuterClass = new TestFocusListner();
InnFocusHandler innFl = tOuterClass.new InnFocusHandler();
JButton b = new JButton("Send");
b.addFocusListener(innFl); p.add(b);
JButton login = new JButton("Login");
login.addFocusListener(innFl); p.add(login);
f.setSize(300, 200);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
40. www.SunilOS.com
40
Adapter vs Interface
private static class
MyFocHandler extends
FocusAdapter {
public void
focusGained(FocusEvent arg0) {
System.out.println("Got Focus");
}
}
private static class
MyFocHandler implements
FocusListner {
public void
focusGained(FocusEvent a) {
System.out.println("Got Focus");
}
public void
focusLost(FocusEvent a) {
}
}
41. www.SunilOS.com
41
Anonymous Classes
JButton b = new JButton(“Click Me");
FocusAdapter fa= new FocusAdapter();
b.addFocusListener(fa);
OR
JButton b = new JButton(“Click Me");
b.addFocusListener(new FocusAdapter());
42. www.SunilOS.com
42
Anonymous Classes
JButton b = new JButton(“Click Me");
b.addFocusListener(
o new FocusAdapter(){
o public void focusGained(FocusEvent e) {
+ JButton b = (JButton)e.getSource();
+ b.setBackground(Color.BLUE);
• }
o public void focusLost(FocusEvent e) {
• JButton b = (JButton)e.getSource();
• b.setBackground(Color.GRAY);
o }
o }
);
43. www.SunilOS.com
43
Enum
An enum type is a datatype whose fields are fixed
set of constants
The enum declaration defines a class
The enum class body can include methods and
other fields
It has a static values() method that returns an
array containing all of the values
All enums implicitly extend java.lang.Enum.
enum cannot extend any other class.
44. www.SunilOS.com
44
public enum <name>
package com.sunrays.enumpk;
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY,
FRIDAY, SATURDAY;
public int getWeekDay() {
o switch (this) {
• case MONDAY:
• return 0;
o }
}
}
45. www.SunilOS.com
45
TestEnum.java
public static void main(String[] args) {
Day d;
d = Day.SATURDAY;
System.out.println(d.getWeekDay());
switch (d) {
o case MONDAY:
o System.out.println("Mondays are bad.");
o break;
o case FRIDAY:
o System.out.println("Fridays are better.");
o break;
}
46. www.SunilOS.com
46
ENUM with Constructor
public enum Human {
KID(10), MAN(50), OLDMAN(70);
private final int weight;
Human(int w) {
this.weight = w;
}
public void display() {
System.out.println(weight);
}
}
49. Disclaimer
This is an educational presentation to enhance the
skill of computer science students.
This presentation is available for free to computer
science students.
Some internet images from different URLs are
used in this presentation to simplify technical
examples and correlate examples with the real
world.
We are grateful to owners of these URLs and
pictures.
www.SunilOS.com 49