import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class UselessFrame
{
    public static JFrame frame;
    public static JLabel cbxLbl;
    public static JLabel opnLbl;
    public static JLabel posLbl;
    public static JFileChooser fc;

    public static void createAndShowGUI()
    {
        frame = new JFrame("Useless Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        // Here we use one type of layout manager. This makes
        // our elements appear vertically.
        Container pane = frame.getContentPane();
        pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
        
        // Button
        JButton btn = new JButton("Button Off");
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                JButton source = (JButton) e.getSource();
                if (source.getText().indexOf("Off") != -1) {
                    source.setText("Button On");
                } else {
                    source.setText("Button Off");
                }
            }
        });

        // Here we use a panel to group some labels using
        // another layout manager.
        JPanel panel = new JPanel();
        panel.setAlignmentX(Component.LEFT_ALIGNMENT);
        panel.setLayout(new FlowLayout(FlowLayout.LEFT));
        for (int i = 1; i <= 5; i++) {
            panel.add(new JLabel("Label " + i));
        }

        // Combobox
        String[] options = { "Choice 1", "Choice 2", "Choice 3" };
        JComboBox cbx = new JComboBox(options);
        cbx.setAlignmentX(Component.LEFT_ALIGNMENT);
        cbxLbl = new JLabel("You've selected " + cbx.getSelectedItem() + ".");

        cbx.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                // Note that to access the label object, we need to make the
                // JLabel object a public static variable. There are other
                // ways to design this. For example, you can make the container
                // that holds both cbxLbl and cbx implement ActionListener and
                // install the this object as the action listener.
                JComboBox cbx = (JComboBox) e.getSource();
                UselessFrame.cbxLbl.setText("You've selected " + cbx.getSelectedItem() + ".");
            }
        });

        // File chooser.
        fc = new JFileChooser();
        JPanel fcpnl = new JPanel();
        fcpnl.setAlignmentX(Component.LEFT_ALIGNMENT);
        fcpnl.setLayout(new FlowLayout(FlowLayout.LEFT));
        JButton opnBtn = new JButton("Open...");
        opnBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                int ret = fc.showOpenDialog(frame);
                if (ret == JFileChooser.APPROVE_OPTION) {
                    opnLbl.setText("Opened: " + fc.getSelectedFile().getName());
                }
            }
        });
        opnLbl = new JLabel("Opened: ");
        fcpnl.add(opnBtn);
        fcpnl.add(opnLbl);

        posLbl = new JLabel("Mouse Position:");
        pane.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseMoved(MouseEvent e)
            {
                UselessFrame.posLbl.setText("Mouse Position: (" + e.getX() + ", " + e.getY() + ")");
            }
        });

        pane.add(btn);
        pane.add(panel);
        pane.add(cbx);
        pane.add(cbxLbl);
        pane.add(fcpnl);
        pane.add(posLbl);
        
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        // Create GUI on the event dispatch thread.
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
