0% found this document useful (0 votes)
16 views14 pages

Java Swing GUI Component Demos

The document contains multiple Java Swing GUI examples demonstrating various components such as JLabel, JButton, JTextField, JCheckBox, JRadioButton, JToggleButton, JMenu, JTable, and JTabbedPane. Each example illustrates the creation and layout of these components within a JFrame, showcasing their functionalities and how they can be integrated into a graphical user interface. Additionally, it discusses layout managers like FlowLayout and BorderLayout used to organize components within containers.

Uploaded by

shreyassupe346
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views14 pages

Java Swing GUI Component Demos

The document contains multiple Java Swing GUI examples demonstrating various components such as JLabel, JButton, JTextField, JCheckBox, JRadioButton, JToggleButton, JMenu, JTable, and JTabbedPane. Each example illustrates the creation and layout of these components within a JFrame, showcasing their functionalities and how they can be integrated into a graphical user interface. Additionally, it discusses layout managers like FlowLayout and BorderLayout used to organize components within containers.

Uploaded by

shreyassupe346
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

KLE Society’s

Bachelor of Computer application


[Link] Science College, HUBBALLI

//Demo on JLabel

import [Link];
import [Link];
import [Link];
import [Link];

public class Jlabeldemo extends JFrame {

Jlabeldemo()
{
setLayout(new FlowLayout());
ImageIcon img=new ImageIcon("D:\\positive thoughts\\[Link]");
JLabel jl1=new JLabel();
JLabel jl2=new JLabel("HELLO");
JLabel jl3=new JLabel(img);
JLabel jl4=new JLabel("Goog Morning",img,[Link]);
add(jl1);add(jl2);add(jl3);add(jl4);
setSize(500,400);
setVisible(true);
}

public static void main(String[] args) {


// TODO Auto-generated method stub
new Jlabeldemo();
}

}
KLE Society’s
Bachelor of Computer application
[Link] Science College, HUBBALLI

//Demo on JButton

import [Link];
import [Link];

import [Link];
import [Link];
import [Link];

public class JButtondemo extends JFrame {


JButtondemo()
{
setLayout(new FlowLayout());
JButton b1=new JButton();
JButton b2=new JButton("CLICK");
ImageIcon img=new ImageIcon("D:\\positive thoughts\\[Link]");
JButton b3=new JButton(img);
[Link](img);
[Link](false);
[Link]("POSITIVE THOUGHT");
[Link](NORMAL);
add(b1);add(b2);add(b3);
setSize(500,400);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new JButtondemo();
}
}
KLE Society’s
Bachelor of Computer application
[Link] Science College, HUBBALLI

//Demo on JTextField

import [Link];

import [Link];
import [Link];

public class Jtextfielddemo extends JFrame {

Jtextfielddemo()
{
setLayout(new FlowLayout());
JTextField t1=new JTextField();
add(t1);
JTextField t2=new JTextField("Enter Name here");
add(t2);
JTextField t3=new JTextField("Enter Age here",20);
add(t3);
JTextField t4=new JTextField(50);
add(t4);

setSize(500,400);
setVisible(true);

}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Jtextfielddemo();
}

}
KLE Society’s
Bachelor of Computer application
[Link] Science College, HUBBALLI

//Demo on JCheckBox
import [Link];
import [Link];

import [Link].*;

public class JCheckBoxdedmo extends JFrame {


JCheckBox c1,c2,c3,c4,c5;

JCheckBoxdedmo(){
setLayout(new FlowLayout());
c1=new JCheckBox("Singing");
ImageIcon img=new ImageIcon("D:\\positive thoughts\\[Link]");
c2=new JCheckBox(img,true);
c3=new JCheckBox(img);
c4=new JCheckBox("Swimming",img);
c5=new JCheckBox("Dancing",img,true);

add(c1);add(c2);add(c3);add(c4);add(c5);
setSize(500,400);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new JCheckBoxdedmo();
}
}

