Thursday 23 February 2017

Java Open Dialogbox

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

    public class FileChooserDemo extends JFrame {
     static private final String newline = "\n";
   
         public FileChooserDemo() {
         super("FileChooserDemo");
       
         //Create the log first, because the action listeners
         //need to refer to it.
         final JTextArea log = new JTextArea(5,20);
         log.setMargin(new Insets(5,5,5,5));
         log.setEditable(false);
         JScrollPane logScrollPane = new JScrollPane(log);
       
         //Create a file chooser
         final JFileChooser fc = new JFileChooser();
       
         //Create the open button
         ImageIcon openIcon = new ImageIcon("images/open.gif");
         JButton openButton = new JButton("Open a File...", openIcon);
             openButton.addActionListener(new ActionListener() {
                 public void actionPerformed(ActionEvent e) {
                 int returnVal = fc.showOpenDialog(FileChooserDemo.this);
               
                     if (returnVal == JFileChooser.APPROVE_OPTION) {
                     File file = fc.getSelectedFile();
                     //this is where a real application would open the file.
                     log.append("Opening: " + file.getName() + "." + newline);
                     } else {
                     log.append("Open command cancelled by user." + newline);
                 }
             }
         });
       
         //Create the save button
         ImageIcon saveIcon = new ImageIcon("save.gif");
         JButton saveButton = new JButton("Save a File...", saveIcon);
             saveButton.addActionListener(new ActionListener() {
                 public void actionPerformed(ActionEvent e) {
                 int returnVal = fc.showSaveDialog(FileChooserDemo.this);
               
                     if (returnVal == JFileChooser.APPROVE_OPTION) {
                     File file = fc.getSelectedFile();
                     //this is where a real application would save the file.
                     log.append("Saving: " + file.getName() + "." + newline);
                     } else {
                     log.append("Save cancelled by user." + newline);
                 }
             }
         });
       
         JPanel buttonPanel = new JPanel();
         buttonPanel.add(openButton);
         buttonPanel.add(saveButton);
         openButton.setNextFocusableComponent(saveButton);
         saveButton.setNextFocusableComponent(openButton);
         Container contentPane = getContentPane();
         contentPane.add(buttonPanel, BorderLayout.NORTH);
         contentPane.add(logScrollPane, BorderLayout.CENTER);
     }
   
         public static void main(String[] args) {
         JFrame frame = new FileChooserDemo();
       
             frame.addWindowListener(new WindowAdapter() {
                 public void windowClosing(WindowEvent e) {
                 System.exit(0);
             }
         });
       
         frame.pack();
         frame.setVisible(true);
     }
}


Java ToolBar Demo

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

    public class ToolBarDemo2 extends JFrame {
     protected JTextArea textArea;
     protected String newline = "\n";
  
         public ToolBarDemo2() {
         //Do frame stuff.
         super("ToolBarDemo2");
             addWindowListener(new WindowAdapter() {
                 public void windowClosing(WindowEvent e) {
                 System.exit(0);
             }
         });
      
         JToolBar toolBar = new JToolBar();
         toolBar.setFloatable(false);
         addButtons(toolBar);
         textArea = new JTextArea(5, 30);
         JScrollPane scrollPane = new JScrollPane(textArea);
      
         //Lay out the content pane.
         JPanel contentPane = new JPanel();
         contentPane.setLayout(new BorderLayout());
         contentPane.setPreferredSize(new Dimension(400, 100));
         contentPane.add(toolBar, BorderLayout.NORTH);
         contentPane.add(scrollPane, BorderLayout.CENTER);
         setContentPane(contentPane);
     }
  
         protected void addButtons(JToolBar toolBar) {
         JButton button = null;
      
         button = new JButton(new ImageIcon("images/left.gif"));
         button.setToolTipText("This is the left button");
             button.addActionListener(new ActionListener() {
                 public void actionPerformed(ActionEvent e) {
                 displayResult("Action for first button");
             }
         });
         toolBar.add(button);
      
         button = new JButton(new ImageIcon("images/middle.gif"));
         button.setToolTipText("This is the middle button");
             button.addActionListener(new ActionListener() {
                 public void actionPerformed(ActionEvent e) {
                 displayResult("Action for second button");
             }
         });
         toolBar.add(button);
      
         button = new JButton(new ImageIcon("images/right.gif"));
         button.setToolTipText("This is the right button");
             button.addActionListener(new ActionListener() {
                 public void actionPerformed(ActionEvent e) {
                 displayResult("Action for third button");
             }
         });
         toolBar.add(button);
      
         toolBar.addSeparator();
      
         button = new JButton("Another button");
             button.addActionListener(new ActionListener() {
                 public void actionPerformed(ActionEvent e) {
                 displayResult("Action for fourth button");
             }
         });
         toolBar.add(button);
      
         JTextField textField = new JTextField("A text field");
         toolBar.add(textField);
     }
  
         protected void displayResult(String actionDescription) {
         textArea.append(actionDescription + newline);
     }
  
         public static void main(String[] args) {
         ToolBarDemo2 frame = new ToolBarDemo2();
         frame.pack();
         frame.setVisible(true);
     }
}


