Saturday, 4 March 2017

JTextField Example

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingControlDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public SwingControlDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
      SwingControlDemo  swingControlDemo = new SwingControlDemo();     
      swingControlDemo.showTextFieldDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
     
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }       
      });   
      headerLabel = new JLabel("", JLabel.CENTER);       
      statusLabel = new JLabel("",JLabel.CENTER);   
      statusLabel.setSize(350,100);

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true); 
   }
   private void showTextFieldDemo(){
      headerLabel.setText("Control in action: JTextField");

      JLabel  namelabel= new JLabel("User ID: ", JLabel.RIGHT);
      JLabel  passwordLabel = new JLabel("Password: ", JLabel.CENTER);
      final JTextField userText = new JTextField(6);
      final JPasswordField passwordText = new JPasswordField(6);     

      JButton loginButton = new JButton("Login");
      loginButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {    
            String data = "Username " + userText.getText();
            data += ", Password: " + new String(passwordText.getPassword());
            statusLabel.setText(data);       
         }
      });
      controlPanel.add(namelabel);
      controlPanel.add(userText);
      controlPanel.add(passwordLabel);      
      controlPanel.add(passwordText);
      controlPanel.add(loginButton);
      mainFrame.setVisible(true); 
   }
}


JComboBox Example

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingControlDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public SwingControlDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
      SwingControlDemo  swingControlDemo = new SwingControlDemo();     
      swingControlDemo.showComboboxDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
     
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }       
      });   
      headerLabel = new JLabel("", JLabel.CENTER);       
      statusLabel = new JLabel("",JLabel.CENTER);   
      statusLabel.setSize(350,100);

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true); 
   }
   private void showComboboxDemo(){                                   
      headerLabel.setText("Control in action: JComboBox");
      final DefaultComboBoxModel fruitsName = new DefaultComboBoxModel();

      fruitsName.addElement("Apple");
      fruitsName.addElement("Grapes");
      fruitsName.addElement("Mango");
      fruitsName.addElement("Peer");

      final JComboBox fruitCombo = new JComboBox(fruitsName);   
      fruitCombo.setSelectedIndex(0);

      JScrollPane fruitListScrollPane = new JScrollPane(fruitCombo);   
      JButton showButton = new JButton("Show");

      showButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            String data = "";
            if (fruitCombo.getSelectedIndex() != -1) {                    
               data = "Fruits Selected: "
                  + fruitCombo.getItemAt
                  (fruitCombo.getSelectedIndex());            
            }             
            statusLabel.setText(data);
         }
      });
      controlPanel.add(fruitListScrollPane);         
      controlPanel.add(showButton);   
      mainFrame.setVisible(true);            
   }
}


JList Example

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingControlDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;
  
   public SwingControlDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
      SwingControlDemo  swingControlDemo = new SwingControlDemo();     
      swingControlDemo.showListDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
     
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }       
      });   
      headerLabel = new JLabel("", JLabel.CENTER);       
      statusLabel = new JLabel("",JLabel.CENTER);   
      statusLabel.setSize(350,100);

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true); 
   }
   private void showListDemo(){                                      
      headerLabel.setText("Control in action: JList");
      final DefaultListModel fruitsName = new DefaultListModel();

      fruitsName.addElement("Apple");
      fruitsName.addElement("Grapes");
      fruitsName.addElement("Mango");
      fruitsName.addElement("Peer");

      final JList fruitList = new JList(fruitsName);
      fruitList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      fruitList.setSelectedIndex(0);
      fruitList.setVisibleRowCount(3);       

      JScrollPane fruitListScrollPane = new JScrollPane(fruitList);   
      final DefaultListModel vegName = new DefaultListModel();

      vegName.addElement("Lady Finger");
      vegName.addElement("Onion");
      vegName.addElement("Potato");
      vegName.addElement("Tomato");

      final JList vegList = new JList(vegName);
      vegList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
      vegList.setSelectedIndex(0);
      vegList.setVisibleRowCount(3);       

      JScrollPane vegListScrollPane = new JScrollPane(vegList);      
      JButton showButton = new JButton("Show");

      showButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            String data = "";
            if (fruitList.getSelectedIndex() != -1) {                    
               data = "Fruits Selected: " + fruitList.getSelectedValue();
               statusLabel.setText(data);
            }
            if(vegList.getSelectedIndex() != -1){
               data += " Vegetables selected: ";
               for(Object vegetable:vegList.getSelectedValues()){
                  data += vegetable + " ";
               }
            }
            statusLabel.setText(data);
         }
      });
      controlPanel.add(fruitListScrollPane);   
      controlPanel.add(vegListScrollPane);   
      controlPanel.add(showButton);   
     
      mainFrame.setVisible(true);            
   }
}


