Trace text of two JTextField in one JTextArea in java Swing


import javax.swing.SpringLayout;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.awt.*;
public class SwingTask1Eg extends WindowAdapter implements ActionListener
{
 JFrame f;
 JCheckBox chkName,chkAge,chkShow;
 JButton bRed,bBlue,bGreen,bShow;

 JTextField tfNmae,tfAge;
 JTextArea taShow;
 JLabel lbl[] = new JLabel[5];

SwingTask1Eg()
{
 f=new JFrame("task1 Example..");

 lbl[0]= new JLabel("This is lable");
 lbl[1]= new JLabel("This is lable");
 lbl[2]= new JLabel("This is lable");
 lbl[3]= new JLabel("This is lable");
 lbl[4]= new JLabel("This is lable");

 lbl[0].setVisible(false);
 lbl[1].setVisible(false);
 lbl[2].setVisible(false);
 lbl[3].setVisible(false);
 lbl[4].setVisible(false);

 chkName=new JCheckBox("Name: ");
 chkAge=new JCheckBox("Age: ");
 chkShow=new JCheckBox("Trace: ");
 tfNmae= new JTextField();
 tfAge= new JTextField();
 taShow= new JTextArea();
 bRed= new JButton("Red");
 bBlue= new JButton("Blue");
 bGreen= new JButton("Green");
 bShow= new JButton("Show");

 tfNmae.setBorder(BorderFactory.createCompoundBorder(
    tfNmae.getBorder(),
    BorderFactory.createLineBorder(Color.WHITE, 20)));

 tfAge.setBorder(BorderFactory.createCompoundBorder(
    tfAge.getBorder(),
    BorderFactory.createLineBorder(Color.WHITE, 20)));

 taShow.setBorder(BorderFactory.createCompoundBorder(
    taShow.getBorder(),
    BorderFactory.createLineBorder(Color.WHITE, 20)));

 f.setLayout(new GridLayout(5,3));

 f.add(chkName);
 f.add(tfNmae);
 f.add(lbl[0]);

 f.add(chkAge);
 f.add(tfAge);
 f.add(lbl[1]);

 f.add(chkShow);
 f.add(taShow);
 f.add(lbl[2]);

 f.add(bRed);
 f.add(bBlue);
 f.add(bGreen);

 f.add(lbl[3]);
 f.add(bShow);
 f.add(lbl[4]);

 f.setSize(400,400);
 f.setVisible(true);

 bRed.addActionListener(this);
 bBlue.addActionListener(this);
 bGreen.addActionListener(this);
 bShow.addActionListener(this);
 f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
 f.addWindowListener(this);

}
public void actionPerformed(ActionEvent ae)
  {
   if(ae.getSource().equals(bRed))//red button event
   {
    if(chkShow.isSelected())
    {
     if((chkName.isSelected())&&(chkAge.isSelected()))
     {
      String s1=tfNmae.getText()+" "+tfAge.getText();


      taShow.setForeground(Color.RED);
      taShow.setText(s1);
     }
     else if(chkName.isSelected())
     {

      taShow.setForeground(Color.RED);
      taShow.setText(tfNmae.getText());
     }
     else if(chkAge.isSelected())
     {
      taShow.setForeground(Color.RED);
      taShow.setText(tfAge.getText());
     }
     else if ((chkName.isSelected()==false)&&(chkAge.isSelected()==false))
     {
      taShow.setText(null);
     }
    }
    else
    {
     taShow.setText(null);
    }
   }
   if(ae.getSource().equals(bBlue))//blue button event
   {
    if(chkShow.isSelected())
    {
     if((chkName.isSelected())&&(chkAge.isSelected()))
     {
      String s1=tfNmae.getText()+" "+tfAge.getText();

      taShow.setForeground(Color.BLUE);
      taShow.setText(s1);
     }
     else if(chkName.isSelected())
     {
      taShow.setForeground(Color.BLUE);
      taShow.setText(tfNmae.getText());
     }
     else if(chkAge.isSelected())
     {
      taShow.setForeground(Color.BLUE);
      taShow.setText(tfAge.getText());
     }
     else if ((chkName.isSelected()==false)&&(chkAge.isSelected()==false))
     {
      taShow.setText(null);
     }
    }
    else
    {
     taShow.setText(null);
    }
   }
   if(ae.getSource().equals(bGreen))//green button event
   {
    if(chkShow.isSelected())
    {
     if((chkName.isSelected())&&(chkAge.isSelected()))
     {
      String s1=tfNmae.getText()+" "+tfAge.getText();

      taShow.setForeground(Color.GREEN);
      taShow.setText(s1);
     }
     else if(chkName.isSelected())
     {
      taShow.setForeground(Color.GREEN);
      taShow.setText(tfNmae.getText());
     }
     else if(chkAge.isSelected())
     {
      taShow.setForeground(Color.GREEN);
      taShow.setText(tfAge.getText());
     }
     else if ((chkName.isSelected()==false)&&(chkAge.isSelected()==false))
     {
      taShow.setText(null);
     }
    }
    else
    {
     taShow.setText(null);
    }
   }
   if(ae.getSource().equals(bShow))//Show button event
   {
    if(chkShow.isSelected())
    {
     if((chkName.isSelected())&&(chkAge.isSelected()))
     {
      String s1=tfNmae.getText()+" "+tfAge.getText();
      taShow.setText(s1);
     }
     else if(chkName.isSelected())
     {
      taShow.setText(tfNmae.getText());
     }
     else if(chkAge.isSelected())
     {
      taShow.setText(tfAge.getText());
     }
     else if ((chkName.isSelected()==false)&&(chkAge.isSelected()==false))
     {
      taShow.setText(null);
     }
    }
    else
    {
     taShow.setText(null);
    }
   }
  }

public void windowClosing(WindowEvent we)//For Exit windowClosing event
{
 int a = JOptionPane.showConfirmDialog(f,"Are You Sure you want to exit? ");
 if(a==JOptionPane.YES_OPTION)
 {
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}
 
 public static void main(String arg[])
 {
  new SwingTask1Eg();
 }
}


Comments

Popular posts from this blog

Java Project of restaurant dish menu System with File handling java IO | With Code

Custom MenuItem with Notification on it

Encoded Password Save in database | asp.net with c#