Using Layout Managers in Java

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

public class UsingLayoutManagers extends JFrame {
  public UsingLayoutManagers() {
    super( "Common Layout Managers" );
    setSize( 500, 380 );

    JDesktopPane desktop = new JDesktopPane();
    getContentPane().add( desktop );

    JInternalFrame fr1 = new JInternalFrame( "FlowLayout", true, true );
    fr1.setBounds( 10, 10, 150, 150 );
    Container c = fr1.getContentPane();
    c.setLayout( new FlowLayout() );
    c.add( new JButton( "1" ) );
    c.add( new JButton( "2" ) );
    c.add( new JButton( "3" ) );
    c.add( new JButton( "4" ) );
    desktop.add( fr1, 0 );

    JInternalFrame fr2 = new JInternalFrame( "GridLayout", true, true );
    fr2.setBounds( 170, 10, 150, 150 );
    c = fr2.getContentPane();
    c.setLayout( new GridLayout( 2, 2 ) );
    c.add( new JButton( "1" ) );
    c.add( new JButton( "2" ) );
    c.add( new JButton( "3" ) );
    c.add( new JButton( "4" ) );
    desktop.add( fr2, 0 );

    JInternalFrame fr3 = new JInternalFrame( "BorderLayout", true, true );
    fr3.setBounds( 330, 10, 150, 150 );
    c = fr3.getContentPane();
    c.add( new JButton( "1" ), BorderLayout.NORTH );
    c.add( new JButton( "2" ), BorderLayout.EAST );
    c.add( new JButton( "3" ), BorderLayout.SOUTH );
    c.add( new JButton( "4" ), BorderLayout.WEST );
    desktop.add( fr3, 0 );

    JInternalFrame fr4 = new JInternalFrame( "BoxLayout - X", true, true );
    fr4.setBounds( 10, 170, 250, 120 );
    c = fr4.getContentPane();
    c.setLayout( new BoxLayout( c, BoxLayout.X_AXIS ) );
    c.add( new JButton( "1" ) );
    c.add( Box.createHorizontalStrut( 12 ) );
    c.add( new JButton( "2" ) );
    c.add( Box.createGlue() );
    c.add( new JButton( "3" ) );
    c.add( Box.createHorizontalGlue() );
    c.add( new JButton( "4" ) );
    desktop.add( fr4, 0 );

    JInternalFrame fr5 = new JInternalFrame( "BoxLayout - Y", true, true );
    fr5.setBounds( 330, 170, 150, 180 );
    c = fr5.getContentPane();
    c.setLayout( new BoxLayout( c, BoxLayout.Y_AXIS ) );
    c.add( new JButton( "1" ) );
    c.add( Box.createVerticalStrut( 10 ) );
    c.add( new JButton( "2" ) );
    c.add( Box.createGlue() );
    c.add( new JButton( "3" ) );
    c.add( Box.createVerticalGlue() );
    c.add( new JButton( "4" ) );
    desktop.add( fr5, 0 );

    try {
      fr1.setSelected( true );
    }
    catch( java.beans.PropertyVetoException ex ) {
    }

    WindowListener wndCloser = new WindowAdapter() {
      public void windowClosing( WindowEvent e ) {
        System.exit( 0 );
      }
    };
    addWindowListener( wndCloser );
    fr1.show();
    fr2.show();
    fr3.show();
    fr4.show();
    fr5.show();
    setVisible( true );
  }

  public static void main( String argv[] ) {
    new UsingLayoutManagers();
  }
}