JRadioButton Example

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class SwingControlDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;
   
   public SwingControlDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
      SwingControlDemo  swingControlDemo = new SwingControlDemo();      
      swingControlDemo.showRadioButtonDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new JLabel("", JLabel.CENTER);        
      statusLabel = new JLabel("",JLabel.CENTER);    
      statusLabel.setSize(350,100);

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }
   private void showRadioButtonDemo(){
      headerLabel.setText("Control in action: RadioButton"); 

      final JRadioButton radApple = new JRadioButton("Apple", true);
      final JRadioButton radMango = new JRadioButton("Mango");
      final JRadioButton radPeer = new JRadioButton("Peer");

      radApple.setMnemonic(KeyEvent.VK_C);
      radMango.setMnemonic(KeyEvent.VK_M);
      radPeer.setMnemonic(KeyEvent.VK_P);

      radApple.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {         
            statusLabel.setText("Apple RadioButton: " 
               + (e.getStateChange()==1?"checked":"unchecked"));
         }           
      });
      radMango.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {             
            statusLabel.setText("Mango RadioButton: " 
               + (e.getStateChange()==1?"checked":"unchecked")); 
         }           
      });
      radPeer.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {             
            statusLabel.setText("Peer RadioButton: " 
               + (e.getStateChange()==1?"checked":"unchecked"));
         }           
      });
      
      //Group the radio buttons.
      ButtonGroup group = new ButtonGroup();
      
      group.add(radApple);
      group.add(radMango);
      group.add(radPeer);

      controlPanel.add(radApple);
      controlPanel.add(radMango);
      controlPanel.add(radPeer);       

      mainFrame.setVisible(true);  
   }
}
 
 

JCheckBox Example

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingControlDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public SwingControlDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
      SwingControlDemo  swingControlDemo = new SwingControlDemo();     
      swingControlDemo.showCheckBoxDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
     
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }       
      });   
      headerLabel = new JLabel("", JLabel.CENTER);       
      statusLabel = new JLabel("",JLabel.CENTER);   
      statusLabel.setSize(350,100);

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true); 
   }
   private void showCheckBoxDemo(){
      headerLabel.setText("Control in action: CheckBox");

      final JCheckBox chkApple = new JCheckBox("Apple");
      final JCheckBox chkMango = new JCheckBox("Mango");
      final JCheckBox chkPeer = new JCheckBox("Peer");

      chkApple.setMnemonic(KeyEvent.VK_C);
      chkMango.setMnemonic(KeyEvent.VK_M);
      chkPeer.setMnemonic(KeyEvent.VK_P);

      chkApple.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {        
            statusLabel.setText("Apple Checkbox: "
               + (e.getStateChange()==1?"checked":"unchecked"));
         }          
      });
      chkMango.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {            
            statusLabel.setText("Mango Checkbox: "
               + (e.getStateChange()==1?"checked":"unchecked"));
         }          
      });
      chkPeer.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {            
            statusLabel.setText("Peer Checkbox: "
               + (e.getStateChange()==1?"checked":"unchecked"));
         }          
      });
      controlPanel.add(chkApple);
      controlPanel.add(chkMango);
      controlPanel.add(chkPeer);      

      mainFrame.setVisible(true); 
   }
}


