Showing posts with label click. Show all posts
Showing posts with label click. Show all posts

Select all JCheckBox in one click

Watch video below :


*********************************************************************
COMPLETE SOURCE CODE FOR : JCheckBoxMultipleSelectInOneClick.java
*********************************************************************

import javax.swing.JCheckBox;
import javax.swing.JFrame;

import java.awt.GridLayout;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class JCheckBoxMultipleSelectInOneClick extends JFrame implements ActionListener
{
 //Create JCheckBox object
 JCheckBox a=new JCheckBox("Select All");
 JCheckBox b=new JCheckBox("Car");
 JCheckBox c=new JCheckBox("Lorry");
 JCheckBox d=new JCheckBox("Motorcycle");
 
 //Constructor for JCheckBoxMultipleSelectInOneClick class
 public JCheckBoxMultipleSelectInOneClick()
 {
  //Set JFrame title
  super("Select All JCheckBox In One Click");
  
  //Set JFrame layout
  setLayout(new GridLayout(4,1));
  
  //Add action listener to JCheckBox a
  a.addActionListener(this);
  
  //Add JCheckBox into JFrame
  add(a);
  add(b);
  add(c);
  add(d);
  
  //Set JFrame's default close operation when click on close window button
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  //Set JFrame size
  setSize(400,400);
  
  //Make JFrame locate at the center of screen
  setLocationRelativeTo(null);
  
  //Make JFrame visible to see
  setVisible(true);
 }
 
 //Override actionPerformed method in ActionListener interface
 public void actionPerformed(ActionEvent e)
 {
  if(e.getSource()==a)
  {
   //Set all JCheckBox to select when JCheckBox a is select  
   if(a.isSelected()==true)
   {
    b.setSelected(true);
    c.setSelected(true);
    d.setSelected(true);
   }
   
   //Set all JCheckBox to unselect when JCheckBox a is select 
   else
   {
    b.setSelected(false);
    c.setSelected(false);
    d.setSelected(false);
   }
  }
 }

 //Main method where this program start
 public static void main(String[]args)
 {
  JCheckBoxMultipleSelectInOneClick myFirstObject = new JCheckBoxMultipleSelectInOneClick();
 }
}


*********************************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************************

Close JFrame when mouse click release in JFrame

Source code below will create a JFrame. After that, click in the JFrame, hold for a few seconds, and after that release your mouse click. The JFrame that created in this program will be close and this application will terminate.

********************************************************************
COMPLETE SOURCE CODE FOR : CloseJFrameWhenMouseReleased.java
********************************************************************


import javax.swing.JFrame;

