×

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

SSRS Interview Questions

1. What is SSRS? The Microsoft SQL Server Reporting Service (SSRS) is a server-based reporting platform. It allows you to produce structured reports. The reports are generally represented in the form of data, graph,...

5 minutes read.

Top 15 Ext Js Interview Question for 2024

1) What is Ext Js? Ext Js refers to Extended JavaScript. Ext JS is a JavaScript framework used to built interactive cross platform web application. 2) What are the features of Ext...

3 minutes read.

Top 25 R programming Language Interview Questions for 2024

1) What is R programming language? R is an open source programming language. It is software environment for statistical computing and graphics. It compiles and runs on a wide variety of...

4 minutes read.

C++ | C Plus Plus Interview Questions for 2024

1) Write a program in C++ to print "Hello World"? #include <iostream.h>     #include <conio.h>   void main()    {       clrscr();       cout << "Hello World";     getch();     } 2) Write a program in C++ to enter two integers and find their sum and average ? #include <iostream.h>   #include <conio.h>   void main()   {   clrscr();   int x,y;   float sum,average;   cout << "Enter 2 integers : " << endl;   cin>>x>>y;   sum=x+y;   average=sum/2;   cout <<"The sum is:"<< sum << endl;   cout <<"The average is:"<< average << endl;   getch();   } 3) Write a program in...

5 minutes read.

Top 13 SAS Interview Questions for 2024

1. What is SAS and its functions? SAS (Statistical Analysis System) is statistical software designed by SAS Institute in 1960 for data inspection and report writing. SAS runs on Windows, UNIX...

5 minutes read.

Top 30 Core Java Interview Questions for 2024

1. What is the difference between HashMap and Hashtable in Java? Ans. S.No. HashMap Hashtable 1. ...

6 minutes read.

Top 15 Framework7 Interview Questions for 2024

1) What is Framework7? Framework7 is an open source mobile HTML framework. It is used todevelop hybrid mobile apps for iOS and Android devices. 2) Why, framework7 is popular? There are various reasons...

8 minutes read.

Top 15 RequireJs Interview Questions for 2024

1) What is RequireJs? RequireJs is a open source JavaScript library which is used to maintain dependencies between JavaScript files and modular programming. 2) What are the features of RequireJs? Following are the...

3 minutes read.

WebSockets Interview Questions

1) What is Web Sockets? Web Sockets is a two way communication between the clients and the servers. Here, the clients and servers both the parties can communicate and exchange data...

3 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 14 Materialize CSS Framework Interview Questions for 2024

1) What is Materialize? Materialize is a CSS framework that contains library file. It is created with CSS, JavaScript and HTML. It helps to create attractive, reliable and functional web pages...

7 minutes read.

Top 15 C Interview Questions for 2024

1) What are different storage class specifiers in C? Register,auto, static, extern are the storage class specifiers in C. 2) What is scope of a variable? How are variables scoped in C? Scope...

3 minutes read.

Top 16 OpenStack Interview Questions for 2024

Most Frequently Asked OpenStack Interview Questions and Answers for Freshers 1. Describe OpenStack. OpenStack is designed as the future of cloud computing and developed as free and open-source software that facilitates a...

8 minutes read.

Top 22 DOJO Interview Questions for 2024

1) What is DOJO? DOJO is an open source JavaScript framework designed for very fast development of JavaScript/AJAX based application and websites having cross platform interdependencies. 2) Enlist all features of DOJO. ...

4 minutes read.

Top 30 Cassandra Interview Questions for 2024

1) Explain. What is Cassandra? Cassandra is an open source data storage system. It was developed at Facebook for inbox search and designed for storing and managing large amount of data. 2)...

5 minutes read.

Top 30 Less Interview Questions for 2024

1) What is Less? Less is a dynamic style sheet language that can be compiled in to CSS (Cascading style Sheet). It runs on client side and server side. It is...

4 minutes read.

Top 20 HTTP Interview Questions for 2024

1) What is HTTP? HTTP stands for Hypertext Transfer Protocol. It is a set of rules for transferring of data on WWW (World Wide Web). 2) What are the basic Features of...

4 minutes read.

Top 31 PHP Interview Questions for 2024

1) What is PHP? PHP is a object-oriented scripting language, open source, interpreted and executed at server side. It is also used to develop web applications. 2) What are the features of PHP? Features...

4 minutes read.

Top 28 Scala Interview Questions for 2024

1) What is Scala? Scala is a functional and object-oriented programming language. It is used to develop web application, mobile application, enterprise application, desktop based application etc. Everything in Scala is in object...

4 minutes read.

Top 30 JSF Interview Questions for 2024

1) What is JSF? JSF stands for Java Server Faces is a Java-Based web application framework. JSF (Java Server Faces) provides a facility to connect UI widgets with data sources. 2) What are...

4 minutes read.