//Jradio button example

import [Link].*;
import [Link].*;
class RadioButtonExample extends JFrame implements ActionListener
{
JRadioButton rb1,rb2;
JButton b;
RadioButtonExample(){
rb1=new JRadioButton("Male");
[Link](100,50,100,30);
rb2=new JRadioButton("Female");
[Link](100,100,100,30);
ButtonGroup bg=new ButtonGroup();
[Link](rb1);[Link](rb2);
KLE Society’s
Bachelor of Computer application
[Link] Science College, HUBBALLI

b=new JButton("click");
[Link](100,150,80,30);
[Link](this);
add(rb1);add(rb2);add(b);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if([Link]()){
[Link](this,"You are Male.");
}
if([Link]()){
[Link](this,"You are Female.");
}
}
public static void main(String args[]){
new RadioButtonExample();
}
}

// Demo On JToggleButton
package lab;

import [Link];
import [Link].*;
import [Link].*;

public class togle1 extends JFrame implements ActionListener {


JToggleButton b1=new JToggleButton("ON");
JLabel l1=new JLabel();
togle1(){
setTitle("JToggleButton with ItemListener Example");
KLE Society’s
Bachelor of Computer application
[Link] Science College, HUBBALLI

setLayout(new FlowLayout());
[Link](this);
add(b1);
add(l1);
setSize(500, 500);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if([Link]())
{
[Link]("It's ON");
[Link]("It is on");
}
else
{
[Link]("It's Off");
[Link]("It is off");
}
}
public static void main(String[] args) {
new togle1();
}
}

// Demo Program on JCheckBox, JRadioButton, JList,


JCombobox,

public class JcheckboxandRadioex extends JFrame implements ItemListener{


JCheckBox c1,c2,c3,c4;
JRadioButton b1,b2;
ButtonGroup cbg;
String msg;
JTextArea t1;
KLE Society’s
Bachelor of Computer application
[Link] Science College, HUBBALLI

JcheckboxandRadioex()
{
setLayout(new FlowLayout());
c1=new JCheckBox("Singing",true);
c2=new JCheckBox("Music",true);
c3=new JCheckBox("Dancing");
c4=new JCheckBox("Swimming");

cbg=new ButtonGroup();
b1=new JRadioButton("1 Sem",true);

b2=new JRadioButton("2 Sem");


[Link](b1);
[Link](b2);
add(c1);add(c2);add(c3);add(c4);
add(b1);add(b2);

String week[]={"mon","tue","wed","thu","fri","sat","sun"};
JList list=new JList(week);

//[Link](100,100, 75,75);
[Link](3);
add(list);

t1=new JTextArea(8,6);
add(t1);

JComboBox ch= new JComboBox(week);


add(ch);
[Link](false);
[Link](this);
[Link](this);
[Link](this);
[Link](this);

setTitle("Check box Demo Program");


setVisible(true);
setSize(500,500);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JcheckboxandRadioex ob=new JcheckboxandRadioex();
}
KLE Society’s
Bachelor of Computer application
[Link] Science College, HUBBALLI

public void itemStateChanged(ItemEvent e) {


if([Link]())
msg="Singing ";
if([Link]())
msg+=" Music";

if([Link]())
msg+="\n1st Sem ";
if([Link]())
msg+="\n2nd Sem ";
[Link](msg);msg="";
}
}

// Demo on JMenu

package lab;

