import javax.swing.*;



import java.awt.*;
import java.awt.event.*;

public class RelocationTest extends JFrame implements ActionListener, ItemListener {
	JTextField currentLocation = null;
	JTextField newLocationX = null;
	JTextField newLocationY = null;
	JLabel currentLocationDisplay = null;
	JLabel newX = null;
	JLabel newY = null;
	JLabel lnfs = null;
	JButton changeLocation = null;
	JButton displayLocation = null;
	JPanel panel = null;
	JComboBox lnf = null;
	ActionListener al = this;
	ItemListener il =this;

	public RelocationTest() throws Exception {
		super("Test Frame");
		SwingUtilities.invokeAndWait(new Runnable() {
			public void run() {
				panel = new JPanel(new GridLayout(5, 2));
				currentLocation = new JTextField("");
				newLocationX = new JTextField("");
				newLocationY = new JTextField("");
				changeLocation = new JButton("Change Location");
				displayLocation = new JButton("Display Location");
				currentLocationDisplay = new JLabel("Current Location");
				newX = new JLabel("New X");
				newY = new JLabel("New Y");
				lnfs = new JLabel("Select Look and Feel");

    
    
		        UIManager.LookAndFeelInfo LookAndFeelInfos[]
		                = UIManager.getInstalledLookAndFeels();
		        String[] availableLookAndFeels = new String[LookAndFeelInfos.length];	        
		        for (int i = 0; i < LookAndFeelInfos.length; i++) {
		        	availableLookAndFeels[i]=LookAndFeelInfos[i].getClassName();
		        }
		        
		        lnf = new JComboBox(availableLookAndFeels);

				changeLocation.addActionListener(al);
				displayLocation.addActionListener(al);
				lnf.addItemListener(il);
				
				panel.add(currentLocationDisplay);
				panel.add(currentLocation);
				panel.add(newX);
				panel.add(newLocationX);
				panel.add(newY);
				panel.add(newLocationY);
				panel.add(changeLocation);
				panel.add(displayLocation);
				panel.add(lnfs);
				panel.add(lnf);

				add(panel);
				setSize(600, 200);
				setVisible(true);
			}
		});
	}

	public void actionPerformed(ActionEvent e) {
		String buttonPressed = e.getActionCommand();

		if (buttonPressed.equals("Display Location")) {
			currentLocation.setText(this.getLocation().toString());
		} else if (buttonPressed.equals("Change Location")) {
			String x = newLocationX.getText();
			String y = newLocationY.getText();

			if (!x.equals("") && !y.equals("")) {
				this.setLocation(Integer.parseInt(x), Integer.parseInt(y));
			}

		}
	}
	
	public void itemStateChanged(ItemEvent ie) {
		
		
		try {
			updateLnf();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	private void updateLnf() throws Exception{
		String selectedItem = (String)lnf.getSelectedItem();
		
		
		UIManager.setLookAndFeel(selectedItem);
		this.updateLookAndFeel();
	}
	
	private void updateLookAndFeel() {
        Window windows[] = Frame.getWindows();

        for(Window window : windows) {
            SwingUtilities.updateComponentTreeUI(window);
            
        }
    }
	public static void main(String args[]) throws Exception {
		RelocationTest test = new RelocationTest();
	}

}
