×

Top 10 Java Swing Interview Questions for 2024

1) What is Java Swing?

It is a part of JFC (Java Foundation Classess) that is used to create window-based applications. Java Swing components are platform independent and lightweight .

2) What is difference between AWT and Swing?

The differences between AWT and Swing are:
AWT Swing
Platform-dependent Platform-independent
Heavyweight components Lightweight components
Doesn't support pluggable look and feel Supports pluggable look and feel
Doesn't Follow MVC (Model View Controller) Follow MVC (Model View Controller)
Less components More components

3) What are the methods of component class in Java Swing?

There are four types of methods of component class are:
  • public void add(Component c)
  • public void setSize(int width, int height)
  • public void setLayout(LayoutManager m)
  • public void setVisible(boolean b)

4) How many ways to create a frame in Java Swing ?

There are two ways to create a frame:
  • By Association(creating the object of Frame class)
  • By Inheritance(extending Frame class)

5) Write a simple Java Swing program to create a JButton and adding it on the JFrame object inside the main() method?

Example:
import javax.swing.*;  
public class SwingExample  
{  
public static void main(String[] args)  
{  
JFrame f=new JFrame("SIMPLE FRAME");  
JButton b=new JButton("Click Me");  
b.setBounds(50, 50, 100, 50);  
f.add(b);  
f.setSize(500, 500);  
f.setLayout(null);  
f.setVisible(true);  
}  
}

6) What are the methods of AbstractButton class in Java Swing?

There are several types of methods of AbstractButton class are:
  • void setIcon(Icon b)
  • Icon getIcon()
  • void setText(String s)
  • String getText()
  • void setEnabled(boolean b)
  • void setMnemonic(int a)
  • void addActionListener(ActionListener a)

7) Write a simple Java Swing program to create a JButton by inherit the JFrame Class?

Example:
import javax.swing.*;  
public class SwingExample2 extends JFrame  
{  
SwingExample2()  
{  
JButton b= new JButton("Click Me");  
b.setBounds(50, 50, 100, 50);  
add(b);  
setSize(500,500);  
setLayout(null);  
setVisible(true);  
}  
public static void main(String[] args)   
{  
new SwingExample2();  
}  
}

8) Write a simple Java Swing program to create a JButton With ActionListener?

Example of JButton with ActionListener:
import java.awt.event.*;  
import javax.swing.*;  
public class SwingExample3   
{  
public static void main(String[] args) {  
JFrame f=new JFrame();  
final JTextField tf=new JTextField();  
tf.setBounds(100, 50, 200, 30);  
JButton b=new JButton("Click Here");  
b.setBounds(100, 100, 95, 35);  
b.addActionListener(new ActionListener(){  
public void actionPerformed(ActionEvent e)  
{  
tf.setText("Welcome JavaSwing Expert");  
}  
});  
f.add(tf) ;f.add(b);  
f.setSize(500, 500);  
f.setLayout(null);  
f.setVisible(true);  
}  
}

9) Write a simple Java Swing program of displaying image on the button?

Example:
import javax.swing.*;  
public class SwingExample4 {  
SwingExample4()  
{  
JFrame f=new JFrame();  
JButton b=new JButton(new ImageIcon("C:\\132.jpg"));  
b.setBounds(125, 125, 125, 75);  
f.add(b);  
f.setSize(400, 400);  
f.setLayout(null);  
f.setVisible(true);  
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
}  
public static void main(String[] args)   
{  
new SwingExample4();  
}

10) What are the methods of JLabel class in Java Swing?

Methods of JLabel class are:
  • void setText(String text)
  • String getText()
  • Icon getIcon()
  • int getHorizontalAlignment()
  • void setHorizontalAlignment(int alignment)

Related Topics

Top 15 Hadoop Interview Questions for 2024

1) What is Hadoop? Hadoop is a framework which is used to store and process Big Data. It is a distributed computing platform and helps in analyzing Big Data. 2) What are the...

2 minutes read.

Top 18 Blockchain Interview Questions and Answers for 2024

Top 20 Commonly Asked Blockchain Interview Questions and Answers 1. What do you understand by Blockchain? Blockchain is a chain of blocks made up of digital pieces of information or can be defined as...

6 minutes read.

Top 25 MySQL Interview Questions for 2024

