Perdorimi i klases JCheckBox dhe ButtonGroup

 Nje JCheckBox eshte nje etikete e pozicionuar anash nje katrori. Ju mund te klikoni katrorin per te shfaqur ose jo nje “check mark”, ne menyre qe te zgjidhni ose jo nje opsion.

Hierarkia e klases JCheckBox:

  • java.lang.Object
    • java.awt.Component
      • java.awt.Container
        • javax.swing.JComponent
          • javax.swing.AbstractButton
            • javax.swing.JToggleButton
              • javax.swing.JCheckBox

Konstruktoret e klases JCheckBox:

JCheckBox()

Krijon nje check box pa tekst dhe te paselektuar.

JCheckBox(Icon icon)

Krijon nje check box pa tekst dhe te paselektuar, me nje ikone.

JCheckBox(Icon icon, boolean selected)

Krijon nje check box me ikone dhe te selektuar ose jo.

JCheckBox(String text)

Krijon nje check box me tekst dhe te paselektuar.

JCheckBox(String text, boolean selected)

Krijon nje check box pa tekst dhe me selektimin e specifikuar.

JCheckBox(String text, Icon icon)

Krijon nje check box me tekst dhe ikone dhe te paselektuar.

JCheckBox(String text, Icon icon, boolean selected)

Krijon nje check box me tekst dhe ikone dhe te selektuar ose jo.

 

Metodat e perdorimit te JCheckBox

void setText(String)  vendos nje tekst ne  JCheckBox

 

String getText() Kthen tekstin ne nje  JCheckBox

void setSelected(boolean) Vendos gjendjen e JCheckBox ne true nese eshte e selektuar dhe false perndryshe.

 

boolean isSelected()  jep gjendjen e JcheckBox (nese eshte e selektuar ose jo)

Shembuj deklarimi:

 

» JCheckBox box2 = new JCheckBox("Check here");

 

» JCheckBox box3 = new JCheckBox("Check here", false);

 

// Shembull

 


     import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CheckBoxDemonstrationOk extends JFrame implements ItemListener
{
FlowLayout flow = new FlowLayout();
JLabel label = new JLabel("What would you like to drink?");
JLabel label1 = new JLabel();
JCheckBox coffee = new JCheckBox("Coffee", false);
JCheckBox cola = new JCheckBox("Cola", false);
JCheckBox milk = new JCheckBox("Milk", false);
JCheckBox water = new JCheckBox("Water", false);
String s1="",s2="",s3="",s4="";
	double fatura = 0.0;
public CheckBoxDemonstrationOk()
{
super("CheckBox Demonstration");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
label.setFont(new Font("Arial", Font.ITALIC, 22));
coffee.addItemListener(this);
cola.addItemListener(this);
milk.addItemListener(this);
water.addItemListener(this);
label1.setFont(new Font("Arial", Font.ITALIC, 22));
add(label);
add(coffee);
add(cola);
add(milk);
add(water);
add(label1);
}
public void itemStateChanged(ItemEvent e)
{
	

	Object source = e.getItem(); // cili objekt gjeneroi ngjarjen
	int select= e.getStateChange();	// percakton nese zgjedhja eshte selektim apo jo
		
	if(source == coffee)
	{
		if (select==ItemEvent.SELECTED)
		{
		s1 =  "Kafe: 50 lek ";
	    fatura+=50;
		}
		else
		{
		s1 =  "";
	    fatura-=50;
		}
		
	}
	
	
	else if(source == cola)
	{
		if (select==ItemEvent.SELECTED)
		{		
		s2 = "Cola 150 lek ";
	    fatura+=150;
		}
		else
		{		
		s2 = "";
	    fatura-=150;
		}
	
	}
		
	
	else if(source == milk)
	{	
		if (select==ItemEvent.SELECTED)
		{	s3 = "Milk 100 lek ";
	        fatura+=100;
		}
		else
		{	s3 = "";
	        fatura-=100;
		}	

	}

		
	else 
		if (select==ItemEvent.SELECTED)
	{
	s4 = "Water 70 lek";
	fatura+=70;
	}
	else
	{
	s4 = "";
	fatura-=70;
	}	
	
if (fatura>0)
     label1.setText("Ju zgjodhet "+s1+s2+s3+s4+ "  Fatura total = "+fatura);
else
     label1.setText("Ju nuk keni zgjedhur ");
}
public static void main(String[] arguments)
{
final int FRAME_WIDTH = 900;
final int FRAME_HEIGHT = 250;
CheckBoxDemonstrationOk frame =new CheckBoxDemonstrationOk();
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setVisible(true);
}
}

Klasa ButtonGroup:

 

  • ButtonGroup

–          Grupon disa komponente ne menyre qe perdoruesi te selektoje vetem nje ne nje kohe.

 

  • Kur ju gruponi objektet JCheckBox te gjithe JCheckBoxe-t e tjera kalojne ne gjendjen DESELECTED  kur nje JCheckBox kalon ne gjendjen SELECTED

 

  • Per te krijuar nje ButtonGroup ne nje JFrame dhe me pas te shtohet nje JCheckBox perdoren instruksionet:

 

ButtonGroup aGroup = new ButtonGroup();

JCheckBox aBox = new JCheckBox();

            aGroup.add(aBox);