swing handbook
TRANSCRIPT
Swing
MrPMSawantDept Of Computer ScienceProfRamkirshna More ACS CollegeAkurdiEmail IdsawanprasadgmailCom
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Swing is a widget toolkit for Java It is part of Sun Microsystems Java Foundation Classes (JFC) mdash an API for providing a graphical user interface (GUI) for Java programsSwing was developed to provide a more sophisticated set of GUI components than the earlier Abstract Window Toolkit Swing provides a native look and feel that emulates the look and feel of several platforms and also supports a pluggable look and feel that allows applications to have a look and feel unrelated to the underlying platform
Relationship to AWTSince early versions of Java a portion of the Abstract Window Toolkit (AWT) has provided platform-independent APIs for user interface components In AWT each component is rendered and controlled by a native peer component specific to the underlying windowing systemBy contrast Swing components are often described as lightweight because they do not require allocation of native resources in the operating systems windowing toolkit The AWT components are referred to as heavyweight componentsMuch of the Swing API is generally a complementary extension of the AWT rather than a direct replacement In fact every Swing lightweight interface ultimately exists within an AWT heavyweight component because all of the top-level components in Swing (JApplet JDialog JFrame and JWindow) extend an AWT top-level container However the use of both lightweight and heavyweight components within the same window is generally discouraged due to Z-order incompatibilitiesThe core rendering functionality used by Swing to draw its lightweight components is provided by Java 2D another part of JFC
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JApplet
Constructor
JApplet() Creates a swing applet instance
Demoimport javaawtimport javaxswingltapplet code=JAppletDemoclass width=300 height=200gtltappletgtpublic class JAppletDemo extends JApplet public void init() Container content = getContentPane() contentsetBackground(Colorwhite) contentsetLayout(new FlowLayout()) contentadd(new JButton(Button 1)) contentadd(new JButton(Button 2)) contentadd(new JButton(Button 3))
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JFrame
Constructor Summary
JFrame() Constructs a new frame that is initially invisible
JFrame(GraphicsConfiguration gc) reates a Frame in the specified GraphicsConfiguration of a screen device and a blank title
JFrame(String title) Creates a new initially invisible Frame with the specified title
JFrame(String title GraphicsConfiguration gc) Creates a JFrame with the specified title and the specified GraphicsConfiguration of a screen device
Demo
import javaawtimport javaxswingpublic class JFrameDemo public static void main(String[] args) JFrame f = new JFrame(JFrame Demo) fsetSize(400 150) Container content = fgetContentPane() contentsetBackground(Colorwhite) contentsetLayout(new FlowLayout()) contentadd(new JButton(Button 1)) contentadd(new JButton(Button 2)) contentadd(new JButton(Button 3)) fsetVisible(true) fsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JLabel
Constructor Summary
JLabel() Creates a JLabel instance with no image and with an empty string for the title
JLabel(Icon image) Creates a JLabel instance with the specified image
JLabel(Icon image int horizontalAlignment) Creates a JLabel instance with the specified image and horizontal alignment
JLabel(String text) Creates a JLabel instance with the specified text
JLabel(String text Icon icon int horizontalAlignment) Creates a JLabel instance with the specified text image and horizontal alignment
JLabel(String text int horizontalAlignment) Creates a JLabel instance with the specified text and horizontal alignment
Demo
import javaawtimport javaxswingpublic class JLabelDemo extends JFrame public static void main(String[] args) new JLabelDemo() public JLabelDemo() JFrame f=new JFrame() JLabel coloredLabel = new JLabel(HelloLabelCENTER) Container c=getContentPane() csetLayout(new GridLayout(01)) cadd(coloredLabel) pack() fadd(c) fsetVisible(true) fsetSize(150150)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JButton
Constructor Summary
JButton() Creates a button with no set text or icon
JButton(Action a) Creates a button where properties are taken from the Action supplied
JButton(Icon icon) Creates a button with an icon
JButton(String text) Creates a button with text
JButton(String text Icon icon) Creates a button with initial text and an icon
Demoimport javaawtimport javaawteventimport javaxswingpublic class JButtonDemo extends JFrame implements ActionListener private JButton btn_simple btn_image public JButtonDemo() setTitle(Testing Buttons) Container container = getContentPane() containersetLayout(new GridLayout(02)) btn_simple = new JButton(Simple Button) btn_simplesetMnemonic(KeyEventVK_S) containeradd(btn_simple) Icon bug1 = new ImageIcon(Dmenu_1gif) btn_image = new JButton(Imagebug1) btn_imagesetMnemonic(KeyEventVK_I) containeradd(btn_image) btn_imageaddActionListener(this) btn_simpleaddActionListener(this) setSize(300100) setVisible(true) public static void main(String args[]) new JButtonDemo() public void actionPerformed(ActionEvent ae) JOptionPaneshowMessageDialog(nullYou pressed + aegetActionCommand())
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JCheckBox
Constructor Summary
JCheckBox() Creates an initially unselected check box button with no text no icon
JCheckBox(Action a) Creates a check box where properties are taken from the Action supplied
JCheckBox(Icon icon) Creates an initially unselected check box with an icon
JCheckBox(Icon icon boolean selected) Creates a check box with an icon and specifies whether or not it is initially selected
JCheckBox(String text) Creates an initially unselected check box with text
JCheckBox(String text boolean selected) Creates a check box with text and specifies whether or not it is initially selected
JCheckBox(String text Icon icon) Creates an initially unselected check box with the specified text and icon
JCheckBox(String text Icon icon boolean selected) Creates a check box with text and icon and specifies whether or not it is initially selected
Demoimport javaawtContainerimport javaawteventimport javaxswingclass JCheckboxDemo public static void main(String[] args) JFrame frame = new JFrame() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Container cp = framegetContentPane() Box box = new Box(BoxLayoutY_AXIS) cpadd(box) boxadd(new JLabel(Tick the sports you like)) String[] sports = Football Rugby CricketBadminton JCheckBox[] cba = new JCheckBox[sportslength] for (int i = 0 i lt sportslength i++) cba[i] = new JCheckBox(sports[i]) boxadd(cba[i]) framepack()copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JRadioButton
Constructor Summary
JRadioButton() Creates an initially unselected radio button with no set text
JRadioButton(Action a) Creates a radiobutton where properties are taken from the Action supplied
JRadioButton(Icon icon) Creates an initially unselected radio button with the specified image but no text
JRadioButton(Icon icon boolean selected) Creates a radio button with the specified image and selection state but no text
JRadioButton(String text) Creates an unselected radio button with the specified text
JRadioButton(String text boolean selected) Creates a radio button with the specified text and selection state
JRadioButton(String text Icon icon) Creates a radio button that has the specified text and image and that is initially unselected
JRadioButton(String text Icon icon boolean selected) Creates a radio button that has the specified text image and selection state
Demoimport javaawtimport javaawteventimport javaxswingclass JRadioButtonDemo public static void main(String[] args) ActionListener listener = new ActionListener() public void actionPerformed (ActionEvent e) Systemoutprintln(egetActionCommand()) Box p = new Box(BoxLayoutY_AXIS) ButtonGroup group = new ButtonGroup() String[] sa = uglikiwipassionkumquat for(int i=0iltsalength++i) JRadioButton b = new JRadioButton(sa[i]) groupadd(b) padd(b) baddActionListener(listener) JFrame frame = new JFrame(Fruits)framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
framegetContentPane()add(p)framepack() framesetVisible(true)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JTextField
Constructor Summary
JTextField() Constructs a new TextField
JTextField(Document doc String text int columns) Constructs a new JTextField that uses the given text storage model and the given number of columns
JTextField(int columns) Constructs a new empty TextField with the specified number of columns
JTextField(String text) Constructs a new TextField initialized with the specified text
JTextField(String text int columns) Constructs a new TextField initialized with the specified text and columns
Demo
import javaawtimport javaawteventimport javaxswing
public class JLabelDemo extends JFrame
public JLabelDemo() JTextField tf=new JTextField() JFrame f = new JFrame(TextFieldDemo) Container c= getContentPane() csetLayout(new GridLayout(02)) JLabel lbl1=new JLabel(TextFieldSwingConstantsLEFT) cadd(lbl1) cadd(tf) fadd(c)
fsetSize(300100) fsetVisible(true) fsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
public static void main(String[] args) new JLabelDemo()
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JMenu
Constructor Summary
JMenu() Constructs a new JMenu with no text
JMenu(Action a) Constructs a menu whose properties are taken from the Action supplied
JMenu(String s) Constructs a new JMenu with the supplied string as its text
JMenu(String s boolean b) Constructs a new JMenu with the supplied string as its text and specified as a tear-off menu or not
Demoimport javaawtimport javaxswingclass JMenuDemo public static void main(String[] args) JMenu menu1 = new JMenu(File) menu1add(new JMenuItem(New)) menu1add(new JMenuItem(Open)) menu1add(new JSeparator()) menu1add(new JMenuItem(Save)) menu1add(new JMenuItem(Save as)) menu1add(new JSeparator()) menu1add(new JMenuItem(Exit)) JMenu menu2 = new JMenu(Edit) menu2add(new JMenuItem(Copy)) menu2add(new JSeparator()) menu2add(new JMenuItem(Paste)) menu2add(new JSeparator()) menu2add(new JMenuItem(Select ALL)) JMenuBar bar = new JMenuBar() baradd(menu1) baradd(menu2) JFrame frame = new JFrame() framesetJMenuBar(bar) framesetSize(350300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JColorChooser
Constructor Summary
JColorChooser() Creates a color chooser pane with an initial color of white
JColorChooser(Color initialColor) Creates a color chooser pane with the specified initial color
JColorChooser(ColorSelectionModel model) Creates a color chooser pane with the specified ColorSelectionModel
Demoimport javaawtimport javaawteventimport javaxswingpublic class ColorCh static Container cp public static void main(String[] args) final JFrame frame = new JFrame(Color Choice) cp = framegetContentPane( ) cpsetLayout(new GridBagLayout()) JButton button = new JButton(select color) cpadd(button) buttonaddActionListener(new ActionListener( ) public void actionPerformed(ActionEvent e) Color c = JColorChoosershowDialog(frameChoose a color Colorred) cpsetBackground(c) ) framesetSize(200 300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Select Any Color and Press OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JOptionPane
Constructor Summary
JOptionPane() Creates a JOptionPane with a test message
JOptionPane(Object message) Creates a instance of JOptionPane to display a message using the plain-message message type and the default options delivered by the UI
JOptionPane(Object message int messageType) Creates an instance of JOptionPane to display a message with the specified message type and the default options
JOptionPane(Object message int messageType int optionType) Creates an instance of JOptionPane to display a message with the specified message type and options
JOptionPane(Object message int messageType int optionType Icon icon) Creates an instance of JOptionPane to display a message with the specified message type options and icon
JOptionPane(Object message int messageType int optionType Icon icon Object[] options) Creates an instance of JOptionPane to display a message with the specified message type icon and options
JOptionPane(Object message int messageType int optionType Icon icon Object[] options Object initialValue) Creates an instance of JOptionPane to display a message with the specified message type icon and options with the initially-selected option specified
Demo
import javaxswingJOptionPanepublic class JOptionPaneDemo public static void main(String[] args) String[] choices = BJP NCP LEFT int doAgain do int response = JOptionPaneshowOptionDialog ( null center over parent
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
How did you vote message Party Poll title in titlebar JOptionPaneYES_NO_OPTION Option type JOptionPanePLAIN_MESSAGE messageType null icon choices Options None of your business initial value ) JOptionPaneshowMessageDialog(null Response = + response) doAgain = JOptionPaneshowConfirmDialog(null Again) while(doAgain == JOptionPaneYES_OPTION) Systemexit(0)
Output
Press Any One
After Pressing OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JProgressBar
Constructor Summary
JProgressBar() Creates a horizontal progress bar that displays a border but no progress string
JProgressBar(BoundedRangeModel newModel) Creates a horizontal progress bar that uses the specified model to hold the progress bars data
JProgressBar(int orient) Creates a progress bar with the specified orientation which can be either SwingConstantsVERTICAL or SwingConstantsHORIZONTAL
JProgressBar(int min int max) Creates a horizontal progress bar with the specified minimum and maximum
JProgressBar(int orient int min int max) Creates a progress bar using the specified orientation minimum and maximum
Demo
import javaawtimport javaawteventimport javaxswingimport javaxswingtexthtmlpublic class SwingProgressBarExample final static int interval = 1000 int i JLabel label JProgressBar pb Timer timer JButton button public SwingProgressBarExample() JFrame frame = new JFrame(Swing Progress Bar) button = new JButton(Start) buttonaddActionListener(new ButtonListener()) pb = new JProgressBar(0 20) pbsetValue(0) pbsetStringPainted(true) label = new JLabel(By Prasad Sawant) JPanel panel = new JPanel() paneladd(button) paneladd(pb)copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JPanel panel1 = new JPanel() panel1setLayout(new BorderLayout()) panel1add(panel BorderLayoutNORTH) panel1add(label BorderLayoutCENTER) panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20)) framesetContentPane(panel1) framepack() framesetVisible(true) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Create a timer timer = new Timer(interval new ActionListener() public void actionPerformed(ActionEvent evt) if (i == 20) ToolkitgetDefaultToolkit()beep() timerstop() buttonsetEnabled(true) pbsetValue(0) String str = lthtmlgt + ltfont color=FF0000gt + ltbgt + Downloading completed + ltbgt + ltfontgt + lthtmlgt labelsetText(str) i = i + 1 pbsetValue(i) ) class ButtonListener implements ActionListener public void actionPerformed(ActionEvent ae) buttonsetEnabled(false) i = 0 String str = lthtmlgt + ltfont color=008000gt + ltbgt +Downloading is in process + ltbgt + ltfontgt + lthtmlgt labelsetText(str) timerstart() public static void main(String[] args) SwingProgressBarExample spb = new SwingProgressBarExample()
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
After Pressing Start Button
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JSpinner
A single line input field that lets the user select a number or an object value from an ordered sequence Spinners typically provide a pair of tiny arrow buttons for stepping through the elements of the sequence The keyboard updown arrow keys also cycle through the elements The user may also be allowed to type a (legal) value directly into the spinner Although combo boxes provide similar functionality spinners are sometimes preferred because they dont require a drop down list that can obscure important data
A JSpinners sequence value is defined by its SpinnerModel The model can be specified as a constructor argument and changed with the model property SpinnerModel classes for some common types are provided SpinnerListModel SpinnerNumberModel and SpinnerDateModel
A JSpinner has a single child component thats responsible for displaying and potentially changing the current element or value of the model which is called the editor The editor is created by the JSpinners constructor and can be changed with the editor property The JSpinners editor stays in sync with the model by listening for ChangeEvents If the user has changed the value displayed by the editor it is possible for the models value to differ from that of the editor To make sure the model has the same value as the editor use the commitEdit method
Demo
import javaawtimport javaxswingimport javaxswingeventclass JSpinnerExample public static void main(String[] args) String[] sizes = smallmediumlargeXLXXL final SpinnerListModel model = new SpinnerListModel(sizes) JSpinner jSpinner = new JSpinner(model) modeladdChangeListener(new ChangeListener() public void stateChanged(ChangeEvent e) Systemoutprintln(modelgetValue()) ) JFrame frame = new JFrame() framesetSize(100100) framegetContentPane()add(jSpinner) framepack() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Press UP KEY
Press UP KEY
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
- Relationship to AWT
-
- JProgressBar
- Demo
- import javaawt
- import javaawtevent
- import javaxswing
- import javaxswingtexthtml
- public class SwingProgressBarExample
- final static int interval = 1000
- int i
- JLabel label
- JProgressBar pb
- Timer timer
- JButton button
- public SwingProgressBarExample()
- JFrame frame = new JFrame(Swing Progress Bar)
- button = new JButton(Start)
- buttonaddActionListener(new ButtonListener())
- pb = new JProgressBar(0 20)
- pbsetValue(0)
- pbsetStringPainted(true)
- label = new JLabel(By Prasad Sawant)
- JPanel panel = new JPanel()
- paneladd(button)
- paneladd(pb)
- JPanel panel1 = new JPanel()
- panel1setLayout(new BorderLayout())
- panel1add(panel BorderLayoutNORTH)
- panel1add(label BorderLayoutCENTER)
- panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20))
- framesetContentPane(panel1)
- framepack()
- framesetVisible(true)
- framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
- Create a timer
- timer = new Timer(interval new ActionListener()
- public void actionPerformed(ActionEvent evt)
- if (i == 20)
- ToolkitgetDefaultToolkit()beep()
- timerstop()
- buttonsetEnabled(true)
- pbsetValue(0)
- String str = lthtmlgt + ltfont color=FF0000gt + ltbgt +
- Downloading completed + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- i = i + 1
- pbsetValue(i)
- )
- class ButtonListener implements ActionListener
- public void actionPerformed(ActionEvent ae)
- buttonsetEnabled(false)
- i = 0
- String str = lthtmlgt + ltfont color=008000gt + ltbgt +
- Downloading is in process + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- timerstart()
- public static void main(String[] args)
- SwingProgressBarExample spb = new SwingProgressBarExample()
- Press UP KEY
- Press UP KEY
-
Swing
Swing is a widget toolkit for Java It is part of Sun Microsystems Java Foundation Classes (JFC) mdash an API for providing a graphical user interface (GUI) for Java programsSwing was developed to provide a more sophisticated set of GUI components than the earlier Abstract Window Toolkit Swing provides a native look and feel that emulates the look and feel of several platforms and also supports a pluggable look and feel that allows applications to have a look and feel unrelated to the underlying platform
Relationship to AWTSince early versions of Java a portion of the Abstract Window Toolkit (AWT) has provided platform-independent APIs for user interface components In AWT each component is rendered and controlled by a native peer component specific to the underlying windowing systemBy contrast Swing components are often described as lightweight because they do not require allocation of native resources in the operating systems windowing toolkit The AWT components are referred to as heavyweight componentsMuch of the Swing API is generally a complementary extension of the AWT rather than a direct replacement In fact every Swing lightweight interface ultimately exists within an AWT heavyweight component because all of the top-level components in Swing (JApplet JDialog JFrame and JWindow) extend an AWT top-level container However the use of both lightweight and heavyweight components within the same window is generally discouraged due to Z-order incompatibilitiesThe core rendering functionality used by Swing to draw its lightweight components is provided by Java 2D another part of JFC
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JApplet
Constructor
JApplet() Creates a swing applet instance
Demoimport javaawtimport javaxswingltapplet code=JAppletDemoclass width=300 height=200gtltappletgtpublic class JAppletDemo extends JApplet public void init() Container content = getContentPane() contentsetBackground(Colorwhite) contentsetLayout(new FlowLayout()) contentadd(new JButton(Button 1)) contentadd(new JButton(Button 2)) contentadd(new JButton(Button 3))
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JFrame
Constructor Summary
JFrame() Constructs a new frame that is initially invisible
JFrame(GraphicsConfiguration gc) reates a Frame in the specified GraphicsConfiguration of a screen device and a blank title
JFrame(String title) Creates a new initially invisible Frame with the specified title
JFrame(String title GraphicsConfiguration gc) Creates a JFrame with the specified title and the specified GraphicsConfiguration of a screen device
Demo
import javaawtimport javaxswingpublic class JFrameDemo public static void main(String[] args) JFrame f = new JFrame(JFrame Demo) fsetSize(400 150) Container content = fgetContentPane() contentsetBackground(Colorwhite) contentsetLayout(new FlowLayout()) contentadd(new JButton(Button 1)) contentadd(new JButton(Button 2)) contentadd(new JButton(Button 3)) fsetVisible(true) fsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JLabel
Constructor Summary
JLabel() Creates a JLabel instance with no image and with an empty string for the title
JLabel(Icon image) Creates a JLabel instance with the specified image
JLabel(Icon image int horizontalAlignment) Creates a JLabel instance with the specified image and horizontal alignment
JLabel(String text) Creates a JLabel instance with the specified text
JLabel(String text Icon icon int horizontalAlignment) Creates a JLabel instance with the specified text image and horizontal alignment
JLabel(String text int horizontalAlignment) Creates a JLabel instance with the specified text and horizontal alignment
Demo
import javaawtimport javaxswingpublic class JLabelDemo extends JFrame public static void main(String[] args) new JLabelDemo() public JLabelDemo() JFrame f=new JFrame() JLabel coloredLabel = new JLabel(HelloLabelCENTER) Container c=getContentPane() csetLayout(new GridLayout(01)) cadd(coloredLabel) pack() fadd(c) fsetVisible(true) fsetSize(150150)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JButton
Constructor Summary
JButton() Creates a button with no set text or icon
JButton(Action a) Creates a button where properties are taken from the Action supplied
JButton(Icon icon) Creates a button with an icon
JButton(String text) Creates a button with text
JButton(String text Icon icon) Creates a button with initial text and an icon
Demoimport javaawtimport javaawteventimport javaxswingpublic class JButtonDemo extends JFrame implements ActionListener private JButton btn_simple btn_image public JButtonDemo() setTitle(Testing Buttons) Container container = getContentPane() containersetLayout(new GridLayout(02)) btn_simple = new JButton(Simple Button) btn_simplesetMnemonic(KeyEventVK_S) containeradd(btn_simple) Icon bug1 = new ImageIcon(Dmenu_1gif) btn_image = new JButton(Imagebug1) btn_imagesetMnemonic(KeyEventVK_I) containeradd(btn_image) btn_imageaddActionListener(this) btn_simpleaddActionListener(this) setSize(300100) setVisible(true) public static void main(String args[]) new JButtonDemo() public void actionPerformed(ActionEvent ae) JOptionPaneshowMessageDialog(nullYou pressed + aegetActionCommand())
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JCheckBox
Constructor Summary
JCheckBox() Creates an initially unselected check box button with no text no icon
JCheckBox(Action a) Creates a check box where properties are taken from the Action supplied
JCheckBox(Icon icon) Creates an initially unselected check box with an icon
JCheckBox(Icon icon boolean selected) Creates a check box with an icon and specifies whether or not it is initially selected
JCheckBox(String text) Creates an initially unselected check box with text
JCheckBox(String text boolean selected) Creates a check box with text and specifies whether or not it is initially selected
JCheckBox(String text Icon icon) Creates an initially unselected check box with the specified text and icon
JCheckBox(String text Icon icon boolean selected) Creates a check box with text and icon and specifies whether or not it is initially selected
Demoimport javaawtContainerimport javaawteventimport javaxswingclass JCheckboxDemo public static void main(String[] args) JFrame frame = new JFrame() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Container cp = framegetContentPane() Box box = new Box(BoxLayoutY_AXIS) cpadd(box) boxadd(new JLabel(Tick the sports you like)) String[] sports = Football Rugby CricketBadminton JCheckBox[] cba = new JCheckBox[sportslength] for (int i = 0 i lt sportslength i++) cba[i] = new JCheckBox(sports[i]) boxadd(cba[i]) framepack()copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JRadioButton
Constructor Summary
JRadioButton() Creates an initially unselected radio button with no set text
JRadioButton(Action a) Creates a radiobutton where properties are taken from the Action supplied
JRadioButton(Icon icon) Creates an initially unselected radio button with the specified image but no text
JRadioButton(Icon icon boolean selected) Creates a radio button with the specified image and selection state but no text
JRadioButton(String text) Creates an unselected radio button with the specified text
JRadioButton(String text boolean selected) Creates a radio button with the specified text and selection state
JRadioButton(String text Icon icon) Creates a radio button that has the specified text and image and that is initially unselected
JRadioButton(String text Icon icon boolean selected) Creates a radio button that has the specified text image and selection state
Demoimport javaawtimport javaawteventimport javaxswingclass JRadioButtonDemo public static void main(String[] args) ActionListener listener = new ActionListener() public void actionPerformed (ActionEvent e) Systemoutprintln(egetActionCommand()) Box p = new Box(BoxLayoutY_AXIS) ButtonGroup group = new ButtonGroup() String[] sa = uglikiwipassionkumquat for(int i=0iltsalength++i) JRadioButton b = new JRadioButton(sa[i]) groupadd(b) padd(b) baddActionListener(listener) JFrame frame = new JFrame(Fruits)framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
framegetContentPane()add(p)framepack() framesetVisible(true)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JTextField
Constructor Summary
JTextField() Constructs a new TextField
JTextField(Document doc String text int columns) Constructs a new JTextField that uses the given text storage model and the given number of columns
JTextField(int columns) Constructs a new empty TextField with the specified number of columns
JTextField(String text) Constructs a new TextField initialized with the specified text
JTextField(String text int columns) Constructs a new TextField initialized with the specified text and columns
Demo
import javaawtimport javaawteventimport javaxswing
public class JLabelDemo extends JFrame
public JLabelDemo() JTextField tf=new JTextField() JFrame f = new JFrame(TextFieldDemo) Container c= getContentPane() csetLayout(new GridLayout(02)) JLabel lbl1=new JLabel(TextFieldSwingConstantsLEFT) cadd(lbl1) cadd(tf) fadd(c)
fsetSize(300100) fsetVisible(true) fsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
public static void main(String[] args) new JLabelDemo()
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JMenu
Constructor Summary
JMenu() Constructs a new JMenu with no text
JMenu(Action a) Constructs a menu whose properties are taken from the Action supplied
JMenu(String s) Constructs a new JMenu with the supplied string as its text
JMenu(String s boolean b) Constructs a new JMenu with the supplied string as its text and specified as a tear-off menu or not
Demoimport javaawtimport javaxswingclass JMenuDemo public static void main(String[] args) JMenu menu1 = new JMenu(File) menu1add(new JMenuItem(New)) menu1add(new JMenuItem(Open)) menu1add(new JSeparator()) menu1add(new JMenuItem(Save)) menu1add(new JMenuItem(Save as)) menu1add(new JSeparator()) menu1add(new JMenuItem(Exit)) JMenu menu2 = new JMenu(Edit) menu2add(new JMenuItem(Copy)) menu2add(new JSeparator()) menu2add(new JMenuItem(Paste)) menu2add(new JSeparator()) menu2add(new JMenuItem(Select ALL)) JMenuBar bar = new JMenuBar() baradd(menu1) baradd(menu2) JFrame frame = new JFrame() framesetJMenuBar(bar) framesetSize(350300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JColorChooser
Constructor Summary
JColorChooser() Creates a color chooser pane with an initial color of white
JColorChooser(Color initialColor) Creates a color chooser pane with the specified initial color
JColorChooser(ColorSelectionModel model) Creates a color chooser pane with the specified ColorSelectionModel
Demoimport javaawtimport javaawteventimport javaxswingpublic class ColorCh static Container cp public static void main(String[] args) final JFrame frame = new JFrame(Color Choice) cp = framegetContentPane( ) cpsetLayout(new GridBagLayout()) JButton button = new JButton(select color) cpadd(button) buttonaddActionListener(new ActionListener( ) public void actionPerformed(ActionEvent e) Color c = JColorChoosershowDialog(frameChoose a color Colorred) cpsetBackground(c) ) framesetSize(200 300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Select Any Color and Press OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JOptionPane
Constructor Summary
JOptionPane() Creates a JOptionPane with a test message
JOptionPane(Object message) Creates a instance of JOptionPane to display a message using the plain-message message type and the default options delivered by the UI
JOptionPane(Object message int messageType) Creates an instance of JOptionPane to display a message with the specified message type and the default options
JOptionPane(Object message int messageType int optionType) Creates an instance of JOptionPane to display a message with the specified message type and options
JOptionPane(Object message int messageType int optionType Icon icon) Creates an instance of JOptionPane to display a message with the specified message type options and icon
JOptionPane(Object message int messageType int optionType Icon icon Object[] options) Creates an instance of JOptionPane to display a message with the specified message type icon and options
JOptionPane(Object message int messageType int optionType Icon icon Object[] options Object initialValue) Creates an instance of JOptionPane to display a message with the specified message type icon and options with the initially-selected option specified
Demo
import javaxswingJOptionPanepublic class JOptionPaneDemo public static void main(String[] args) String[] choices = BJP NCP LEFT int doAgain do int response = JOptionPaneshowOptionDialog ( null center over parent
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
How did you vote message Party Poll title in titlebar JOptionPaneYES_NO_OPTION Option type JOptionPanePLAIN_MESSAGE messageType null icon choices Options None of your business initial value ) JOptionPaneshowMessageDialog(null Response = + response) doAgain = JOptionPaneshowConfirmDialog(null Again) while(doAgain == JOptionPaneYES_OPTION) Systemexit(0)
Output
Press Any One
After Pressing OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JProgressBar
Constructor Summary
JProgressBar() Creates a horizontal progress bar that displays a border but no progress string
JProgressBar(BoundedRangeModel newModel) Creates a horizontal progress bar that uses the specified model to hold the progress bars data
JProgressBar(int orient) Creates a progress bar with the specified orientation which can be either SwingConstantsVERTICAL or SwingConstantsHORIZONTAL
JProgressBar(int min int max) Creates a horizontal progress bar with the specified minimum and maximum
JProgressBar(int orient int min int max) Creates a progress bar using the specified orientation minimum and maximum
Demo
import javaawtimport javaawteventimport javaxswingimport javaxswingtexthtmlpublic class SwingProgressBarExample final static int interval = 1000 int i JLabel label JProgressBar pb Timer timer JButton button public SwingProgressBarExample() JFrame frame = new JFrame(Swing Progress Bar) button = new JButton(Start) buttonaddActionListener(new ButtonListener()) pb = new JProgressBar(0 20) pbsetValue(0) pbsetStringPainted(true) label = new JLabel(By Prasad Sawant) JPanel panel = new JPanel() paneladd(button) paneladd(pb)copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JPanel panel1 = new JPanel() panel1setLayout(new BorderLayout()) panel1add(panel BorderLayoutNORTH) panel1add(label BorderLayoutCENTER) panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20)) framesetContentPane(panel1) framepack() framesetVisible(true) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Create a timer timer = new Timer(interval new ActionListener() public void actionPerformed(ActionEvent evt) if (i == 20) ToolkitgetDefaultToolkit()beep() timerstop() buttonsetEnabled(true) pbsetValue(0) String str = lthtmlgt + ltfont color=FF0000gt + ltbgt + Downloading completed + ltbgt + ltfontgt + lthtmlgt labelsetText(str) i = i + 1 pbsetValue(i) ) class ButtonListener implements ActionListener public void actionPerformed(ActionEvent ae) buttonsetEnabled(false) i = 0 String str = lthtmlgt + ltfont color=008000gt + ltbgt +Downloading is in process + ltbgt + ltfontgt + lthtmlgt labelsetText(str) timerstart() public static void main(String[] args) SwingProgressBarExample spb = new SwingProgressBarExample()
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
After Pressing Start Button
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JSpinner
A single line input field that lets the user select a number or an object value from an ordered sequence Spinners typically provide a pair of tiny arrow buttons for stepping through the elements of the sequence The keyboard updown arrow keys also cycle through the elements The user may also be allowed to type a (legal) value directly into the spinner Although combo boxes provide similar functionality spinners are sometimes preferred because they dont require a drop down list that can obscure important data
A JSpinners sequence value is defined by its SpinnerModel The model can be specified as a constructor argument and changed with the model property SpinnerModel classes for some common types are provided SpinnerListModel SpinnerNumberModel and SpinnerDateModel
A JSpinner has a single child component thats responsible for displaying and potentially changing the current element or value of the model which is called the editor The editor is created by the JSpinners constructor and can be changed with the editor property The JSpinners editor stays in sync with the model by listening for ChangeEvents If the user has changed the value displayed by the editor it is possible for the models value to differ from that of the editor To make sure the model has the same value as the editor use the commitEdit method
Demo
import javaawtimport javaxswingimport javaxswingeventclass JSpinnerExample public static void main(String[] args) String[] sizes = smallmediumlargeXLXXL final SpinnerListModel model = new SpinnerListModel(sizes) JSpinner jSpinner = new JSpinner(model) modeladdChangeListener(new ChangeListener() public void stateChanged(ChangeEvent e) Systemoutprintln(modelgetValue()) ) JFrame frame = new JFrame() framesetSize(100100) framegetContentPane()add(jSpinner) framepack() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Press UP KEY
Press UP KEY
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
- Relationship to AWT
-
- JProgressBar
- Demo
- import javaawt
- import javaawtevent
- import javaxswing
- import javaxswingtexthtml
- public class SwingProgressBarExample
- final static int interval = 1000
- int i
- JLabel label
- JProgressBar pb
- Timer timer
- JButton button
- public SwingProgressBarExample()
- JFrame frame = new JFrame(Swing Progress Bar)
- button = new JButton(Start)
- buttonaddActionListener(new ButtonListener())
- pb = new JProgressBar(0 20)
- pbsetValue(0)
- pbsetStringPainted(true)
- label = new JLabel(By Prasad Sawant)
- JPanel panel = new JPanel()
- paneladd(button)
- paneladd(pb)
- JPanel panel1 = new JPanel()
- panel1setLayout(new BorderLayout())
- panel1add(panel BorderLayoutNORTH)
- panel1add(label BorderLayoutCENTER)
- panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20))
- framesetContentPane(panel1)
- framepack()
- framesetVisible(true)
- framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
- Create a timer
- timer = new Timer(interval new ActionListener()
- public void actionPerformed(ActionEvent evt)
- if (i == 20)
- ToolkitgetDefaultToolkit()beep()
- timerstop()
- buttonsetEnabled(true)
- pbsetValue(0)
- String str = lthtmlgt + ltfont color=FF0000gt + ltbgt +
- Downloading completed + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- i = i + 1
- pbsetValue(i)
- )
- class ButtonListener implements ActionListener
- public void actionPerformed(ActionEvent ae)
- buttonsetEnabled(false)
- i = 0
- String str = lthtmlgt + ltfont color=008000gt + ltbgt +
- Downloading is in process + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- timerstart()
- public static void main(String[] args)
- SwingProgressBarExample spb = new SwingProgressBarExample()
- Press UP KEY
- Press UP KEY
-
Swing
JApplet
Constructor
JApplet() Creates a swing applet instance
Demoimport javaawtimport javaxswingltapplet code=JAppletDemoclass width=300 height=200gtltappletgtpublic class JAppletDemo extends JApplet public void init() Container content = getContentPane() contentsetBackground(Colorwhite) contentsetLayout(new FlowLayout()) contentadd(new JButton(Button 1)) contentadd(new JButton(Button 2)) contentadd(new JButton(Button 3))
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JFrame
Constructor Summary
JFrame() Constructs a new frame that is initially invisible
JFrame(GraphicsConfiguration gc) reates a Frame in the specified GraphicsConfiguration of a screen device and a blank title
JFrame(String title) Creates a new initially invisible Frame with the specified title
JFrame(String title GraphicsConfiguration gc) Creates a JFrame with the specified title and the specified GraphicsConfiguration of a screen device
Demo
import javaawtimport javaxswingpublic class JFrameDemo public static void main(String[] args) JFrame f = new JFrame(JFrame Demo) fsetSize(400 150) Container content = fgetContentPane() contentsetBackground(Colorwhite) contentsetLayout(new FlowLayout()) contentadd(new JButton(Button 1)) contentadd(new JButton(Button 2)) contentadd(new JButton(Button 3)) fsetVisible(true) fsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JLabel
Constructor Summary
JLabel() Creates a JLabel instance with no image and with an empty string for the title
JLabel(Icon image) Creates a JLabel instance with the specified image
JLabel(Icon image int horizontalAlignment) Creates a JLabel instance with the specified image and horizontal alignment
JLabel(String text) Creates a JLabel instance with the specified text
JLabel(String text Icon icon int horizontalAlignment) Creates a JLabel instance with the specified text image and horizontal alignment
JLabel(String text int horizontalAlignment) Creates a JLabel instance with the specified text and horizontal alignment
Demo
import javaawtimport javaxswingpublic class JLabelDemo extends JFrame public static void main(String[] args) new JLabelDemo() public JLabelDemo() JFrame f=new JFrame() JLabel coloredLabel = new JLabel(HelloLabelCENTER) Container c=getContentPane() csetLayout(new GridLayout(01)) cadd(coloredLabel) pack() fadd(c) fsetVisible(true) fsetSize(150150)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JButton
Constructor Summary
JButton() Creates a button with no set text or icon
JButton(Action a) Creates a button where properties are taken from the Action supplied
JButton(Icon icon) Creates a button with an icon
JButton(String text) Creates a button with text
JButton(String text Icon icon) Creates a button with initial text and an icon
Demoimport javaawtimport javaawteventimport javaxswingpublic class JButtonDemo extends JFrame implements ActionListener private JButton btn_simple btn_image public JButtonDemo() setTitle(Testing Buttons) Container container = getContentPane() containersetLayout(new GridLayout(02)) btn_simple = new JButton(Simple Button) btn_simplesetMnemonic(KeyEventVK_S) containeradd(btn_simple) Icon bug1 = new ImageIcon(Dmenu_1gif) btn_image = new JButton(Imagebug1) btn_imagesetMnemonic(KeyEventVK_I) containeradd(btn_image) btn_imageaddActionListener(this) btn_simpleaddActionListener(this) setSize(300100) setVisible(true) public static void main(String args[]) new JButtonDemo() public void actionPerformed(ActionEvent ae) JOptionPaneshowMessageDialog(nullYou pressed + aegetActionCommand())
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JCheckBox
Constructor Summary
JCheckBox() Creates an initially unselected check box button with no text no icon
JCheckBox(Action a) Creates a check box where properties are taken from the Action supplied
JCheckBox(Icon icon) Creates an initially unselected check box with an icon
JCheckBox(Icon icon boolean selected) Creates a check box with an icon and specifies whether or not it is initially selected
JCheckBox(String text) Creates an initially unselected check box with text
JCheckBox(String text boolean selected) Creates a check box with text and specifies whether or not it is initially selected
JCheckBox(String text Icon icon) Creates an initially unselected check box with the specified text and icon
JCheckBox(String text Icon icon boolean selected) Creates a check box with text and icon and specifies whether or not it is initially selected
Demoimport javaawtContainerimport javaawteventimport javaxswingclass JCheckboxDemo public static void main(String[] args) JFrame frame = new JFrame() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Container cp = framegetContentPane() Box box = new Box(BoxLayoutY_AXIS) cpadd(box) boxadd(new JLabel(Tick the sports you like)) String[] sports = Football Rugby CricketBadminton JCheckBox[] cba = new JCheckBox[sportslength] for (int i = 0 i lt sportslength i++) cba[i] = new JCheckBox(sports[i]) boxadd(cba[i]) framepack()copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JRadioButton
Constructor Summary
JRadioButton() Creates an initially unselected radio button with no set text
JRadioButton(Action a) Creates a radiobutton where properties are taken from the Action supplied
JRadioButton(Icon icon) Creates an initially unselected radio button with the specified image but no text
JRadioButton(Icon icon boolean selected) Creates a radio button with the specified image and selection state but no text
JRadioButton(String text) Creates an unselected radio button with the specified text
JRadioButton(String text boolean selected) Creates a radio button with the specified text and selection state
JRadioButton(String text Icon icon) Creates a radio button that has the specified text and image and that is initially unselected
JRadioButton(String text Icon icon boolean selected) Creates a radio button that has the specified text image and selection state
Demoimport javaawtimport javaawteventimport javaxswingclass JRadioButtonDemo public static void main(String[] args) ActionListener listener = new ActionListener() public void actionPerformed (ActionEvent e) Systemoutprintln(egetActionCommand()) Box p = new Box(BoxLayoutY_AXIS) ButtonGroup group = new ButtonGroup() String[] sa = uglikiwipassionkumquat for(int i=0iltsalength++i) JRadioButton b = new JRadioButton(sa[i]) groupadd(b) padd(b) baddActionListener(listener) JFrame frame = new JFrame(Fruits)framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
framegetContentPane()add(p)framepack() framesetVisible(true)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JTextField
Constructor Summary
JTextField() Constructs a new TextField
JTextField(Document doc String text int columns) Constructs a new JTextField that uses the given text storage model and the given number of columns
JTextField(int columns) Constructs a new empty TextField with the specified number of columns
JTextField(String text) Constructs a new TextField initialized with the specified text
JTextField(String text int columns) Constructs a new TextField initialized with the specified text and columns
Demo
import javaawtimport javaawteventimport javaxswing
public class JLabelDemo extends JFrame
public JLabelDemo() JTextField tf=new JTextField() JFrame f = new JFrame(TextFieldDemo) Container c= getContentPane() csetLayout(new GridLayout(02)) JLabel lbl1=new JLabel(TextFieldSwingConstantsLEFT) cadd(lbl1) cadd(tf) fadd(c)
fsetSize(300100) fsetVisible(true) fsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
public static void main(String[] args) new JLabelDemo()
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JMenu
Constructor Summary
JMenu() Constructs a new JMenu with no text
JMenu(Action a) Constructs a menu whose properties are taken from the Action supplied
JMenu(String s) Constructs a new JMenu with the supplied string as its text
JMenu(String s boolean b) Constructs a new JMenu with the supplied string as its text and specified as a tear-off menu or not
Demoimport javaawtimport javaxswingclass JMenuDemo public static void main(String[] args) JMenu menu1 = new JMenu(File) menu1add(new JMenuItem(New)) menu1add(new JMenuItem(Open)) menu1add(new JSeparator()) menu1add(new JMenuItem(Save)) menu1add(new JMenuItem(Save as)) menu1add(new JSeparator()) menu1add(new JMenuItem(Exit)) JMenu menu2 = new JMenu(Edit) menu2add(new JMenuItem(Copy)) menu2add(new JSeparator()) menu2add(new JMenuItem(Paste)) menu2add(new JSeparator()) menu2add(new JMenuItem(Select ALL)) JMenuBar bar = new JMenuBar() baradd(menu1) baradd(menu2) JFrame frame = new JFrame() framesetJMenuBar(bar) framesetSize(350300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JColorChooser
Constructor Summary
JColorChooser() Creates a color chooser pane with an initial color of white
JColorChooser(Color initialColor) Creates a color chooser pane with the specified initial color
JColorChooser(ColorSelectionModel model) Creates a color chooser pane with the specified ColorSelectionModel
Demoimport javaawtimport javaawteventimport javaxswingpublic class ColorCh static Container cp public static void main(String[] args) final JFrame frame = new JFrame(Color Choice) cp = framegetContentPane( ) cpsetLayout(new GridBagLayout()) JButton button = new JButton(select color) cpadd(button) buttonaddActionListener(new ActionListener( ) public void actionPerformed(ActionEvent e) Color c = JColorChoosershowDialog(frameChoose a color Colorred) cpsetBackground(c) ) framesetSize(200 300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Select Any Color and Press OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JOptionPane
Constructor Summary
JOptionPane() Creates a JOptionPane with a test message
JOptionPane(Object message) Creates a instance of JOptionPane to display a message using the plain-message message type and the default options delivered by the UI
JOptionPane(Object message int messageType) Creates an instance of JOptionPane to display a message with the specified message type and the default options
JOptionPane(Object message int messageType int optionType) Creates an instance of JOptionPane to display a message with the specified message type and options
JOptionPane(Object message int messageType int optionType Icon icon) Creates an instance of JOptionPane to display a message with the specified message type options and icon
JOptionPane(Object message int messageType int optionType Icon icon Object[] options) Creates an instance of JOptionPane to display a message with the specified message type icon and options
JOptionPane(Object message int messageType int optionType Icon icon Object[] options Object initialValue) Creates an instance of JOptionPane to display a message with the specified message type icon and options with the initially-selected option specified
Demo
import javaxswingJOptionPanepublic class JOptionPaneDemo public static void main(String[] args) String[] choices = BJP NCP LEFT int doAgain do int response = JOptionPaneshowOptionDialog ( null center over parent
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
How did you vote message Party Poll title in titlebar JOptionPaneYES_NO_OPTION Option type JOptionPanePLAIN_MESSAGE messageType null icon choices Options None of your business initial value ) JOptionPaneshowMessageDialog(null Response = + response) doAgain = JOptionPaneshowConfirmDialog(null Again) while(doAgain == JOptionPaneYES_OPTION) Systemexit(0)
Output
Press Any One
After Pressing OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JProgressBar
Constructor Summary
JProgressBar() Creates a horizontal progress bar that displays a border but no progress string
JProgressBar(BoundedRangeModel newModel) Creates a horizontal progress bar that uses the specified model to hold the progress bars data
JProgressBar(int orient) Creates a progress bar with the specified orientation which can be either SwingConstantsVERTICAL or SwingConstantsHORIZONTAL
JProgressBar(int min int max) Creates a horizontal progress bar with the specified minimum and maximum
JProgressBar(int orient int min int max) Creates a progress bar using the specified orientation minimum and maximum
Demo
import javaawtimport javaawteventimport javaxswingimport javaxswingtexthtmlpublic class SwingProgressBarExample final static int interval = 1000 int i JLabel label JProgressBar pb Timer timer JButton button public SwingProgressBarExample() JFrame frame = new JFrame(Swing Progress Bar) button = new JButton(Start) buttonaddActionListener(new ButtonListener()) pb = new JProgressBar(0 20) pbsetValue(0) pbsetStringPainted(true) label = new JLabel(By Prasad Sawant) JPanel panel = new JPanel() paneladd(button) paneladd(pb)copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JPanel panel1 = new JPanel() panel1setLayout(new BorderLayout()) panel1add(panel BorderLayoutNORTH) panel1add(label BorderLayoutCENTER) panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20)) framesetContentPane(panel1) framepack() framesetVisible(true) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Create a timer timer = new Timer(interval new ActionListener() public void actionPerformed(ActionEvent evt) if (i == 20) ToolkitgetDefaultToolkit()beep() timerstop() buttonsetEnabled(true) pbsetValue(0) String str = lthtmlgt + ltfont color=FF0000gt + ltbgt + Downloading completed + ltbgt + ltfontgt + lthtmlgt labelsetText(str) i = i + 1 pbsetValue(i) ) class ButtonListener implements ActionListener public void actionPerformed(ActionEvent ae) buttonsetEnabled(false) i = 0 String str = lthtmlgt + ltfont color=008000gt + ltbgt +Downloading is in process + ltbgt + ltfontgt + lthtmlgt labelsetText(str) timerstart() public static void main(String[] args) SwingProgressBarExample spb = new SwingProgressBarExample()
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
After Pressing Start Button
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JSpinner
A single line input field that lets the user select a number or an object value from an ordered sequence Spinners typically provide a pair of tiny arrow buttons for stepping through the elements of the sequence The keyboard updown arrow keys also cycle through the elements The user may also be allowed to type a (legal) value directly into the spinner Although combo boxes provide similar functionality spinners are sometimes preferred because they dont require a drop down list that can obscure important data
A JSpinners sequence value is defined by its SpinnerModel The model can be specified as a constructor argument and changed with the model property SpinnerModel classes for some common types are provided SpinnerListModel SpinnerNumberModel and SpinnerDateModel
A JSpinner has a single child component thats responsible for displaying and potentially changing the current element or value of the model which is called the editor The editor is created by the JSpinners constructor and can be changed with the editor property The JSpinners editor stays in sync with the model by listening for ChangeEvents If the user has changed the value displayed by the editor it is possible for the models value to differ from that of the editor To make sure the model has the same value as the editor use the commitEdit method
Demo
import javaawtimport javaxswingimport javaxswingeventclass JSpinnerExample public static void main(String[] args) String[] sizes = smallmediumlargeXLXXL final SpinnerListModel model = new SpinnerListModel(sizes) JSpinner jSpinner = new JSpinner(model) modeladdChangeListener(new ChangeListener() public void stateChanged(ChangeEvent e) Systemoutprintln(modelgetValue()) ) JFrame frame = new JFrame() framesetSize(100100) framegetContentPane()add(jSpinner) framepack() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Press UP KEY
Press UP KEY
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
- Relationship to AWT
-
- JProgressBar
- Demo
- import javaawt
- import javaawtevent
- import javaxswing
- import javaxswingtexthtml
- public class SwingProgressBarExample
- final static int interval = 1000
- int i
- JLabel label
- JProgressBar pb
- Timer timer
- JButton button
- public SwingProgressBarExample()
- JFrame frame = new JFrame(Swing Progress Bar)
- button = new JButton(Start)
- buttonaddActionListener(new ButtonListener())
- pb = new JProgressBar(0 20)
- pbsetValue(0)
- pbsetStringPainted(true)
- label = new JLabel(By Prasad Sawant)
- JPanel panel = new JPanel()
- paneladd(button)
- paneladd(pb)
- JPanel panel1 = new JPanel()
- panel1setLayout(new BorderLayout())
- panel1add(panel BorderLayoutNORTH)
- panel1add(label BorderLayoutCENTER)
- panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20))
- framesetContentPane(panel1)
- framepack()
- framesetVisible(true)
- framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
- Create a timer
- timer = new Timer(interval new ActionListener()
- public void actionPerformed(ActionEvent evt)
- if (i == 20)
- ToolkitgetDefaultToolkit()beep()
- timerstop()
- buttonsetEnabled(true)
- pbsetValue(0)
- String str = lthtmlgt + ltfont color=FF0000gt + ltbgt +
- Downloading completed + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- i = i + 1
- pbsetValue(i)
- )
- class ButtonListener implements ActionListener
- public void actionPerformed(ActionEvent ae)
- buttonsetEnabled(false)
- i = 0
- String str = lthtmlgt + ltfont color=008000gt + ltbgt +
- Downloading is in process + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- timerstart()
- public static void main(String[] args)
- SwingProgressBarExample spb = new SwingProgressBarExample()
- Press UP KEY
- Press UP KEY
-
Swing
JFrame
Constructor Summary
JFrame() Constructs a new frame that is initially invisible
JFrame(GraphicsConfiguration gc) reates a Frame in the specified GraphicsConfiguration of a screen device and a blank title
JFrame(String title) Creates a new initially invisible Frame with the specified title
JFrame(String title GraphicsConfiguration gc) Creates a JFrame with the specified title and the specified GraphicsConfiguration of a screen device
Demo
import javaawtimport javaxswingpublic class JFrameDemo public static void main(String[] args) JFrame f = new JFrame(JFrame Demo) fsetSize(400 150) Container content = fgetContentPane() contentsetBackground(Colorwhite) contentsetLayout(new FlowLayout()) contentadd(new JButton(Button 1)) contentadd(new JButton(Button 2)) contentadd(new JButton(Button 3)) fsetVisible(true) fsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JLabel
Constructor Summary
JLabel() Creates a JLabel instance with no image and with an empty string for the title
JLabel(Icon image) Creates a JLabel instance with the specified image
JLabel(Icon image int horizontalAlignment) Creates a JLabel instance with the specified image and horizontal alignment
JLabel(String text) Creates a JLabel instance with the specified text
JLabel(String text Icon icon int horizontalAlignment) Creates a JLabel instance with the specified text image and horizontal alignment
JLabel(String text int horizontalAlignment) Creates a JLabel instance with the specified text and horizontal alignment
Demo
import javaawtimport javaxswingpublic class JLabelDemo extends JFrame public static void main(String[] args) new JLabelDemo() public JLabelDemo() JFrame f=new JFrame() JLabel coloredLabel = new JLabel(HelloLabelCENTER) Container c=getContentPane() csetLayout(new GridLayout(01)) cadd(coloredLabel) pack() fadd(c) fsetVisible(true) fsetSize(150150)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JButton
Constructor Summary
JButton() Creates a button with no set text or icon
JButton(Action a) Creates a button where properties are taken from the Action supplied
JButton(Icon icon) Creates a button with an icon
JButton(String text) Creates a button with text
JButton(String text Icon icon) Creates a button with initial text and an icon
Demoimport javaawtimport javaawteventimport javaxswingpublic class JButtonDemo extends JFrame implements ActionListener private JButton btn_simple btn_image public JButtonDemo() setTitle(Testing Buttons) Container container = getContentPane() containersetLayout(new GridLayout(02)) btn_simple = new JButton(Simple Button) btn_simplesetMnemonic(KeyEventVK_S) containeradd(btn_simple) Icon bug1 = new ImageIcon(Dmenu_1gif) btn_image = new JButton(Imagebug1) btn_imagesetMnemonic(KeyEventVK_I) containeradd(btn_image) btn_imageaddActionListener(this) btn_simpleaddActionListener(this) setSize(300100) setVisible(true) public static void main(String args[]) new JButtonDemo() public void actionPerformed(ActionEvent ae) JOptionPaneshowMessageDialog(nullYou pressed + aegetActionCommand())
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JCheckBox
Constructor Summary
JCheckBox() Creates an initially unselected check box button with no text no icon
JCheckBox(Action a) Creates a check box where properties are taken from the Action supplied
JCheckBox(Icon icon) Creates an initially unselected check box with an icon
JCheckBox(Icon icon boolean selected) Creates a check box with an icon and specifies whether or not it is initially selected
JCheckBox(String text) Creates an initially unselected check box with text
JCheckBox(String text boolean selected) Creates a check box with text and specifies whether or not it is initially selected
JCheckBox(String text Icon icon) Creates an initially unselected check box with the specified text and icon
JCheckBox(String text Icon icon boolean selected) Creates a check box with text and icon and specifies whether or not it is initially selected
Demoimport javaawtContainerimport javaawteventimport javaxswingclass JCheckboxDemo public static void main(String[] args) JFrame frame = new JFrame() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Container cp = framegetContentPane() Box box = new Box(BoxLayoutY_AXIS) cpadd(box) boxadd(new JLabel(Tick the sports you like)) String[] sports = Football Rugby CricketBadminton JCheckBox[] cba = new JCheckBox[sportslength] for (int i = 0 i lt sportslength i++) cba[i] = new JCheckBox(sports[i]) boxadd(cba[i]) framepack()copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JRadioButton
Constructor Summary
JRadioButton() Creates an initially unselected radio button with no set text
JRadioButton(Action a) Creates a radiobutton where properties are taken from the Action supplied
JRadioButton(Icon icon) Creates an initially unselected radio button with the specified image but no text
JRadioButton(Icon icon boolean selected) Creates a radio button with the specified image and selection state but no text
JRadioButton(String text) Creates an unselected radio button with the specified text
JRadioButton(String text boolean selected) Creates a radio button with the specified text and selection state
JRadioButton(String text Icon icon) Creates a radio button that has the specified text and image and that is initially unselected
JRadioButton(String text Icon icon boolean selected) Creates a radio button that has the specified text image and selection state
Demoimport javaawtimport javaawteventimport javaxswingclass JRadioButtonDemo public static void main(String[] args) ActionListener listener = new ActionListener() public void actionPerformed (ActionEvent e) Systemoutprintln(egetActionCommand()) Box p = new Box(BoxLayoutY_AXIS) ButtonGroup group = new ButtonGroup() String[] sa = uglikiwipassionkumquat for(int i=0iltsalength++i) JRadioButton b = new JRadioButton(sa[i]) groupadd(b) padd(b) baddActionListener(listener) JFrame frame = new JFrame(Fruits)framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
framegetContentPane()add(p)framepack() framesetVisible(true)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JTextField
Constructor Summary
JTextField() Constructs a new TextField
JTextField(Document doc String text int columns) Constructs a new JTextField that uses the given text storage model and the given number of columns
JTextField(int columns) Constructs a new empty TextField with the specified number of columns
JTextField(String text) Constructs a new TextField initialized with the specified text
JTextField(String text int columns) Constructs a new TextField initialized with the specified text and columns
Demo
import javaawtimport javaawteventimport javaxswing
public class JLabelDemo extends JFrame
public JLabelDemo() JTextField tf=new JTextField() JFrame f = new JFrame(TextFieldDemo) Container c= getContentPane() csetLayout(new GridLayout(02)) JLabel lbl1=new JLabel(TextFieldSwingConstantsLEFT) cadd(lbl1) cadd(tf) fadd(c)
fsetSize(300100) fsetVisible(true) fsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
public static void main(String[] args) new JLabelDemo()
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JMenu
Constructor Summary
JMenu() Constructs a new JMenu with no text
JMenu(Action a) Constructs a menu whose properties are taken from the Action supplied
JMenu(String s) Constructs a new JMenu with the supplied string as its text
JMenu(String s boolean b) Constructs a new JMenu with the supplied string as its text and specified as a tear-off menu or not
Demoimport javaawtimport javaxswingclass JMenuDemo public static void main(String[] args) JMenu menu1 = new JMenu(File) menu1add(new JMenuItem(New)) menu1add(new JMenuItem(Open)) menu1add(new JSeparator()) menu1add(new JMenuItem(Save)) menu1add(new JMenuItem(Save as)) menu1add(new JSeparator()) menu1add(new JMenuItem(Exit)) JMenu menu2 = new JMenu(Edit) menu2add(new JMenuItem(Copy)) menu2add(new JSeparator()) menu2add(new JMenuItem(Paste)) menu2add(new JSeparator()) menu2add(new JMenuItem(Select ALL)) JMenuBar bar = new JMenuBar() baradd(menu1) baradd(menu2) JFrame frame = new JFrame() framesetJMenuBar(bar) framesetSize(350300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JColorChooser
Constructor Summary
JColorChooser() Creates a color chooser pane with an initial color of white
JColorChooser(Color initialColor) Creates a color chooser pane with the specified initial color
JColorChooser(ColorSelectionModel model) Creates a color chooser pane with the specified ColorSelectionModel
Demoimport javaawtimport javaawteventimport javaxswingpublic class ColorCh static Container cp public static void main(String[] args) final JFrame frame = new JFrame(Color Choice) cp = framegetContentPane( ) cpsetLayout(new GridBagLayout()) JButton button = new JButton(select color) cpadd(button) buttonaddActionListener(new ActionListener( ) public void actionPerformed(ActionEvent e) Color c = JColorChoosershowDialog(frameChoose a color Colorred) cpsetBackground(c) ) framesetSize(200 300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Select Any Color and Press OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JOptionPane
Constructor Summary
JOptionPane() Creates a JOptionPane with a test message
JOptionPane(Object message) Creates a instance of JOptionPane to display a message using the plain-message message type and the default options delivered by the UI
JOptionPane(Object message int messageType) Creates an instance of JOptionPane to display a message with the specified message type and the default options
JOptionPane(Object message int messageType int optionType) Creates an instance of JOptionPane to display a message with the specified message type and options
JOptionPane(Object message int messageType int optionType Icon icon) Creates an instance of JOptionPane to display a message with the specified message type options and icon
JOptionPane(Object message int messageType int optionType Icon icon Object[] options) Creates an instance of JOptionPane to display a message with the specified message type icon and options
JOptionPane(Object message int messageType int optionType Icon icon Object[] options Object initialValue) Creates an instance of JOptionPane to display a message with the specified message type icon and options with the initially-selected option specified
Demo
import javaxswingJOptionPanepublic class JOptionPaneDemo public static void main(String[] args) String[] choices = BJP NCP LEFT int doAgain do int response = JOptionPaneshowOptionDialog ( null center over parent
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
How did you vote message Party Poll title in titlebar JOptionPaneYES_NO_OPTION Option type JOptionPanePLAIN_MESSAGE messageType null icon choices Options None of your business initial value ) JOptionPaneshowMessageDialog(null Response = + response) doAgain = JOptionPaneshowConfirmDialog(null Again) while(doAgain == JOptionPaneYES_OPTION) Systemexit(0)
Output
Press Any One
After Pressing OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JProgressBar
Constructor Summary
JProgressBar() Creates a horizontal progress bar that displays a border but no progress string
JProgressBar(BoundedRangeModel newModel) Creates a horizontal progress bar that uses the specified model to hold the progress bars data
JProgressBar(int orient) Creates a progress bar with the specified orientation which can be either SwingConstantsVERTICAL or SwingConstantsHORIZONTAL
JProgressBar(int min int max) Creates a horizontal progress bar with the specified minimum and maximum
JProgressBar(int orient int min int max) Creates a progress bar using the specified orientation minimum and maximum
Demo
import javaawtimport javaawteventimport javaxswingimport javaxswingtexthtmlpublic class SwingProgressBarExample final static int interval = 1000 int i JLabel label JProgressBar pb Timer timer JButton button public SwingProgressBarExample() JFrame frame = new JFrame(Swing Progress Bar) button = new JButton(Start) buttonaddActionListener(new ButtonListener()) pb = new JProgressBar(0 20) pbsetValue(0) pbsetStringPainted(true) label = new JLabel(By Prasad Sawant) JPanel panel = new JPanel() paneladd(button) paneladd(pb)copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JPanel panel1 = new JPanel() panel1setLayout(new BorderLayout()) panel1add(panel BorderLayoutNORTH) panel1add(label BorderLayoutCENTER) panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20)) framesetContentPane(panel1) framepack() framesetVisible(true) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Create a timer timer = new Timer(interval new ActionListener() public void actionPerformed(ActionEvent evt) if (i == 20) ToolkitgetDefaultToolkit()beep() timerstop() buttonsetEnabled(true) pbsetValue(0) String str = lthtmlgt + ltfont color=FF0000gt + ltbgt + Downloading completed + ltbgt + ltfontgt + lthtmlgt labelsetText(str) i = i + 1 pbsetValue(i) ) class ButtonListener implements ActionListener public void actionPerformed(ActionEvent ae) buttonsetEnabled(false) i = 0 String str = lthtmlgt + ltfont color=008000gt + ltbgt +Downloading is in process + ltbgt + ltfontgt + lthtmlgt labelsetText(str) timerstart() public static void main(String[] args) SwingProgressBarExample spb = new SwingProgressBarExample()
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
After Pressing Start Button
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JSpinner
A single line input field that lets the user select a number or an object value from an ordered sequence Spinners typically provide a pair of tiny arrow buttons for stepping through the elements of the sequence The keyboard updown arrow keys also cycle through the elements The user may also be allowed to type a (legal) value directly into the spinner Although combo boxes provide similar functionality spinners are sometimes preferred because they dont require a drop down list that can obscure important data
A JSpinners sequence value is defined by its SpinnerModel The model can be specified as a constructor argument and changed with the model property SpinnerModel classes for some common types are provided SpinnerListModel SpinnerNumberModel and SpinnerDateModel
A JSpinner has a single child component thats responsible for displaying and potentially changing the current element or value of the model which is called the editor The editor is created by the JSpinners constructor and can be changed with the editor property The JSpinners editor stays in sync with the model by listening for ChangeEvents If the user has changed the value displayed by the editor it is possible for the models value to differ from that of the editor To make sure the model has the same value as the editor use the commitEdit method
Demo
import javaawtimport javaxswingimport javaxswingeventclass JSpinnerExample public static void main(String[] args) String[] sizes = smallmediumlargeXLXXL final SpinnerListModel model = new SpinnerListModel(sizes) JSpinner jSpinner = new JSpinner(model) modeladdChangeListener(new ChangeListener() public void stateChanged(ChangeEvent e) Systemoutprintln(modelgetValue()) ) JFrame frame = new JFrame() framesetSize(100100) framegetContentPane()add(jSpinner) framepack() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Press UP KEY
Press UP KEY
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
- Relationship to AWT
-
- JProgressBar
- Demo
- import javaawt
- import javaawtevent
- import javaxswing
- import javaxswingtexthtml
- public class SwingProgressBarExample
- final static int interval = 1000
- int i
- JLabel label
- JProgressBar pb
- Timer timer
- JButton button
- public SwingProgressBarExample()
- JFrame frame = new JFrame(Swing Progress Bar)
- button = new JButton(Start)
- buttonaddActionListener(new ButtonListener())
- pb = new JProgressBar(0 20)
- pbsetValue(0)
- pbsetStringPainted(true)
- label = new JLabel(By Prasad Sawant)
- JPanel panel = new JPanel()
- paneladd(button)
- paneladd(pb)
- JPanel panel1 = new JPanel()
- panel1setLayout(new BorderLayout())
- panel1add(panel BorderLayoutNORTH)
- panel1add(label BorderLayoutCENTER)
- panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20))
- framesetContentPane(panel1)
- framepack()
- framesetVisible(true)
- framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
- Create a timer
- timer = new Timer(interval new ActionListener()
- public void actionPerformed(ActionEvent evt)
- if (i == 20)
- ToolkitgetDefaultToolkit()beep()
- timerstop()
- buttonsetEnabled(true)
- pbsetValue(0)
- String str = lthtmlgt + ltfont color=FF0000gt + ltbgt +
- Downloading completed + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- i = i + 1
- pbsetValue(i)
- )
- class ButtonListener implements ActionListener
- public void actionPerformed(ActionEvent ae)
- buttonsetEnabled(false)
- i = 0
- String str = lthtmlgt + ltfont color=008000gt + ltbgt +
- Downloading is in process + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- timerstart()
- public static void main(String[] args)
- SwingProgressBarExample spb = new SwingProgressBarExample()
- Press UP KEY
- Press UP KEY
-
Swing
JLabel
Constructor Summary
JLabel() Creates a JLabel instance with no image and with an empty string for the title
JLabel(Icon image) Creates a JLabel instance with the specified image
JLabel(Icon image int horizontalAlignment) Creates a JLabel instance with the specified image and horizontal alignment
JLabel(String text) Creates a JLabel instance with the specified text
JLabel(String text Icon icon int horizontalAlignment) Creates a JLabel instance with the specified text image and horizontal alignment
JLabel(String text int horizontalAlignment) Creates a JLabel instance with the specified text and horizontal alignment
Demo
import javaawtimport javaxswingpublic class JLabelDemo extends JFrame public static void main(String[] args) new JLabelDemo() public JLabelDemo() JFrame f=new JFrame() JLabel coloredLabel = new JLabel(HelloLabelCENTER) Container c=getContentPane() csetLayout(new GridLayout(01)) cadd(coloredLabel) pack() fadd(c) fsetVisible(true) fsetSize(150150)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JButton
Constructor Summary
JButton() Creates a button with no set text or icon
JButton(Action a) Creates a button where properties are taken from the Action supplied
JButton(Icon icon) Creates a button with an icon
JButton(String text) Creates a button with text
JButton(String text Icon icon) Creates a button with initial text and an icon
Demoimport javaawtimport javaawteventimport javaxswingpublic class JButtonDemo extends JFrame implements ActionListener private JButton btn_simple btn_image public JButtonDemo() setTitle(Testing Buttons) Container container = getContentPane() containersetLayout(new GridLayout(02)) btn_simple = new JButton(Simple Button) btn_simplesetMnemonic(KeyEventVK_S) containeradd(btn_simple) Icon bug1 = new ImageIcon(Dmenu_1gif) btn_image = new JButton(Imagebug1) btn_imagesetMnemonic(KeyEventVK_I) containeradd(btn_image) btn_imageaddActionListener(this) btn_simpleaddActionListener(this) setSize(300100) setVisible(true) public static void main(String args[]) new JButtonDemo() public void actionPerformed(ActionEvent ae) JOptionPaneshowMessageDialog(nullYou pressed + aegetActionCommand())
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JCheckBox
Constructor Summary
JCheckBox() Creates an initially unselected check box button with no text no icon
JCheckBox(Action a) Creates a check box where properties are taken from the Action supplied
JCheckBox(Icon icon) Creates an initially unselected check box with an icon
JCheckBox(Icon icon boolean selected) Creates a check box with an icon and specifies whether or not it is initially selected
JCheckBox(String text) Creates an initially unselected check box with text
JCheckBox(String text boolean selected) Creates a check box with text and specifies whether or not it is initially selected
JCheckBox(String text Icon icon) Creates an initially unselected check box with the specified text and icon
JCheckBox(String text Icon icon boolean selected) Creates a check box with text and icon and specifies whether or not it is initially selected
Demoimport javaawtContainerimport javaawteventimport javaxswingclass JCheckboxDemo public static void main(String[] args) JFrame frame = new JFrame() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Container cp = framegetContentPane() Box box = new Box(BoxLayoutY_AXIS) cpadd(box) boxadd(new JLabel(Tick the sports you like)) String[] sports = Football Rugby CricketBadminton JCheckBox[] cba = new JCheckBox[sportslength] for (int i = 0 i lt sportslength i++) cba[i] = new JCheckBox(sports[i]) boxadd(cba[i]) framepack()copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JRadioButton
Constructor Summary
JRadioButton() Creates an initially unselected radio button with no set text
JRadioButton(Action a) Creates a radiobutton where properties are taken from the Action supplied
JRadioButton(Icon icon) Creates an initially unselected radio button with the specified image but no text
JRadioButton(Icon icon boolean selected) Creates a radio button with the specified image and selection state but no text
JRadioButton(String text) Creates an unselected radio button with the specified text
JRadioButton(String text boolean selected) Creates a radio button with the specified text and selection state
JRadioButton(String text Icon icon) Creates a radio button that has the specified text and image and that is initially unselected
JRadioButton(String text Icon icon boolean selected) Creates a radio button that has the specified text image and selection state
Demoimport javaawtimport javaawteventimport javaxswingclass JRadioButtonDemo public static void main(String[] args) ActionListener listener = new ActionListener() public void actionPerformed (ActionEvent e) Systemoutprintln(egetActionCommand()) Box p = new Box(BoxLayoutY_AXIS) ButtonGroup group = new ButtonGroup() String[] sa = uglikiwipassionkumquat for(int i=0iltsalength++i) JRadioButton b = new JRadioButton(sa[i]) groupadd(b) padd(b) baddActionListener(listener) JFrame frame = new JFrame(Fruits)framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
framegetContentPane()add(p)framepack() framesetVisible(true)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JTextField
Constructor Summary
JTextField() Constructs a new TextField
JTextField(Document doc String text int columns) Constructs a new JTextField that uses the given text storage model and the given number of columns
JTextField(int columns) Constructs a new empty TextField with the specified number of columns
JTextField(String text) Constructs a new TextField initialized with the specified text
JTextField(String text int columns) Constructs a new TextField initialized with the specified text and columns
Demo
import javaawtimport javaawteventimport javaxswing
public class JLabelDemo extends JFrame
public JLabelDemo() JTextField tf=new JTextField() JFrame f = new JFrame(TextFieldDemo) Container c= getContentPane() csetLayout(new GridLayout(02)) JLabel lbl1=new JLabel(TextFieldSwingConstantsLEFT) cadd(lbl1) cadd(tf) fadd(c)
fsetSize(300100) fsetVisible(true) fsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
public static void main(String[] args) new JLabelDemo()
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JMenu
Constructor Summary
JMenu() Constructs a new JMenu with no text
JMenu(Action a) Constructs a menu whose properties are taken from the Action supplied
JMenu(String s) Constructs a new JMenu with the supplied string as its text
JMenu(String s boolean b) Constructs a new JMenu with the supplied string as its text and specified as a tear-off menu or not
Demoimport javaawtimport javaxswingclass JMenuDemo public static void main(String[] args) JMenu menu1 = new JMenu(File) menu1add(new JMenuItem(New)) menu1add(new JMenuItem(Open)) menu1add(new JSeparator()) menu1add(new JMenuItem(Save)) menu1add(new JMenuItem(Save as)) menu1add(new JSeparator()) menu1add(new JMenuItem(Exit)) JMenu menu2 = new JMenu(Edit) menu2add(new JMenuItem(Copy)) menu2add(new JSeparator()) menu2add(new JMenuItem(Paste)) menu2add(new JSeparator()) menu2add(new JMenuItem(Select ALL)) JMenuBar bar = new JMenuBar() baradd(menu1) baradd(menu2) JFrame frame = new JFrame() framesetJMenuBar(bar) framesetSize(350300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JColorChooser
Constructor Summary
JColorChooser() Creates a color chooser pane with an initial color of white
JColorChooser(Color initialColor) Creates a color chooser pane with the specified initial color
JColorChooser(ColorSelectionModel model) Creates a color chooser pane with the specified ColorSelectionModel
Demoimport javaawtimport javaawteventimport javaxswingpublic class ColorCh static Container cp public static void main(String[] args) final JFrame frame = new JFrame(Color Choice) cp = framegetContentPane( ) cpsetLayout(new GridBagLayout()) JButton button = new JButton(select color) cpadd(button) buttonaddActionListener(new ActionListener( ) public void actionPerformed(ActionEvent e) Color c = JColorChoosershowDialog(frameChoose a color Colorred) cpsetBackground(c) ) framesetSize(200 300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Select Any Color and Press OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JOptionPane
Constructor Summary
JOptionPane() Creates a JOptionPane with a test message
JOptionPane(Object message) Creates a instance of JOptionPane to display a message using the plain-message message type and the default options delivered by the UI
JOptionPane(Object message int messageType) Creates an instance of JOptionPane to display a message with the specified message type and the default options
JOptionPane(Object message int messageType int optionType) Creates an instance of JOptionPane to display a message with the specified message type and options
JOptionPane(Object message int messageType int optionType Icon icon) Creates an instance of JOptionPane to display a message with the specified message type options and icon
JOptionPane(Object message int messageType int optionType Icon icon Object[] options) Creates an instance of JOptionPane to display a message with the specified message type icon and options
JOptionPane(Object message int messageType int optionType Icon icon Object[] options Object initialValue) Creates an instance of JOptionPane to display a message with the specified message type icon and options with the initially-selected option specified
Demo
import javaxswingJOptionPanepublic class JOptionPaneDemo public static void main(String[] args) String[] choices = BJP NCP LEFT int doAgain do int response = JOptionPaneshowOptionDialog ( null center over parent
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
How did you vote message Party Poll title in titlebar JOptionPaneYES_NO_OPTION Option type JOptionPanePLAIN_MESSAGE messageType null icon choices Options None of your business initial value ) JOptionPaneshowMessageDialog(null Response = + response) doAgain = JOptionPaneshowConfirmDialog(null Again) while(doAgain == JOptionPaneYES_OPTION) Systemexit(0)
Output
Press Any One
After Pressing OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JProgressBar
Constructor Summary
JProgressBar() Creates a horizontal progress bar that displays a border but no progress string
JProgressBar(BoundedRangeModel newModel) Creates a horizontal progress bar that uses the specified model to hold the progress bars data
JProgressBar(int orient) Creates a progress bar with the specified orientation which can be either SwingConstantsVERTICAL or SwingConstantsHORIZONTAL
JProgressBar(int min int max) Creates a horizontal progress bar with the specified minimum and maximum
JProgressBar(int orient int min int max) Creates a progress bar using the specified orientation minimum and maximum
Demo
import javaawtimport javaawteventimport javaxswingimport javaxswingtexthtmlpublic class SwingProgressBarExample final static int interval = 1000 int i JLabel label JProgressBar pb Timer timer JButton button public SwingProgressBarExample() JFrame frame = new JFrame(Swing Progress Bar) button = new JButton(Start) buttonaddActionListener(new ButtonListener()) pb = new JProgressBar(0 20) pbsetValue(0) pbsetStringPainted(true) label = new JLabel(By Prasad Sawant) JPanel panel = new JPanel() paneladd(button) paneladd(pb)copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JPanel panel1 = new JPanel() panel1setLayout(new BorderLayout()) panel1add(panel BorderLayoutNORTH) panel1add(label BorderLayoutCENTER) panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20)) framesetContentPane(panel1) framepack() framesetVisible(true) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Create a timer timer = new Timer(interval new ActionListener() public void actionPerformed(ActionEvent evt) if (i == 20) ToolkitgetDefaultToolkit()beep() timerstop() buttonsetEnabled(true) pbsetValue(0) String str = lthtmlgt + ltfont color=FF0000gt + ltbgt + Downloading completed + ltbgt + ltfontgt + lthtmlgt labelsetText(str) i = i + 1 pbsetValue(i) ) class ButtonListener implements ActionListener public void actionPerformed(ActionEvent ae) buttonsetEnabled(false) i = 0 String str = lthtmlgt + ltfont color=008000gt + ltbgt +Downloading is in process + ltbgt + ltfontgt + lthtmlgt labelsetText(str) timerstart() public static void main(String[] args) SwingProgressBarExample spb = new SwingProgressBarExample()
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
After Pressing Start Button
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JSpinner
A single line input field that lets the user select a number or an object value from an ordered sequence Spinners typically provide a pair of tiny arrow buttons for stepping through the elements of the sequence The keyboard updown arrow keys also cycle through the elements The user may also be allowed to type a (legal) value directly into the spinner Although combo boxes provide similar functionality spinners are sometimes preferred because they dont require a drop down list that can obscure important data
A JSpinners sequence value is defined by its SpinnerModel The model can be specified as a constructor argument and changed with the model property SpinnerModel classes for some common types are provided SpinnerListModel SpinnerNumberModel and SpinnerDateModel
A JSpinner has a single child component thats responsible for displaying and potentially changing the current element or value of the model which is called the editor The editor is created by the JSpinners constructor and can be changed with the editor property The JSpinners editor stays in sync with the model by listening for ChangeEvents If the user has changed the value displayed by the editor it is possible for the models value to differ from that of the editor To make sure the model has the same value as the editor use the commitEdit method
Demo
import javaawtimport javaxswingimport javaxswingeventclass JSpinnerExample public static void main(String[] args) String[] sizes = smallmediumlargeXLXXL final SpinnerListModel model = new SpinnerListModel(sizes) JSpinner jSpinner = new JSpinner(model) modeladdChangeListener(new ChangeListener() public void stateChanged(ChangeEvent e) Systemoutprintln(modelgetValue()) ) JFrame frame = new JFrame() framesetSize(100100) framegetContentPane()add(jSpinner) framepack() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Press UP KEY
Press UP KEY
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
- Relationship to AWT
-
- JProgressBar
- Demo
- import javaawt
- import javaawtevent
- import javaxswing
- import javaxswingtexthtml
- public class SwingProgressBarExample
- final static int interval = 1000
- int i
- JLabel label
- JProgressBar pb
- Timer timer
- JButton button
- public SwingProgressBarExample()
- JFrame frame = new JFrame(Swing Progress Bar)
- button = new JButton(Start)
- buttonaddActionListener(new ButtonListener())
- pb = new JProgressBar(0 20)
- pbsetValue(0)
- pbsetStringPainted(true)
- label = new JLabel(By Prasad Sawant)
- JPanel panel = new JPanel()
- paneladd(button)
- paneladd(pb)
- JPanel panel1 = new JPanel()
- panel1setLayout(new BorderLayout())
- panel1add(panel BorderLayoutNORTH)
- panel1add(label BorderLayoutCENTER)
- panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20))
- framesetContentPane(panel1)
- framepack()
- framesetVisible(true)
- framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
- Create a timer
- timer = new Timer(interval new ActionListener()
- public void actionPerformed(ActionEvent evt)
- if (i == 20)
- ToolkitgetDefaultToolkit()beep()
- timerstop()
- buttonsetEnabled(true)
- pbsetValue(0)
- String str = lthtmlgt + ltfont color=FF0000gt + ltbgt +
- Downloading completed + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- i = i + 1
- pbsetValue(i)
- )
- class ButtonListener implements ActionListener
- public void actionPerformed(ActionEvent ae)
- buttonsetEnabled(false)
- i = 0
- String str = lthtmlgt + ltfont color=008000gt + ltbgt +
- Downloading is in process + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- timerstart()
- public static void main(String[] args)
- SwingProgressBarExample spb = new SwingProgressBarExample()
- Press UP KEY
- Press UP KEY
-
Swing
JButton
Constructor Summary
JButton() Creates a button with no set text or icon
JButton(Action a) Creates a button where properties are taken from the Action supplied
JButton(Icon icon) Creates a button with an icon
JButton(String text) Creates a button with text
JButton(String text Icon icon) Creates a button with initial text and an icon
Demoimport javaawtimport javaawteventimport javaxswingpublic class JButtonDemo extends JFrame implements ActionListener private JButton btn_simple btn_image public JButtonDemo() setTitle(Testing Buttons) Container container = getContentPane() containersetLayout(new GridLayout(02)) btn_simple = new JButton(Simple Button) btn_simplesetMnemonic(KeyEventVK_S) containeradd(btn_simple) Icon bug1 = new ImageIcon(Dmenu_1gif) btn_image = new JButton(Imagebug1) btn_imagesetMnemonic(KeyEventVK_I) containeradd(btn_image) btn_imageaddActionListener(this) btn_simpleaddActionListener(this) setSize(300100) setVisible(true) public static void main(String args[]) new JButtonDemo() public void actionPerformed(ActionEvent ae) JOptionPaneshowMessageDialog(nullYou pressed + aegetActionCommand())
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JCheckBox
Constructor Summary
JCheckBox() Creates an initially unselected check box button with no text no icon
JCheckBox(Action a) Creates a check box where properties are taken from the Action supplied
JCheckBox(Icon icon) Creates an initially unselected check box with an icon
JCheckBox(Icon icon boolean selected) Creates a check box with an icon and specifies whether or not it is initially selected
JCheckBox(String text) Creates an initially unselected check box with text
JCheckBox(String text boolean selected) Creates a check box with text and specifies whether or not it is initially selected
JCheckBox(String text Icon icon) Creates an initially unselected check box with the specified text and icon
JCheckBox(String text Icon icon boolean selected) Creates a check box with text and icon and specifies whether or not it is initially selected
Demoimport javaawtContainerimport javaawteventimport javaxswingclass JCheckboxDemo public static void main(String[] args) JFrame frame = new JFrame() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Container cp = framegetContentPane() Box box = new Box(BoxLayoutY_AXIS) cpadd(box) boxadd(new JLabel(Tick the sports you like)) String[] sports = Football Rugby CricketBadminton JCheckBox[] cba = new JCheckBox[sportslength] for (int i = 0 i lt sportslength i++) cba[i] = new JCheckBox(sports[i]) boxadd(cba[i]) framepack()copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JRadioButton
Constructor Summary
JRadioButton() Creates an initially unselected radio button with no set text
JRadioButton(Action a) Creates a radiobutton where properties are taken from the Action supplied
JRadioButton(Icon icon) Creates an initially unselected radio button with the specified image but no text
JRadioButton(Icon icon boolean selected) Creates a radio button with the specified image and selection state but no text
JRadioButton(String text) Creates an unselected radio button with the specified text
JRadioButton(String text boolean selected) Creates a radio button with the specified text and selection state
JRadioButton(String text Icon icon) Creates a radio button that has the specified text and image and that is initially unselected
JRadioButton(String text Icon icon boolean selected) Creates a radio button that has the specified text image and selection state
Demoimport javaawtimport javaawteventimport javaxswingclass JRadioButtonDemo public static void main(String[] args) ActionListener listener = new ActionListener() public void actionPerformed (ActionEvent e) Systemoutprintln(egetActionCommand()) Box p = new Box(BoxLayoutY_AXIS) ButtonGroup group = new ButtonGroup() String[] sa = uglikiwipassionkumquat for(int i=0iltsalength++i) JRadioButton b = new JRadioButton(sa[i]) groupadd(b) padd(b) baddActionListener(listener) JFrame frame = new JFrame(Fruits)framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
framegetContentPane()add(p)framepack() framesetVisible(true)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JTextField
Constructor Summary
JTextField() Constructs a new TextField
JTextField(Document doc String text int columns) Constructs a new JTextField that uses the given text storage model and the given number of columns
JTextField(int columns) Constructs a new empty TextField with the specified number of columns
JTextField(String text) Constructs a new TextField initialized with the specified text
JTextField(String text int columns) Constructs a new TextField initialized with the specified text and columns
Demo
import javaawtimport javaawteventimport javaxswing
public class JLabelDemo extends JFrame
public JLabelDemo() JTextField tf=new JTextField() JFrame f = new JFrame(TextFieldDemo) Container c= getContentPane() csetLayout(new GridLayout(02)) JLabel lbl1=new JLabel(TextFieldSwingConstantsLEFT) cadd(lbl1) cadd(tf) fadd(c)
fsetSize(300100) fsetVisible(true) fsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
public static void main(String[] args) new JLabelDemo()
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JMenu
Constructor Summary
JMenu() Constructs a new JMenu with no text
JMenu(Action a) Constructs a menu whose properties are taken from the Action supplied
JMenu(String s) Constructs a new JMenu with the supplied string as its text
JMenu(String s boolean b) Constructs a new JMenu with the supplied string as its text and specified as a tear-off menu or not
Demoimport javaawtimport javaxswingclass JMenuDemo public static void main(String[] args) JMenu menu1 = new JMenu(File) menu1add(new JMenuItem(New)) menu1add(new JMenuItem(Open)) menu1add(new JSeparator()) menu1add(new JMenuItem(Save)) menu1add(new JMenuItem(Save as)) menu1add(new JSeparator()) menu1add(new JMenuItem(Exit)) JMenu menu2 = new JMenu(Edit) menu2add(new JMenuItem(Copy)) menu2add(new JSeparator()) menu2add(new JMenuItem(Paste)) menu2add(new JSeparator()) menu2add(new JMenuItem(Select ALL)) JMenuBar bar = new JMenuBar() baradd(menu1) baradd(menu2) JFrame frame = new JFrame() framesetJMenuBar(bar) framesetSize(350300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JColorChooser
Constructor Summary
JColorChooser() Creates a color chooser pane with an initial color of white
JColorChooser(Color initialColor) Creates a color chooser pane with the specified initial color
JColorChooser(ColorSelectionModel model) Creates a color chooser pane with the specified ColorSelectionModel
Demoimport javaawtimport javaawteventimport javaxswingpublic class ColorCh static Container cp public static void main(String[] args) final JFrame frame = new JFrame(Color Choice) cp = framegetContentPane( ) cpsetLayout(new GridBagLayout()) JButton button = new JButton(select color) cpadd(button) buttonaddActionListener(new ActionListener( ) public void actionPerformed(ActionEvent e) Color c = JColorChoosershowDialog(frameChoose a color Colorred) cpsetBackground(c) ) framesetSize(200 300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Select Any Color and Press OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JOptionPane
Constructor Summary
JOptionPane() Creates a JOptionPane with a test message
JOptionPane(Object message) Creates a instance of JOptionPane to display a message using the plain-message message type and the default options delivered by the UI
JOptionPane(Object message int messageType) Creates an instance of JOptionPane to display a message with the specified message type and the default options
JOptionPane(Object message int messageType int optionType) Creates an instance of JOptionPane to display a message with the specified message type and options
JOptionPane(Object message int messageType int optionType Icon icon) Creates an instance of JOptionPane to display a message with the specified message type options and icon
JOptionPane(Object message int messageType int optionType Icon icon Object[] options) Creates an instance of JOptionPane to display a message with the specified message type icon and options
JOptionPane(Object message int messageType int optionType Icon icon Object[] options Object initialValue) Creates an instance of JOptionPane to display a message with the specified message type icon and options with the initially-selected option specified
Demo
import javaxswingJOptionPanepublic class JOptionPaneDemo public static void main(String[] args) String[] choices = BJP NCP LEFT int doAgain do int response = JOptionPaneshowOptionDialog ( null center over parent
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
How did you vote message Party Poll title in titlebar JOptionPaneYES_NO_OPTION Option type JOptionPanePLAIN_MESSAGE messageType null icon choices Options None of your business initial value ) JOptionPaneshowMessageDialog(null Response = + response) doAgain = JOptionPaneshowConfirmDialog(null Again) while(doAgain == JOptionPaneYES_OPTION) Systemexit(0)
Output
Press Any One
After Pressing OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JProgressBar
Constructor Summary
JProgressBar() Creates a horizontal progress bar that displays a border but no progress string
JProgressBar(BoundedRangeModel newModel) Creates a horizontal progress bar that uses the specified model to hold the progress bars data
JProgressBar(int orient) Creates a progress bar with the specified orientation which can be either SwingConstantsVERTICAL or SwingConstantsHORIZONTAL
JProgressBar(int min int max) Creates a horizontal progress bar with the specified minimum and maximum
JProgressBar(int orient int min int max) Creates a progress bar using the specified orientation minimum and maximum
Demo
import javaawtimport javaawteventimport javaxswingimport javaxswingtexthtmlpublic class SwingProgressBarExample final static int interval = 1000 int i JLabel label JProgressBar pb Timer timer JButton button public SwingProgressBarExample() JFrame frame = new JFrame(Swing Progress Bar) button = new JButton(Start) buttonaddActionListener(new ButtonListener()) pb = new JProgressBar(0 20) pbsetValue(0) pbsetStringPainted(true) label = new JLabel(By Prasad Sawant) JPanel panel = new JPanel() paneladd(button) paneladd(pb)copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JPanel panel1 = new JPanel() panel1setLayout(new BorderLayout()) panel1add(panel BorderLayoutNORTH) panel1add(label BorderLayoutCENTER) panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20)) framesetContentPane(panel1) framepack() framesetVisible(true) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Create a timer timer = new Timer(interval new ActionListener() public void actionPerformed(ActionEvent evt) if (i == 20) ToolkitgetDefaultToolkit()beep() timerstop() buttonsetEnabled(true) pbsetValue(0) String str = lthtmlgt + ltfont color=FF0000gt + ltbgt + Downloading completed + ltbgt + ltfontgt + lthtmlgt labelsetText(str) i = i + 1 pbsetValue(i) ) class ButtonListener implements ActionListener public void actionPerformed(ActionEvent ae) buttonsetEnabled(false) i = 0 String str = lthtmlgt + ltfont color=008000gt + ltbgt +Downloading is in process + ltbgt + ltfontgt + lthtmlgt labelsetText(str) timerstart() public static void main(String[] args) SwingProgressBarExample spb = new SwingProgressBarExample()
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
After Pressing Start Button
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JSpinner
A single line input field that lets the user select a number or an object value from an ordered sequence Spinners typically provide a pair of tiny arrow buttons for stepping through the elements of the sequence The keyboard updown arrow keys also cycle through the elements The user may also be allowed to type a (legal) value directly into the spinner Although combo boxes provide similar functionality spinners are sometimes preferred because they dont require a drop down list that can obscure important data
A JSpinners sequence value is defined by its SpinnerModel The model can be specified as a constructor argument and changed with the model property SpinnerModel classes for some common types are provided SpinnerListModel SpinnerNumberModel and SpinnerDateModel
A JSpinner has a single child component thats responsible for displaying and potentially changing the current element or value of the model which is called the editor The editor is created by the JSpinners constructor and can be changed with the editor property The JSpinners editor stays in sync with the model by listening for ChangeEvents If the user has changed the value displayed by the editor it is possible for the models value to differ from that of the editor To make sure the model has the same value as the editor use the commitEdit method
Demo
import javaawtimport javaxswingimport javaxswingeventclass JSpinnerExample public static void main(String[] args) String[] sizes = smallmediumlargeXLXXL final SpinnerListModel model = new SpinnerListModel(sizes) JSpinner jSpinner = new JSpinner(model) modeladdChangeListener(new ChangeListener() public void stateChanged(ChangeEvent e) Systemoutprintln(modelgetValue()) ) JFrame frame = new JFrame() framesetSize(100100) framegetContentPane()add(jSpinner) framepack() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Press UP KEY
Press UP KEY
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
- Relationship to AWT
-
- JProgressBar
- Demo
- import javaawt
- import javaawtevent
- import javaxswing
- import javaxswingtexthtml
- public class SwingProgressBarExample
- final static int interval = 1000
- int i
- JLabel label
- JProgressBar pb
- Timer timer
- JButton button
- public SwingProgressBarExample()
- JFrame frame = new JFrame(Swing Progress Bar)
- button = new JButton(Start)
- buttonaddActionListener(new ButtonListener())
- pb = new JProgressBar(0 20)
- pbsetValue(0)
- pbsetStringPainted(true)
- label = new JLabel(By Prasad Sawant)
- JPanel panel = new JPanel()
- paneladd(button)
- paneladd(pb)
- JPanel panel1 = new JPanel()
- panel1setLayout(new BorderLayout())
- panel1add(panel BorderLayoutNORTH)
- panel1add(label BorderLayoutCENTER)
- panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20))
- framesetContentPane(panel1)
- framepack()
- framesetVisible(true)
- framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
- Create a timer
- timer = new Timer(interval new ActionListener()
- public void actionPerformed(ActionEvent evt)
- if (i == 20)
- ToolkitgetDefaultToolkit()beep()
- timerstop()
- buttonsetEnabled(true)
- pbsetValue(0)
- String str = lthtmlgt + ltfont color=FF0000gt + ltbgt +
- Downloading completed + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- i = i + 1
- pbsetValue(i)
- )
- class ButtonListener implements ActionListener
- public void actionPerformed(ActionEvent ae)
- buttonsetEnabled(false)
- i = 0
- String str = lthtmlgt + ltfont color=008000gt + ltbgt +
- Downloading is in process + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- timerstart()
- public static void main(String[] args)
- SwingProgressBarExample spb = new SwingProgressBarExample()
- Press UP KEY
- Press UP KEY
-
Swing
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JCheckBox
Constructor Summary
JCheckBox() Creates an initially unselected check box button with no text no icon
JCheckBox(Action a) Creates a check box where properties are taken from the Action supplied
JCheckBox(Icon icon) Creates an initially unselected check box with an icon
JCheckBox(Icon icon boolean selected) Creates a check box with an icon and specifies whether or not it is initially selected
JCheckBox(String text) Creates an initially unselected check box with text
JCheckBox(String text boolean selected) Creates a check box with text and specifies whether or not it is initially selected
JCheckBox(String text Icon icon) Creates an initially unselected check box with the specified text and icon
JCheckBox(String text Icon icon boolean selected) Creates a check box with text and icon and specifies whether or not it is initially selected
Demoimport javaawtContainerimport javaawteventimport javaxswingclass JCheckboxDemo public static void main(String[] args) JFrame frame = new JFrame() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Container cp = framegetContentPane() Box box = new Box(BoxLayoutY_AXIS) cpadd(box) boxadd(new JLabel(Tick the sports you like)) String[] sports = Football Rugby CricketBadminton JCheckBox[] cba = new JCheckBox[sportslength] for (int i = 0 i lt sportslength i++) cba[i] = new JCheckBox(sports[i]) boxadd(cba[i]) framepack()copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JRadioButton
Constructor Summary
JRadioButton() Creates an initially unselected radio button with no set text
JRadioButton(Action a) Creates a radiobutton where properties are taken from the Action supplied
JRadioButton(Icon icon) Creates an initially unselected radio button with the specified image but no text
JRadioButton(Icon icon boolean selected) Creates a radio button with the specified image and selection state but no text
JRadioButton(String text) Creates an unselected radio button with the specified text
JRadioButton(String text boolean selected) Creates a radio button with the specified text and selection state
JRadioButton(String text Icon icon) Creates a radio button that has the specified text and image and that is initially unselected
JRadioButton(String text Icon icon boolean selected) Creates a radio button that has the specified text image and selection state
Demoimport javaawtimport javaawteventimport javaxswingclass JRadioButtonDemo public static void main(String[] args) ActionListener listener = new ActionListener() public void actionPerformed (ActionEvent e) Systemoutprintln(egetActionCommand()) Box p = new Box(BoxLayoutY_AXIS) ButtonGroup group = new ButtonGroup() String[] sa = uglikiwipassionkumquat for(int i=0iltsalength++i) JRadioButton b = new JRadioButton(sa[i]) groupadd(b) padd(b) baddActionListener(listener) JFrame frame = new JFrame(Fruits)framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
framegetContentPane()add(p)framepack() framesetVisible(true)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JTextField
Constructor Summary
JTextField() Constructs a new TextField
JTextField(Document doc String text int columns) Constructs a new JTextField that uses the given text storage model and the given number of columns
JTextField(int columns) Constructs a new empty TextField with the specified number of columns
JTextField(String text) Constructs a new TextField initialized with the specified text
JTextField(String text int columns) Constructs a new TextField initialized with the specified text and columns
Demo
import javaawtimport javaawteventimport javaxswing
public class JLabelDemo extends JFrame
public JLabelDemo() JTextField tf=new JTextField() JFrame f = new JFrame(TextFieldDemo) Container c= getContentPane() csetLayout(new GridLayout(02)) JLabel lbl1=new JLabel(TextFieldSwingConstantsLEFT) cadd(lbl1) cadd(tf) fadd(c)
fsetSize(300100) fsetVisible(true) fsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
public static void main(String[] args) new JLabelDemo()
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JMenu
Constructor Summary
JMenu() Constructs a new JMenu with no text
JMenu(Action a) Constructs a menu whose properties are taken from the Action supplied
JMenu(String s) Constructs a new JMenu with the supplied string as its text
JMenu(String s boolean b) Constructs a new JMenu with the supplied string as its text and specified as a tear-off menu or not
Demoimport javaawtimport javaxswingclass JMenuDemo public static void main(String[] args) JMenu menu1 = new JMenu(File) menu1add(new JMenuItem(New)) menu1add(new JMenuItem(Open)) menu1add(new JSeparator()) menu1add(new JMenuItem(Save)) menu1add(new JMenuItem(Save as)) menu1add(new JSeparator()) menu1add(new JMenuItem(Exit)) JMenu menu2 = new JMenu(Edit) menu2add(new JMenuItem(Copy)) menu2add(new JSeparator()) menu2add(new JMenuItem(Paste)) menu2add(new JSeparator()) menu2add(new JMenuItem(Select ALL)) JMenuBar bar = new JMenuBar() baradd(menu1) baradd(menu2) JFrame frame = new JFrame() framesetJMenuBar(bar) framesetSize(350300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JColorChooser
Constructor Summary
JColorChooser() Creates a color chooser pane with an initial color of white
JColorChooser(Color initialColor) Creates a color chooser pane with the specified initial color
JColorChooser(ColorSelectionModel model) Creates a color chooser pane with the specified ColorSelectionModel
Demoimport javaawtimport javaawteventimport javaxswingpublic class ColorCh static Container cp public static void main(String[] args) final JFrame frame = new JFrame(Color Choice) cp = framegetContentPane( ) cpsetLayout(new GridBagLayout()) JButton button = new JButton(select color) cpadd(button) buttonaddActionListener(new ActionListener( ) public void actionPerformed(ActionEvent e) Color c = JColorChoosershowDialog(frameChoose a color Colorred) cpsetBackground(c) ) framesetSize(200 300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Select Any Color and Press OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JOptionPane
Constructor Summary
JOptionPane() Creates a JOptionPane with a test message
JOptionPane(Object message) Creates a instance of JOptionPane to display a message using the plain-message message type and the default options delivered by the UI
JOptionPane(Object message int messageType) Creates an instance of JOptionPane to display a message with the specified message type and the default options
JOptionPane(Object message int messageType int optionType) Creates an instance of JOptionPane to display a message with the specified message type and options
JOptionPane(Object message int messageType int optionType Icon icon) Creates an instance of JOptionPane to display a message with the specified message type options and icon
JOptionPane(Object message int messageType int optionType Icon icon Object[] options) Creates an instance of JOptionPane to display a message with the specified message type icon and options
JOptionPane(Object message int messageType int optionType Icon icon Object[] options Object initialValue) Creates an instance of JOptionPane to display a message with the specified message type icon and options with the initially-selected option specified
Demo
import javaxswingJOptionPanepublic class JOptionPaneDemo public static void main(String[] args) String[] choices = BJP NCP LEFT int doAgain do int response = JOptionPaneshowOptionDialog ( null center over parent
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
How did you vote message Party Poll title in titlebar JOptionPaneYES_NO_OPTION Option type JOptionPanePLAIN_MESSAGE messageType null icon choices Options None of your business initial value ) JOptionPaneshowMessageDialog(null Response = + response) doAgain = JOptionPaneshowConfirmDialog(null Again) while(doAgain == JOptionPaneYES_OPTION) Systemexit(0)
Output
Press Any One
After Pressing OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JProgressBar
Constructor Summary
JProgressBar() Creates a horizontal progress bar that displays a border but no progress string
JProgressBar(BoundedRangeModel newModel) Creates a horizontal progress bar that uses the specified model to hold the progress bars data
JProgressBar(int orient) Creates a progress bar with the specified orientation which can be either SwingConstantsVERTICAL or SwingConstantsHORIZONTAL
JProgressBar(int min int max) Creates a horizontal progress bar with the specified minimum and maximum
JProgressBar(int orient int min int max) Creates a progress bar using the specified orientation minimum and maximum
Demo
import javaawtimport javaawteventimport javaxswingimport javaxswingtexthtmlpublic class SwingProgressBarExample final static int interval = 1000 int i JLabel label JProgressBar pb Timer timer JButton button public SwingProgressBarExample() JFrame frame = new JFrame(Swing Progress Bar) button = new JButton(Start) buttonaddActionListener(new ButtonListener()) pb = new JProgressBar(0 20) pbsetValue(0) pbsetStringPainted(true) label = new JLabel(By Prasad Sawant) JPanel panel = new JPanel() paneladd(button) paneladd(pb)copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JPanel panel1 = new JPanel() panel1setLayout(new BorderLayout()) panel1add(panel BorderLayoutNORTH) panel1add(label BorderLayoutCENTER) panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20)) framesetContentPane(panel1) framepack() framesetVisible(true) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Create a timer timer = new Timer(interval new ActionListener() public void actionPerformed(ActionEvent evt) if (i == 20) ToolkitgetDefaultToolkit()beep() timerstop() buttonsetEnabled(true) pbsetValue(0) String str = lthtmlgt + ltfont color=FF0000gt + ltbgt + Downloading completed + ltbgt + ltfontgt + lthtmlgt labelsetText(str) i = i + 1 pbsetValue(i) ) class ButtonListener implements ActionListener public void actionPerformed(ActionEvent ae) buttonsetEnabled(false) i = 0 String str = lthtmlgt + ltfont color=008000gt + ltbgt +Downloading is in process + ltbgt + ltfontgt + lthtmlgt labelsetText(str) timerstart() public static void main(String[] args) SwingProgressBarExample spb = new SwingProgressBarExample()
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
After Pressing Start Button
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JSpinner
A single line input field that lets the user select a number or an object value from an ordered sequence Spinners typically provide a pair of tiny arrow buttons for stepping through the elements of the sequence The keyboard updown arrow keys also cycle through the elements The user may also be allowed to type a (legal) value directly into the spinner Although combo boxes provide similar functionality spinners are sometimes preferred because they dont require a drop down list that can obscure important data
A JSpinners sequence value is defined by its SpinnerModel The model can be specified as a constructor argument and changed with the model property SpinnerModel classes for some common types are provided SpinnerListModel SpinnerNumberModel and SpinnerDateModel
A JSpinner has a single child component thats responsible for displaying and potentially changing the current element or value of the model which is called the editor The editor is created by the JSpinners constructor and can be changed with the editor property The JSpinners editor stays in sync with the model by listening for ChangeEvents If the user has changed the value displayed by the editor it is possible for the models value to differ from that of the editor To make sure the model has the same value as the editor use the commitEdit method
Demo
import javaawtimport javaxswingimport javaxswingeventclass JSpinnerExample public static void main(String[] args) String[] sizes = smallmediumlargeXLXXL final SpinnerListModel model = new SpinnerListModel(sizes) JSpinner jSpinner = new JSpinner(model) modeladdChangeListener(new ChangeListener() public void stateChanged(ChangeEvent e) Systemoutprintln(modelgetValue()) ) JFrame frame = new JFrame() framesetSize(100100) framegetContentPane()add(jSpinner) framepack() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Press UP KEY
Press UP KEY
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
- Relationship to AWT
-
- JProgressBar
- Demo
- import javaawt
- import javaawtevent
- import javaxswing
- import javaxswingtexthtml
- public class SwingProgressBarExample
- final static int interval = 1000
- int i
- JLabel label
- JProgressBar pb
- Timer timer
- JButton button
- public SwingProgressBarExample()
- JFrame frame = new JFrame(Swing Progress Bar)
- button = new JButton(Start)
- buttonaddActionListener(new ButtonListener())
- pb = new JProgressBar(0 20)
- pbsetValue(0)
- pbsetStringPainted(true)
- label = new JLabel(By Prasad Sawant)
- JPanel panel = new JPanel()
- paneladd(button)
- paneladd(pb)
- JPanel panel1 = new JPanel()
- panel1setLayout(new BorderLayout())
- panel1add(panel BorderLayoutNORTH)
- panel1add(label BorderLayoutCENTER)
- panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20))
- framesetContentPane(panel1)
- framepack()
- framesetVisible(true)
- framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
- Create a timer
- timer = new Timer(interval new ActionListener()
- public void actionPerformed(ActionEvent evt)
- if (i == 20)
- ToolkitgetDefaultToolkit()beep()
- timerstop()
- buttonsetEnabled(true)
- pbsetValue(0)
- String str = lthtmlgt + ltfont color=FF0000gt + ltbgt +
- Downloading completed + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- i = i + 1
- pbsetValue(i)
- )
- class ButtonListener implements ActionListener
- public void actionPerformed(ActionEvent ae)
- buttonsetEnabled(false)
- i = 0
- String str = lthtmlgt + ltfont color=008000gt + ltbgt +
- Downloading is in process + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- timerstart()
- public static void main(String[] args)
- SwingProgressBarExample spb = new SwingProgressBarExample()
- Press UP KEY
- Press UP KEY
-
Swing
JCheckBox
Constructor Summary
JCheckBox() Creates an initially unselected check box button with no text no icon
JCheckBox(Action a) Creates a check box where properties are taken from the Action supplied
JCheckBox(Icon icon) Creates an initially unselected check box with an icon
JCheckBox(Icon icon boolean selected) Creates a check box with an icon and specifies whether or not it is initially selected
JCheckBox(String text) Creates an initially unselected check box with text
JCheckBox(String text boolean selected) Creates a check box with text and specifies whether or not it is initially selected
JCheckBox(String text Icon icon) Creates an initially unselected check box with the specified text and icon
JCheckBox(String text Icon icon boolean selected) Creates a check box with text and icon and specifies whether or not it is initially selected
Demoimport javaawtContainerimport javaawteventimport javaxswingclass JCheckboxDemo public static void main(String[] args) JFrame frame = new JFrame() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Container cp = framegetContentPane() Box box = new Box(BoxLayoutY_AXIS) cpadd(box) boxadd(new JLabel(Tick the sports you like)) String[] sports = Football Rugby CricketBadminton JCheckBox[] cba = new JCheckBox[sportslength] for (int i = 0 i lt sportslength i++) cba[i] = new JCheckBox(sports[i]) boxadd(cba[i]) framepack()copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JRadioButton
Constructor Summary
JRadioButton() Creates an initially unselected radio button with no set text
JRadioButton(Action a) Creates a radiobutton where properties are taken from the Action supplied
JRadioButton(Icon icon) Creates an initially unselected radio button with the specified image but no text
JRadioButton(Icon icon boolean selected) Creates a radio button with the specified image and selection state but no text
JRadioButton(String text) Creates an unselected radio button with the specified text
JRadioButton(String text boolean selected) Creates a radio button with the specified text and selection state
JRadioButton(String text Icon icon) Creates a radio button that has the specified text and image and that is initially unselected
JRadioButton(String text Icon icon boolean selected) Creates a radio button that has the specified text image and selection state
Demoimport javaawtimport javaawteventimport javaxswingclass JRadioButtonDemo public static void main(String[] args) ActionListener listener = new ActionListener() public void actionPerformed (ActionEvent e) Systemoutprintln(egetActionCommand()) Box p = new Box(BoxLayoutY_AXIS) ButtonGroup group = new ButtonGroup() String[] sa = uglikiwipassionkumquat for(int i=0iltsalength++i) JRadioButton b = new JRadioButton(sa[i]) groupadd(b) padd(b) baddActionListener(listener) JFrame frame = new JFrame(Fruits)framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
framegetContentPane()add(p)framepack() framesetVisible(true)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JTextField
Constructor Summary
JTextField() Constructs a new TextField
JTextField(Document doc String text int columns) Constructs a new JTextField that uses the given text storage model and the given number of columns
JTextField(int columns) Constructs a new empty TextField with the specified number of columns
JTextField(String text) Constructs a new TextField initialized with the specified text
JTextField(String text int columns) Constructs a new TextField initialized with the specified text and columns
Demo
import javaawtimport javaawteventimport javaxswing
public class JLabelDemo extends JFrame
public JLabelDemo() JTextField tf=new JTextField() JFrame f = new JFrame(TextFieldDemo) Container c= getContentPane() csetLayout(new GridLayout(02)) JLabel lbl1=new JLabel(TextFieldSwingConstantsLEFT) cadd(lbl1) cadd(tf) fadd(c)
fsetSize(300100) fsetVisible(true) fsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
public static void main(String[] args) new JLabelDemo()
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JMenu
Constructor Summary
JMenu() Constructs a new JMenu with no text
JMenu(Action a) Constructs a menu whose properties are taken from the Action supplied
JMenu(String s) Constructs a new JMenu with the supplied string as its text
JMenu(String s boolean b) Constructs a new JMenu with the supplied string as its text and specified as a tear-off menu or not
Demoimport javaawtimport javaxswingclass JMenuDemo public static void main(String[] args) JMenu menu1 = new JMenu(File) menu1add(new JMenuItem(New)) menu1add(new JMenuItem(Open)) menu1add(new JSeparator()) menu1add(new JMenuItem(Save)) menu1add(new JMenuItem(Save as)) menu1add(new JSeparator()) menu1add(new JMenuItem(Exit)) JMenu menu2 = new JMenu(Edit) menu2add(new JMenuItem(Copy)) menu2add(new JSeparator()) menu2add(new JMenuItem(Paste)) menu2add(new JSeparator()) menu2add(new JMenuItem(Select ALL)) JMenuBar bar = new JMenuBar() baradd(menu1) baradd(menu2) JFrame frame = new JFrame() framesetJMenuBar(bar) framesetSize(350300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JColorChooser
Constructor Summary
JColorChooser() Creates a color chooser pane with an initial color of white
JColorChooser(Color initialColor) Creates a color chooser pane with the specified initial color
JColorChooser(ColorSelectionModel model) Creates a color chooser pane with the specified ColorSelectionModel
Demoimport javaawtimport javaawteventimport javaxswingpublic class ColorCh static Container cp public static void main(String[] args) final JFrame frame = new JFrame(Color Choice) cp = framegetContentPane( ) cpsetLayout(new GridBagLayout()) JButton button = new JButton(select color) cpadd(button) buttonaddActionListener(new ActionListener( ) public void actionPerformed(ActionEvent e) Color c = JColorChoosershowDialog(frameChoose a color Colorred) cpsetBackground(c) ) framesetSize(200 300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Select Any Color and Press OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JOptionPane
Constructor Summary
JOptionPane() Creates a JOptionPane with a test message
JOptionPane(Object message) Creates a instance of JOptionPane to display a message using the plain-message message type and the default options delivered by the UI
JOptionPane(Object message int messageType) Creates an instance of JOptionPane to display a message with the specified message type and the default options
JOptionPane(Object message int messageType int optionType) Creates an instance of JOptionPane to display a message with the specified message type and options
JOptionPane(Object message int messageType int optionType Icon icon) Creates an instance of JOptionPane to display a message with the specified message type options and icon
JOptionPane(Object message int messageType int optionType Icon icon Object[] options) Creates an instance of JOptionPane to display a message with the specified message type icon and options
JOptionPane(Object message int messageType int optionType Icon icon Object[] options Object initialValue) Creates an instance of JOptionPane to display a message with the specified message type icon and options with the initially-selected option specified
Demo
import javaxswingJOptionPanepublic class JOptionPaneDemo public static void main(String[] args) String[] choices = BJP NCP LEFT int doAgain do int response = JOptionPaneshowOptionDialog ( null center over parent
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
How did you vote message Party Poll title in titlebar JOptionPaneYES_NO_OPTION Option type JOptionPanePLAIN_MESSAGE messageType null icon choices Options None of your business initial value ) JOptionPaneshowMessageDialog(null Response = + response) doAgain = JOptionPaneshowConfirmDialog(null Again) while(doAgain == JOptionPaneYES_OPTION) Systemexit(0)
Output
Press Any One
After Pressing OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JProgressBar
Constructor Summary
JProgressBar() Creates a horizontal progress bar that displays a border but no progress string
JProgressBar(BoundedRangeModel newModel) Creates a horizontal progress bar that uses the specified model to hold the progress bars data
JProgressBar(int orient) Creates a progress bar with the specified orientation which can be either SwingConstantsVERTICAL or SwingConstantsHORIZONTAL
JProgressBar(int min int max) Creates a horizontal progress bar with the specified minimum and maximum
JProgressBar(int orient int min int max) Creates a progress bar using the specified orientation minimum and maximum
Demo
import javaawtimport javaawteventimport javaxswingimport javaxswingtexthtmlpublic class SwingProgressBarExample final static int interval = 1000 int i JLabel label JProgressBar pb Timer timer JButton button public SwingProgressBarExample() JFrame frame = new JFrame(Swing Progress Bar) button = new JButton(Start) buttonaddActionListener(new ButtonListener()) pb = new JProgressBar(0 20) pbsetValue(0) pbsetStringPainted(true) label = new JLabel(By Prasad Sawant) JPanel panel = new JPanel() paneladd(button) paneladd(pb)copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JPanel panel1 = new JPanel() panel1setLayout(new BorderLayout()) panel1add(panel BorderLayoutNORTH) panel1add(label BorderLayoutCENTER) panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20)) framesetContentPane(panel1) framepack() framesetVisible(true) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Create a timer timer = new Timer(interval new ActionListener() public void actionPerformed(ActionEvent evt) if (i == 20) ToolkitgetDefaultToolkit()beep() timerstop() buttonsetEnabled(true) pbsetValue(0) String str = lthtmlgt + ltfont color=FF0000gt + ltbgt + Downloading completed + ltbgt + ltfontgt + lthtmlgt labelsetText(str) i = i + 1 pbsetValue(i) ) class ButtonListener implements ActionListener public void actionPerformed(ActionEvent ae) buttonsetEnabled(false) i = 0 String str = lthtmlgt + ltfont color=008000gt + ltbgt +Downloading is in process + ltbgt + ltfontgt + lthtmlgt labelsetText(str) timerstart() public static void main(String[] args) SwingProgressBarExample spb = new SwingProgressBarExample()
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
After Pressing Start Button
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JSpinner
A single line input field that lets the user select a number or an object value from an ordered sequence Spinners typically provide a pair of tiny arrow buttons for stepping through the elements of the sequence The keyboard updown arrow keys also cycle through the elements The user may also be allowed to type a (legal) value directly into the spinner Although combo boxes provide similar functionality spinners are sometimes preferred because they dont require a drop down list that can obscure important data
A JSpinners sequence value is defined by its SpinnerModel The model can be specified as a constructor argument and changed with the model property SpinnerModel classes for some common types are provided SpinnerListModel SpinnerNumberModel and SpinnerDateModel
A JSpinner has a single child component thats responsible for displaying and potentially changing the current element or value of the model which is called the editor The editor is created by the JSpinners constructor and can be changed with the editor property The JSpinners editor stays in sync with the model by listening for ChangeEvents If the user has changed the value displayed by the editor it is possible for the models value to differ from that of the editor To make sure the model has the same value as the editor use the commitEdit method
Demo
import javaawtimport javaxswingimport javaxswingeventclass JSpinnerExample public static void main(String[] args) String[] sizes = smallmediumlargeXLXXL final SpinnerListModel model = new SpinnerListModel(sizes) JSpinner jSpinner = new JSpinner(model) modeladdChangeListener(new ChangeListener() public void stateChanged(ChangeEvent e) Systemoutprintln(modelgetValue()) ) JFrame frame = new JFrame() framesetSize(100100) framegetContentPane()add(jSpinner) framepack() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Press UP KEY
Press UP KEY
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
- Relationship to AWT
-
- JProgressBar
- Demo
- import javaawt
- import javaawtevent
- import javaxswing
- import javaxswingtexthtml
- public class SwingProgressBarExample
- final static int interval = 1000
- int i
- JLabel label
- JProgressBar pb
- Timer timer
- JButton button
- public SwingProgressBarExample()
- JFrame frame = new JFrame(Swing Progress Bar)
- button = new JButton(Start)
- buttonaddActionListener(new ButtonListener())
- pb = new JProgressBar(0 20)
- pbsetValue(0)
- pbsetStringPainted(true)
- label = new JLabel(By Prasad Sawant)
- JPanel panel = new JPanel()
- paneladd(button)
- paneladd(pb)
- JPanel panel1 = new JPanel()
- panel1setLayout(new BorderLayout())
- panel1add(panel BorderLayoutNORTH)
- panel1add(label BorderLayoutCENTER)
- panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20))
- framesetContentPane(panel1)
- framepack()
- framesetVisible(true)
- framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
- Create a timer
- timer = new Timer(interval new ActionListener()
- public void actionPerformed(ActionEvent evt)
- if (i == 20)
- ToolkitgetDefaultToolkit()beep()
- timerstop()
- buttonsetEnabled(true)
- pbsetValue(0)
- String str = lthtmlgt + ltfont color=FF0000gt + ltbgt +
- Downloading completed + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- i = i + 1
- pbsetValue(i)
- )
- class ButtonListener implements ActionListener
- public void actionPerformed(ActionEvent ae)
- buttonsetEnabled(false)
- i = 0
- String str = lthtmlgt + ltfont color=008000gt + ltbgt +
- Downloading is in process + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- timerstart()
- public static void main(String[] args)
- SwingProgressBarExample spb = new SwingProgressBarExample()
- Press UP KEY
- Press UP KEY
-
Swing
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JRadioButton
Constructor Summary
JRadioButton() Creates an initially unselected radio button with no set text
JRadioButton(Action a) Creates a radiobutton where properties are taken from the Action supplied
JRadioButton(Icon icon) Creates an initially unselected radio button with the specified image but no text
JRadioButton(Icon icon boolean selected) Creates a radio button with the specified image and selection state but no text
JRadioButton(String text) Creates an unselected radio button with the specified text
JRadioButton(String text boolean selected) Creates a radio button with the specified text and selection state
JRadioButton(String text Icon icon) Creates a radio button that has the specified text and image and that is initially unselected
JRadioButton(String text Icon icon boolean selected) Creates a radio button that has the specified text image and selection state
Demoimport javaawtimport javaawteventimport javaxswingclass JRadioButtonDemo public static void main(String[] args) ActionListener listener = new ActionListener() public void actionPerformed (ActionEvent e) Systemoutprintln(egetActionCommand()) Box p = new Box(BoxLayoutY_AXIS) ButtonGroup group = new ButtonGroup() String[] sa = uglikiwipassionkumquat for(int i=0iltsalength++i) JRadioButton b = new JRadioButton(sa[i]) groupadd(b) padd(b) baddActionListener(listener) JFrame frame = new JFrame(Fruits)framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
framegetContentPane()add(p)framepack() framesetVisible(true)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JTextField
Constructor Summary
JTextField() Constructs a new TextField
JTextField(Document doc String text int columns) Constructs a new JTextField that uses the given text storage model and the given number of columns
JTextField(int columns) Constructs a new empty TextField with the specified number of columns
JTextField(String text) Constructs a new TextField initialized with the specified text
JTextField(String text int columns) Constructs a new TextField initialized with the specified text and columns
Demo
import javaawtimport javaawteventimport javaxswing
public class JLabelDemo extends JFrame
public JLabelDemo() JTextField tf=new JTextField() JFrame f = new JFrame(TextFieldDemo) Container c= getContentPane() csetLayout(new GridLayout(02)) JLabel lbl1=new JLabel(TextFieldSwingConstantsLEFT) cadd(lbl1) cadd(tf) fadd(c)
fsetSize(300100) fsetVisible(true) fsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
public static void main(String[] args) new JLabelDemo()
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JMenu
Constructor Summary
JMenu() Constructs a new JMenu with no text
JMenu(Action a) Constructs a menu whose properties are taken from the Action supplied
JMenu(String s) Constructs a new JMenu with the supplied string as its text
JMenu(String s boolean b) Constructs a new JMenu with the supplied string as its text and specified as a tear-off menu or not
Demoimport javaawtimport javaxswingclass JMenuDemo public static void main(String[] args) JMenu menu1 = new JMenu(File) menu1add(new JMenuItem(New)) menu1add(new JMenuItem(Open)) menu1add(new JSeparator()) menu1add(new JMenuItem(Save)) menu1add(new JMenuItem(Save as)) menu1add(new JSeparator()) menu1add(new JMenuItem(Exit)) JMenu menu2 = new JMenu(Edit) menu2add(new JMenuItem(Copy)) menu2add(new JSeparator()) menu2add(new JMenuItem(Paste)) menu2add(new JSeparator()) menu2add(new JMenuItem(Select ALL)) JMenuBar bar = new JMenuBar() baradd(menu1) baradd(menu2) JFrame frame = new JFrame() framesetJMenuBar(bar) framesetSize(350300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JColorChooser
Constructor Summary
JColorChooser() Creates a color chooser pane with an initial color of white
JColorChooser(Color initialColor) Creates a color chooser pane with the specified initial color
JColorChooser(ColorSelectionModel model) Creates a color chooser pane with the specified ColorSelectionModel
Demoimport javaawtimport javaawteventimport javaxswingpublic class ColorCh static Container cp public static void main(String[] args) final JFrame frame = new JFrame(Color Choice) cp = framegetContentPane( ) cpsetLayout(new GridBagLayout()) JButton button = new JButton(select color) cpadd(button) buttonaddActionListener(new ActionListener( ) public void actionPerformed(ActionEvent e) Color c = JColorChoosershowDialog(frameChoose a color Colorred) cpsetBackground(c) ) framesetSize(200 300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Select Any Color and Press OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JOptionPane
Constructor Summary
JOptionPane() Creates a JOptionPane with a test message
JOptionPane(Object message) Creates a instance of JOptionPane to display a message using the plain-message message type and the default options delivered by the UI
JOptionPane(Object message int messageType) Creates an instance of JOptionPane to display a message with the specified message type and the default options
JOptionPane(Object message int messageType int optionType) Creates an instance of JOptionPane to display a message with the specified message type and options
JOptionPane(Object message int messageType int optionType Icon icon) Creates an instance of JOptionPane to display a message with the specified message type options and icon
JOptionPane(Object message int messageType int optionType Icon icon Object[] options) Creates an instance of JOptionPane to display a message with the specified message type icon and options
JOptionPane(Object message int messageType int optionType Icon icon Object[] options Object initialValue) Creates an instance of JOptionPane to display a message with the specified message type icon and options with the initially-selected option specified
Demo
import javaxswingJOptionPanepublic class JOptionPaneDemo public static void main(String[] args) String[] choices = BJP NCP LEFT int doAgain do int response = JOptionPaneshowOptionDialog ( null center over parent
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
How did you vote message Party Poll title in titlebar JOptionPaneYES_NO_OPTION Option type JOptionPanePLAIN_MESSAGE messageType null icon choices Options None of your business initial value ) JOptionPaneshowMessageDialog(null Response = + response) doAgain = JOptionPaneshowConfirmDialog(null Again) while(doAgain == JOptionPaneYES_OPTION) Systemexit(0)
Output
Press Any One
After Pressing OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JProgressBar
Constructor Summary
JProgressBar() Creates a horizontal progress bar that displays a border but no progress string
JProgressBar(BoundedRangeModel newModel) Creates a horizontal progress bar that uses the specified model to hold the progress bars data
JProgressBar(int orient) Creates a progress bar with the specified orientation which can be either SwingConstantsVERTICAL or SwingConstantsHORIZONTAL
JProgressBar(int min int max) Creates a horizontal progress bar with the specified minimum and maximum
JProgressBar(int orient int min int max) Creates a progress bar using the specified orientation minimum and maximum
Demo
import javaawtimport javaawteventimport javaxswingimport javaxswingtexthtmlpublic class SwingProgressBarExample final static int interval = 1000 int i JLabel label JProgressBar pb Timer timer JButton button public SwingProgressBarExample() JFrame frame = new JFrame(Swing Progress Bar) button = new JButton(Start) buttonaddActionListener(new ButtonListener()) pb = new JProgressBar(0 20) pbsetValue(0) pbsetStringPainted(true) label = new JLabel(By Prasad Sawant) JPanel panel = new JPanel() paneladd(button) paneladd(pb)copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JPanel panel1 = new JPanel() panel1setLayout(new BorderLayout()) panel1add(panel BorderLayoutNORTH) panel1add(label BorderLayoutCENTER) panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20)) framesetContentPane(panel1) framepack() framesetVisible(true) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Create a timer timer = new Timer(interval new ActionListener() public void actionPerformed(ActionEvent evt) if (i == 20) ToolkitgetDefaultToolkit()beep() timerstop() buttonsetEnabled(true) pbsetValue(0) String str = lthtmlgt + ltfont color=FF0000gt + ltbgt + Downloading completed + ltbgt + ltfontgt + lthtmlgt labelsetText(str) i = i + 1 pbsetValue(i) ) class ButtonListener implements ActionListener public void actionPerformed(ActionEvent ae) buttonsetEnabled(false) i = 0 String str = lthtmlgt + ltfont color=008000gt + ltbgt +Downloading is in process + ltbgt + ltfontgt + lthtmlgt labelsetText(str) timerstart() public static void main(String[] args) SwingProgressBarExample spb = new SwingProgressBarExample()
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
After Pressing Start Button
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JSpinner
A single line input field that lets the user select a number or an object value from an ordered sequence Spinners typically provide a pair of tiny arrow buttons for stepping through the elements of the sequence The keyboard updown arrow keys also cycle through the elements The user may also be allowed to type a (legal) value directly into the spinner Although combo boxes provide similar functionality spinners are sometimes preferred because they dont require a drop down list that can obscure important data
A JSpinners sequence value is defined by its SpinnerModel The model can be specified as a constructor argument and changed with the model property SpinnerModel classes for some common types are provided SpinnerListModel SpinnerNumberModel and SpinnerDateModel
A JSpinner has a single child component thats responsible for displaying and potentially changing the current element or value of the model which is called the editor The editor is created by the JSpinners constructor and can be changed with the editor property The JSpinners editor stays in sync with the model by listening for ChangeEvents If the user has changed the value displayed by the editor it is possible for the models value to differ from that of the editor To make sure the model has the same value as the editor use the commitEdit method
Demo
import javaawtimport javaxswingimport javaxswingeventclass JSpinnerExample public static void main(String[] args) String[] sizes = smallmediumlargeXLXXL final SpinnerListModel model = new SpinnerListModel(sizes) JSpinner jSpinner = new JSpinner(model) modeladdChangeListener(new ChangeListener() public void stateChanged(ChangeEvent e) Systemoutprintln(modelgetValue()) ) JFrame frame = new JFrame() framesetSize(100100) framegetContentPane()add(jSpinner) framepack() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Press UP KEY
Press UP KEY
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
- Relationship to AWT
-
- JProgressBar
- Demo
- import javaawt
- import javaawtevent
- import javaxswing
- import javaxswingtexthtml
- public class SwingProgressBarExample
- final static int interval = 1000
- int i
- JLabel label
- JProgressBar pb
- Timer timer
- JButton button
- public SwingProgressBarExample()
- JFrame frame = new JFrame(Swing Progress Bar)
- button = new JButton(Start)
- buttonaddActionListener(new ButtonListener())
- pb = new JProgressBar(0 20)
- pbsetValue(0)
- pbsetStringPainted(true)
- label = new JLabel(By Prasad Sawant)
- JPanel panel = new JPanel()
- paneladd(button)
- paneladd(pb)
- JPanel panel1 = new JPanel()
- panel1setLayout(new BorderLayout())
- panel1add(panel BorderLayoutNORTH)
- panel1add(label BorderLayoutCENTER)
- panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20))
- framesetContentPane(panel1)
- framepack()
- framesetVisible(true)
- framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
- Create a timer
- timer = new Timer(interval new ActionListener()
- public void actionPerformed(ActionEvent evt)
- if (i == 20)
- ToolkitgetDefaultToolkit()beep()
- timerstop()
- buttonsetEnabled(true)
- pbsetValue(0)
- String str = lthtmlgt + ltfont color=FF0000gt + ltbgt +
- Downloading completed + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- i = i + 1
- pbsetValue(i)
- )
- class ButtonListener implements ActionListener
- public void actionPerformed(ActionEvent ae)
- buttonsetEnabled(false)
- i = 0
- String str = lthtmlgt + ltfont color=008000gt + ltbgt +
- Downloading is in process + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- timerstart()
- public static void main(String[] args)
- SwingProgressBarExample spb = new SwingProgressBarExample()
- Press UP KEY
- Press UP KEY
-
Swing
JRadioButton
Constructor Summary
JRadioButton() Creates an initially unselected radio button with no set text
JRadioButton(Action a) Creates a radiobutton where properties are taken from the Action supplied
JRadioButton(Icon icon) Creates an initially unselected radio button with the specified image but no text
JRadioButton(Icon icon boolean selected) Creates a radio button with the specified image and selection state but no text
JRadioButton(String text) Creates an unselected radio button with the specified text
JRadioButton(String text boolean selected) Creates a radio button with the specified text and selection state
JRadioButton(String text Icon icon) Creates a radio button that has the specified text and image and that is initially unselected
JRadioButton(String text Icon icon boolean selected) Creates a radio button that has the specified text image and selection state
Demoimport javaawtimport javaawteventimport javaxswingclass JRadioButtonDemo public static void main(String[] args) ActionListener listener = new ActionListener() public void actionPerformed (ActionEvent e) Systemoutprintln(egetActionCommand()) Box p = new Box(BoxLayoutY_AXIS) ButtonGroup group = new ButtonGroup() String[] sa = uglikiwipassionkumquat for(int i=0iltsalength++i) JRadioButton b = new JRadioButton(sa[i]) groupadd(b) padd(b) baddActionListener(listener) JFrame frame = new JFrame(Fruits)framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
framegetContentPane()add(p)framepack() framesetVisible(true)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JTextField
Constructor Summary
JTextField() Constructs a new TextField
JTextField(Document doc String text int columns) Constructs a new JTextField that uses the given text storage model and the given number of columns
JTextField(int columns) Constructs a new empty TextField with the specified number of columns
JTextField(String text) Constructs a new TextField initialized with the specified text
JTextField(String text int columns) Constructs a new TextField initialized with the specified text and columns
Demo
import javaawtimport javaawteventimport javaxswing
public class JLabelDemo extends JFrame
public JLabelDemo() JTextField tf=new JTextField() JFrame f = new JFrame(TextFieldDemo) Container c= getContentPane() csetLayout(new GridLayout(02)) JLabel lbl1=new JLabel(TextFieldSwingConstantsLEFT) cadd(lbl1) cadd(tf) fadd(c)
fsetSize(300100) fsetVisible(true) fsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
public static void main(String[] args) new JLabelDemo()
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JMenu
Constructor Summary
JMenu() Constructs a new JMenu with no text
JMenu(Action a) Constructs a menu whose properties are taken from the Action supplied
JMenu(String s) Constructs a new JMenu with the supplied string as its text
JMenu(String s boolean b) Constructs a new JMenu with the supplied string as its text and specified as a tear-off menu or not
Demoimport javaawtimport javaxswingclass JMenuDemo public static void main(String[] args) JMenu menu1 = new JMenu(File) menu1add(new JMenuItem(New)) menu1add(new JMenuItem(Open)) menu1add(new JSeparator()) menu1add(new JMenuItem(Save)) menu1add(new JMenuItem(Save as)) menu1add(new JSeparator()) menu1add(new JMenuItem(Exit)) JMenu menu2 = new JMenu(Edit) menu2add(new JMenuItem(Copy)) menu2add(new JSeparator()) menu2add(new JMenuItem(Paste)) menu2add(new JSeparator()) menu2add(new JMenuItem(Select ALL)) JMenuBar bar = new JMenuBar() baradd(menu1) baradd(menu2) JFrame frame = new JFrame() framesetJMenuBar(bar) framesetSize(350300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JColorChooser
Constructor Summary
JColorChooser() Creates a color chooser pane with an initial color of white
JColorChooser(Color initialColor) Creates a color chooser pane with the specified initial color
JColorChooser(ColorSelectionModel model) Creates a color chooser pane with the specified ColorSelectionModel
Demoimport javaawtimport javaawteventimport javaxswingpublic class ColorCh static Container cp public static void main(String[] args) final JFrame frame = new JFrame(Color Choice) cp = framegetContentPane( ) cpsetLayout(new GridBagLayout()) JButton button = new JButton(select color) cpadd(button) buttonaddActionListener(new ActionListener( ) public void actionPerformed(ActionEvent e) Color c = JColorChoosershowDialog(frameChoose a color Colorred) cpsetBackground(c) ) framesetSize(200 300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Select Any Color and Press OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JOptionPane
Constructor Summary
JOptionPane() Creates a JOptionPane with a test message
JOptionPane(Object message) Creates a instance of JOptionPane to display a message using the plain-message message type and the default options delivered by the UI
JOptionPane(Object message int messageType) Creates an instance of JOptionPane to display a message with the specified message type and the default options
JOptionPane(Object message int messageType int optionType) Creates an instance of JOptionPane to display a message with the specified message type and options
JOptionPane(Object message int messageType int optionType Icon icon) Creates an instance of JOptionPane to display a message with the specified message type options and icon
JOptionPane(Object message int messageType int optionType Icon icon Object[] options) Creates an instance of JOptionPane to display a message with the specified message type icon and options
JOptionPane(Object message int messageType int optionType Icon icon Object[] options Object initialValue) Creates an instance of JOptionPane to display a message with the specified message type icon and options with the initially-selected option specified
Demo
import javaxswingJOptionPanepublic class JOptionPaneDemo public static void main(String[] args) String[] choices = BJP NCP LEFT int doAgain do int response = JOptionPaneshowOptionDialog ( null center over parent
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
How did you vote message Party Poll title in titlebar JOptionPaneYES_NO_OPTION Option type JOptionPanePLAIN_MESSAGE messageType null icon choices Options None of your business initial value ) JOptionPaneshowMessageDialog(null Response = + response) doAgain = JOptionPaneshowConfirmDialog(null Again) while(doAgain == JOptionPaneYES_OPTION) Systemexit(0)
Output
Press Any One
After Pressing OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JProgressBar
Constructor Summary
JProgressBar() Creates a horizontal progress bar that displays a border but no progress string
JProgressBar(BoundedRangeModel newModel) Creates a horizontal progress bar that uses the specified model to hold the progress bars data
JProgressBar(int orient) Creates a progress bar with the specified orientation which can be either SwingConstantsVERTICAL or SwingConstantsHORIZONTAL
JProgressBar(int min int max) Creates a horizontal progress bar with the specified minimum and maximum
JProgressBar(int orient int min int max) Creates a progress bar using the specified orientation minimum and maximum
Demo
import javaawtimport javaawteventimport javaxswingimport javaxswingtexthtmlpublic class SwingProgressBarExample final static int interval = 1000 int i JLabel label JProgressBar pb Timer timer JButton button public SwingProgressBarExample() JFrame frame = new JFrame(Swing Progress Bar) button = new JButton(Start) buttonaddActionListener(new ButtonListener()) pb = new JProgressBar(0 20) pbsetValue(0) pbsetStringPainted(true) label = new JLabel(By Prasad Sawant) JPanel panel = new JPanel() paneladd(button) paneladd(pb)copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JPanel panel1 = new JPanel() panel1setLayout(new BorderLayout()) panel1add(panel BorderLayoutNORTH) panel1add(label BorderLayoutCENTER) panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20)) framesetContentPane(panel1) framepack() framesetVisible(true) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Create a timer timer = new Timer(interval new ActionListener() public void actionPerformed(ActionEvent evt) if (i == 20) ToolkitgetDefaultToolkit()beep() timerstop() buttonsetEnabled(true) pbsetValue(0) String str = lthtmlgt + ltfont color=FF0000gt + ltbgt + Downloading completed + ltbgt + ltfontgt + lthtmlgt labelsetText(str) i = i + 1 pbsetValue(i) ) class ButtonListener implements ActionListener public void actionPerformed(ActionEvent ae) buttonsetEnabled(false) i = 0 String str = lthtmlgt + ltfont color=008000gt + ltbgt +Downloading is in process + ltbgt + ltfontgt + lthtmlgt labelsetText(str) timerstart() public static void main(String[] args) SwingProgressBarExample spb = new SwingProgressBarExample()
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
After Pressing Start Button
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JSpinner
A single line input field that lets the user select a number or an object value from an ordered sequence Spinners typically provide a pair of tiny arrow buttons for stepping through the elements of the sequence The keyboard updown arrow keys also cycle through the elements The user may also be allowed to type a (legal) value directly into the spinner Although combo boxes provide similar functionality spinners are sometimes preferred because they dont require a drop down list that can obscure important data
A JSpinners sequence value is defined by its SpinnerModel The model can be specified as a constructor argument and changed with the model property SpinnerModel classes for some common types are provided SpinnerListModel SpinnerNumberModel and SpinnerDateModel
A JSpinner has a single child component thats responsible for displaying and potentially changing the current element or value of the model which is called the editor The editor is created by the JSpinners constructor and can be changed with the editor property The JSpinners editor stays in sync with the model by listening for ChangeEvents If the user has changed the value displayed by the editor it is possible for the models value to differ from that of the editor To make sure the model has the same value as the editor use the commitEdit method
Demo
import javaawtimport javaxswingimport javaxswingeventclass JSpinnerExample public static void main(String[] args) String[] sizes = smallmediumlargeXLXXL final SpinnerListModel model = new SpinnerListModel(sizes) JSpinner jSpinner = new JSpinner(model) modeladdChangeListener(new ChangeListener() public void stateChanged(ChangeEvent e) Systemoutprintln(modelgetValue()) ) JFrame frame = new JFrame() framesetSize(100100) framegetContentPane()add(jSpinner) framepack() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Press UP KEY
Press UP KEY
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
- Relationship to AWT
-
- JProgressBar
- Demo
- import javaawt
- import javaawtevent
- import javaxswing
- import javaxswingtexthtml
- public class SwingProgressBarExample
- final static int interval = 1000
- int i
- JLabel label
- JProgressBar pb
- Timer timer
- JButton button
- public SwingProgressBarExample()
- JFrame frame = new JFrame(Swing Progress Bar)
- button = new JButton(Start)
- buttonaddActionListener(new ButtonListener())
- pb = new JProgressBar(0 20)
- pbsetValue(0)
- pbsetStringPainted(true)
- label = new JLabel(By Prasad Sawant)
- JPanel panel = new JPanel()
- paneladd(button)
- paneladd(pb)
- JPanel panel1 = new JPanel()
- panel1setLayout(new BorderLayout())
- panel1add(panel BorderLayoutNORTH)
- panel1add(label BorderLayoutCENTER)
- panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20))
- framesetContentPane(panel1)
- framepack()
- framesetVisible(true)
- framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
- Create a timer
- timer = new Timer(interval new ActionListener()
- public void actionPerformed(ActionEvent evt)
- if (i == 20)
- ToolkitgetDefaultToolkit()beep()
- timerstop()
- buttonsetEnabled(true)
- pbsetValue(0)
- String str = lthtmlgt + ltfont color=FF0000gt + ltbgt +
- Downloading completed + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- i = i + 1
- pbsetValue(i)
- )
- class ButtonListener implements ActionListener
- public void actionPerformed(ActionEvent ae)
- buttonsetEnabled(false)
- i = 0
- String str = lthtmlgt + ltfont color=008000gt + ltbgt +
- Downloading is in process + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- timerstart()
- public static void main(String[] args)
- SwingProgressBarExample spb = new SwingProgressBarExample()
- Press UP KEY
- Press UP KEY
-
Swing
framegetContentPane()add(p)framepack() framesetVisible(true)
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JTextField
Constructor Summary
JTextField() Constructs a new TextField
JTextField(Document doc String text int columns) Constructs a new JTextField that uses the given text storage model and the given number of columns
JTextField(int columns) Constructs a new empty TextField with the specified number of columns
JTextField(String text) Constructs a new TextField initialized with the specified text
JTextField(String text int columns) Constructs a new TextField initialized with the specified text and columns
Demo
import javaawtimport javaawteventimport javaxswing
public class JLabelDemo extends JFrame
public JLabelDemo() JTextField tf=new JTextField() JFrame f = new JFrame(TextFieldDemo) Container c= getContentPane() csetLayout(new GridLayout(02)) JLabel lbl1=new JLabel(TextFieldSwingConstantsLEFT) cadd(lbl1) cadd(tf) fadd(c)
fsetSize(300100) fsetVisible(true) fsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
public static void main(String[] args) new JLabelDemo()
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JMenu
Constructor Summary
JMenu() Constructs a new JMenu with no text
JMenu(Action a) Constructs a menu whose properties are taken from the Action supplied
JMenu(String s) Constructs a new JMenu with the supplied string as its text
JMenu(String s boolean b) Constructs a new JMenu with the supplied string as its text and specified as a tear-off menu or not
Demoimport javaawtimport javaxswingclass JMenuDemo public static void main(String[] args) JMenu menu1 = new JMenu(File) menu1add(new JMenuItem(New)) menu1add(new JMenuItem(Open)) menu1add(new JSeparator()) menu1add(new JMenuItem(Save)) menu1add(new JMenuItem(Save as)) menu1add(new JSeparator()) menu1add(new JMenuItem(Exit)) JMenu menu2 = new JMenu(Edit) menu2add(new JMenuItem(Copy)) menu2add(new JSeparator()) menu2add(new JMenuItem(Paste)) menu2add(new JSeparator()) menu2add(new JMenuItem(Select ALL)) JMenuBar bar = new JMenuBar() baradd(menu1) baradd(menu2) JFrame frame = new JFrame() framesetJMenuBar(bar) framesetSize(350300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JColorChooser
Constructor Summary
JColorChooser() Creates a color chooser pane with an initial color of white
JColorChooser(Color initialColor) Creates a color chooser pane with the specified initial color
JColorChooser(ColorSelectionModel model) Creates a color chooser pane with the specified ColorSelectionModel
Demoimport javaawtimport javaawteventimport javaxswingpublic class ColorCh static Container cp public static void main(String[] args) final JFrame frame = new JFrame(Color Choice) cp = framegetContentPane( ) cpsetLayout(new GridBagLayout()) JButton button = new JButton(select color) cpadd(button) buttonaddActionListener(new ActionListener( ) public void actionPerformed(ActionEvent e) Color c = JColorChoosershowDialog(frameChoose a color Colorred) cpsetBackground(c) ) framesetSize(200 300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Select Any Color and Press OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JOptionPane
Constructor Summary
JOptionPane() Creates a JOptionPane with a test message
JOptionPane(Object message) Creates a instance of JOptionPane to display a message using the plain-message message type and the default options delivered by the UI
JOptionPane(Object message int messageType) Creates an instance of JOptionPane to display a message with the specified message type and the default options
JOptionPane(Object message int messageType int optionType) Creates an instance of JOptionPane to display a message with the specified message type and options
JOptionPane(Object message int messageType int optionType Icon icon) Creates an instance of JOptionPane to display a message with the specified message type options and icon
JOptionPane(Object message int messageType int optionType Icon icon Object[] options) Creates an instance of JOptionPane to display a message with the specified message type icon and options
JOptionPane(Object message int messageType int optionType Icon icon Object[] options Object initialValue) Creates an instance of JOptionPane to display a message with the specified message type icon and options with the initially-selected option specified
Demo
import javaxswingJOptionPanepublic class JOptionPaneDemo public static void main(String[] args) String[] choices = BJP NCP LEFT int doAgain do int response = JOptionPaneshowOptionDialog ( null center over parent
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
How did you vote message Party Poll title in titlebar JOptionPaneYES_NO_OPTION Option type JOptionPanePLAIN_MESSAGE messageType null icon choices Options None of your business initial value ) JOptionPaneshowMessageDialog(null Response = + response) doAgain = JOptionPaneshowConfirmDialog(null Again) while(doAgain == JOptionPaneYES_OPTION) Systemexit(0)
Output
Press Any One
After Pressing OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JProgressBar
Constructor Summary
JProgressBar() Creates a horizontal progress bar that displays a border but no progress string
JProgressBar(BoundedRangeModel newModel) Creates a horizontal progress bar that uses the specified model to hold the progress bars data
JProgressBar(int orient) Creates a progress bar with the specified orientation which can be either SwingConstantsVERTICAL or SwingConstantsHORIZONTAL
JProgressBar(int min int max) Creates a horizontal progress bar with the specified minimum and maximum
JProgressBar(int orient int min int max) Creates a progress bar using the specified orientation minimum and maximum
Demo
import javaawtimport javaawteventimport javaxswingimport javaxswingtexthtmlpublic class SwingProgressBarExample final static int interval = 1000 int i JLabel label JProgressBar pb Timer timer JButton button public SwingProgressBarExample() JFrame frame = new JFrame(Swing Progress Bar) button = new JButton(Start) buttonaddActionListener(new ButtonListener()) pb = new JProgressBar(0 20) pbsetValue(0) pbsetStringPainted(true) label = new JLabel(By Prasad Sawant) JPanel panel = new JPanel() paneladd(button) paneladd(pb)copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JPanel panel1 = new JPanel() panel1setLayout(new BorderLayout()) panel1add(panel BorderLayoutNORTH) panel1add(label BorderLayoutCENTER) panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20)) framesetContentPane(panel1) framepack() framesetVisible(true) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Create a timer timer = new Timer(interval new ActionListener() public void actionPerformed(ActionEvent evt) if (i == 20) ToolkitgetDefaultToolkit()beep() timerstop() buttonsetEnabled(true) pbsetValue(0) String str = lthtmlgt + ltfont color=FF0000gt + ltbgt + Downloading completed + ltbgt + ltfontgt + lthtmlgt labelsetText(str) i = i + 1 pbsetValue(i) ) class ButtonListener implements ActionListener public void actionPerformed(ActionEvent ae) buttonsetEnabled(false) i = 0 String str = lthtmlgt + ltfont color=008000gt + ltbgt +Downloading is in process + ltbgt + ltfontgt + lthtmlgt labelsetText(str) timerstart() public static void main(String[] args) SwingProgressBarExample spb = new SwingProgressBarExample()
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
After Pressing Start Button
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JSpinner
A single line input field that lets the user select a number or an object value from an ordered sequence Spinners typically provide a pair of tiny arrow buttons for stepping through the elements of the sequence The keyboard updown arrow keys also cycle through the elements The user may also be allowed to type a (legal) value directly into the spinner Although combo boxes provide similar functionality spinners are sometimes preferred because they dont require a drop down list that can obscure important data
A JSpinners sequence value is defined by its SpinnerModel The model can be specified as a constructor argument and changed with the model property SpinnerModel classes for some common types are provided SpinnerListModel SpinnerNumberModel and SpinnerDateModel
A JSpinner has a single child component thats responsible for displaying and potentially changing the current element or value of the model which is called the editor The editor is created by the JSpinners constructor and can be changed with the editor property The JSpinners editor stays in sync with the model by listening for ChangeEvents If the user has changed the value displayed by the editor it is possible for the models value to differ from that of the editor To make sure the model has the same value as the editor use the commitEdit method
Demo
import javaawtimport javaxswingimport javaxswingeventclass JSpinnerExample public static void main(String[] args) String[] sizes = smallmediumlargeXLXXL final SpinnerListModel model = new SpinnerListModel(sizes) JSpinner jSpinner = new JSpinner(model) modeladdChangeListener(new ChangeListener() public void stateChanged(ChangeEvent e) Systemoutprintln(modelgetValue()) ) JFrame frame = new JFrame() framesetSize(100100) framegetContentPane()add(jSpinner) framepack() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Press UP KEY
Press UP KEY
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
- Relationship to AWT
-
- JProgressBar
- Demo
- import javaawt
- import javaawtevent
- import javaxswing
- import javaxswingtexthtml
- public class SwingProgressBarExample
- final static int interval = 1000
- int i
- JLabel label
- JProgressBar pb
- Timer timer
- JButton button
- public SwingProgressBarExample()
- JFrame frame = new JFrame(Swing Progress Bar)
- button = new JButton(Start)
- buttonaddActionListener(new ButtonListener())
- pb = new JProgressBar(0 20)
- pbsetValue(0)
- pbsetStringPainted(true)
- label = new JLabel(By Prasad Sawant)
- JPanel panel = new JPanel()
- paneladd(button)
- paneladd(pb)
- JPanel panel1 = new JPanel()
- panel1setLayout(new BorderLayout())
- panel1add(panel BorderLayoutNORTH)
- panel1add(label BorderLayoutCENTER)
- panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20))
- framesetContentPane(panel1)
- framepack()
- framesetVisible(true)
- framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
- Create a timer
- timer = new Timer(interval new ActionListener()
- public void actionPerformed(ActionEvent evt)
- if (i == 20)
- ToolkitgetDefaultToolkit()beep()
- timerstop()
- buttonsetEnabled(true)
- pbsetValue(0)
- String str = lthtmlgt + ltfont color=FF0000gt + ltbgt +
- Downloading completed + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- i = i + 1
- pbsetValue(i)
- )
- class ButtonListener implements ActionListener
- public void actionPerformed(ActionEvent ae)
- buttonsetEnabled(false)
- i = 0
- String str = lthtmlgt + ltfont color=008000gt + ltbgt +
- Downloading is in process + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- timerstart()
- public static void main(String[] args)
- SwingProgressBarExample spb = new SwingProgressBarExample()
- Press UP KEY
- Press UP KEY
-
Swing
JTextField
Constructor Summary
JTextField() Constructs a new TextField
JTextField(Document doc String text int columns) Constructs a new JTextField that uses the given text storage model and the given number of columns
JTextField(int columns) Constructs a new empty TextField with the specified number of columns
JTextField(String text) Constructs a new TextField initialized with the specified text
JTextField(String text int columns) Constructs a new TextField initialized with the specified text and columns
Demo
import javaawtimport javaawteventimport javaxswing
public class JLabelDemo extends JFrame
public JLabelDemo() JTextField tf=new JTextField() JFrame f = new JFrame(TextFieldDemo) Container c= getContentPane() csetLayout(new GridLayout(02)) JLabel lbl1=new JLabel(TextFieldSwingConstantsLEFT) cadd(lbl1) cadd(tf) fadd(c)
fsetSize(300100) fsetVisible(true) fsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
public static void main(String[] args) new JLabelDemo()
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JMenu
Constructor Summary
JMenu() Constructs a new JMenu with no text
JMenu(Action a) Constructs a menu whose properties are taken from the Action supplied
JMenu(String s) Constructs a new JMenu with the supplied string as its text
JMenu(String s boolean b) Constructs a new JMenu with the supplied string as its text and specified as a tear-off menu or not
Demoimport javaawtimport javaxswingclass JMenuDemo public static void main(String[] args) JMenu menu1 = new JMenu(File) menu1add(new JMenuItem(New)) menu1add(new JMenuItem(Open)) menu1add(new JSeparator()) menu1add(new JMenuItem(Save)) menu1add(new JMenuItem(Save as)) menu1add(new JSeparator()) menu1add(new JMenuItem(Exit)) JMenu menu2 = new JMenu(Edit) menu2add(new JMenuItem(Copy)) menu2add(new JSeparator()) menu2add(new JMenuItem(Paste)) menu2add(new JSeparator()) menu2add(new JMenuItem(Select ALL)) JMenuBar bar = new JMenuBar() baradd(menu1) baradd(menu2) JFrame frame = new JFrame() framesetJMenuBar(bar) framesetSize(350300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JColorChooser
Constructor Summary
JColorChooser() Creates a color chooser pane with an initial color of white
JColorChooser(Color initialColor) Creates a color chooser pane with the specified initial color
JColorChooser(ColorSelectionModel model) Creates a color chooser pane with the specified ColorSelectionModel
Demoimport javaawtimport javaawteventimport javaxswingpublic class ColorCh static Container cp public static void main(String[] args) final JFrame frame = new JFrame(Color Choice) cp = framegetContentPane( ) cpsetLayout(new GridBagLayout()) JButton button = new JButton(select color) cpadd(button) buttonaddActionListener(new ActionListener( ) public void actionPerformed(ActionEvent e) Color c = JColorChoosershowDialog(frameChoose a color Colorred) cpsetBackground(c) ) framesetSize(200 300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Select Any Color and Press OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JOptionPane
Constructor Summary
JOptionPane() Creates a JOptionPane with a test message
JOptionPane(Object message) Creates a instance of JOptionPane to display a message using the plain-message message type and the default options delivered by the UI
JOptionPane(Object message int messageType) Creates an instance of JOptionPane to display a message with the specified message type and the default options
JOptionPane(Object message int messageType int optionType) Creates an instance of JOptionPane to display a message with the specified message type and options
JOptionPane(Object message int messageType int optionType Icon icon) Creates an instance of JOptionPane to display a message with the specified message type options and icon
JOptionPane(Object message int messageType int optionType Icon icon Object[] options) Creates an instance of JOptionPane to display a message with the specified message type icon and options
JOptionPane(Object message int messageType int optionType Icon icon Object[] options Object initialValue) Creates an instance of JOptionPane to display a message with the specified message type icon and options with the initially-selected option specified
Demo
import javaxswingJOptionPanepublic class JOptionPaneDemo public static void main(String[] args) String[] choices = BJP NCP LEFT int doAgain do int response = JOptionPaneshowOptionDialog ( null center over parent
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
How did you vote message Party Poll title in titlebar JOptionPaneYES_NO_OPTION Option type JOptionPanePLAIN_MESSAGE messageType null icon choices Options None of your business initial value ) JOptionPaneshowMessageDialog(null Response = + response) doAgain = JOptionPaneshowConfirmDialog(null Again) while(doAgain == JOptionPaneYES_OPTION) Systemexit(0)
Output
Press Any One
After Pressing OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JProgressBar
Constructor Summary
JProgressBar() Creates a horizontal progress bar that displays a border but no progress string
JProgressBar(BoundedRangeModel newModel) Creates a horizontal progress bar that uses the specified model to hold the progress bars data
JProgressBar(int orient) Creates a progress bar with the specified orientation which can be either SwingConstantsVERTICAL or SwingConstantsHORIZONTAL
JProgressBar(int min int max) Creates a horizontal progress bar with the specified minimum and maximum
JProgressBar(int orient int min int max) Creates a progress bar using the specified orientation minimum and maximum
Demo
import javaawtimport javaawteventimport javaxswingimport javaxswingtexthtmlpublic class SwingProgressBarExample final static int interval = 1000 int i JLabel label JProgressBar pb Timer timer JButton button public SwingProgressBarExample() JFrame frame = new JFrame(Swing Progress Bar) button = new JButton(Start) buttonaddActionListener(new ButtonListener()) pb = new JProgressBar(0 20) pbsetValue(0) pbsetStringPainted(true) label = new JLabel(By Prasad Sawant) JPanel panel = new JPanel() paneladd(button) paneladd(pb)copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JPanel panel1 = new JPanel() panel1setLayout(new BorderLayout()) panel1add(panel BorderLayoutNORTH) panel1add(label BorderLayoutCENTER) panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20)) framesetContentPane(panel1) framepack() framesetVisible(true) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Create a timer timer = new Timer(interval new ActionListener() public void actionPerformed(ActionEvent evt) if (i == 20) ToolkitgetDefaultToolkit()beep() timerstop() buttonsetEnabled(true) pbsetValue(0) String str = lthtmlgt + ltfont color=FF0000gt + ltbgt + Downloading completed + ltbgt + ltfontgt + lthtmlgt labelsetText(str) i = i + 1 pbsetValue(i) ) class ButtonListener implements ActionListener public void actionPerformed(ActionEvent ae) buttonsetEnabled(false) i = 0 String str = lthtmlgt + ltfont color=008000gt + ltbgt +Downloading is in process + ltbgt + ltfontgt + lthtmlgt labelsetText(str) timerstart() public static void main(String[] args) SwingProgressBarExample spb = new SwingProgressBarExample()
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
After Pressing Start Button
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JSpinner
A single line input field that lets the user select a number or an object value from an ordered sequence Spinners typically provide a pair of tiny arrow buttons for stepping through the elements of the sequence The keyboard updown arrow keys also cycle through the elements The user may also be allowed to type a (legal) value directly into the spinner Although combo boxes provide similar functionality spinners are sometimes preferred because they dont require a drop down list that can obscure important data
A JSpinners sequence value is defined by its SpinnerModel The model can be specified as a constructor argument and changed with the model property SpinnerModel classes for some common types are provided SpinnerListModel SpinnerNumberModel and SpinnerDateModel
A JSpinner has a single child component thats responsible for displaying and potentially changing the current element or value of the model which is called the editor The editor is created by the JSpinners constructor and can be changed with the editor property The JSpinners editor stays in sync with the model by listening for ChangeEvents If the user has changed the value displayed by the editor it is possible for the models value to differ from that of the editor To make sure the model has the same value as the editor use the commitEdit method
Demo
import javaawtimport javaxswingimport javaxswingeventclass JSpinnerExample public static void main(String[] args) String[] sizes = smallmediumlargeXLXXL final SpinnerListModel model = new SpinnerListModel(sizes) JSpinner jSpinner = new JSpinner(model) modeladdChangeListener(new ChangeListener() public void stateChanged(ChangeEvent e) Systemoutprintln(modelgetValue()) ) JFrame frame = new JFrame() framesetSize(100100) framegetContentPane()add(jSpinner) framepack() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Press UP KEY
Press UP KEY
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
- Relationship to AWT
-
- JProgressBar
- Demo
- import javaawt
- import javaawtevent
- import javaxswing
- import javaxswingtexthtml
- public class SwingProgressBarExample
- final static int interval = 1000
- int i
- JLabel label
- JProgressBar pb
- Timer timer
- JButton button
- public SwingProgressBarExample()
- JFrame frame = new JFrame(Swing Progress Bar)
- button = new JButton(Start)
- buttonaddActionListener(new ButtonListener())
- pb = new JProgressBar(0 20)
- pbsetValue(0)
- pbsetStringPainted(true)
- label = new JLabel(By Prasad Sawant)
- JPanel panel = new JPanel()
- paneladd(button)
- paneladd(pb)
- JPanel panel1 = new JPanel()
- panel1setLayout(new BorderLayout())
- panel1add(panel BorderLayoutNORTH)
- panel1add(label BorderLayoutCENTER)
- panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20))
- framesetContentPane(panel1)
- framepack()
- framesetVisible(true)
- framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
- Create a timer
- timer = new Timer(interval new ActionListener()
- public void actionPerformed(ActionEvent evt)
- if (i == 20)
- ToolkitgetDefaultToolkit()beep()
- timerstop()
- buttonsetEnabled(true)
- pbsetValue(0)
- String str = lthtmlgt + ltfont color=FF0000gt + ltbgt +
- Downloading completed + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- i = i + 1
- pbsetValue(i)
- )
- class ButtonListener implements ActionListener
- public void actionPerformed(ActionEvent ae)
- buttonsetEnabled(false)
- i = 0
- String str = lthtmlgt + ltfont color=008000gt + ltbgt +
- Downloading is in process + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- timerstart()
- public static void main(String[] args)
- SwingProgressBarExample spb = new SwingProgressBarExample()
- Press UP KEY
- Press UP KEY
-
Swing
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JMenu
Constructor Summary
JMenu() Constructs a new JMenu with no text
JMenu(Action a) Constructs a menu whose properties are taken from the Action supplied
JMenu(String s) Constructs a new JMenu with the supplied string as its text
JMenu(String s boolean b) Constructs a new JMenu with the supplied string as its text and specified as a tear-off menu or not
Demoimport javaawtimport javaxswingclass JMenuDemo public static void main(String[] args) JMenu menu1 = new JMenu(File) menu1add(new JMenuItem(New)) menu1add(new JMenuItem(Open)) menu1add(new JSeparator()) menu1add(new JMenuItem(Save)) menu1add(new JMenuItem(Save as)) menu1add(new JSeparator()) menu1add(new JMenuItem(Exit)) JMenu menu2 = new JMenu(Edit) menu2add(new JMenuItem(Copy)) menu2add(new JSeparator()) menu2add(new JMenuItem(Paste)) menu2add(new JSeparator()) menu2add(new JMenuItem(Select ALL)) JMenuBar bar = new JMenuBar() baradd(menu1) baradd(menu2) JFrame frame = new JFrame() framesetJMenuBar(bar) framesetSize(350300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JColorChooser
Constructor Summary
JColorChooser() Creates a color chooser pane with an initial color of white
JColorChooser(Color initialColor) Creates a color chooser pane with the specified initial color
JColorChooser(ColorSelectionModel model) Creates a color chooser pane with the specified ColorSelectionModel
Demoimport javaawtimport javaawteventimport javaxswingpublic class ColorCh static Container cp public static void main(String[] args) final JFrame frame = new JFrame(Color Choice) cp = framegetContentPane( ) cpsetLayout(new GridBagLayout()) JButton button = new JButton(select color) cpadd(button) buttonaddActionListener(new ActionListener( ) public void actionPerformed(ActionEvent e) Color c = JColorChoosershowDialog(frameChoose a color Colorred) cpsetBackground(c) ) framesetSize(200 300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Select Any Color and Press OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JOptionPane
Constructor Summary
JOptionPane() Creates a JOptionPane with a test message
JOptionPane(Object message) Creates a instance of JOptionPane to display a message using the plain-message message type and the default options delivered by the UI
JOptionPane(Object message int messageType) Creates an instance of JOptionPane to display a message with the specified message type and the default options
JOptionPane(Object message int messageType int optionType) Creates an instance of JOptionPane to display a message with the specified message type and options
JOptionPane(Object message int messageType int optionType Icon icon) Creates an instance of JOptionPane to display a message with the specified message type options and icon
JOptionPane(Object message int messageType int optionType Icon icon Object[] options) Creates an instance of JOptionPane to display a message with the specified message type icon and options
JOptionPane(Object message int messageType int optionType Icon icon Object[] options Object initialValue) Creates an instance of JOptionPane to display a message with the specified message type icon and options with the initially-selected option specified
Demo
import javaxswingJOptionPanepublic class JOptionPaneDemo public static void main(String[] args) String[] choices = BJP NCP LEFT int doAgain do int response = JOptionPaneshowOptionDialog ( null center over parent
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
How did you vote message Party Poll title in titlebar JOptionPaneYES_NO_OPTION Option type JOptionPanePLAIN_MESSAGE messageType null icon choices Options None of your business initial value ) JOptionPaneshowMessageDialog(null Response = + response) doAgain = JOptionPaneshowConfirmDialog(null Again) while(doAgain == JOptionPaneYES_OPTION) Systemexit(0)
Output
Press Any One
After Pressing OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JProgressBar
Constructor Summary
JProgressBar() Creates a horizontal progress bar that displays a border but no progress string
JProgressBar(BoundedRangeModel newModel) Creates a horizontal progress bar that uses the specified model to hold the progress bars data
JProgressBar(int orient) Creates a progress bar with the specified orientation which can be either SwingConstantsVERTICAL or SwingConstantsHORIZONTAL
JProgressBar(int min int max) Creates a horizontal progress bar with the specified minimum and maximum
JProgressBar(int orient int min int max) Creates a progress bar using the specified orientation minimum and maximum
Demo
import javaawtimport javaawteventimport javaxswingimport javaxswingtexthtmlpublic class SwingProgressBarExample final static int interval = 1000 int i JLabel label JProgressBar pb Timer timer JButton button public SwingProgressBarExample() JFrame frame = new JFrame(Swing Progress Bar) button = new JButton(Start) buttonaddActionListener(new ButtonListener()) pb = new JProgressBar(0 20) pbsetValue(0) pbsetStringPainted(true) label = new JLabel(By Prasad Sawant) JPanel panel = new JPanel() paneladd(button) paneladd(pb)copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JPanel panel1 = new JPanel() panel1setLayout(new BorderLayout()) panel1add(panel BorderLayoutNORTH) panel1add(label BorderLayoutCENTER) panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20)) framesetContentPane(panel1) framepack() framesetVisible(true) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Create a timer timer = new Timer(interval new ActionListener() public void actionPerformed(ActionEvent evt) if (i == 20) ToolkitgetDefaultToolkit()beep() timerstop() buttonsetEnabled(true) pbsetValue(0) String str = lthtmlgt + ltfont color=FF0000gt + ltbgt + Downloading completed + ltbgt + ltfontgt + lthtmlgt labelsetText(str) i = i + 1 pbsetValue(i) ) class ButtonListener implements ActionListener public void actionPerformed(ActionEvent ae) buttonsetEnabled(false) i = 0 String str = lthtmlgt + ltfont color=008000gt + ltbgt +Downloading is in process + ltbgt + ltfontgt + lthtmlgt labelsetText(str) timerstart() public static void main(String[] args) SwingProgressBarExample spb = new SwingProgressBarExample()
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
After Pressing Start Button
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JSpinner
A single line input field that lets the user select a number or an object value from an ordered sequence Spinners typically provide a pair of tiny arrow buttons for stepping through the elements of the sequence The keyboard updown arrow keys also cycle through the elements The user may also be allowed to type a (legal) value directly into the spinner Although combo boxes provide similar functionality spinners are sometimes preferred because they dont require a drop down list that can obscure important data
A JSpinners sequence value is defined by its SpinnerModel The model can be specified as a constructor argument and changed with the model property SpinnerModel classes for some common types are provided SpinnerListModel SpinnerNumberModel and SpinnerDateModel
A JSpinner has a single child component thats responsible for displaying and potentially changing the current element or value of the model which is called the editor The editor is created by the JSpinners constructor and can be changed with the editor property The JSpinners editor stays in sync with the model by listening for ChangeEvents If the user has changed the value displayed by the editor it is possible for the models value to differ from that of the editor To make sure the model has the same value as the editor use the commitEdit method
Demo
import javaawtimport javaxswingimport javaxswingeventclass JSpinnerExample public static void main(String[] args) String[] sizes = smallmediumlargeXLXXL final SpinnerListModel model = new SpinnerListModel(sizes) JSpinner jSpinner = new JSpinner(model) modeladdChangeListener(new ChangeListener() public void stateChanged(ChangeEvent e) Systemoutprintln(modelgetValue()) ) JFrame frame = new JFrame() framesetSize(100100) framegetContentPane()add(jSpinner) framepack() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Press UP KEY
Press UP KEY
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
- Relationship to AWT
-
- JProgressBar
- Demo
- import javaawt
- import javaawtevent
- import javaxswing
- import javaxswingtexthtml
- public class SwingProgressBarExample
- final static int interval = 1000
- int i
- JLabel label
- JProgressBar pb
- Timer timer
- JButton button
- public SwingProgressBarExample()
- JFrame frame = new JFrame(Swing Progress Bar)
- button = new JButton(Start)
- buttonaddActionListener(new ButtonListener())
- pb = new JProgressBar(0 20)
- pbsetValue(0)
- pbsetStringPainted(true)
- label = new JLabel(By Prasad Sawant)
- JPanel panel = new JPanel()
- paneladd(button)
- paneladd(pb)
- JPanel panel1 = new JPanel()
- panel1setLayout(new BorderLayout())
- panel1add(panel BorderLayoutNORTH)
- panel1add(label BorderLayoutCENTER)
- panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20))
- framesetContentPane(panel1)
- framepack()
- framesetVisible(true)
- framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
- Create a timer
- timer = new Timer(interval new ActionListener()
- public void actionPerformed(ActionEvent evt)
- if (i == 20)
- ToolkitgetDefaultToolkit()beep()
- timerstop()
- buttonsetEnabled(true)
- pbsetValue(0)
- String str = lthtmlgt + ltfont color=FF0000gt + ltbgt +
- Downloading completed + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- i = i + 1
- pbsetValue(i)
- )
- class ButtonListener implements ActionListener
- public void actionPerformed(ActionEvent ae)
- buttonsetEnabled(false)
- i = 0
- String str = lthtmlgt + ltfont color=008000gt + ltbgt +
- Downloading is in process + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- timerstart()
- public static void main(String[] args)
- SwingProgressBarExample spb = new SwingProgressBarExample()
- Press UP KEY
- Press UP KEY
-
Swing
JMenu
Constructor Summary
JMenu() Constructs a new JMenu with no text
JMenu(Action a) Constructs a menu whose properties are taken from the Action supplied
JMenu(String s) Constructs a new JMenu with the supplied string as its text
JMenu(String s boolean b) Constructs a new JMenu with the supplied string as its text and specified as a tear-off menu or not
Demoimport javaawtimport javaxswingclass JMenuDemo public static void main(String[] args) JMenu menu1 = new JMenu(File) menu1add(new JMenuItem(New)) menu1add(new JMenuItem(Open)) menu1add(new JSeparator()) menu1add(new JMenuItem(Save)) menu1add(new JMenuItem(Save as)) menu1add(new JSeparator()) menu1add(new JMenuItem(Exit)) JMenu menu2 = new JMenu(Edit) menu2add(new JMenuItem(Copy)) menu2add(new JSeparator()) menu2add(new JMenuItem(Paste)) menu2add(new JSeparator()) menu2add(new JMenuItem(Select ALL)) JMenuBar bar = new JMenuBar() baradd(menu1) baradd(menu2) JFrame frame = new JFrame() framesetJMenuBar(bar) framesetSize(350300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JColorChooser
Constructor Summary
JColorChooser() Creates a color chooser pane with an initial color of white
JColorChooser(Color initialColor) Creates a color chooser pane with the specified initial color
JColorChooser(ColorSelectionModel model) Creates a color chooser pane with the specified ColorSelectionModel
Demoimport javaawtimport javaawteventimport javaxswingpublic class ColorCh static Container cp public static void main(String[] args) final JFrame frame = new JFrame(Color Choice) cp = framegetContentPane( ) cpsetLayout(new GridBagLayout()) JButton button = new JButton(select color) cpadd(button) buttonaddActionListener(new ActionListener( ) public void actionPerformed(ActionEvent e) Color c = JColorChoosershowDialog(frameChoose a color Colorred) cpsetBackground(c) ) framesetSize(200 300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Select Any Color and Press OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JOptionPane
Constructor Summary
JOptionPane() Creates a JOptionPane with a test message
JOptionPane(Object message) Creates a instance of JOptionPane to display a message using the plain-message message type and the default options delivered by the UI
JOptionPane(Object message int messageType) Creates an instance of JOptionPane to display a message with the specified message type and the default options
JOptionPane(Object message int messageType int optionType) Creates an instance of JOptionPane to display a message with the specified message type and options
JOptionPane(Object message int messageType int optionType Icon icon) Creates an instance of JOptionPane to display a message with the specified message type options and icon
JOptionPane(Object message int messageType int optionType Icon icon Object[] options) Creates an instance of JOptionPane to display a message with the specified message type icon and options
JOptionPane(Object message int messageType int optionType Icon icon Object[] options Object initialValue) Creates an instance of JOptionPane to display a message with the specified message type icon and options with the initially-selected option specified
Demo
import javaxswingJOptionPanepublic class JOptionPaneDemo public static void main(String[] args) String[] choices = BJP NCP LEFT int doAgain do int response = JOptionPaneshowOptionDialog ( null center over parent
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
How did you vote message Party Poll title in titlebar JOptionPaneYES_NO_OPTION Option type JOptionPanePLAIN_MESSAGE messageType null icon choices Options None of your business initial value ) JOptionPaneshowMessageDialog(null Response = + response) doAgain = JOptionPaneshowConfirmDialog(null Again) while(doAgain == JOptionPaneYES_OPTION) Systemexit(0)
Output
Press Any One
After Pressing OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JProgressBar
Constructor Summary
JProgressBar() Creates a horizontal progress bar that displays a border but no progress string
JProgressBar(BoundedRangeModel newModel) Creates a horizontal progress bar that uses the specified model to hold the progress bars data
JProgressBar(int orient) Creates a progress bar with the specified orientation which can be either SwingConstantsVERTICAL or SwingConstantsHORIZONTAL
JProgressBar(int min int max) Creates a horizontal progress bar with the specified minimum and maximum
JProgressBar(int orient int min int max) Creates a progress bar using the specified orientation minimum and maximum
Demo
import javaawtimport javaawteventimport javaxswingimport javaxswingtexthtmlpublic class SwingProgressBarExample final static int interval = 1000 int i JLabel label JProgressBar pb Timer timer JButton button public SwingProgressBarExample() JFrame frame = new JFrame(Swing Progress Bar) button = new JButton(Start) buttonaddActionListener(new ButtonListener()) pb = new JProgressBar(0 20) pbsetValue(0) pbsetStringPainted(true) label = new JLabel(By Prasad Sawant) JPanel panel = new JPanel() paneladd(button) paneladd(pb)copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JPanel panel1 = new JPanel() panel1setLayout(new BorderLayout()) panel1add(panel BorderLayoutNORTH) panel1add(label BorderLayoutCENTER) panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20)) framesetContentPane(panel1) framepack() framesetVisible(true) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Create a timer timer = new Timer(interval new ActionListener() public void actionPerformed(ActionEvent evt) if (i == 20) ToolkitgetDefaultToolkit()beep() timerstop() buttonsetEnabled(true) pbsetValue(0) String str = lthtmlgt + ltfont color=FF0000gt + ltbgt + Downloading completed + ltbgt + ltfontgt + lthtmlgt labelsetText(str) i = i + 1 pbsetValue(i) ) class ButtonListener implements ActionListener public void actionPerformed(ActionEvent ae) buttonsetEnabled(false) i = 0 String str = lthtmlgt + ltfont color=008000gt + ltbgt +Downloading is in process + ltbgt + ltfontgt + lthtmlgt labelsetText(str) timerstart() public static void main(String[] args) SwingProgressBarExample spb = new SwingProgressBarExample()
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
After Pressing Start Button
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JSpinner
A single line input field that lets the user select a number or an object value from an ordered sequence Spinners typically provide a pair of tiny arrow buttons for stepping through the elements of the sequence The keyboard updown arrow keys also cycle through the elements The user may also be allowed to type a (legal) value directly into the spinner Although combo boxes provide similar functionality spinners are sometimes preferred because they dont require a drop down list that can obscure important data
A JSpinners sequence value is defined by its SpinnerModel The model can be specified as a constructor argument and changed with the model property SpinnerModel classes for some common types are provided SpinnerListModel SpinnerNumberModel and SpinnerDateModel
A JSpinner has a single child component thats responsible for displaying and potentially changing the current element or value of the model which is called the editor The editor is created by the JSpinners constructor and can be changed with the editor property The JSpinners editor stays in sync with the model by listening for ChangeEvents If the user has changed the value displayed by the editor it is possible for the models value to differ from that of the editor To make sure the model has the same value as the editor use the commitEdit method
Demo
import javaawtimport javaxswingimport javaxswingeventclass JSpinnerExample public static void main(String[] args) String[] sizes = smallmediumlargeXLXXL final SpinnerListModel model = new SpinnerListModel(sizes) JSpinner jSpinner = new JSpinner(model) modeladdChangeListener(new ChangeListener() public void stateChanged(ChangeEvent e) Systemoutprintln(modelgetValue()) ) JFrame frame = new JFrame() framesetSize(100100) framegetContentPane()add(jSpinner) framepack() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Press UP KEY
Press UP KEY
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
- Relationship to AWT
-
- JProgressBar
- Demo
- import javaawt
- import javaawtevent
- import javaxswing
- import javaxswingtexthtml
- public class SwingProgressBarExample
- final static int interval = 1000
- int i
- JLabel label
- JProgressBar pb
- Timer timer
- JButton button
- public SwingProgressBarExample()
- JFrame frame = new JFrame(Swing Progress Bar)
- button = new JButton(Start)
- buttonaddActionListener(new ButtonListener())
- pb = new JProgressBar(0 20)
- pbsetValue(0)
- pbsetStringPainted(true)
- label = new JLabel(By Prasad Sawant)
- JPanel panel = new JPanel()
- paneladd(button)
- paneladd(pb)
- JPanel panel1 = new JPanel()
- panel1setLayout(new BorderLayout())
- panel1add(panel BorderLayoutNORTH)
- panel1add(label BorderLayoutCENTER)
- panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20))
- framesetContentPane(panel1)
- framepack()
- framesetVisible(true)
- framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
- Create a timer
- timer = new Timer(interval new ActionListener()
- public void actionPerformed(ActionEvent evt)
- if (i == 20)
- ToolkitgetDefaultToolkit()beep()
- timerstop()
- buttonsetEnabled(true)
- pbsetValue(0)
- String str = lthtmlgt + ltfont color=FF0000gt + ltbgt +
- Downloading completed + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- i = i + 1
- pbsetValue(i)
- )
- class ButtonListener implements ActionListener
- public void actionPerformed(ActionEvent ae)
- buttonsetEnabled(false)
- i = 0
- String str = lthtmlgt + ltfont color=008000gt + ltbgt +
- Downloading is in process + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- timerstart()
- public static void main(String[] args)
- SwingProgressBarExample spb = new SwingProgressBarExample()
- Press UP KEY
- Press UP KEY
-
Swing
Output
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JColorChooser
Constructor Summary
JColorChooser() Creates a color chooser pane with an initial color of white
JColorChooser(Color initialColor) Creates a color chooser pane with the specified initial color
JColorChooser(ColorSelectionModel model) Creates a color chooser pane with the specified ColorSelectionModel
Demoimport javaawtimport javaawteventimport javaxswingpublic class ColorCh static Container cp public static void main(String[] args) final JFrame frame = new JFrame(Color Choice) cp = framegetContentPane( ) cpsetLayout(new GridBagLayout()) JButton button = new JButton(select color) cpadd(button) buttonaddActionListener(new ActionListener( ) public void actionPerformed(ActionEvent e) Color c = JColorChoosershowDialog(frameChoose a color Colorred) cpsetBackground(c) ) framesetSize(200 300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Select Any Color and Press OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JOptionPane
Constructor Summary
JOptionPane() Creates a JOptionPane with a test message
JOptionPane(Object message) Creates a instance of JOptionPane to display a message using the plain-message message type and the default options delivered by the UI
JOptionPane(Object message int messageType) Creates an instance of JOptionPane to display a message with the specified message type and the default options
JOptionPane(Object message int messageType int optionType) Creates an instance of JOptionPane to display a message with the specified message type and options
JOptionPane(Object message int messageType int optionType Icon icon) Creates an instance of JOptionPane to display a message with the specified message type options and icon
JOptionPane(Object message int messageType int optionType Icon icon Object[] options) Creates an instance of JOptionPane to display a message with the specified message type icon and options
JOptionPane(Object message int messageType int optionType Icon icon Object[] options Object initialValue) Creates an instance of JOptionPane to display a message with the specified message type icon and options with the initially-selected option specified
Demo
import javaxswingJOptionPanepublic class JOptionPaneDemo public static void main(String[] args) String[] choices = BJP NCP LEFT int doAgain do int response = JOptionPaneshowOptionDialog ( null center over parent
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
How did you vote message Party Poll title in titlebar JOptionPaneYES_NO_OPTION Option type JOptionPanePLAIN_MESSAGE messageType null icon choices Options None of your business initial value ) JOptionPaneshowMessageDialog(null Response = + response) doAgain = JOptionPaneshowConfirmDialog(null Again) while(doAgain == JOptionPaneYES_OPTION) Systemexit(0)
Output
Press Any One
After Pressing OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JProgressBar
Constructor Summary
JProgressBar() Creates a horizontal progress bar that displays a border but no progress string
JProgressBar(BoundedRangeModel newModel) Creates a horizontal progress bar that uses the specified model to hold the progress bars data
JProgressBar(int orient) Creates a progress bar with the specified orientation which can be either SwingConstantsVERTICAL or SwingConstantsHORIZONTAL
JProgressBar(int min int max) Creates a horizontal progress bar with the specified minimum and maximum
JProgressBar(int orient int min int max) Creates a progress bar using the specified orientation minimum and maximum
Demo
import javaawtimport javaawteventimport javaxswingimport javaxswingtexthtmlpublic class SwingProgressBarExample final static int interval = 1000 int i JLabel label JProgressBar pb Timer timer JButton button public SwingProgressBarExample() JFrame frame = new JFrame(Swing Progress Bar) button = new JButton(Start) buttonaddActionListener(new ButtonListener()) pb = new JProgressBar(0 20) pbsetValue(0) pbsetStringPainted(true) label = new JLabel(By Prasad Sawant) JPanel panel = new JPanel() paneladd(button) paneladd(pb)copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JPanel panel1 = new JPanel() panel1setLayout(new BorderLayout()) panel1add(panel BorderLayoutNORTH) panel1add(label BorderLayoutCENTER) panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20)) framesetContentPane(panel1) framepack() framesetVisible(true) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Create a timer timer = new Timer(interval new ActionListener() public void actionPerformed(ActionEvent evt) if (i == 20) ToolkitgetDefaultToolkit()beep() timerstop() buttonsetEnabled(true) pbsetValue(0) String str = lthtmlgt + ltfont color=FF0000gt + ltbgt + Downloading completed + ltbgt + ltfontgt + lthtmlgt labelsetText(str) i = i + 1 pbsetValue(i) ) class ButtonListener implements ActionListener public void actionPerformed(ActionEvent ae) buttonsetEnabled(false) i = 0 String str = lthtmlgt + ltfont color=008000gt + ltbgt +Downloading is in process + ltbgt + ltfontgt + lthtmlgt labelsetText(str) timerstart() public static void main(String[] args) SwingProgressBarExample spb = new SwingProgressBarExample()
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
After Pressing Start Button
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JSpinner
A single line input field that lets the user select a number or an object value from an ordered sequence Spinners typically provide a pair of tiny arrow buttons for stepping through the elements of the sequence The keyboard updown arrow keys also cycle through the elements The user may also be allowed to type a (legal) value directly into the spinner Although combo boxes provide similar functionality spinners are sometimes preferred because they dont require a drop down list that can obscure important data
A JSpinners sequence value is defined by its SpinnerModel The model can be specified as a constructor argument and changed with the model property SpinnerModel classes for some common types are provided SpinnerListModel SpinnerNumberModel and SpinnerDateModel
A JSpinner has a single child component thats responsible for displaying and potentially changing the current element or value of the model which is called the editor The editor is created by the JSpinners constructor and can be changed with the editor property The JSpinners editor stays in sync with the model by listening for ChangeEvents If the user has changed the value displayed by the editor it is possible for the models value to differ from that of the editor To make sure the model has the same value as the editor use the commitEdit method
Demo
import javaawtimport javaxswingimport javaxswingeventclass JSpinnerExample public static void main(String[] args) String[] sizes = smallmediumlargeXLXXL final SpinnerListModel model = new SpinnerListModel(sizes) JSpinner jSpinner = new JSpinner(model) modeladdChangeListener(new ChangeListener() public void stateChanged(ChangeEvent e) Systemoutprintln(modelgetValue()) ) JFrame frame = new JFrame() framesetSize(100100) framegetContentPane()add(jSpinner) framepack() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Press UP KEY
Press UP KEY
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
- Relationship to AWT
-
- JProgressBar
- Demo
- import javaawt
- import javaawtevent
- import javaxswing
- import javaxswingtexthtml
- public class SwingProgressBarExample
- final static int interval = 1000
- int i
- JLabel label
- JProgressBar pb
- Timer timer
- JButton button
- public SwingProgressBarExample()
- JFrame frame = new JFrame(Swing Progress Bar)
- button = new JButton(Start)
- buttonaddActionListener(new ButtonListener())
- pb = new JProgressBar(0 20)
- pbsetValue(0)
- pbsetStringPainted(true)
- label = new JLabel(By Prasad Sawant)
- JPanel panel = new JPanel()
- paneladd(button)
- paneladd(pb)
- JPanel panel1 = new JPanel()
- panel1setLayout(new BorderLayout())
- panel1add(panel BorderLayoutNORTH)
- panel1add(label BorderLayoutCENTER)
- panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20))
- framesetContentPane(panel1)
- framepack()
- framesetVisible(true)
- framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
- Create a timer
- timer = new Timer(interval new ActionListener()
- public void actionPerformed(ActionEvent evt)
- if (i == 20)
- ToolkitgetDefaultToolkit()beep()
- timerstop()
- buttonsetEnabled(true)
- pbsetValue(0)
- String str = lthtmlgt + ltfont color=FF0000gt + ltbgt +
- Downloading completed + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- i = i + 1
- pbsetValue(i)
- )
- class ButtonListener implements ActionListener
- public void actionPerformed(ActionEvent ae)
- buttonsetEnabled(false)
- i = 0
- String str = lthtmlgt + ltfont color=008000gt + ltbgt +
- Downloading is in process + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- timerstart()
- public static void main(String[] args)
- SwingProgressBarExample spb = new SwingProgressBarExample()
- Press UP KEY
- Press UP KEY
-
Swing
JColorChooser
Constructor Summary
JColorChooser() Creates a color chooser pane with an initial color of white
JColorChooser(Color initialColor) Creates a color chooser pane with the specified initial color
JColorChooser(ColorSelectionModel model) Creates a color chooser pane with the specified ColorSelectionModel
Demoimport javaawtimport javaawteventimport javaxswingpublic class ColorCh static Container cp public static void main(String[] args) final JFrame frame = new JFrame(Color Choice) cp = framegetContentPane( ) cpsetLayout(new GridBagLayout()) JButton button = new JButton(select color) cpadd(button) buttonaddActionListener(new ActionListener( ) public void actionPerformed(ActionEvent e) Color c = JColorChoosershowDialog(frameChoose a color Colorred) cpsetBackground(c) ) framesetSize(200 300) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Select Any Color and Press OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JOptionPane
Constructor Summary
JOptionPane() Creates a JOptionPane with a test message
JOptionPane(Object message) Creates a instance of JOptionPane to display a message using the plain-message message type and the default options delivered by the UI
JOptionPane(Object message int messageType) Creates an instance of JOptionPane to display a message with the specified message type and the default options
JOptionPane(Object message int messageType int optionType) Creates an instance of JOptionPane to display a message with the specified message type and options
JOptionPane(Object message int messageType int optionType Icon icon) Creates an instance of JOptionPane to display a message with the specified message type options and icon
JOptionPane(Object message int messageType int optionType Icon icon Object[] options) Creates an instance of JOptionPane to display a message with the specified message type icon and options
JOptionPane(Object message int messageType int optionType Icon icon Object[] options Object initialValue) Creates an instance of JOptionPane to display a message with the specified message type icon and options with the initially-selected option specified
Demo
import javaxswingJOptionPanepublic class JOptionPaneDemo public static void main(String[] args) String[] choices = BJP NCP LEFT int doAgain do int response = JOptionPaneshowOptionDialog ( null center over parent
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
How did you vote message Party Poll title in titlebar JOptionPaneYES_NO_OPTION Option type JOptionPanePLAIN_MESSAGE messageType null icon choices Options None of your business initial value ) JOptionPaneshowMessageDialog(null Response = + response) doAgain = JOptionPaneshowConfirmDialog(null Again) while(doAgain == JOptionPaneYES_OPTION) Systemexit(0)
Output
Press Any One
After Pressing OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JProgressBar
Constructor Summary
JProgressBar() Creates a horizontal progress bar that displays a border but no progress string
JProgressBar(BoundedRangeModel newModel) Creates a horizontal progress bar that uses the specified model to hold the progress bars data
JProgressBar(int orient) Creates a progress bar with the specified orientation which can be either SwingConstantsVERTICAL or SwingConstantsHORIZONTAL
JProgressBar(int min int max) Creates a horizontal progress bar with the specified minimum and maximum
JProgressBar(int orient int min int max) Creates a progress bar using the specified orientation minimum and maximum
Demo
import javaawtimport javaawteventimport javaxswingimport javaxswingtexthtmlpublic class SwingProgressBarExample final static int interval = 1000 int i JLabel label JProgressBar pb Timer timer JButton button public SwingProgressBarExample() JFrame frame = new JFrame(Swing Progress Bar) button = new JButton(Start) buttonaddActionListener(new ButtonListener()) pb = new JProgressBar(0 20) pbsetValue(0) pbsetStringPainted(true) label = new JLabel(By Prasad Sawant) JPanel panel = new JPanel() paneladd(button) paneladd(pb)copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JPanel panel1 = new JPanel() panel1setLayout(new BorderLayout()) panel1add(panel BorderLayoutNORTH) panel1add(label BorderLayoutCENTER) panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20)) framesetContentPane(panel1) framepack() framesetVisible(true) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Create a timer timer = new Timer(interval new ActionListener() public void actionPerformed(ActionEvent evt) if (i == 20) ToolkitgetDefaultToolkit()beep() timerstop() buttonsetEnabled(true) pbsetValue(0) String str = lthtmlgt + ltfont color=FF0000gt + ltbgt + Downloading completed + ltbgt + ltfontgt + lthtmlgt labelsetText(str) i = i + 1 pbsetValue(i) ) class ButtonListener implements ActionListener public void actionPerformed(ActionEvent ae) buttonsetEnabled(false) i = 0 String str = lthtmlgt + ltfont color=008000gt + ltbgt +Downloading is in process + ltbgt + ltfontgt + lthtmlgt labelsetText(str) timerstart() public static void main(String[] args) SwingProgressBarExample spb = new SwingProgressBarExample()
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
After Pressing Start Button
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JSpinner
A single line input field that lets the user select a number or an object value from an ordered sequence Spinners typically provide a pair of tiny arrow buttons for stepping through the elements of the sequence The keyboard updown arrow keys also cycle through the elements The user may also be allowed to type a (legal) value directly into the spinner Although combo boxes provide similar functionality spinners are sometimes preferred because they dont require a drop down list that can obscure important data
A JSpinners sequence value is defined by its SpinnerModel The model can be specified as a constructor argument and changed with the model property SpinnerModel classes for some common types are provided SpinnerListModel SpinnerNumberModel and SpinnerDateModel
A JSpinner has a single child component thats responsible for displaying and potentially changing the current element or value of the model which is called the editor The editor is created by the JSpinners constructor and can be changed with the editor property The JSpinners editor stays in sync with the model by listening for ChangeEvents If the user has changed the value displayed by the editor it is possible for the models value to differ from that of the editor To make sure the model has the same value as the editor use the commitEdit method
Demo
import javaawtimport javaxswingimport javaxswingeventclass JSpinnerExample public static void main(String[] args) String[] sizes = smallmediumlargeXLXXL final SpinnerListModel model = new SpinnerListModel(sizes) JSpinner jSpinner = new JSpinner(model) modeladdChangeListener(new ChangeListener() public void stateChanged(ChangeEvent e) Systemoutprintln(modelgetValue()) ) JFrame frame = new JFrame() framesetSize(100100) framegetContentPane()add(jSpinner) framepack() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Press UP KEY
Press UP KEY
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
- Relationship to AWT
-
- JProgressBar
- Demo
- import javaawt
- import javaawtevent
- import javaxswing
- import javaxswingtexthtml
- public class SwingProgressBarExample
- final static int interval = 1000
- int i
- JLabel label
- JProgressBar pb
- Timer timer
- JButton button
- public SwingProgressBarExample()
- JFrame frame = new JFrame(Swing Progress Bar)
- button = new JButton(Start)
- buttonaddActionListener(new ButtonListener())
- pb = new JProgressBar(0 20)
- pbsetValue(0)
- pbsetStringPainted(true)
- label = new JLabel(By Prasad Sawant)
- JPanel panel = new JPanel()
- paneladd(button)
- paneladd(pb)
- JPanel panel1 = new JPanel()
- panel1setLayout(new BorderLayout())
- panel1add(panel BorderLayoutNORTH)
- panel1add(label BorderLayoutCENTER)
- panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20))
- framesetContentPane(panel1)
- framepack()
- framesetVisible(true)
- framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
- Create a timer
- timer = new Timer(interval new ActionListener()
- public void actionPerformed(ActionEvent evt)
- if (i == 20)
- ToolkitgetDefaultToolkit()beep()
- timerstop()
- buttonsetEnabled(true)
- pbsetValue(0)
- String str = lthtmlgt + ltfont color=FF0000gt + ltbgt +
- Downloading completed + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- i = i + 1
- pbsetValue(i)
- )
- class ButtonListener implements ActionListener
- public void actionPerformed(ActionEvent ae)
- buttonsetEnabled(false)
- i = 0
- String str = lthtmlgt + ltfont color=008000gt + ltbgt +
- Downloading is in process + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- timerstart()
- public static void main(String[] args)
- SwingProgressBarExample spb = new SwingProgressBarExample()
- Press UP KEY
- Press UP KEY
-
Swing
Select Any Color and Press OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JOptionPane
Constructor Summary
JOptionPane() Creates a JOptionPane with a test message
JOptionPane(Object message) Creates a instance of JOptionPane to display a message using the plain-message message type and the default options delivered by the UI
JOptionPane(Object message int messageType) Creates an instance of JOptionPane to display a message with the specified message type and the default options
JOptionPane(Object message int messageType int optionType) Creates an instance of JOptionPane to display a message with the specified message type and options
JOptionPane(Object message int messageType int optionType Icon icon) Creates an instance of JOptionPane to display a message with the specified message type options and icon
JOptionPane(Object message int messageType int optionType Icon icon Object[] options) Creates an instance of JOptionPane to display a message with the specified message type icon and options
JOptionPane(Object message int messageType int optionType Icon icon Object[] options Object initialValue) Creates an instance of JOptionPane to display a message with the specified message type icon and options with the initially-selected option specified
Demo
import javaxswingJOptionPanepublic class JOptionPaneDemo public static void main(String[] args) String[] choices = BJP NCP LEFT int doAgain do int response = JOptionPaneshowOptionDialog ( null center over parent
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
How did you vote message Party Poll title in titlebar JOptionPaneYES_NO_OPTION Option type JOptionPanePLAIN_MESSAGE messageType null icon choices Options None of your business initial value ) JOptionPaneshowMessageDialog(null Response = + response) doAgain = JOptionPaneshowConfirmDialog(null Again) while(doAgain == JOptionPaneYES_OPTION) Systemexit(0)
Output
Press Any One
After Pressing OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JProgressBar
Constructor Summary
JProgressBar() Creates a horizontal progress bar that displays a border but no progress string
JProgressBar(BoundedRangeModel newModel) Creates a horizontal progress bar that uses the specified model to hold the progress bars data
JProgressBar(int orient) Creates a progress bar with the specified orientation which can be either SwingConstantsVERTICAL or SwingConstantsHORIZONTAL
JProgressBar(int min int max) Creates a horizontal progress bar with the specified minimum and maximum
JProgressBar(int orient int min int max) Creates a progress bar using the specified orientation minimum and maximum
Demo
import javaawtimport javaawteventimport javaxswingimport javaxswingtexthtmlpublic class SwingProgressBarExample final static int interval = 1000 int i JLabel label JProgressBar pb Timer timer JButton button public SwingProgressBarExample() JFrame frame = new JFrame(Swing Progress Bar) button = new JButton(Start) buttonaddActionListener(new ButtonListener()) pb = new JProgressBar(0 20) pbsetValue(0) pbsetStringPainted(true) label = new JLabel(By Prasad Sawant) JPanel panel = new JPanel() paneladd(button) paneladd(pb)copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JPanel panel1 = new JPanel() panel1setLayout(new BorderLayout()) panel1add(panel BorderLayoutNORTH) panel1add(label BorderLayoutCENTER) panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20)) framesetContentPane(panel1) framepack() framesetVisible(true) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Create a timer timer = new Timer(interval new ActionListener() public void actionPerformed(ActionEvent evt) if (i == 20) ToolkitgetDefaultToolkit()beep() timerstop() buttonsetEnabled(true) pbsetValue(0) String str = lthtmlgt + ltfont color=FF0000gt + ltbgt + Downloading completed + ltbgt + ltfontgt + lthtmlgt labelsetText(str) i = i + 1 pbsetValue(i) ) class ButtonListener implements ActionListener public void actionPerformed(ActionEvent ae) buttonsetEnabled(false) i = 0 String str = lthtmlgt + ltfont color=008000gt + ltbgt +Downloading is in process + ltbgt + ltfontgt + lthtmlgt labelsetText(str) timerstart() public static void main(String[] args) SwingProgressBarExample spb = new SwingProgressBarExample()
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
After Pressing Start Button
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JSpinner
A single line input field that lets the user select a number or an object value from an ordered sequence Spinners typically provide a pair of tiny arrow buttons for stepping through the elements of the sequence The keyboard updown arrow keys also cycle through the elements The user may also be allowed to type a (legal) value directly into the spinner Although combo boxes provide similar functionality spinners are sometimes preferred because they dont require a drop down list that can obscure important data
A JSpinners sequence value is defined by its SpinnerModel The model can be specified as a constructor argument and changed with the model property SpinnerModel classes for some common types are provided SpinnerListModel SpinnerNumberModel and SpinnerDateModel
A JSpinner has a single child component thats responsible for displaying and potentially changing the current element or value of the model which is called the editor The editor is created by the JSpinners constructor and can be changed with the editor property The JSpinners editor stays in sync with the model by listening for ChangeEvents If the user has changed the value displayed by the editor it is possible for the models value to differ from that of the editor To make sure the model has the same value as the editor use the commitEdit method
Demo
import javaawtimport javaxswingimport javaxswingeventclass JSpinnerExample public static void main(String[] args) String[] sizes = smallmediumlargeXLXXL final SpinnerListModel model = new SpinnerListModel(sizes) JSpinner jSpinner = new JSpinner(model) modeladdChangeListener(new ChangeListener() public void stateChanged(ChangeEvent e) Systemoutprintln(modelgetValue()) ) JFrame frame = new JFrame() framesetSize(100100) framegetContentPane()add(jSpinner) framepack() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Press UP KEY
Press UP KEY
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
- Relationship to AWT
-
- JProgressBar
- Demo
- import javaawt
- import javaawtevent
- import javaxswing
- import javaxswingtexthtml
- public class SwingProgressBarExample
- final static int interval = 1000
- int i
- JLabel label
- JProgressBar pb
- Timer timer
- JButton button
- public SwingProgressBarExample()
- JFrame frame = new JFrame(Swing Progress Bar)
- button = new JButton(Start)
- buttonaddActionListener(new ButtonListener())
- pb = new JProgressBar(0 20)
- pbsetValue(0)
- pbsetStringPainted(true)
- label = new JLabel(By Prasad Sawant)
- JPanel panel = new JPanel()
- paneladd(button)
- paneladd(pb)
- JPanel panel1 = new JPanel()
- panel1setLayout(new BorderLayout())
- panel1add(panel BorderLayoutNORTH)
- panel1add(label BorderLayoutCENTER)
- panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20))
- framesetContentPane(panel1)
- framepack()
- framesetVisible(true)
- framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
- Create a timer
- timer = new Timer(interval new ActionListener()
- public void actionPerformed(ActionEvent evt)
- if (i == 20)
- ToolkitgetDefaultToolkit()beep()
- timerstop()
- buttonsetEnabled(true)
- pbsetValue(0)
- String str = lthtmlgt + ltfont color=FF0000gt + ltbgt +
- Downloading completed + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- i = i + 1
- pbsetValue(i)
- )
- class ButtonListener implements ActionListener
- public void actionPerformed(ActionEvent ae)
- buttonsetEnabled(false)
- i = 0
- String str = lthtmlgt + ltfont color=008000gt + ltbgt +
- Downloading is in process + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- timerstart()
- public static void main(String[] args)
- SwingProgressBarExample spb = new SwingProgressBarExample()
- Press UP KEY
- Press UP KEY
-
Swing
JOptionPane
Constructor Summary
JOptionPane() Creates a JOptionPane with a test message
JOptionPane(Object message) Creates a instance of JOptionPane to display a message using the plain-message message type and the default options delivered by the UI
JOptionPane(Object message int messageType) Creates an instance of JOptionPane to display a message with the specified message type and the default options
JOptionPane(Object message int messageType int optionType) Creates an instance of JOptionPane to display a message with the specified message type and options
JOptionPane(Object message int messageType int optionType Icon icon) Creates an instance of JOptionPane to display a message with the specified message type options and icon
JOptionPane(Object message int messageType int optionType Icon icon Object[] options) Creates an instance of JOptionPane to display a message with the specified message type icon and options
JOptionPane(Object message int messageType int optionType Icon icon Object[] options Object initialValue) Creates an instance of JOptionPane to display a message with the specified message type icon and options with the initially-selected option specified
Demo
import javaxswingJOptionPanepublic class JOptionPaneDemo public static void main(String[] args) String[] choices = BJP NCP LEFT int doAgain do int response = JOptionPaneshowOptionDialog ( null center over parent
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
How did you vote message Party Poll title in titlebar JOptionPaneYES_NO_OPTION Option type JOptionPanePLAIN_MESSAGE messageType null icon choices Options None of your business initial value ) JOptionPaneshowMessageDialog(null Response = + response) doAgain = JOptionPaneshowConfirmDialog(null Again) while(doAgain == JOptionPaneYES_OPTION) Systemexit(0)
Output
Press Any One
After Pressing OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JProgressBar
Constructor Summary
JProgressBar() Creates a horizontal progress bar that displays a border but no progress string
JProgressBar(BoundedRangeModel newModel) Creates a horizontal progress bar that uses the specified model to hold the progress bars data
JProgressBar(int orient) Creates a progress bar with the specified orientation which can be either SwingConstantsVERTICAL or SwingConstantsHORIZONTAL
JProgressBar(int min int max) Creates a horizontal progress bar with the specified minimum and maximum
JProgressBar(int orient int min int max) Creates a progress bar using the specified orientation minimum and maximum
Demo
import javaawtimport javaawteventimport javaxswingimport javaxswingtexthtmlpublic class SwingProgressBarExample final static int interval = 1000 int i JLabel label JProgressBar pb Timer timer JButton button public SwingProgressBarExample() JFrame frame = new JFrame(Swing Progress Bar) button = new JButton(Start) buttonaddActionListener(new ButtonListener()) pb = new JProgressBar(0 20) pbsetValue(0) pbsetStringPainted(true) label = new JLabel(By Prasad Sawant) JPanel panel = new JPanel() paneladd(button) paneladd(pb)copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JPanel panel1 = new JPanel() panel1setLayout(new BorderLayout()) panel1add(panel BorderLayoutNORTH) panel1add(label BorderLayoutCENTER) panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20)) framesetContentPane(panel1) framepack() framesetVisible(true) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Create a timer timer = new Timer(interval new ActionListener() public void actionPerformed(ActionEvent evt) if (i == 20) ToolkitgetDefaultToolkit()beep() timerstop() buttonsetEnabled(true) pbsetValue(0) String str = lthtmlgt + ltfont color=FF0000gt + ltbgt + Downloading completed + ltbgt + ltfontgt + lthtmlgt labelsetText(str) i = i + 1 pbsetValue(i) ) class ButtonListener implements ActionListener public void actionPerformed(ActionEvent ae) buttonsetEnabled(false) i = 0 String str = lthtmlgt + ltfont color=008000gt + ltbgt +Downloading is in process + ltbgt + ltfontgt + lthtmlgt labelsetText(str) timerstart() public static void main(String[] args) SwingProgressBarExample spb = new SwingProgressBarExample()
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
After Pressing Start Button
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JSpinner
A single line input field that lets the user select a number or an object value from an ordered sequence Spinners typically provide a pair of tiny arrow buttons for stepping through the elements of the sequence The keyboard updown arrow keys also cycle through the elements The user may also be allowed to type a (legal) value directly into the spinner Although combo boxes provide similar functionality spinners are sometimes preferred because they dont require a drop down list that can obscure important data
A JSpinners sequence value is defined by its SpinnerModel The model can be specified as a constructor argument and changed with the model property SpinnerModel classes for some common types are provided SpinnerListModel SpinnerNumberModel and SpinnerDateModel
A JSpinner has a single child component thats responsible for displaying and potentially changing the current element or value of the model which is called the editor The editor is created by the JSpinners constructor and can be changed with the editor property The JSpinners editor stays in sync with the model by listening for ChangeEvents If the user has changed the value displayed by the editor it is possible for the models value to differ from that of the editor To make sure the model has the same value as the editor use the commitEdit method
Demo
import javaawtimport javaxswingimport javaxswingeventclass JSpinnerExample public static void main(String[] args) String[] sizes = smallmediumlargeXLXXL final SpinnerListModel model = new SpinnerListModel(sizes) JSpinner jSpinner = new JSpinner(model) modeladdChangeListener(new ChangeListener() public void stateChanged(ChangeEvent e) Systemoutprintln(modelgetValue()) ) JFrame frame = new JFrame() framesetSize(100100) framegetContentPane()add(jSpinner) framepack() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Press UP KEY
Press UP KEY
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
- Relationship to AWT
-
- JProgressBar
- Demo
- import javaawt
- import javaawtevent
- import javaxswing
- import javaxswingtexthtml
- public class SwingProgressBarExample
- final static int interval = 1000
- int i
- JLabel label
- JProgressBar pb
- Timer timer
- JButton button
- public SwingProgressBarExample()
- JFrame frame = new JFrame(Swing Progress Bar)
- button = new JButton(Start)
- buttonaddActionListener(new ButtonListener())
- pb = new JProgressBar(0 20)
- pbsetValue(0)
- pbsetStringPainted(true)
- label = new JLabel(By Prasad Sawant)
- JPanel panel = new JPanel()
- paneladd(button)
- paneladd(pb)
- JPanel panel1 = new JPanel()
- panel1setLayout(new BorderLayout())
- panel1add(panel BorderLayoutNORTH)
- panel1add(label BorderLayoutCENTER)
- panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20))
- framesetContentPane(panel1)
- framepack()
- framesetVisible(true)
- framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
- Create a timer
- timer = new Timer(interval new ActionListener()
- public void actionPerformed(ActionEvent evt)
- if (i == 20)
- ToolkitgetDefaultToolkit()beep()
- timerstop()
- buttonsetEnabled(true)
- pbsetValue(0)
- String str = lthtmlgt + ltfont color=FF0000gt + ltbgt +
- Downloading completed + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- i = i + 1
- pbsetValue(i)
- )
- class ButtonListener implements ActionListener
- public void actionPerformed(ActionEvent ae)
- buttonsetEnabled(false)
- i = 0
- String str = lthtmlgt + ltfont color=008000gt + ltbgt +
- Downloading is in process + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- timerstart()
- public static void main(String[] args)
- SwingProgressBarExample spb = new SwingProgressBarExample()
- Press UP KEY
- Press UP KEY
-
Swing
How did you vote message Party Poll title in titlebar JOptionPaneYES_NO_OPTION Option type JOptionPanePLAIN_MESSAGE messageType null icon choices Options None of your business initial value ) JOptionPaneshowMessageDialog(null Response = + response) doAgain = JOptionPaneshowConfirmDialog(null Again) while(doAgain == JOptionPaneYES_OPTION) Systemexit(0)
Output
Press Any One
After Pressing OK
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JProgressBar
Constructor Summary
JProgressBar() Creates a horizontal progress bar that displays a border but no progress string
JProgressBar(BoundedRangeModel newModel) Creates a horizontal progress bar that uses the specified model to hold the progress bars data
JProgressBar(int orient) Creates a progress bar with the specified orientation which can be either SwingConstantsVERTICAL or SwingConstantsHORIZONTAL
JProgressBar(int min int max) Creates a horizontal progress bar with the specified minimum and maximum
JProgressBar(int orient int min int max) Creates a progress bar using the specified orientation minimum and maximum
Demo
import javaawtimport javaawteventimport javaxswingimport javaxswingtexthtmlpublic class SwingProgressBarExample final static int interval = 1000 int i JLabel label JProgressBar pb Timer timer JButton button public SwingProgressBarExample() JFrame frame = new JFrame(Swing Progress Bar) button = new JButton(Start) buttonaddActionListener(new ButtonListener()) pb = new JProgressBar(0 20) pbsetValue(0) pbsetStringPainted(true) label = new JLabel(By Prasad Sawant) JPanel panel = new JPanel() paneladd(button) paneladd(pb)copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JPanel panel1 = new JPanel() panel1setLayout(new BorderLayout()) panel1add(panel BorderLayoutNORTH) panel1add(label BorderLayoutCENTER) panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20)) framesetContentPane(panel1) framepack() framesetVisible(true) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Create a timer timer = new Timer(interval new ActionListener() public void actionPerformed(ActionEvent evt) if (i == 20) ToolkitgetDefaultToolkit()beep() timerstop() buttonsetEnabled(true) pbsetValue(0) String str = lthtmlgt + ltfont color=FF0000gt + ltbgt + Downloading completed + ltbgt + ltfontgt + lthtmlgt labelsetText(str) i = i + 1 pbsetValue(i) ) class ButtonListener implements ActionListener public void actionPerformed(ActionEvent ae) buttonsetEnabled(false) i = 0 String str = lthtmlgt + ltfont color=008000gt + ltbgt +Downloading is in process + ltbgt + ltfontgt + lthtmlgt labelsetText(str) timerstart() public static void main(String[] args) SwingProgressBarExample spb = new SwingProgressBarExample()
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
After Pressing Start Button
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JSpinner
A single line input field that lets the user select a number or an object value from an ordered sequence Spinners typically provide a pair of tiny arrow buttons for stepping through the elements of the sequence The keyboard updown arrow keys also cycle through the elements The user may also be allowed to type a (legal) value directly into the spinner Although combo boxes provide similar functionality spinners are sometimes preferred because they dont require a drop down list that can obscure important data
A JSpinners sequence value is defined by its SpinnerModel The model can be specified as a constructor argument and changed with the model property SpinnerModel classes for some common types are provided SpinnerListModel SpinnerNumberModel and SpinnerDateModel
A JSpinner has a single child component thats responsible for displaying and potentially changing the current element or value of the model which is called the editor The editor is created by the JSpinners constructor and can be changed with the editor property The JSpinners editor stays in sync with the model by listening for ChangeEvents If the user has changed the value displayed by the editor it is possible for the models value to differ from that of the editor To make sure the model has the same value as the editor use the commitEdit method
Demo
import javaawtimport javaxswingimport javaxswingeventclass JSpinnerExample public static void main(String[] args) String[] sizes = smallmediumlargeXLXXL final SpinnerListModel model = new SpinnerListModel(sizes) JSpinner jSpinner = new JSpinner(model) modeladdChangeListener(new ChangeListener() public void stateChanged(ChangeEvent e) Systemoutprintln(modelgetValue()) ) JFrame frame = new JFrame() framesetSize(100100) framegetContentPane()add(jSpinner) framepack() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Press UP KEY
Press UP KEY
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
- Relationship to AWT
-
- JProgressBar
- Demo
- import javaawt
- import javaawtevent
- import javaxswing
- import javaxswingtexthtml
- public class SwingProgressBarExample
- final static int interval = 1000
- int i
- JLabel label
- JProgressBar pb
- Timer timer
- JButton button
- public SwingProgressBarExample()
- JFrame frame = new JFrame(Swing Progress Bar)
- button = new JButton(Start)
- buttonaddActionListener(new ButtonListener())
- pb = new JProgressBar(0 20)
- pbsetValue(0)
- pbsetStringPainted(true)
- label = new JLabel(By Prasad Sawant)
- JPanel panel = new JPanel()
- paneladd(button)
- paneladd(pb)
- JPanel panel1 = new JPanel()
- panel1setLayout(new BorderLayout())
- panel1add(panel BorderLayoutNORTH)
- panel1add(label BorderLayoutCENTER)
- panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20))
- framesetContentPane(panel1)
- framepack()
- framesetVisible(true)
- framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
- Create a timer
- timer = new Timer(interval new ActionListener()
- public void actionPerformed(ActionEvent evt)
- if (i == 20)
- ToolkitgetDefaultToolkit()beep()
- timerstop()
- buttonsetEnabled(true)
- pbsetValue(0)
- String str = lthtmlgt + ltfont color=FF0000gt + ltbgt +
- Downloading completed + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- i = i + 1
- pbsetValue(i)
- )
- class ButtonListener implements ActionListener
- public void actionPerformed(ActionEvent ae)
- buttonsetEnabled(false)
- i = 0
- String str = lthtmlgt + ltfont color=008000gt + ltbgt +
- Downloading is in process + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- timerstart()
- public static void main(String[] args)
- SwingProgressBarExample spb = new SwingProgressBarExample()
- Press UP KEY
- Press UP KEY
-
Swing
JProgressBar
Constructor Summary
JProgressBar() Creates a horizontal progress bar that displays a border but no progress string
JProgressBar(BoundedRangeModel newModel) Creates a horizontal progress bar that uses the specified model to hold the progress bars data
JProgressBar(int orient) Creates a progress bar with the specified orientation which can be either SwingConstantsVERTICAL or SwingConstantsHORIZONTAL
JProgressBar(int min int max) Creates a horizontal progress bar with the specified minimum and maximum
JProgressBar(int orient int min int max) Creates a progress bar using the specified orientation minimum and maximum
Demo
import javaawtimport javaawteventimport javaxswingimport javaxswingtexthtmlpublic class SwingProgressBarExample final static int interval = 1000 int i JLabel label JProgressBar pb Timer timer JButton button public SwingProgressBarExample() JFrame frame = new JFrame(Swing Progress Bar) button = new JButton(Start) buttonaddActionListener(new ButtonListener()) pb = new JProgressBar(0 20) pbsetValue(0) pbsetStringPainted(true) label = new JLabel(By Prasad Sawant) JPanel panel = new JPanel() paneladd(button) paneladd(pb)copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JPanel panel1 = new JPanel() panel1setLayout(new BorderLayout()) panel1add(panel BorderLayoutNORTH) panel1add(label BorderLayoutCENTER) panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20)) framesetContentPane(panel1) framepack() framesetVisible(true) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Create a timer timer = new Timer(interval new ActionListener() public void actionPerformed(ActionEvent evt) if (i == 20) ToolkitgetDefaultToolkit()beep() timerstop() buttonsetEnabled(true) pbsetValue(0) String str = lthtmlgt + ltfont color=FF0000gt + ltbgt + Downloading completed + ltbgt + ltfontgt + lthtmlgt labelsetText(str) i = i + 1 pbsetValue(i) ) class ButtonListener implements ActionListener public void actionPerformed(ActionEvent ae) buttonsetEnabled(false) i = 0 String str = lthtmlgt + ltfont color=008000gt + ltbgt +Downloading is in process + ltbgt + ltfontgt + lthtmlgt labelsetText(str) timerstart() public static void main(String[] args) SwingProgressBarExample spb = new SwingProgressBarExample()
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
After Pressing Start Button
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JSpinner
A single line input field that lets the user select a number or an object value from an ordered sequence Spinners typically provide a pair of tiny arrow buttons for stepping through the elements of the sequence The keyboard updown arrow keys also cycle through the elements The user may also be allowed to type a (legal) value directly into the spinner Although combo boxes provide similar functionality spinners are sometimes preferred because they dont require a drop down list that can obscure important data
A JSpinners sequence value is defined by its SpinnerModel The model can be specified as a constructor argument and changed with the model property SpinnerModel classes for some common types are provided SpinnerListModel SpinnerNumberModel and SpinnerDateModel
A JSpinner has a single child component thats responsible for displaying and potentially changing the current element or value of the model which is called the editor The editor is created by the JSpinners constructor and can be changed with the editor property The JSpinners editor stays in sync with the model by listening for ChangeEvents If the user has changed the value displayed by the editor it is possible for the models value to differ from that of the editor To make sure the model has the same value as the editor use the commitEdit method
Demo
import javaawtimport javaxswingimport javaxswingeventclass JSpinnerExample public static void main(String[] args) String[] sizes = smallmediumlargeXLXXL final SpinnerListModel model = new SpinnerListModel(sizes) JSpinner jSpinner = new JSpinner(model) modeladdChangeListener(new ChangeListener() public void stateChanged(ChangeEvent e) Systemoutprintln(modelgetValue()) ) JFrame frame = new JFrame() framesetSize(100100) framegetContentPane()add(jSpinner) framepack() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Press UP KEY
Press UP KEY
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
- Relationship to AWT
-
- JProgressBar
- Demo
- import javaawt
- import javaawtevent
- import javaxswing
- import javaxswingtexthtml
- public class SwingProgressBarExample
- final static int interval = 1000
- int i
- JLabel label
- JProgressBar pb
- Timer timer
- JButton button
- public SwingProgressBarExample()
- JFrame frame = new JFrame(Swing Progress Bar)
- button = new JButton(Start)
- buttonaddActionListener(new ButtonListener())
- pb = new JProgressBar(0 20)
- pbsetValue(0)
- pbsetStringPainted(true)
- label = new JLabel(By Prasad Sawant)
- JPanel panel = new JPanel()
- paneladd(button)
- paneladd(pb)
- JPanel panel1 = new JPanel()
- panel1setLayout(new BorderLayout())
- panel1add(panel BorderLayoutNORTH)
- panel1add(label BorderLayoutCENTER)
- panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20))
- framesetContentPane(panel1)
- framepack()
- framesetVisible(true)
- framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
- Create a timer
- timer = new Timer(interval new ActionListener()
- public void actionPerformed(ActionEvent evt)
- if (i == 20)
- ToolkitgetDefaultToolkit()beep()
- timerstop()
- buttonsetEnabled(true)
- pbsetValue(0)
- String str = lthtmlgt + ltfont color=FF0000gt + ltbgt +
- Downloading completed + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- i = i + 1
- pbsetValue(i)
- )
- class ButtonListener implements ActionListener
- public void actionPerformed(ActionEvent ae)
- buttonsetEnabled(false)
- i = 0
- String str = lthtmlgt + ltfont color=008000gt + ltbgt +
- Downloading is in process + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- timerstart()
- public static void main(String[] args)
- SwingProgressBarExample spb = new SwingProgressBarExample()
- Press UP KEY
- Press UP KEY
-
Swing
JPanel panel1 = new JPanel() panel1setLayout(new BorderLayout()) panel1add(panel BorderLayoutNORTH) panel1add(label BorderLayoutCENTER) panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20)) framesetContentPane(panel1) framepack() framesetVisible(true) framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) Create a timer timer = new Timer(interval new ActionListener() public void actionPerformed(ActionEvent evt) if (i == 20) ToolkitgetDefaultToolkit()beep() timerstop() buttonsetEnabled(true) pbsetValue(0) String str = lthtmlgt + ltfont color=FF0000gt + ltbgt + Downloading completed + ltbgt + ltfontgt + lthtmlgt labelsetText(str) i = i + 1 pbsetValue(i) ) class ButtonListener implements ActionListener public void actionPerformed(ActionEvent ae) buttonsetEnabled(false) i = 0 String str = lthtmlgt + ltfont color=008000gt + ltbgt +Downloading is in process + ltbgt + ltfontgt + lthtmlgt labelsetText(str) timerstart() public static void main(String[] args) SwingProgressBarExample spb = new SwingProgressBarExample()
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
After Pressing Start Button
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JSpinner
A single line input field that lets the user select a number or an object value from an ordered sequence Spinners typically provide a pair of tiny arrow buttons for stepping through the elements of the sequence The keyboard updown arrow keys also cycle through the elements The user may also be allowed to type a (legal) value directly into the spinner Although combo boxes provide similar functionality spinners are sometimes preferred because they dont require a drop down list that can obscure important data
A JSpinners sequence value is defined by its SpinnerModel The model can be specified as a constructor argument and changed with the model property SpinnerModel classes for some common types are provided SpinnerListModel SpinnerNumberModel and SpinnerDateModel
A JSpinner has a single child component thats responsible for displaying and potentially changing the current element or value of the model which is called the editor The editor is created by the JSpinners constructor and can be changed with the editor property The JSpinners editor stays in sync with the model by listening for ChangeEvents If the user has changed the value displayed by the editor it is possible for the models value to differ from that of the editor To make sure the model has the same value as the editor use the commitEdit method
Demo
import javaawtimport javaxswingimport javaxswingeventclass JSpinnerExample public static void main(String[] args) String[] sizes = smallmediumlargeXLXXL final SpinnerListModel model = new SpinnerListModel(sizes) JSpinner jSpinner = new JSpinner(model) modeladdChangeListener(new ChangeListener() public void stateChanged(ChangeEvent e) Systemoutprintln(modelgetValue()) ) JFrame frame = new JFrame() framesetSize(100100) framegetContentPane()add(jSpinner) framepack() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Press UP KEY
Press UP KEY
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
- Relationship to AWT
-
- JProgressBar
- Demo
- import javaawt
- import javaawtevent
- import javaxswing
- import javaxswingtexthtml
- public class SwingProgressBarExample
- final static int interval = 1000
- int i
- JLabel label
- JProgressBar pb
- Timer timer
- JButton button
- public SwingProgressBarExample()
- JFrame frame = new JFrame(Swing Progress Bar)
- button = new JButton(Start)
- buttonaddActionListener(new ButtonListener())
- pb = new JProgressBar(0 20)
- pbsetValue(0)
- pbsetStringPainted(true)
- label = new JLabel(By Prasad Sawant)
- JPanel panel = new JPanel()
- paneladd(button)
- paneladd(pb)
- JPanel panel1 = new JPanel()
- panel1setLayout(new BorderLayout())
- panel1add(panel BorderLayoutNORTH)
- panel1add(label BorderLayoutCENTER)
- panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20))
- framesetContentPane(panel1)
- framepack()
- framesetVisible(true)
- framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
- Create a timer
- timer = new Timer(interval new ActionListener()
- public void actionPerformed(ActionEvent evt)
- if (i == 20)
- ToolkitgetDefaultToolkit()beep()
- timerstop()
- buttonsetEnabled(true)
- pbsetValue(0)
- String str = lthtmlgt + ltfont color=FF0000gt + ltbgt +
- Downloading completed + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- i = i + 1
- pbsetValue(i)
- )
- class ButtonListener implements ActionListener
- public void actionPerformed(ActionEvent ae)
- buttonsetEnabled(false)
- i = 0
- String str = lthtmlgt + ltfont color=008000gt + ltbgt +
- Downloading is in process + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- timerstart()
- public static void main(String[] args)
- SwingProgressBarExample spb = new SwingProgressBarExample()
- Press UP KEY
- Press UP KEY
-
Swing
After Pressing Start Button
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
JSpinner
A single line input field that lets the user select a number or an object value from an ordered sequence Spinners typically provide a pair of tiny arrow buttons for stepping through the elements of the sequence The keyboard updown arrow keys also cycle through the elements The user may also be allowed to type a (legal) value directly into the spinner Although combo boxes provide similar functionality spinners are sometimes preferred because they dont require a drop down list that can obscure important data
A JSpinners sequence value is defined by its SpinnerModel The model can be specified as a constructor argument and changed with the model property SpinnerModel classes for some common types are provided SpinnerListModel SpinnerNumberModel and SpinnerDateModel
A JSpinner has a single child component thats responsible for displaying and potentially changing the current element or value of the model which is called the editor The editor is created by the JSpinners constructor and can be changed with the editor property The JSpinners editor stays in sync with the model by listening for ChangeEvents If the user has changed the value displayed by the editor it is possible for the models value to differ from that of the editor To make sure the model has the same value as the editor use the commitEdit method
Demo
import javaawtimport javaxswingimport javaxswingeventclass JSpinnerExample public static void main(String[] args) String[] sizes = smallmediumlargeXLXXL final SpinnerListModel model = new SpinnerListModel(sizes) JSpinner jSpinner = new JSpinner(model) modeladdChangeListener(new ChangeListener() public void stateChanged(ChangeEvent e) Systemoutprintln(modelgetValue()) ) JFrame frame = new JFrame() framesetSize(100100) framegetContentPane()add(jSpinner) framepack() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Press UP KEY
Press UP KEY
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
- Relationship to AWT
-
- JProgressBar
- Demo
- import javaawt
- import javaawtevent
- import javaxswing
- import javaxswingtexthtml
- public class SwingProgressBarExample
- final static int interval = 1000
- int i
- JLabel label
- JProgressBar pb
- Timer timer
- JButton button
- public SwingProgressBarExample()
- JFrame frame = new JFrame(Swing Progress Bar)
- button = new JButton(Start)
- buttonaddActionListener(new ButtonListener())
- pb = new JProgressBar(0 20)
- pbsetValue(0)
- pbsetStringPainted(true)
- label = new JLabel(By Prasad Sawant)
- JPanel panel = new JPanel()
- paneladd(button)
- paneladd(pb)
- JPanel panel1 = new JPanel()
- panel1setLayout(new BorderLayout())
- panel1add(panel BorderLayoutNORTH)
- panel1add(label BorderLayoutCENTER)
- panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20))
- framesetContentPane(panel1)
- framepack()
- framesetVisible(true)
- framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
- Create a timer
- timer = new Timer(interval new ActionListener()
- public void actionPerformed(ActionEvent evt)
- if (i == 20)
- ToolkitgetDefaultToolkit()beep()
- timerstop()
- buttonsetEnabled(true)
- pbsetValue(0)
- String str = lthtmlgt + ltfont color=FF0000gt + ltbgt +
- Downloading completed + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- i = i + 1
- pbsetValue(i)
- )
- class ButtonListener implements ActionListener
- public void actionPerformed(ActionEvent ae)
- buttonsetEnabled(false)
- i = 0
- String str = lthtmlgt + ltfont color=008000gt + ltbgt +
- Downloading is in process + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- timerstart()
- public static void main(String[] args)
- SwingProgressBarExample spb = new SwingProgressBarExample()
- Press UP KEY
- Press UP KEY
-
Swing
JSpinner
A single line input field that lets the user select a number or an object value from an ordered sequence Spinners typically provide a pair of tiny arrow buttons for stepping through the elements of the sequence The keyboard updown arrow keys also cycle through the elements The user may also be allowed to type a (legal) value directly into the spinner Although combo boxes provide similar functionality spinners are sometimes preferred because they dont require a drop down list that can obscure important data
A JSpinners sequence value is defined by its SpinnerModel The model can be specified as a constructor argument and changed with the model property SpinnerModel classes for some common types are provided SpinnerListModel SpinnerNumberModel and SpinnerDateModel
A JSpinner has a single child component thats responsible for displaying and potentially changing the current element or value of the model which is called the editor The editor is created by the JSpinners constructor and can be changed with the editor property The JSpinners editor stays in sync with the model by listening for ChangeEvents If the user has changed the value displayed by the editor it is possible for the models value to differ from that of the editor To make sure the model has the same value as the editor use the commitEdit method
Demo
import javaawtimport javaxswingimport javaxswingeventclass JSpinnerExample public static void main(String[] args) String[] sizes = smallmediumlargeXLXXL final SpinnerListModel model = new SpinnerListModel(sizes) JSpinner jSpinner = new JSpinner(model) modeladdChangeListener(new ChangeListener() public void stateChanged(ChangeEvent e) Systemoutprintln(modelgetValue()) ) JFrame frame = new JFrame() framesetSize(100100) framegetContentPane()add(jSpinner) framepack() framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE) framesetVisible(true)
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
Swing
Press UP KEY
Press UP KEY
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
- Relationship to AWT
-
- JProgressBar
- Demo
- import javaawt
- import javaawtevent
- import javaxswing
- import javaxswingtexthtml
- public class SwingProgressBarExample
- final static int interval = 1000
- int i
- JLabel label
- JProgressBar pb
- Timer timer
- JButton button
- public SwingProgressBarExample()
- JFrame frame = new JFrame(Swing Progress Bar)
- button = new JButton(Start)
- buttonaddActionListener(new ButtonListener())
- pb = new JProgressBar(0 20)
- pbsetValue(0)
- pbsetStringPainted(true)
- label = new JLabel(By Prasad Sawant)
- JPanel panel = new JPanel()
- paneladd(button)
- paneladd(pb)
- JPanel panel1 = new JPanel()
- panel1setLayout(new BorderLayout())
- panel1add(panel BorderLayoutNORTH)
- panel1add(label BorderLayoutCENTER)
- panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20))
- framesetContentPane(panel1)
- framepack()
- framesetVisible(true)
- framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
- Create a timer
- timer = new Timer(interval new ActionListener()
- public void actionPerformed(ActionEvent evt)
- if (i == 20)
- ToolkitgetDefaultToolkit()beep()
- timerstop()
- buttonsetEnabled(true)
- pbsetValue(0)
- String str = lthtmlgt + ltfont color=FF0000gt + ltbgt +
- Downloading completed + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- i = i + 1
- pbsetValue(i)
- )
- class ButtonListener implements ActionListener
- public void actionPerformed(ActionEvent ae)
- buttonsetEnabled(false)
- i = 0
- String str = lthtmlgt + ltfont color=008000gt + ltbgt +
- Downloading is in process + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- timerstart()
- public static void main(String[] args)
- SwingProgressBarExample spb = new SwingProgressBarExample()
- Press UP KEY
- Press UP KEY
-
Swing
Press UP KEY
Press UP KEY
copy Dept Of Computer Science
ProfRamkirshna More ACS CollegeAkurdi
- Relationship to AWT
-
- JProgressBar
- Demo
- import javaawt
- import javaawtevent
- import javaxswing
- import javaxswingtexthtml
- public class SwingProgressBarExample
- final static int interval = 1000
- int i
- JLabel label
- JProgressBar pb
- Timer timer
- JButton button
- public SwingProgressBarExample()
- JFrame frame = new JFrame(Swing Progress Bar)
- button = new JButton(Start)
- buttonaddActionListener(new ButtonListener())
- pb = new JProgressBar(0 20)
- pbsetValue(0)
- pbsetStringPainted(true)
- label = new JLabel(By Prasad Sawant)
- JPanel panel = new JPanel()
- paneladd(button)
- paneladd(pb)
- JPanel panel1 = new JPanel()
- panel1setLayout(new BorderLayout())
- panel1add(panel BorderLayoutNORTH)
- panel1add(label BorderLayoutCENTER)
- panel1setBorder(BorderFactorycreateEmptyBorder(20 20 20 20))
- framesetContentPane(panel1)
- framepack()
- framesetVisible(true)
- framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
- Create a timer
- timer = new Timer(interval new ActionListener()
- public void actionPerformed(ActionEvent evt)
- if (i == 20)
- ToolkitgetDefaultToolkit()beep()
- timerstop()
- buttonsetEnabled(true)
- pbsetValue(0)
- String str = lthtmlgt + ltfont color=FF0000gt + ltbgt +
- Downloading completed + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- i = i + 1
- pbsetValue(i)
- )
- class ButtonListener implements ActionListener
- public void actionPerformed(ActionEvent ae)
- buttonsetEnabled(false)
- i = 0
- String str = lthtmlgt + ltfont color=008000gt + ltbgt +
- Downloading is in process + ltbgt + ltfontgt + lthtmlgt
- labelsetText(str)
- timerstart()
- public static void main(String[] args)
- SwingProgressBarExample spb = new SwingProgressBarExample()
- Press UP KEY
- Press UP KEY
-