1) What is MySQL? MySql is defined as multi-user DBMS which is built and distributed my MySQLab now acquired by Oracle Co. 2) Enlist the features of MySQL. Cross-Platform Support Wide range...

3 minutes read.

Top 30 CodeIgniter interview Questions for 2024

1) What is CodeIgniter? CodeIgniter is an open source PHP framework. It is used to develop web applications and websites. It is loosely based on MVC pattern and easy to use...

4 minutes read.

Top 15 Data structure & Algorithm Interview Questions for 2024

1) What is Data Structure? Data structure is a way of storing, collecting and organizing data into the main memory. It makes data access more efficient and increase performance. Array, List, Queue...

3 minutes read.

Top 40 TensorFlow Interview Questions and Answers

Commonly Asked TensorFlow Interview Questions for Fresher Q1. What is TensorFlow? TensorFlow is the machine learning library developed by Google's brain team, it supports multiple abstraction levels, and we can choose the...

13 minutes read.

Top 30 SASS Interview Questions for 2024

1) What is Sass? SASS: Systematically Awesome Style Sheets is an extension of CSS. It is also known as CSS pre-processor which helps to reduce repetition with CSS and saves time. It is...

4 minutes read.

Top 30 WordPress Interview Questions for 2024

Most Frequently asked WordPress Interview Questions and Answers 2019 1) What is WordPress? WordPress is an open-source CMS (Content Management System) and a blogging tool. It is used to create web sites and...

5 minutes read.

Top 15 VBA Interview Questions for 2024

1) What is VBA? VBA stands for Visual Basic Application. It is an event-driven programming language. It is mainly used with Microsoft Office applications such as MS -Excel, MS-Word and MS-Access. 2)...

3 minutes read.

Top 22 MariaDB Interview Question for 2024

1) What is MariaDB? It is a community based database devoloped by MySQL Devolopers. It provides same features as MySQL also, can be said that it is a replacement of MySQL. 2)...

3 minutes read.

Top 15 Java JDBC Interview Questions for 2024

1) What are the types of JDBC Drivers in Java? JDBC Drivers are of four types are: JDBC-ODBC bridge driver Native-API driver Network Protocol driver Thin driver 2) What are the five...

4 minutes read.

Top 15 MATLAB Interview Questions for 2024

1) What is MATLAB? MATLAB (matrix laboratory) is fourth-generation programming language. It allows matrix manipulations, implementations of algorithm and many more. 2) Who developed MATLAB? MATLAB is developed by MathWorks. 3) In which language...

2 minutes read.

Top 15 JavaFX Interview Questions for 2024

1) What is JavaFX? JavaFX is a software platform for creating desktop applications or internet application. It uses java library. It can run various devices such as Desktop Computer, Mobile, TVs...

3 minutes read.

Top 30 AngularJS Interview Questions and Answers for 2024

Most Frequently asked AngularJS Interview Questions and Answers for Fresher 1) Describe Angular JS ? It is a powerful framework of JavaScript. AngularJS is used to design Rich Internet Application. This framework...

5 minutes read.

Top 16 Ionic Interview Questions for 2024

Top 20 Ionic Interview Questions and Answers for Freshers 1) What is Ionic framework? Ionic is open source HTML5 framework used for hybrid mobile application development. It provides tools and services for...

3 minutes read.

Top 15 SVG Interview Questions for 2024

1) What is SVG? SVG stands for Scalable Vector Graphics, It is XML based format used to draw dimentional vector images. It is a W3C standard. SVG supports all image formats. 2) What are...

2 minutes read.

VB.NET Interview Questions

1) What is VB.NET? VB.NET stands for Visual Basic .NET. It is an object oriented programming language. It is implemented on .NET framework and launched by Microsoft in 2001. 2) Who is...

3 minutes read.

Top 15 Foundation Interview Questions for 2024

1) What is Foundation? Foundation is a front-end framework that is for designing beautiful responsive websites. This framework is compatible with all types of devices and provides you with HTML, CSS...

2 minutes read.

Top 15 XHTML Interview Questions for 202

1) What is XHTML? XHTML (Extensible Hyper Text Markup Language) it is similar to the HTML but has some strict rule than HTML. It is said to be HTML defined as...

3 minutes read.

Top 15 XML Interview Questions for 2024

1) What is XML? XML (eXtensible Markup Language) is designed to transport and store data. It has user executive tags i.e. no pre-defined tags used as in HTML. This language is...

2 minutes read.