import [Link].*;
import [Link].*;
public class menu1 extends JFrame
{
JMenuBar mb=new JMenuBar();
JMenu fl=new JMenu("File");
JMenu f2=new JMenu("Edit");
JMenu f3=new JMenu("View");
JMenu f4=new JMenu("Help");
JMenuItem m1=new JMenuItem("New");
JMenuItem m2=new JMenuItem("Open File");
JMenuItem m3=new JMenuItem("Save");
JMenuItem m4=new JMenuItem("Save As");
JMenuItem m5=new JMenuItem("Select");
KLE Society’s
Bachelor of Computer application
[Link] Science College, HUBBALLI

JMenuItem m6=new JMenuItem("Cut");


JMenuItem m7=new JMenuItem("Copy");
JMenuItem m8=new JMenuItem("Paste");
menu1(){
[Link](m1);[Link](m2);
[Link]();
[Link](m3);[Link](m4);
[Link](m5);[Link](m6);
[Link]();
[Link](m7);
[Link](m8);
[Link](fl);
[Link](f2);
[Link](f3);
[Link](f4);
setJMenuBar(mb);
setSize(500,500);
setVisible(true);
}
public static void main(String[] args) {
new menu1();
}
}

// Demo on JTable
package lab;

import [Link].*;
public class shopping extends JFrame {
shopping(){
KLE Society’s
Bachelor of Computer application
[Link] Science College, HUBBALLI

String result[][] ={{"I-Phone","68000","2022","America"},


{"Lap-Top","78000","2010","India"}};
String title[]={"Product-Name","Price","Model","Made in"};
JTable tb=new JTable(result,title);
JScrollPane sp=new JScrollPane(tb);
add(sp);
setTitle("Table Creation Example");
setSize(600,600);
setVisible(true);
}
public static void main(String[] args) {
new shopping();
}
}

// Demo on Tabbed Pane


import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class Tabbedpaneex extends JFrame {


Tabbedpaneex()
{
JTabbedPane tb=new JTabbedPane();
JCheckBox c1=new JCheckBox("Singing",true);
JCheckBox c2=new JCheckBox("Music",true);
JPanel p1=new JPanel();
[Link](c1);
[Link](c2);

JCheckBox c3=new JCheckBox("UI UX",true);


JCheckBox c4=new JCheckBox("Fine Arts",true);
KLE Society’s
Bachelor of Computer application
[Link] Science College, HUBBALLI

JPanel p2=new JPanel();


[Link](c3);
[Link](c4);
[Link]("Hobby",p1);
[Link]("OEC",p2);
add(tb);
setTitle("Tabbed Pane Example Program");
setVisible(true);
setSize(500,500);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Tabbedpaneex();
}
}

Containers - Layout
• Each container has a layout manager
– Determines the size, location of contained widgets.
• Setting the current layout of a container:
void setLayout(LayoutManager lm)
• LayoutManager implementing classes:
– BorderLayout
– BoxLayout
– FlowLayout
– GridLayout
KLE Society’s
Bachelor of Computer application
[Link] Science College, HUBBALLI

// Program on FlowLayout

import [Link].*;
import [Link];
import [Link];

public class Flowlayoutdemo extends JFrame {

JButton b1,b2,b3,b4,b5,b6;
public Flowlayoutdemo()
{
KLE Society’s
Bachelor of Computer application
[Link] Science College, HUBBALLI

setLayout(new FlowLayout([Link],20,50));
b1=new JButton("ONE");
b2=new JButton("TWO");
b3=new JButton("THREE");
b4=new JButton("FOUR");
b5=new JButton("FIVE");
b6=new JButton("SIX");
add(b1);add(b2);add(b3);add(b4);add(b5);add(b6);
setTitle("Flow Layout Demo");
setSize(500,400);
setVisible(true);

}
public static void main(String a[])
{
new Flowlayoutdemo();
}
}

// Program on BorderLayout
import [Link].*;
import [Link];
import [Link];

public class BorderLayoutdemo extends JFrame {

JButton b1,b2,b3,b4,b5,b6;
public BorderLayoutdemo ()
{
KLE Society’s
Bachelor of Computer application
[Link] Science College, HUBBALLI

setLayout(new BorderLayout());
b1=new JButton("ONE");
b2=new JButton("TWO");
b3=new JButton("THREE");
b4=new JButton("FOUR");
b5=new JButton("FIVE");

add(b1,[Link]);
add(b2,[Link]);
add(b3,[Link]);
add(b4,[Link]);
add(b5,[Link]);
setTitle("Border Layout Demo");
setSize(500,400);
setVisible(true);

}
public static void main(String a[])
{
new BorderLayoutdemo ();
}
}

