Jsin Java
Jsin Java
The JProgressBar class is used to display the progress of the task. It inherits JComponent
class.
Constructor Description
Method Description
void setValue(int value) It is used to set the current value on the progress bar.
METHOD EXPLANATION
list to i
Changes the
constructs a read-only
setListData(Vector l) specified.
list
setFixedCellWidth(int w) parameter.
setFixedCellHeight(int h) as parameter.
selected indices, in
getDragEnabled() enable
l) the list
Output :
2. Program to create a list and add itemListener to it (program to select your birthday
using lists) .
// (program to select your birthday using lists) .
import javax.swing.event.*;
import java.awt.*;
import javax.swing.*;
class solve extends JFrame implements ListSelectionListener
{
//frame
static JFrame f;
//lists
static JList b,b1,b2;
//label
static JLabel l1;
//main class
public static void main(String[] args)
{
//create a new frame
f = new JFrame("frame");
//create a object
solve s=new solve();
//create a panel
JPanel p =new JPanel();
//create a new label
JLabel l= new JLabel("select your bithday");
l1= new JLabel();
//String array to store weekdays
String month[]= { "January", "February", "March",
"April", "May", "June", "July", "August",
"September", "October", "November", "December"};
//create a array for months and year
String date[]=new String[31],year[]=new String[31];
//add month number and year to list
for(int i=0;i<31;i++)
{
date[i]=""+(int)(i+1);
year[i]=""+(int)(2018-i);
}
//create lists
b= new JList(date);
b1= new JList(month);
b2= new JList(year);
//set a selected index
b.setSelectedIndex(2);
b1.setSelectedIndex(1);
b2.setSelectedIndex(2);
l1.setText(b.getSelectedValue()+" "+b1.getSelectedValue()
+" "+b2.getSelectedValue());
//add item listener
b.addListSelectionListener(s);
b1.addListSelectionListener(s);
b2.addListSelectionListener(s);
//add list to panel
p.add(l);
p.add(b);
p.add(b1);
p.add(b2);
p.add(l1);
f.add(p);
//set the size of frame
f.setSize(500,600);
f.show();
}
public void valueChanged(ListSelectionEvent e)
{
//set the text of the label to the selected value of lists
l1.setText(b.getSelectedValue()+" "+b1.getSelectedValue()
+" "+b2.getSelectedValue());
}
Output :
// default constructor
spinner()
{
}
// main class
public static void main(String[] args)
{
// create a new frame
f = new JFrame("spinner");
// create a JSpinner
JSpinner s = new JSpinner();
f.show();
}
}
Output :
2. Program to create a JSpinner and add ChangeListener to it ; Program to select
your date of birth using JSpinner)
// Java program to select your
// date of birth using JSpinner
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
class spinner1 extends JFrame implements ChangeListener {
// frame
static JFrame f;
// label
static JLabel l, l1;
// spinner
static JSpinner s, s1, s2;
// default constructor
spinner1()
{
}
// main class
public static void main(String[] args)
{
// create an object of the class
spinner1 sp1 = new spinner1();
// create a label
l = new JLabel("select your date of birth");
l1 = new JLabel("1 January 2000");
// setvalue of year
s.setValue(2000);
// add label
f.add(l);
f.add(l1);
f.add(s);
f.add(s1);
f.add(s2);
f.show();
}
Output :
Note : This program will not run in an online compiler please use an Offline IDE
// Constructor
JTableExamples()
{
// Frame initiallization
f = new JFrame();
// Frame Title
f.setTitle("JTable Example");
// Column Names
String[] columnNames = { "Name", "Roll Number", "Department" };
// adding it to JScrollPane
JScrollPane sp = new JScrollPane(j);
f.add(sp);
// Frame Size
f.setSize(500, 200);
// Frame Visible = true
f.setVisible(true);
}
// Driver method
public static void main(String[] args)
{
new JTableExamples();
}
}
Output:
Java Swing | JFileChooser
JFileChooser is a part of java Swing package. The java Swing package is part of
JavaTM Foundation Classes(JFC) . JFC contains many features that help in building
graphical user interface in java . Java Swing provides components such as buttons,
panels, dialogs, etc . JFileChooser is a easy and an effective way to prompt the user to
choose a file or a directory .
In this article we will see how to use JFileChooser in java swing .
Constructors of JFileChooser are :
1. JFileChooser() – empty constructor that points to user’s default directory
// Using this process to invoke the contructor,
// JFileChooser points to user's default directory
JFileChooser j = new JFileChooser();
Note : The code given above are code snippets not the full code, the code snippets given
above should be used to invoke the constructor as per the need and discretion of the
programmer, the paths mentioned above are arbitrary. User should set the path according to
their need.
updateUI() changed
value.
Creating a Custom Chooser Panel: The default color chooser provides five chooser
panels:
1. Swatches: For choosing a color from a collection of swatches.
2. HSV: For choosing a color using the Hue-Saturation-Value color representation.
Prior to JDK 7, It was known as HSB, for Hue-Saturation-Brightness.
3. HSL: For choosing a color using the Hue-Saturation-Lightness color
representation.
4. RGB: For choosing a color using the Red-Green-Blue color model.
5. CMYK: For choosing a color using the process color or four color model.
Below programs illustrate the use of JColorChooser class:
1. Java program to implement JColorChooser class using ChangeListener: In
this program, we first create a label at the top of the window where some text is
shown in which we will apply color changes. Set the foreground and background
color. Set the size and type of the font. Create a Panel and set its layout. Now set
up the color chooser for setting text color. Using stateChanged() method, event is
generated for change in color of the text by using getColor() method. Now create
the GUI, create a setup window. Set the default close operation of the window.
Create and set up the content Pane and add content to the frame and display the
window.
// Java program to implement JColorChooser
// class using ChangeListener
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.colorchooser.*;
implements ChangeListener {
public ColorChooserDemo()
{
super(new BorderLayout());
add(bannerPanel, BorderLayout.CENTER);
add(Jcc, BorderLayout.PAGE_END);
}
// Main Method
public static void main(String[] args)
{
createAndShowGUI();
}
});
}
}
// create a button
JButton b = new JButton("color");
Container c = getContentPane();
// Constructor
ColorChooserExample()
{
// set Layout
c.setLayout(new FlowLayout());
// add Listener
b.addActionListener(this);
// Main Method
public static void main(String[] args)
{
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Java Swing | JMenuBar
JMenuBar, JMenu and JMenuItems are a part of Java Swing package. JMenuBar is an
implementation of menu bar . the JMenuBar contains one or more JMenu objects, when
the JMenu objects are selected they display a popup showing one or more
JMenuItems .
JMenu basically represents a menu . It contains several JMenuItem Object . It may also
contain JMenu Objects (or submenu).
Constructors :
1. JMenuBar() : Creates a new MenuBar.
2. JMenu() : Creates a new Menu with no text.
3. JMenu(String name) : Creates a new Menu with a specified name.
4. JMenu(String name, boolean b) : Creates a new Menu with a specified name
and boolean
value specifies it as a tear-off menu or not. A tear-off menu can be opened and
dragged away from its parent menu bar or menu.
Commonly used methods:
1. dd(JMenu c) : Adds menu to the menu bar. Adds JMenu object to the Menu bar.
2. add(Component c) : Add component to the end of JMenu
3. add(Component c, int index) : Add component to the specified index of JMenu
4. add(JMenuItem menuItem) : Adds menu item to the end of the menu.
5. add(String s) : Creates a menu item with specified string and appends it to the
end of menu.
6. getItem(int index) : Returns the specified menuitem at the given index
// JMenu
static JMenu x;
// Menu items
static JMenuItem m1, m2, m3;
// create a frame
static JFrame f;
public static void main()
{
// create a frame
f = new JFrame("Menu demo");
// create a menubar
mb = new JMenuBar();
// create a menu
x = new JMenu("Menu");
// create menuitems
m1 = new JMenuItem("MenuItem1");
m2 = new JMenuItem("MenuItem2");
m3 = new JMenuItem("MenuItem3");
Output :
// JMenu
static JMenu x, x1;
// Menu items
static JMenuItem m1, m2, m3, s1, s2;
// create a frame
static JFrame f;
// a label
static JLabel l;
// main class
public static void main()
{
// create an object of the class
menu1 m = new menu1();
// create a frame
f = new JFrame("Menu demo");
// create a label
l = new JLabel("no task ");
// create a menubar
mb = new JMenuBar();
// create a menu
x = new JMenu("Menu");
x1 = new JMenu("submenu");
// create menuitems
m1 = new JMenuItem("MenuItem1");
m2 = new JMenuItem("MenuItem2");
m3 = new JMenuItem("MenuItem3");
s1 = new JMenuItem("SubMenuItem1");
s2 = new JMenuItem("SubMenuItem2");
// add submenu
x.add(x1);
// add label
f.add(l);
Output:
Note : The following program might not run in an online compiler, please use an offline
IDE.
import java.awt.GridLayout;
import java.awt.Label;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
frame.getContentPane().add(tabbedPane);
javax.swing.SwingUtilities.invokeLater(new Runnable() {
createAndShowGUI();
});
}
}
This was an example on how to create a JTabbedPane component in Java.
JCalendar
Introduction
JCalendar is a Java date chooser bean for graphically picking a date. JCalendar is composed of
several other Java beans, a JDayChooser, a JMonthChooser and a JYearChooser. All these beans
have a locale property, provide several icons (Color 16×16, Color 32×32, Mono 16×16 and
Mono 32×32) and their own locale property editor. So they can easily be used in GUI builders.
Also part of the package is a JDateChooser, a bean composed of an IDateEditor (for direct date
editing) and a button for opening a JCalendar for selecting the date.
License
This program is free software; you can redistribute it and/or modify it under the terms of
the GNU Lesser General Public License as published by the Free Software Foundation. If you
like and use it, just let me know. If you prefer a commercial license without any of the LGPL
restrictions, please contact Kai Toedter.
Installation
The installation is very easy, just put jcalendar.jar in your class path. If you want to run the
JCalendar demos (see below) or just use the great JGoodies Looks Look and Feel, put also
jgoodies-looks-2.4.1.jar in your class path. Both are in the lib directory of this JCalendar
distribution.
Components
The following table shows a list of used components (all Java Beans). All the screen shots use
the great Plastic 3D Look and Feel (included in JGoodies Looks by JGoodies), which is bundled
with the JCalendar bean.
Icon Icon
Component Description
16×16 32×32
JDateChooser allows you to pick a date. You could either edit
the date directly or click the image to popup a JCalendar to
choose the date. The default date editor provides coloring of
invalid dates as well as optional showing a mask. Also a
JSpinnerDateEditor is provided that uses a JSpinner to display
the date.
JCalendar allows you to choose a year, a month and a day.
Depending on the locale, the month names and the weekday
names change. The foreground of “today” is painted red.
JCalendar is composed of several other beans described below.
Requirements
All beans use Swing components, so you need to have the Java SE installed. All beans also work with Java SE
1.4.x and 5.