Tuesday, December 31, 2013

Standard Dialog Boxes

int response=JOptionPane.showConfirmDialog(null,"Delete Files?");


response=JOptionPane.showConfirmDialog(null,
                             "Error Reading Files?",
                             "File Input Error",
                             JOptionPane.YES_NO_OPTION,
                             JOptionPane.ERROR_MESSAGE);  



String resp;
resp=JOptionPane.showInputDialog(null,"Enter your Name");





JOptionPane.showMessageDialog(null,
                              "The message you want to expose",
                              "Message Title",
                              JOptionPane.WARNING_MESSAGE);
 



Monday, December 30, 2013

Greatest Common Divisor and Least Common Multiple Java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class mygcdlcm extends JFrame implements ActionListener
{   JButton jbgcd= new JButton("GCD");
    JButton jblcm= new JButton("LCM");
    JLabel jl1 = new JLabel("Number 1 ");
    JLabel jl2 = new JLabel("Number 2 ");
    JLabel jl3 = new JLabel("Result = ");
    JTextField jt1=new JTextField(10);
    JTextField jt2=new JTextField(10);
    JTextField jt3=new JTextField(10);
    public mygcdlcm()
    {   super("GCD LCM");                
        setSize(200, 200);
        jbgcd.addActionListener(this);   
        jblcm.addActionListener(this);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel pane = new JPanel();
        pane.setLayout(new GridLayout(4,2));
        pane.add(jl1);    pane.add(jt1);
        pane.add(jl2);    pane.add(jt2);
        pane.add(jl3);    pane.add(jt3);
        pane.add(jbgcd); pane.add(jblcm);
        jt3.setEditable(false);       
        setContentPane(pane);
        setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
    {  int n1,n2,i,sn,ln;
       n1=Integer.parseInt(jt1.getText());
       n2=Integer.parseInt(jt2.getText());
       sn= n1>n2 ? n2 : n1;
       ln= n1>n2 ? n1 : n2;
       if(e.getSource()==jbgcd)
       {  for(i=sn;i>=1;i--)
              if(n1%i==0 && n2 %i==0) break;
          jt3.setText(i+"");
      jl3.setText("GCD");
       }
       if(e.getSource()==jblcm)
       {  for(i=ln;i<=n1*n2;i++)
              if(i%n1==0 && i%n2==0) break;
          jt3.setText(i+"");
      jl3.setText("LCM");
       }
    }
    public static void main(String[] arguments) {
        mygcdlcm rb = new mygcdlcm();
        rb.show();
    }
}


JColorChooser sample Java

//JColorChooser Sample
// There will be a JFrame with a button in it. Click
// the button and choose a color the button color
// will be the chosen color
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class CColor extends JFrame implements ActionListener
{
    JButton jb1;
    JPanel pane;
    public CColor()
    {
        setSize(300,200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jb1=new JButton("Change Color");
        jb1.addActionListener(this);
        pane=new JPanel();
        pane.add(jb1);
        setContentPane(pane);
        setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==jb1)
        {
          Color bgColor= JColorChooser.showDialog(this,"Choose Color",getBackground());
          if (bgColor != null)
            jb1.setBackground(bgColor);
        }
    }
    public static void main(String args[])
    {
        new CColor();
    }
}

Change Program Icon Java


//Changing the program icon
import javax.swing.*;
import java.awt.*;
class icontest extends JFrame
{
  public icontest()
  {
    super("Icon Test");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300,200);
    setLocationRelativeTo(null);
    Toolkit kit=Toolkit.getDefaultToolkit();
    Image img=kit.getImage("myicon.gif");
    setIconImage(img);
    setVisible(true);
  }
  public static void main(String args[])
  {
    icontest one=new icontest();
  }
}

Sunday, December 29, 2013

Mnemonic for JButton (Shortcut Key assignment)



import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class MnemonicTest extends JFrame implements ActionListener
{
    JPanel pane;
    JButton jb1,jb2,jb3;
    JTextField jtf1;
    public MnemonicTest()
    {
        super("Testing buttons");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300,200);
        jb1=new JButton("Display");
        jb2=new JButton("Print");
        jb3=new JButton("Sign up");

        jb1.setMnemonic('D');
        jb2.setMnemonic('P');
        jb3.setMnemonic('S');

        jtf1=new JTextField(15);
        pane=new JPanel();
        pane.add(jb1);
        pane.add(jb2);
        pane.add(jb3);
        pane.add(jtf1);
        setContentPane(pane);
        setVisible(true);
        jb1.addActionListener(this);
        jb2.addActionListener(this);
        jb3.addActionListener(this);
       
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==jb1)
            jtf1.setText("Display click");

        if(e.getSource()==jb2)
            jtf1.setText("Print click");

        if(e.getSource()==jb3)
            jtf1.setText("Sign up click");
    }
    public static void main(String args[])
    {
        MnemonicTest one=new MnemonicTest();
    }
}