Video tutorial to demonstrate how to use Combo Box (Drop Down list) in Java using Swing API.
Free Data Structures and Algorithms Course
Showing posts with label AWT and Swing. Show all posts
Showing posts with label AWT and Swing. Show all posts
How to use Combo Box (Drop Down list) in Java using Swing API ? (Video Tutorial)
Video tutorial to demonstrate how to use Combo Box (Drop Down list) in Java using Swing API.
How to create a simple Swing Application in Java ? (Video Tutorial)
Video tutorial to demonstrate how to create a simple Swing Application in Java.
Abstract Window Toolkit and Swing Programming in Java
- How to Create a Simple Frame using Swing API in Java?.
- How to add a Button to a Frame using Swing API in Java ?
- How to display information about available fonts in Java Swing API
- How to add ItemListener to JCheckbox using Swing API in Java ?
- How to add Event Handling to a Button using Anonymous class in Java
- How to add Event Handling to a Button in Java using Swing API
- How to add Radio Buttons to a Swing frame in Java using JRadioButton class?
- How to add Image button to a Frame using Swing API in Java ?
- How to use BorderLayout in Java using Swing ?.
- How to use Combo Box (Drop-Down list) in Java using Swing ?.
- How to use FlowLayout in Java using Swing ?.
- How to use GridLayout in Java using Swing ?.
- How to add label to a Swing frame in Java ?
- How to create Eclipse Menu and Menu Bar in a simple Frame in Java ?.
- How to add Text Area to a Frame using Swing API in Java ?
- How to implement Tabbed Pane in a Frame using Java Swing API ?.
- Program to implement List on a Frame using Java Swing API
- How to implement JScrollPane into a Frame in Java using Swing API ?.
- How to implement JFileChooser dialog into a Frame in Java using Swing ?.
- How to implement JSplitPane into a Frame in Java using Swing API?.
- How to use and set ToolTip for Swing components in Java ?.
- How to demonstrate working of CheckBox and adding ActionListener to it in Java's Swing Framework ?.
- How to use JOptionPane in Java using Swing Framework API ?.
- How to use JTextField in Java using Swing Framework API ?.
- How to use JProgressBar in Java using Swing Framework API ?.
- How to handle Mouse Click events in Java ?.
- How to create a simple JTree in Java using Java Swing API ?.
- How to set different Look and Feel in Java Swing ?.
- JColorChooser in Java Swing
- JOptionPane : Message Dialog in Java Swing
JOptionPane : Message Dialog in Java Swing
Program to demonstrate working of JOptionPane's Message Dialog in Java Swing
Output of the program :
Watch the Youtube video below for better understanding ...
import javax.swing.ImageIcon; import javax.swing.JOptionPane; public class MessageDialogDemo { public static void main(String[] args) { JOptionPane.showMessageDialog(null, "Hello World !!!"); JOptionPane.showMessageDialog(null, "Hello World !!!", "Welcome", JOptionPane.PLAIN_MESSAGE); JOptionPane.showMessageDialog(null, "Hello World !!!", "Welcome", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Hello World !!!", "Welcome", JOptionPane.WARNING_MESSAGE); JOptionPane.showMessageDialog(null, "Hello World !!!", "Welcome", JOptionPane.QUESTION_MESSAGE); JOptionPane.showMessageDialog(null, "Hello World !!!", "Welcome", JOptionPane.ERROR_MESSAGE); ImageIcon icon = new ImageIcon("CD.png"); JOptionPane.showMessageDialog(null, "Hello World !!!", "Welcome", JOptionPane.INFORMATION_MESSAGE, icon); } }
Output of the program :
Watch the Youtube video below for better understanding ...
JColorChooser in Java Swing
Program to demonstrate how to use JColorChooser in Java Swing
package com.hubberspot.swing; import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JColorChooser; import javax.swing.JFrame; import javax.swing.JPanel; class JColorChooserFrame extends JFrame { private JPanel panel; private JButton button; private Color color = Color.BLUE; public JColorChooserFrame() { setTitle("JColorChooser Test Frame"); panel = new JPanel(); panel.setBackground(color); button = new JButton(); button.setText("Select Color"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { color = JColorChooser.showDialog(null, "Pick a color", color); panel.setBackground(color); } }); add(panel,BorderLayout.CENTER); add(button, BorderLayout.SOUTH); } } public class JColorChooserDemo { public static void main(String[] args) { JColorChooserFrame frame = new JColorChooserFrame(); frame.setSize(450, 200); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
Output of the program :
How to set different Look and Feel in Java Swing ?.
A simple Java program to set different installed look and feel in Java Swing
Output of the program :
package com.hubberspot.swing; import java.awt.Color; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.UIManager.LookAndFeelInfo; class LookAndFeel extends JFrame { private JPanel panel; private JTextField textField; public LookAndFeel() { setTitle("Look And Feel Demo"); setSize(300, 200); panel = new JPanel(); textField = new JTextField("Metal look and feel"); textField.setBackground(Color.cyan); LookAndFeelInfo[] installedLookAndFeels = UIManager.getInstalledLookAndFeels(); for(LookAndFeelInfo lookAndFeel : installedLookAndFeels) { final String name = lookAndFeel.getName(); final String className = lookAndFeel.getClassName(); JButton button = new JButton(name); panel.add(button); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { try { UIManager.setLookAndFeel(className); SwingUtilities.updateComponentTreeUI(LookAndFeel.this); textField.setText(name + " look and feel"); } catch (Exception e) { e.printStackTrace(); } } }); } panel.add(textField); add(panel); } } public class LookAndFeelTest { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { LookAndFeel frame = new LookAndFeel(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); } }
Output of the program :
How to create a simple JTree in Java using Java Swing API ?.
Program to demonstrate how to create a simple JTree in Java using Java Swing API ?.
Output of the program :
package com.hubberspot.swing.examples; import java.awt.*; import javax.swing.*; import javax.swing.tree.DefaultMutableTreeNode; public class JTreeDemo { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { JFrame frame = new JTreeFrame(); frame.setTitle("JTree Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); } } class JTreeFrame extends JFrame { public JTreeFrame() { setSize(300, 300); DefaultMutableTreeNode country = new DefaultMutableTreeNode("India"); DefaultMutableTreeNode state = new DefaultMutableTreeNode("Maharashtra"); country.add(state); DefaultMutableTreeNode city = new DefaultMutableTreeNode("Pune"); state.add(city); city = new DefaultMutableTreeNode("Mumbai"); state.add(city); state = new DefaultMutableTreeNode("Madhya Pradesh"); country.add(state); city = new DefaultMutableTreeNode("Indore"); state.add(city); city = new DefaultMutableTreeNode("Bhopal"); state.add(city); state = new DefaultMutableTreeNode("Gujrat"); country.add(state); city = new DefaultMutableTreeNode("Ahmedabad"); state.add(city); JTree tree = new JTree(country); add(new JScrollPane(tree)); } }
Output of the program :
How to handle Mouse Click events in Java ?.
Program to demonstrate how to handle Mouse Click events in Java.
Output of the program :
package com.hubberspot.swing.examples; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.SwingUtilities; public class MouseClickEventHandling extends JFrame { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new MouseClickEventHandling().setVisible(true); } }); } public MouseClickEventHandling() { mouseEventHandling(); } private void mouseEventHandling() { setSize(300, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JTextArea textArea = new JTextArea(); textArea.setText("Browse Text Area Below !!! "); textArea.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { textArea.setText(" Mouse Released ... \n Position :" + e.getX() +" , "+ e.getY() + "\n Click count: "+ e.getClickCount()); } @Override public void mousePressed(MouseEvent e) { textArea.setText(" Mouse Pressed ... \n Position :" + e.getX() +" , "+ e.getY() + "\n Click count: "+ e.getClickCount()); } @Override public void mouseExited(MouseEvent e) { textArea.setText("Mouse Exited ... \n Position :" + e.getX() +" , "+ e.getY()); } @Override public void mouseEntered(MouseEvent e) { textArea.setText("Mouse Entered ... \n Position :" + e.getX() +" , "+ e.getY()); } @Override public void mouseClicked(MouseEvent e) { textArea.setText(" Mouse Clicked ... \n Position :" + e.getX() +" , "+ e.getY() + "\n Click count: "+ e.getClickCount()); } }); getContentPane().add(textArea); } }
Output of the program :
How to use JProgressBar in Java using Swing Framework API ?.
Program to demonstrate working of JProgressBar in Java using Swing API
package com.hubberspot.swing.code; import javax.swing.*; public class ProgressBarDemo extends JPanel { // 1. Create a instance variable of JProgressBar JProgressBar progressBar; // 2. Constructor call to ProgressBarDemo will // create a object of JProgressBar and set // minimum and maximum value of progress bar // to 0 and 100 public ProgressBarDemo() { progressBar = new JProgressBar(); progressBar.setMinimum(0); progressBar.setMaximum(100); // 3. Add Progress bar to Panel add(progressBar); } // 4. progress method will update the value of // progress bar every 100 ms after executing // sleep method public void progress(int value) { progressBar.setValue(value); } public static void main(String args[]) { // 5. Create a object of customized JPanel final ProgressBarDemo progressBarDemo = new ProgressBarDemo(); // 6. Create a Object of JFrame and setting title as // "Progress Bar Demo" JFrame frame = new JFrame("Progress Bar Demo"); // Give frame a size in pixels as say 300,300 frame.setSize(200, 100); // 7. set a operation when a user close the frame, here it // closes the frame and exits the application frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 8. Setting the Content Pane as ProgressBarDemo frame.setContentPane(progressBarDemo); // 9. Packing all the components together frame.pack(); // 10. after setting contentPane, we make it // visible on the frame by calling the method as // setVisible and passing value as true. frame.setVisible(true); // 11. Having for loop which will loop from 0 to 100 // it will invoke a dedicated thread to Swing Component // "JProgressBar" and on each loop will update the value // of JProgressBar by 1. It will accomplish this by calling // progressBarDemo.progress method and pass the updated or // completed status to it. for (int i = 0; i <= 100; i++) { final int updateCompleted = i; try { SwingUtilities.invokeLater(new Runnable() { public void run() { progressBarDemo.progress(updateCompleted); } }); // 12. Giving each loop a pause of 100 ms and // updating JProgressBar meter by 1. Thread.sleep(100); } catch (InterruptedException e) {;} } } }Output of the program :
How to use JTextField in Java using Swing Framework API ?.
Program to demonstrate working of JTextField in Java using Swing API
Output of the program :
package com.hubberspot.code; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class JTextFieldDemo { public static void main(String[] args) { // 1. Create a simple frame by creating an object // of JFrame. JFrame frame = new JFrame(); // 2. Create a label by creating an Object of class // JLabel JLabel label = new JLabel(); // 3. set the text of label as "Name : " label.setText("Name : "); // 4. Create a textfield by creating an Object of class // JTextField JTextField textfield = new JTextField(); // 5. set the text of textfield as "add text ... " textfield.setText("add text ... "); // 6. We create a Layout for Swing Components // here we are using FlowLayout. frame.setLayout(new FlowLayout()); // 7. We add the textfield and label to frame frame.add(label); frame.add(textfield); // 8. set a operation when a user close the frame, here it // closes the frame and exits the application frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 9. Give frame a size in pixels as say 300,300 frame.setSize(200, 100); // 10. Give the frame a title "JTextField Demo" by calling // setTitle method on frame object frame.setTitle("JTextField Demo"); // 11. after adding textfield and label, we make it // visible on the frame by calling the method as // setVisible and passing value as true. frame.setVisible(true); } }
Output of the program :
How to use JOptionPane in Java using Swing Framework API ?.
Program to demonstrate working of JOptionPane in Java using Swing API
package com.hubberspot.code; // Import swing package for using JOptionPane import javax.swing.*; public class JOptionPaneDemo { public static void main(String[] args) { // JOptionPane.showInputDialog shows a simple message along // with an Input box where user enters and gets it back // it as a String value String name = JOptionPane.showInputDialog(null, "Who is this visitor to Hubberspot ?."); // JOptionPane.showConfirmDialog shows a simple message // and Yes/No/Cancel small buttons for user to choose a // choice. The selected choice is returned back as a // int having following values : // 1. JOptionPane.YES_OPTION if user clicked 'Yes' // 2. JOptionPane.NO_OPTION if user clicked 'No' // 3. JOptionPane.CANCEL_OPTION if user clicked 'Cancel' // int option = JOptionPane.showConfirmDialog(null, "Hi "+ name + ", do you like Hubberspot ?."); if(option == JOptionPane.YES_OPTION) { // JOptionPane.showMessageDialog shows a dialog box // having a simple message for the user JOptionPane.showMessageDialog(null, "Thank you so much !!!"); } else { JOptionPane.showMessageDialog(null, "So like us !!!"); } } }
Output of the program :
1. JOptionPane's Input Dialog :
2. JOptionPane's Confirm Dialog :
3. JOptionPane's Message Dialog :
How to demonstrate working of CheckBox and adding ActionListener to it in Java's Swing Framework ?.
Program to demonstrate working of CheckBox and adding ActionListener to it in Java's Swing Framework.
Output of the program :
package com.hubberspot.swing; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; public class CheckBoxDemo { public static void main(String[] args) { // 1. Create a simple frame by creating an object // of JFrame. JFrame frame = new JFrame(); // 2. Give the frame a title "CheckBox Demo" by calling // setTitle method on frame object frame.setTitle("CheckBox Demo"); // 3. Give frame a size in pixels as say 300,300 frame.setSize(300, 200); // 4. set a operation when a user close the frame, here it // closes the frame and exits the application frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 5. Create a checkbox by creating an Object of class // JCheckBox. final JCheckBox checkBox1 = new JCheckBox(); // 6. set the text of checkbox1 as "check box 1" checkBox1.setText("check box 1"); // 7. Create another checkbox by creating an Object of class // JCheckBox. final JCheckBox checkBox2 = new JCheckBox(); // 8. set the text of checkBox2 as "check box 2" checkBox2.setText("check box 2"); // 9. Make it selected that is (checked) checkBox2.setSelected(true); // 10. Create a label by creating an Object of class // JLabel final JLabel label = new JLabel(); // 11. set the text of label as "check box 2 selected" // as initially we have made checkbox2 selected label.setText("check box 2 selected"); // 12. We add an ActionListener to the checkbox1 // which will listen about the action user performs // over the checkbox1. Here user only checks and unchecks // it. As soon as user checks checkbox1 the text of the // label changes from check box 2 selected to check box 1 selected // and checkbox2 is unchecked checkBox1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { JCheckBox checkBox = (JCheckBox) event.getSource(); if (checkBox.isSelected()) { checkBox.setSelected(true); checkBox2.setSelected(false); label.setText("check box 1 selected"); } } }); // 13. We add an ActionListener to the checkbox2 // which will listen about the action user performs // over the checkbox2. Here user only checks and unchecks // it. As soon as user checks checkbox2 the text of the // label changes from check box 1 selected to check box 2 selected // and checkbox1 is unchecked checkBox2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { JCheckBox checkBox = (JCheckBox) event.getSource(); if (checkBox.isSelected()) { checkBox.setSelected(true); checkBox1.setSelected(false); label.setText("check box 2 selected"); } } }); // 14. for adding all these swing components to frame // we first get the content pane which returns a // Container Container container = frame.getContentPane(); // 15. We create a Layout for Swing Components // here we are using FlowLayout with value as "center' // it will make the swing components float to center FlowLayout layout = new FlowLayout(FlowLayout.CENTER); // 16. We set the layout for container container.setLayout(layout); // 17. We add the checkboxes and label to it container.add(checkBox1); container.add(checkBox2); container.add(label); // 18. after adding checkboxes and label, we make it // visible on the frame by calling the method as // setVisible and passing value as true. frame.setVisible(true); } }
Output of the program :
How to use and set ToolTip for Swing components in Java ?.
Program to demonstrate how to use and set ToolTip for Swing components in Java
Output of the program :
package com.hubberspot.example; import java.awt.Container; import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; public class ToolTipDemo { public static void main(String[] args) { // 1. Create a simple frame by creating an object // of JFrame. JFrame frame = new JFrame(); // 2. Give the frame a title "Tool Tip Frame" by calling // setTitle method on frame object frame.setTitle("Tool Tip Frame"); // 3. Give frame a size in pixels as say 300,300 frame.setSize(300, 300); // 4. set a operation when a user close the frame, here it // closes the frame and exits the application frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 5. Create a button by creating an Object of class // JButton. JButton button = new JButton(); // 6. set the text of button as "Button" button.setText("Button"); // 7. Create a label by creating an Object of class // JLabel JLabel label = new JLabel(); // 8. set the text of label as "Label" label.setText("Label"); // 9. In order to create a tool tip for label, // button or any swing components, we call setToolTipText // method and passing it the information we want to display // when user hovers over the swing components button.setToolTipText("Tool Tip for Button"); label.setToolTipText("Tool Tip for Label"); // 10. for adding all these swing components to frame // we first get the content pane which returns a // Container Container container = frame.getContentPane(); // 11. We create a Layout for Swing Components // here we are using FlowLayout with value as "center' // it will make the swing components float to center FlowLayout layout = new FlowLayout(FlowLayout.CENTER); // 12. We set the layout for container container.setLayout(layout); // 13. We add the button and label to it container.add(button); container.add(label); // 14. after adding button and label, we make it // visible on the frame by calling the method as // setVisible and passing value as true. frame.setVisible(true); } }
Output of the program :
How to implement JSplitPane into a Frame in Java using Swing API?.
A Simple program demonstrating the working of JSplitPane in Java using Swing framework.
Output of the program :
package com.hubberspot.swing.example; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SplitPaneDemo extends JFrame { static String text = "This is a simple program that demonstrate " + "the use of JSplitPane which is a simple split pane which help " + "us in dividing two text areas and giving us a way to put to " + "sample text side by side."; public SplitPaneDemo() { JTextArea textarea1 = new JTextArea(text); JTextArea textarea2 = new JTextArea(text); textarea1.setLineWrap(true); textarea2.setLineWrap(true); textarea1.setMinimumSize(new Dimension(150, 150)); textarea2.setMinimumSize(new Dimension(150, 150)); textarea1.setPreferredSize(new Dimension(250, 200)); JSplitPane splitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, textarea1, textarea2); getContentPane().add(splitpane, BorderLayout.CENTER); } public static void main(String[] args) { setFrame(new SplitPaneDemo(), 500, 400); } public static void setFrame(final JFrame frame, final int width, final int height) { SwingUtilities.invokeLater(new Runnable() { public void run() { frame.setTitle(frame.getClass().getSimpleName()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(width, height); frame.setVisible(true); } }); } }
Output of the program :
How to implement JFileChooser dialog into a Frame in Java using Swing ?.
A Simple program demonstrating the working of JFileChooser dialog in Java using Swing framework.
Output of the program :

package com.hubberspot.swing.example; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingUtilities; public class FileChooserDemo extends JFrame { private JTextField display = new JTextField(); private JButton open = new JButton("Open"); private JButton save = new JButton("Save"); public FileChooserDemo() { JPanel panel = new JPanel(); open.addActionListener(new OpenClass()); panel.add(open); save.addActionListener(new SaveClass()); panel.add(save); add(panel, BorderLayout.SOUTH); display.setEditable(false); panel = new JPanel(); panel.setLayout(new GridLayout(2,1)); panel.add(display); add(panel, BorderLayout.NORTH); } class OpenClass implements ActionListener { public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); int option = chooser.showOpenDialog(FileChooserDemo.this); if(option == JFileChooser.APPROVE_OPTION) { display.setText("You chose " + ((chooser.getSelectedFile()!=null)? chooser.getSelectedFile().getName(): "nothing")); } if(option == JFileChooser.CANCEL_OPTION) { display.setText("You canceled."); } } } class SaveClass implements ActionListener { public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); int option = chooser.showSaveDialog(FileChooserDemo.this); if(option == JFileChooser.APPROVE_OPTION) { display.setText("You chose " + ((chooser.getSelectedFile()!=null)? chooser.getSelectedFile().getName(): "nothing")); } if(option == JFileChooser.CANCEL_OPTION) { display.setText("You canceled."); } } } public static void main(String[] args) { setFrame(new FileChooserDemo(), 200, 100); } public static void setFrame(final JFrame frame, final int width, final int height) { SwingUtilities.invokeLater(new Runnable() { public void run() { frame.setTitle(frame.getClass().getSimpleName()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(width, height); frame.setVisible(true); } }); } }
Output of the program :

How to implement JScrollPane into a Frame in Java using Swing API ?.
A Simple program demonstrating the working of JScrollPane in Java using Swing framework.
Output of the program :
package com.hubberspot.swing.example; import javax.swing.*; import java.awt.*; public class ScrollPaneDemo extends JFrame { JScrollPane scrollpane; public ScrollPaneDemo() { String teams[] = { "Deccan Chargers", "Kolkata Night Riders", "Mumbai Indians", "Chennai Super Kings", "Rajasthan Royals", "Royal Challengers Bangalore", "Kings XI Punjab", "Pune Warriors India", "Delhi Daredevils" }; JList list = new JList(teams); scrollpane = new JScrollPane(list); getContentPane().add(scrollpane, BorderLayout.CENTER); } public static void main(String[] args) { setFrame(new ScrollPaneDemo(), 300, 150); } public static void setFrame(final JFrame frame, final int width, final int height) { SwingUtilities.invokeLater(new Runnable() { public void run() { frame.setTitle(frame.getClass().getSimpleName()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(width, height); frame.setVisible(true); } }); } }
Output of the program :
Program to implement List on a Frame using Java Swing API
A Simple program demonstrating the working of JList list in Java using Swing framework.
package com.hubberspot.swing.example; import java.awt.Color; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JTextArea; import javax.swing.SwingUtilities; import javax.swing.border.Border; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; public class ListDemo extends JFrame { private String[] ipl = { "Deccan Chargers", "Kolkata Night Riders", "Mumbai Indians", "Chennai Super Kings", "Rajasthan Royals", "Royal Challengers Bangalore", "Kings XI Punjab", "Pune Warriors India", "Delhi Daredevils" }; private int counter = 0; private DefaultListModel model = new DefaultListModel(); private JList list = new JList(model); private JTextArea textarea = new JTextArea(ipl.length, 20); private JButton button = new JButton("Add Team"); private ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent e) { if(counter < ipl.length) { model.add(0, ipl[counter++]); } else { button.setEnabled(false); } } }; private ListSelectionListener lstlistener = new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { if(e.getValueIsAdjusting()) return; textarea.setText(""); for(Object item : list.getSelectedValues()) textarea.append(item + "\n"); } }; public ListDemo() { textarea.setEditable(false); setLayout(new FlowLayout()); for(int i = 0; i < 4; i++) model.addElement(ipl[counter++]); add(textarea); add(list); add(button); list.addListSelectionListener(lstlistener); button.addActionListener(listener); } public static void main(String[] args) { setFrame(new ListDemo(), 300, 450); } public static void setFrame(final JFrame frame, final int width, final int height) { SwingUtilities.invokeLater(new Runnable() { public void run() { frame.setTitle(frame.getClass().getSimpleName()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(width, height); frame.setVisible(true); } }); } }Output of the program :
How to implement Tabbed Pane in a Frame using Java Swing API ?.
A Simple program to demonstrate how to use Tabbed Pane in a Frame using Java Swing API ?.
Output of the program :
package com.iplt20.swing.example; import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTabbedPane; import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class TabbedPaneDemo extends JFrame { private String[] ipl = { "Deccan Chargers", "Kolkata Night Riders", "Mumbai Indians", "Chennai Super Kings", "Rajasthan Royals", "Royal Challengers Bangalore", "Kings XI Punjab", "Pune Warriors India", "Delhi Daredevils" }; private JTabbedPane tab = new JTabbedPane(); private JTextField textfield = new JTextField(20); public TabbedPaneDemo() { int i = 0; for(String teams : ipl){ tab.addTab(ipl[i], new JTextField("Who will win IPL this year?.")); i++; } tab.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { textfield.setText("You selected: " + tab.getTitleAt(tab.getSelectedIndex())); } }); add(BorderLayout.SOUTH, textfield); add(tab); } public static void main(String[] args) { setFrame(new TabbedPaneDemo(), 450, 175); } public static void setFrame(final JFrame frame, final int width, final int height) { SwingUtilities.invokeLater(new Runnable() { public void run() { frame.setTitle(frame.getClass().getSimpleName()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(width, height); frame.setVisible(true); } }); } }
Output of the program :
How to add Text Area to a Frame using Swing API in Java ?
Program to demonstrate adding of Text Area to a Frame.
Output of the program :
package com.hubberspot.swing.example; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.SwingUtilities; public class TextAreaDemo extends JFrame { private JButton add = new JButton("Add Data"); private JButton clear = new JButton("Clear Data"); private JTextArea textarea = new JTextArea(2, 25); public TextAreaDemo() { add.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textarea.setText("Welcome to Hubberspot.com!"); } }); clear.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textarea.setText(""); } }); setLayout(new FlowLayout()); add(new JScrollPane(textarea)); add(add); add(clear); } public static void main(String[] args) { setFrame(new TextAreaDemo(), 300, 150); } public static void setFrame(final JFrame frame, final int width, final int height) { SwingUtilities.invokeLater(new Runnable() { public void run() { frame.setTitle(frame.getClass().getSimpleName()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(width, height); frame.setVisible(true); } }); } }
Output of the program :
How to create Eclipse Menu and Menu Bar in a simple Frame in Java ?.
Program to demonstrate how to create Eclipse like Menu and Menu Bar in a simple Frame in Java ?.
package com.hubberspot.swing.example; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JTextField; import javax.swing.SwingUtilities; public class MenusDemo extends JFrame { private JMenu[] menu = { new JMenu("File"), new JMenu("Edit"), new JMenu("Run"), new JMenu("Source"), new JMenu("Refactor"), new JMenu("Navigate"), new JMenu("Search"), new JMenu("Project"), new JMenu("Window"), new JMenu("Help") }; private JMenuItem[] item = { new JMenuItem("New"), new JMenuItem("Undo Typing"), new JMenuItem("Run"), new JMenuItem("Toggle Comment"), new JMenuItem("Android"), new JMenuItem("Go Into"), new JMenuItem("Search"), new JMenuItem("Open Project"), new JMenuItem("New Window"), new JMenuItem("Welcome"), new JMenuItem("Open File"), new JMenuItem("Redo"), new JMenuItem("Debug"), new JMenuItem("Add Block Comment"), new JMenuItem("Rename"), new JMenuItem("Go To"), new JMenuItem("File"), new JMenuItem("Close Project"), new JMenuItem("New Editor"), new JMenuItem("Help Contents"), new JMenuItem("Close"), new JMenuItem("Cut"), new JMenuItem("Run History"), new JMenuItem("Remove Block Comment"), new JMenuItem("Move"), new JMenuItem("Open Declaration"), new JMenuItem("Java"), new JMenuItem("Build All"), new JMenuItem("Open Perspective"), new JMenuItem("Search"), new JMenuItem("Close All"), new JMenuItem("Copy"), new JMenuItem("Run As"), new JMenuItem("Generate Element Comment"), new JMenuItem("Change Method Signature"), new JMenuItem("Open Type Hierarchy"), new JMenuItem("Text"), new JMenuItem("Build Project"), new JMenuItem("Show View"), new JMenuItem("Dynamic Help") }; public MenusDemo() { for(int i = 0; i < item.length; i++) { menu[i % 10].add(item[i]); } JMenuBar menubar = new JMenuBar(); for(JMenu menus : menu) menubar.add(menus); setJMenuBar(menubar); setLayout(new FlowLayout()); } public static void main(String[] args) { setFrame(new MenusDemo(), 500, 200); } public static void setFrame(final JFrame frame, final int width, final int height) { SwingUtilities.invokeLater(new Runnable() { public void run() { frame.setTitle(frame.getClass().getSimpleName()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(width, height); frame.setVisible(true); } }); } }Output of the program :
Subscribe to:
Posts (Atom)