import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class CloseJFrameWhenMouseReleased
{
public static void main(String[]args)
{
//Create mouse listener that will listen when mouse released in JFrame
MouseListener ml=new MouseAdapter()
{
public void mouseReleased(MouseEvent event)
{
//Put JFrame close code here
System.exit(0);
}
};

//Create JFrame with title ( CLICK ON ME,HOLD FOR A FEW SECOND,RELEASE YOUR CLICK )
JFrame frame=new JFrame("CLICK ON ME,HOLD FOR A FEW SECOND,RELEASE YOUR CLICK");

//Add mouse listener to JFrame
frame.addMouseListener(ml);

//Set default close operation to JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size to :
//Width : 700 pixels
//Height : 400 pixels
frame.setSize(700,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


********************************************************************
JUST COMPILE AND EXECUTE IT
********************************************************************

Close JFrame using mouse click

Source code below will show you, how to close JFrame when click on it.

*****************************************************************************
COMPLETE SOURCE CODE FOR : CloseJFrameUsingMouseClick.java
*****************************************************************************


import javax.swing.JFrame;

import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class CloseJFrameUsingMouseClick
{
public static void main(String[]args)
{
//Create mouse listener that will listen when someone click on JFrame
MouseListener ml=new MouseAdapter()
{
public void mouseClicked(MouseEvent event)
{
//Put JFrame close code here
System.exit(0);
}
};

//Create JFrame with title ( CLICK IN THE BOX BELOW !!!!!!!!! )
JFrame frame=new JFrame("CLICK IN THE BOX BELOW !!!!!!!!!");

//Add mouse listener to JFrame
frame.addMouseListener(ml);

//Set default close operation to JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size to :
//Width : 400 pixels
//Height : 400 pixels
frame.setSize(400,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


*****************************************************************************
JUST COMPILE AND EXECUTE IT
*****************************************************************************

Close JWindow using mouse click on it

Source code below will show you how to create a JWindow that can terminate when we click on it.

************************************************************************
COMPLETE SOURCE CODE FOR : ClosingJWindowUsingMouseClick.java
************************************************************************


import javax.swing.JWindow;
import javax.swing.JLabel;

import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import java.awt.FlowLayout;

public class ClosingJWindowUsingMouseClick
{
public static void main(String[]args)
{
//Create a JWindow
JWindow window=new JWindow();

//Create a JLabel that we want to add to JWindow
JLabel label=new JLabel("Click me to exit");

//Set layout for JWindow
window.setLayout(new FlowLayout());

//Add label to JWindow
window.add(label);

//Create Mouse Listener that listen when someone click on that JWindow
MouseListener ml=new MouseAdapter()
{
public void mousePressed(MouseEvent evt)
{
//Exit application
System.exit(0);
}
};

//Add Mouse Listener to JWindow
window.addMouseListener(ml);

//Set JWindow size with width=400 pixels and height=400 pixels
window.setSize(400,400);

//Make JWindow visible
window.setVisible(true);
}
}


************************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************************

Close application when click on JFrame close button


*********************************************************
COMPLETE SOURCE CODE FOR : ExitApplicationUsingJFrameCloseButton.java
*********************************************************


import javax.swing.JFrame;

public class ExitApplicationUsingJFrameCloseButton
{
public static void main(String[]args)
{
JFrame a=new JFrame("EXIT AN APPLICATION USING JFRAME CLOSE BUTTON");

//JFrame.EXIT_ON_CLOSE will make your application close when you hit JFrame close button
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a.setSize(200,200);
a.setVisible(true);
}
}


*********************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************

JFrame hide when click close button


**********************************************************
COMPLETE SOURCE CODE FOR : HidingJFrameWithoutDestroy.java
**********************************************************


import javax.swing.JFrame;
import javax.swing.JOptionPane;

import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class HidingJFrameWithoutDestroy
{
public static void main(String[]args)
{
JFrame a=new JFrame("HIDE A JFRAME");

a.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
try
{
JFrame temp=(JFrame)evt.getSource();
temp.setVisible(false);
String a=JOptionPane.showInputDialog(null,"CHOOSE ONE : \n 1-Make It Visible \n 2-Exit");
int b=Integer.parseInt(a);

if(b==1)
{
temp.show(true);
}

else if(b==2)
{
System.exit(0);
}

else
{
JOptionPane.showMessageDialog(null,"YOU PUT WRONG NUMBER");
}
}
catch(Exception exception)
{
JOptionPane.showMessageDialog(null,"ERROR, DON'T DO THAT\nTHIS PROGRAM WILL CLOSE");
System.exit(0);
}
}

public void windowActivated(WindowEvent event)
{
JFrame temp=(JFrame)event.getSource();
temp.setVisible(true);
}
}
);

a.setSize(200,200);
a.setVisible(true);
}
}


**********************************************************
JUST COMPILE AND EXECUTE IT
**********************************************************

Set jframe size and it's location when click on maximize button


***********************************************************
COMPLETE SOURCE CODE FOR : SettingBound.java
***********************************************************


/**
*THIS TUTORIAL WILL SHOW YOU HOW TO SET THE MAXIMUM SIZE OF YOUR FRAME
*AND IT'S LOCATION WHEN YOU CLICK
*MAXIMIZE BUTTON BESIDE JFRAME CLOSE BUTTON
**/

import java.awt.Rectangle;
import javax.swing.JFrame;

public class SettingBound
{
public static void main(String[]args)
{
//first value, 400=x coordinate on your screen
//second value, 400=y coordinate on your screen
//third value, 800=JFrame width
//fourth value, 200=JFrame height
Rectangle rec=new Rectangle(400,400,800,200);

JFrame frame1=new JFrame("Setting Bound");

frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setMaximizedBounds(rec);//set maximize setting for your JFrame
frame1.setVisible(true);
}
}


***********************************************************
JUST COMPILE AND EXECUTE IT
***********************************************************

RELAXING NATURE VIDEO