Hellow everybody,

Somebody please tell me how to trigger the right Event listener to a text 
field in java

In the example below all components modify their corresponding panel's 
background color including the text field

the latter do this streatement by writing the color among five colors,my 
issue is what is the right listener or adapter to use for it

Thanks in advance

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerModel;
import javax.swing.border.TitledBorder;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;


public class Exercice5 extends JFrame {
	JPanel pan = new JPanel(new GridLayout(3,1));
	JPanel phaut = new JPanel(new FlowLayout(FlowLayout.CENTER,25,15));
	JPanel pcentre = new JPanel();
	JPanel pbas = new JPanel(); 
	JSlider slid = new JSlider(JSlider.HORIZONTAL, 0, 4, 0);
	JRadioButton rgreen = new JRadioButton("green");
	JRadioButton rblue = new JRadioButton("blue");
	JRadioButton rred = new JRadioButton("red");
	JRadioButton rmagenta = new JRadioButton("magenta");
	JRadioButton rorange = new JRadioButton("orange");
	String   couleurs[] = {"green","blue","red","magenta","orange"};
	JComboBox cb1 = new JComboBox(couleurs);
	SpinnerModel spm = new SpinnerListModel(couleurs);
	JSpinner spin = new JSpinner(spm);
	JTextField field1 = new JTextField("blanc",15);
	 
    public Exercice5 () {
    	
	this.setTitle("Exercice 5");
	this.setBounds(300, 100, 750, 225);
	this.setContentPane(pan); 	 
	spin.getEditor().setPreferredSize(new Dimension(60, 20));
	pan.add(phaut,BorderLayout.NORTH);
	pan.add(pcentre,BorderLayout.CENTER);
	pan.add(pbas,BorderLayout.SOUTH); 
	phaut.add(spin);
	slid.setMinorTickSpacing(1);
	slid.setPaintTicks(true);
	ButtonGroup group = new ButtonGroup();
	group.add(rgreen);
	group.add(rblue);
	group.add(rred);
	group.add(rmagenta);
	group.add(rorange);
	pcentre.setLayout(new FlowLayout(FlowLayout.CENTER,20,10));
    pcentre.add(cb1);
    pcentre.add(slid);
    pcentre.add(rgreen);
    pcentre.add(rblue);
    pcentre.add(rred);
    pcentre.add(rmagenta);
    pcentre.add(rorange); 
    pbas.setLayout(new FlowLayout(FlowLayout.CENTER,20,10));
    TitledBorder postitle = BorderFactory.createTitledBorder("panel de saisie");
    pbas.setBorder(postitle);
    pbas.add(new JLabel("Color name (English)?"));
    pbas.add(field1);
    class Testcouleurs{
    	JPanel pl;
    	String s;
    	Testcouleurs(JPanel pl,String s) {
    		this.pl=pl;
    		this.s=s;
    		if(s=="green") pl.setBackground(Color.GREEN);
    		if(s=="blue") pl.setBackground(Color.BLUE);
    		if(s=="red") pl.setBackground(Color.RED);
    		if(s=="magenta") pl.setBackground(Color.MAGENTA);
    		if(s=="orange") pl.setBackground(Color.ORANGE);
    	}	
    }
    class EventChange implements ChangeListener { 
    	JPanel panel; 
    	JComponent c;
    	 EventChange(JPanel pan,JComponent cp){
    		 this.panel = pan;
    		 this.c = cp;
    	 }
		public void stateChanged(ChangeEvent a) { 
			    String sp = spin.getValue().toString();
			    new Testcouleurs(panel,sp);		
		} 			 
	}
    class EventItem implements ItemListener {
    	JPanel panel;  
    	 EventItem(JPanel pan){
    		 this.panel = pan; 
    	 }
		public void itemStateChanged(ItemEvent e) {
			 String sp = cb1.getSelectedItem().toString();
			 new Testcouleurs(panel,sp);	
		}  
    } 
    class EventDocument implements DocumentListener {
    	JPanel panel; 
    	EventDocument(JPanel pan){
   		 this.panel = pan; 
   	 } 
		public void changedUpdate(DocumentEvent a) {
			 String sp = field1.getText();
			 new Testcouleurs(panel,sp);
		} 
		public void insertUpdate(DocumentEvent a) {} 
		public void removeUpdate(DocumentEvent a) {} 			 
	} 
    class FocusDocument implements FocusListener { 
		public void focusGained(FocusEvent arg0) {
			 
		} 
		public void focusLost(FocusEvent arg0) {
			 
		} 
    }
	class mousel implements MouseListener { 
		JPanel p;
		 mousel(JPanel i){
			this.p = i;
		}
		public void mouseClicked(MouseEvent e1) {
			
		}  
		public void mouseReleased(MouseEvent a) {
			 String sp = field1.getText();
			 new Testcouleurs(p,sp);
		}
		public void mouseEntered(MouseEvent e1) {} 
		public void mouseExited(MouseEvent e1) {} 
		public void mousePressed(MouseEvent e1) {}   
	} 
	class caretl implements CaretListener {
		JPanel p;
		caretl(JPanel i){
			this.p = i;
		}
		public void caretUpdate(CaretEvent arg0) {
			 String sp = field1.getText();
			 new Testcouleurs(p,sp);
		}
		
	}
	class colorlistener implements ActionListener {
		Color c;
		colorlistener(Color i){
			this.c = i;
		}
		public void actionPerformed(ActionEvent e1) {
 			pcentre.setBackground(c);
	}			 
	}
	spin.addChangeListener(new EventChange(phaut,spin)); 
	cb1.addItemListener(new EventItem(pcentre));
	rgreen.addActionListener(new colorlistener(Color.GREEN));
	rblue.addActionListener(new colorlistener(Color.BLUE));
	rmagenta.addActionListener(new colorlistener(Color.MAGENTA));
	rred.addActionListener(new colorlistener(Color.RED));
	rorange.addActionListener(new colorlistener(Color.ORANGE));
	// field1.getDocument().addDocumentListener(new EventDocument(pbas));
	// field1.addMouseListener(new mousel(pbas));
	// field1.addFocusListener(new FocusAdapter() {
	//	    public void focusGained(FocusEvent e) {
	//	    	String sp = field1.getText();
	//			 new Testcouleurs(pbas,sp);
	//	    }
    //	});
	field1.addCaretListener(new caretl(pbas));
	
    }
    
    public static void main(String[] args) {
		Exercice5 e5 = new Exercice5();
		e5.setVisible(true);
		e5.setResizable(false);
	}
}

Reply via email to