JColorChooser

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingControlDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public SwingControlDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
      SwingControlDemo  swingControlDemo = new SwingControlDemo();     
      swingControlDemo.showColorChooserDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
     
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }       
      });   
      headerLabel = new JLabel("", JLabel.CENTER);       
      statusLabel = new JLabel("",JLabel.CENTER);   
      statusLabel.setSize(350,100);

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true); 
   }
   private void showColorChooserDemo(){
      headerLabel.setText("Control in action: JColorChooser");
      JButton chooseButton = new JButton("Choose Background");       
     
      chooseButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            Color backgroundColor = JColorChooser.showDialog(mainFrame,
               "Choose background color", Color.white);
            if(backgroundColor != null){
               controlPanel.setBackground(backgroundColor);
               mainFrame.getContentPane().setBackground(backgroundColor);
            }
         }
      });
      controlPanel.add(chooseButton);
      mainFrame.setVisible(true); 
   }
}


JLabel Example

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingControlDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public SwingControlDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
      SwingControlDemo  swingControlDemo = new SwingControlDemo();     
      swingControlDemo.showLabelDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
     
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }       
      });   
      headerLabel = new JLabel("", JLabel.CENTER);       
      statusLabel = new JLabel("",JLabel.CENTER);   
      statusLabel.setSize(350,100);
      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true); 
   }
   private void showLabelDemo(){
      headerLabel.setText("Control in action: JLabel");     
      JLabel label  = new JLabel("", JLabel.CENTER);       
      label.setText("Welcome to Java Program Codes.");
      label.setOpaque(true);
      label.setBackground(Color.GRAY);
      label.setForeground(Color.WHITE);
      controlPanel.add(label);
     
      mainFrame.setVisible(true); 
   }
}


JButton Example

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingControlDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public SwingControlDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
      SwingControlDemo  swingControlDemo = new SwingControlDemo();    
      swingControlDemo.showButtonDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
    
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }      
      });  
      headerLabel = new JLabel("", JLabel.CENTER);      
      statusLabel = new JLabel("",JLabel.CENTER);  
      statusLabel.setSize(350,100);

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);
   }
   private static ImageIcon createImageIcon(String path, String description) {
      java.net.URL imgURL = SwingControlDemo.class.getResource(path);
      if (imgURL != null) {
         return new ImageIcon(imgURL, description);
      } else {          
         System.err.println("Couldn't find file: " + path);
         return null;
      }
   } 
   private void showButtonDemo(){
      headerLabel.setText("Control in action: Button");

      //resources folder should be inside SWING folder.
      ImageIcon icon = createImageIcon("/resources/java_icon.png","Java");

      JButton okButton = new JButton("OK");      
      JButton javaButton = new JButton("Submit", icon);
      JButton cancelButton = new JButton("Cancel", icon);
      cancelButton.setHorizontalTextPosition(SwingConstants.LEFT); 

      okButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            statusLabel.setText("Ok Button clicked.");
         }        
      });
      javaButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            statusLabel.setText("Submit Button clicked.");
         }
      });
      cancelButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            statusLabel.setText("Cancel Button clicked.");
         }
      });
      controlPanel.add(okButton);
      controlPanel.add(javaButton);
      controlPanel.add(cancelButton);     

      mainFrame.setVisible(true);
   }
}



JTable Example

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;

public class JTableExample extends JFrame {

  String data[][] = {{"sateesh", "Visakhapatnam", "Programmer"},
                     {"Davis", "Amaravathi", "Student"},
                     {"Santosh", "Hyderabad", "Associate"},
                     {"Swamy", "Kakinada", "Developer"},
  };

  String fields[] = {"Name", "Surname", "Status"};

  public static void main( String[] argv ) {
    JTableExample myExample = new JTableExample( "JTable Example" );
  }

  public JTableExample( String title ) {
    super( title );
    setSize( 150, 150 );
    addWindowListener( new WindowAdapter() {
      public void windowClosing( WindowEvent we ) {
        dispose();
        System.exit( 0 );
      }
    } );
    init();
    pack();
    setVisible( true );
  }

  private void init() {
    JTable jt = new JTable( data, fields );
    JScrollPane pane = new JScrollPane( jt );
    getContentPane().add( pane );
  }
}