Common questions

Powered by AI

Selecting the appropriate layout manager is crucial in a Java Swing application because it dictates the organization, alignment, and resizing behavior of components within a container. BorderLayout divides the space into five areas, allowing versatile placement with convergence on the center area, ideal for complex UIs that require fixed region allocation. GridLayout arranges components in a grid of equal-sized cells, which is suitable for forms or dashboards needing uniform sizes. The choice affects both aesthetic and functional aspects of the application, impacting usability and the user experience .

A JScrollPane provides a scrollable view for components whose content may be larger than the available display area, like JTables, ensuring that the user can access all content smoothly by scrolling. The process involves wrapping the component, such as a JTable, inside a JScrollPane, which automatically manages its viewport, adding horizontal and vertical scrollbars as needed. This enhances the user experience by keeping the interface compact while maintaining access to extensive data .

JTable in Java Swing is advantageous for representing data in a tabular format, supporting functionalities like sorting, filtering, and editing of cells, which makes it suitable for complex data sets. In contrast, a JTextArea allows for multi-line text input and display but lacks the built-in structure and interactiveness for organized data manipulation. JTable provides a more powerful and flexible way to display and manage structured data compared to JTextArea .

A JMenu adds functionality to GUI applications by organizing commands and options into a structured menu interface, enhancing usability and accessibility. The typical hierarchy in setting up menus involves creating a JMenuBar to hold multiple JMenu objects, each containing JMenuItem elements that represent specific actions. This structure allows users to navigate a complex set of options in an organized manner .

JComboBox is utilized in Java Swing applications to facilitate user selection from a pre-defined list of options, enhancing efficiency and preventing input errors. It displays a compact dropdown list that saves space while allowing multiple choices, which can be dynamically populated or modified based on the application’s logic. This widget is especially effective in forms or applications where space is limited but diverse options are necessary .

A JToggleButton in Java allows users to switch between two states, 'on' and 'off', providing visual feedback of the current state which can simplify the process of making a binary choice, like toggling settings. Benefits include intuitive use and the capacity to be part of larger button groups. However, potential limitations include confusion if the toggle state is not visually distinct or if no clear context is provided about the state change, leading to possible usability issues .

JRadioButton in Java is used to create a group of radio buttons where only one can be selected at a time. This contrasts with JCheckBox, which allows multiple selections, enabling users to choose more than one option. Radio buttons are grouped using ButtonGroup so that selecting one button will automatically deselect others, typically used when presenting a list of mutually exclusive options .

ActionListener is essential for handling interactions in Java GUI components, as it allows developers to specify actions that occur in response to user inputs, such as button clicks. This event-driven model significantly impacts an application’s responsiveness by enabling asynchronous handling of user actions, leading to more dynamic and interactive applications. However, relying extensively on ActionListeners may complicate maintenance if not managed properly, particularly in complex applications with numerous interactive components, requiring careful design patterns to organize code effectively .

The setMnemonic() method assigns keyboard shortcuts to JButton objects, allowing users to trigger button actions via the keyboard, thus enhancing accessibility and convenience. This feature improves user interactions by permitting faster navigation and operation without relying on mouse inputs, which is particularly beneficial in applications aiming for efficiency and usability .

FlowLayout in Java is used to arrange components in a left-to-right flow, similar to text in a paragraph, which wraps around to the next line when no more components fit on the current line. This layout manager affects component placement by aligning components in a row and moving them to the next line if there is insufficient horizontal space. It is particularly useful when building simple applications where elements need to be placed in a natural sequential order .

You might also like