1
\$\begingroup\$

This is in continuation of my previous post for code review of Java Swing Library: Java Swing Library

Since the limit on total number of characters is 65K, I split my program in two parts. The first was Java Swing Library and the second one is this one.

I have implemented a program that uses my Java Swing Library to show examples of many Swing components.

You can use code from this program in your code.

Where applicable, appropriate listeners are also implemented.

The code of the program is below:


Examples_Of_Many_Swing_Components_In_One_Program.java


import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.text.*;
import javax.swing.tree.*;
import java.time.*;
import java.time.format.*;

public class Examples_Of_Many_Swing_Components_In_One_Program extends MouseAdapter implements ActionListener,
                                                                                               ItemListener,
                                                                                               ListSelectionListener,
                                                                                               ChangeListener,
                                                                                               PropertyChangeListener,
                                                                                               TreeSelectionListener {

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    int screenWidth = screenSize.width;
    int screenHeight = screenSize.height;
    int midScreenWidth = screenWidth / 2;
    int midScreenHeight = screenHeight / 2;
    int xMargin = screenWidth/10;
    int vGap = 25; // vertical gap between components
    int hGap = 15; // horizontal gap between components
    int lineLabelHeight = 10;
    int currentYPos = 0;
    int componentHeight = 25;
    int buttonWidth = 100;
    Color lightBlue = new Color(173, 216, 230);

    JFrame jf = null;
    JPanel jp = null;
    JButton jbButtonExample = null;
    JTextField jtfTextFieldExample = null;
    JButton jbTextFieldExample = null;
    JFormattedTextField jftfFormattedTextFieldExample = null;
    JButton jbFormattedTextFieldExample = null;
    JCheckBox jcb1CheckBoxExample = null;
    JCheckBox jcb2CheckBoxExample = null;
    JCheckBox jcb3CheckBoxExample = null;
    JCheckBox jcb4CheckBoxExample = null;
    JLabel jlCheckBoxExample = null;
    JRadioButton jrb1RadioButtonExample = null;
    JRadioButton jrb2RadioButtonExample = null;
    JRadioButton jrb3RadioButtonExample = null;
    JRadioButton jrb4RadioButtonExample = null;
    JLabel jlRadioButtonExample = null;
    JButton jbFileChooserExample = null;
    JTextField jtfFileChooserExample = null;
    JTextField jpfPasswordFieldExample = null;
    JButton jbPasswordFieldExample = null;
    JTextArea jtaTextAreaExample = null;
    JScrollPane jspScrollableTextAreaExample = null;
    JButton jbScrollableTextAreaExample = null;
    JList<String> jlistScrollableListExample = null;
    JScrollPane jspScrollableListExample = null;
    JLabel jlScrollableListExample = null;
    JComboBox<String> jcbComboBoxExample = null;
    JLabel jlComboBoxExample = null;
    JProgressBar jpbProgressBarExample = null;
    JButton jbProgressBarExample = null;
    JLabel jlProgressBarExample = null;
    boolean stopThreadProgressBarExample = false;
    JSlider jsSliderExample = null;
    JLabel jlSliderExample = null;
    JTree jtreeScrollableTreeExample = null;
    JScrollPane jspScrollableTreeExample = null;
    JLabel jlScrollableTreeExample = null;
    JSpinner jsSpinnerExample = null;
    JLabel jlSpinnerExample = null;
    JColorChooser jccColorChooserExample = null;
    JLabel jlColorChooserExample = null;
    JButton jbOptionPaneExamplesMessage = null;
    JButton jbOptionPaneExamplesInput = null;
    JButton jbOptionPaneExamplesConfirm = null;
    JButton jbOptionPaneExamplesOption = null;
    JDialog jdDialogExample = null;
    JComboBox jcbDialogExample = null;
    JFormattedTextField jftfDialogExample = null;
    JList jlistDialogExample = null;
    JButton jbDialogExample1 = null;
    JButton jbDialogExample2 = null;
    JDialog jdScrollableDialogExample = null;
    JPanel jpScrollableDialogPanel = null;
    JButton jbDisplayScrollableDialog = null;
    JButton jbCloseScrollableDialog = null;
    JPopupMenu jpmPopupMenuExample = null;
    JMenuItem jmiPopupMenuExample1 = null;
    JMenuItem jmiPopupMenuExample2 = null;
    JToggleButton jtbToggleButtonExample = null;
    JButton jbToolBarExample = null;
    JComboBox jcbToolBarExample = null;
    JMenuBar jmb = null;
    JMenu jm = null;
    JMenu jmSubMenu = null;
    JMenuItem jmiMenuItemExample = null;
    JCheckBoxMenuItem jcbmiCheckBoxMenuItemExample = null;
    JRadioButtonMenuItem jrbmiRadioButtonMenuItem1 = null;
    JRadioButtonMenuItem jrbmiRadioButtonMenuItem2 = null;

    public void actionPerformed(ActionEvent ae) {
        Object source = ae.getSource();
        if (source == jbButtonExample) {
            JOptionPane.showMessageDialog(jf, "You clicked the button!", "Info", JOptionPane.INFORMATION_MESSAGE);
        } else if (source == jbTextFieldExample) {
            String text = "You entered: " + jtfTextFieldExample.getText();
            JOptionPane.showMessageDialog(jf, text, "Info", JOptionPane.INFORMATION_MESSAGE);
        } else if ((source == jrb1RadioButtonExample) || (source == jrb2RadioButtonExample) || (source == jrb3RadioButtonExample) || (source == jrb4RadioButtonExample)) {
            String text = "You selected " + "\"" + ((JRadioButton)source).getText() + "\".";
            jlRadioButtonExample.setText(text);
        } else if (source == jbFileChooserExample) {
            JFileChooser jfc = new JFileChooser();
            if ((jfc.showOpenDialog(jf)) == JFileChooser.APPROVE_OPTION) {
                File selectedFile = jfc.getSelectedFile();
                jtfFileChooserExample.setText(selectedFile.getAbsolutePath());
            }
        } else if (source == jbPasswordFieldExample) {
            String text = "You entered: " + jpfPasswordFieldExample.getText();
            JOptionPane.showMessageDialog(jf, text, "Info", JOptionPane.INFORMATION_MESSAGE);
        } else if (source == jbScrollableTextAreaExample) {
            String text = "You wrote: " + jtaTextAreaExample.getText();
            JOptionPane.showMessageDialog(jf, text, "Info", JOptionPane.INFORMATION_MESSAGE);
        } else if (source == jcbComboBoxExample) {
            // source is used here to show that selected item can be gotten from source
            // instead of using jcbComboBoxExample.
            String text = (String)(((JComboBox)source).getSelectedItem());
            if (text.isBlank() == true) {
                jlComboBoxExample.setText(text);
            } else {
                text = "You selected \"" + text + "\".";
                jlComboBoxExample.setText(text);
            }
        } else if (source == jbProgressBarExample) {
            String buttonText = jbProgressBarExample.getText();
            if ((buttonText.equals("Start Task") == true) || (buttonText.equals("Start Task Again") == true)) {
                // example of anonymous subclass
                Thread thread = new Thread() {
                    public void run() {
                        int progress = 10;
                        while (progress <= 100) {
                            if (stopThreadProgressBarExample == true) {
                                stopThreadProgressBarExample = false;
                                jpbProgressBarExample.setValue(0);
                                jbProgressBarExample.setText("Start Task Again");
                                jlProgressBarExample.setText("Task cancelled.");
                                return;
                            } // end of if stopThreadProgressBarExample
                            jlProgressBarExample.setText("Task is running..");
                            jpbProgressBarExample.setValue(progress);
                            if (progress == 100) {
                                break;
                            }
                            try { Thread.sleep(1000); } catch (Exception e) {}
                            progress = progress + 10;
                        } // end of while
                        jbProgressBarExample.setText("Start Task Again");
                        jlProgressBarExample.setText("Task completed.");
                    }
                }; // end of new thread
                thread.start();
                jbProgressBarExample.setText("Cancel Task");
            } else if (buttonText.equals("Cancel Task") == true) {
                stopThreadProgressBarExample = true;
            } // end of if else (comparing strings)
        } else if (source == jbToolBarExample) {
            JOptionPane.showMessageDialog(jf, "You clicked Button 1.", "Info", JOptionPane.INFORMATION_MESSAGE);
        } else if (source == jcbToolBarExample) {
            // source is used here to show that selected item can be gotten from source
            // instead of using jcbToolBarExample.
            String text = (String)(((JComboBox)source).getSelectedItem());
            if (text.isBlank() == false) {
                text = "You selected \"" + text + "\".";
                JOptionPane.showMessageDialog(jf, text, "Info", JOptionPane.INFORMATION_MESSAGE);
            }
        } else if (source == jmiMenuItemExample) {
            String text = "This one program implements examples of many swing components.";
            JOptionPane.showMessageDialog(jf, text, "Info", JOptionPane.INFORMATION_MESSAGE);
        } else if (source == jcbmiCheckBoxMenuItemExample) {
            if (jcbmiCheckBoxMenuItemExample.isSelected() == true) {
                JOptionPane.showMessageDialog(jf, "You have selected check box menu item", "Info", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(jf, "You have unselected check box menu item", "Info", JOptionPane.INFORMATION_MESSAGE);
            }
        } else if (source == jrbmiRadioButtonMenuItem1) {
            jp.setBackground(Color.WHITE);
        } else if (source == jrbmiRadioButtonMenuItem2) {
            jp.setBackground(null);
        } else if (source == jmiPopupMenuExample1) {
            LocalTime time = LocalTime.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm a");
            String text = time.format(formatter);
            JOptionPane.showMessageDialog(jf, text, "Current Time", JOptionPane.INFORMATION_MESSAGE);
        } else if (source == jmiPopupMenuExample2) {
            LocalDate date = LocalDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-LLL-yyyy");
            String text = date.format(formatter);
            JOptionPane.showMessageDialog(jf, text, "Today's Date", JOptionPane.INFORMATION_MESSAGE);
        } else if (source == jbDialogExample2) {
            jcbDialogExample.setSelectedIndex(0);
            jftfDialogExample.setText("");
            jlistDialogExample.clearSelection();
            jdDialogExample.setVisible(true);
        } else if (source == jbDialogExample1) {
            String text = "";
            boolean error = false;
            String textMonth = "";
            String textYear = "";
            String textCountry = "";

            textMonth = (String)(jcbDialogExample.getSelectedItem());
            if (jftfDialogExample.getValue() != null) {
                textYear = jftfDialogExample.getText();
            }
            if (jlistDialogExample.getSelectedValue() != null) {
                textCountry = (String)(jlistDialogExample.getSelectedValue());
            }

            if (textMonth.isBlank() == true) {
                error = true;
                text = "Please select your month of birth.\n";
            }
            if (textYear.isBlank() == true) {
                error = true;
                text = text + "Please enter your year of birth.\n";
            } else if (Integer.valueOf(textYear) <= 0) {
                error = true;
                text = text + "Please enter a valid year of birth.\n";
            }
            if (textCountry.isBlank() == true) {
                error = true;
                text = text + "Please select your country of birth.\n";
            }

            if (error == true) {
                JOptionPane.showMessageDialog(jf, text, "Error", JOptionPane.ERROR_MESSAGE);
            } else {
                text = "";
                text = "Your month of birth is: " + textMonth + "\n";
                text = text + "Your year of birth is: " + textYear + "\n";
                text = text + "Your counry of birth is: " + textCountry + "\n";
                JOptionPane.showMessageDialog(jf, text, "Your deatils", JOptionPane.INFORMATION_MESSAGE);
            }
        } else if (source == jbDisplayScrollableDialog) {
            jdScrollableDialogExample.setVisible(true);
        } else if (source == jbCloseScrollableDialog) {
            jdScrollableDialogExample.dispose();
        } else if (source == jbOptionPaneExamplesMessage) {
            JOptionPane.showMessageDialog(jf, "This is a Message Dialog Box.", "Message Dialog Box", JOptionPane.INFORMATION_MESSAGE);
        } else if (source == jbOptionPaneExamplesInput) {
            String text = "You entered: ";
            String input  = JOptionPane.showInputDialog(jf, "Please input some text below:", "Input Dialog Box", JOptionPane.PLAIN_MESSAGE);
            if (input != null) {
                text = text + input;
                JOptionPane.showMessageDialog(jf, text, "Text you entered", JOptionPane.INFORMATION_MESSAGE);
            }
        } else if (source == jbOptionPaneExamplesConfirm) {
            JPasswordField jpf = new JPasswordField();
            int result = JOptionPane.showConfirmDialog(jf, jpf, "Please input your password", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
            if (result == JOptionPane.OK_OPTION) {
                String text = "Your password is: " + new String(jpf.getPassword());
                JOptionPane.showMessageDialog(jf, text, "Your password", JOptionPane.INFORMATION_MESSAGE);
            }
        } else if (source == jbOptionPaneExamplesOption) {
            /*
            JRadioButton jrb1 = new JRadioButton("Proceed");
            JRadioButton jrb2 = new JRadioButton("Do not proceed, stop here");
            JRadioButton jrb3 = new JRadioButton("Do not proceed, revert back");
            Object[] objs = {jrb1, jrb2, jrb3, "Submit"};
            */
            Object[] objs = new Object[4];
            objs[0] = new JRadioButton("Proceed");
            objs[1] = new JRadioButton("Do not proceed, stop here");
            objs[2] = new JRadioButton("Do not proceed, revert back");
            objs[3] = "Submit";

            ButtonGroup bg = new ButtonGroup();
            bg.add((JRadioButton)(objs[0]));
            bg.add((JRadioButton)(objs[1]));
            bg.add((JRadioButton)(objs[2]));

            int result = JOptionPane.showOptionDialog(jf, "Please select a radio button", "Option Dialog Box", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, objs, null);
            if (result == 3) { // 3 is the index of "Submit"
                String text = "";
                // if-else-if should be used but I am using ifs only to see that whether
                // more than one radio button gets selected or not or whether something
                // wrong is happening in UI.
                if ((((JRadioButton)(objs[0]))).isSelected() == true) {
                    text = text + "You selected: \"Proceed\".";
                }
                if ((((JRadioButton)(objs[1]))).isSelected() == true) {
                    text = text + "You selected: \"Do not proceed, stop here\".";
                }
                if ((((JRadioButton)(objs[2]))).isSelected() == true) {
                    text = text + "You selected: \"Do not proceed, revert back\".";
                }
                if (text.isBlank() == true) {
                    text = "Nothing selected.";
                }
                JOptionPane.showMessageDialog(jf, text, "Your selected choice", JOptionPane.INFORMATION_MESSAGE);
            }
            //String text = "Result is: " + result;
            //JOptionPane.showMessageDialog(jf, text, "Info", JOptionPane.INFORMATION_MESSAGE);
        }
    } // end of actionPerformed

    // for JCheckBox and JToggleButton
    public void itemStateChanged(ItemEvent ie) {
        String text = "";
        Object source = ie.getItemSelectable();
        if ((source == jcb1CheckBoxExample) || (source == jcb2CheckBoxExample) || (source == jcb3CheckBoxExample) || (source == jcb4CheckBoxExample)) {
            if (ie.getStateChange() == ItemEvent.SELECTED) {
                text = "You selected ";
            } else {
                text = "You unselected ";
            }
            text = text + "\"" + ((JCheckBox)source).getText() + "\".";
            jlCheckBoxExample.setText(text);
        } else if (source == jtbToggleButtonExample) {
            if (jtbToggleButtonExample.isSelected() == true) {
                jtbToggleButtonExample.setText("ON");
                jtbToggleButtonExample.setOpaque(true);
                jtbToggleButtonExample.setBackground(Color.GREEN);
            } else {
                jtbToggleButtonExample.setText("OFF");
                jtbToggleButtonExample.setOpaque(true);
                jtbToggleButtonExample.setBackground(Color.YELLOW);
            }
        }
    } // end of itemStateChanged

    // for JList
    public void valueChanged(ListSelectionEvent lse) {
        String text = "";
        Object source = lse.getSource();
        if (source == jlistScrollableListExample) {
            List<String> lst = jlistScrollableListExample.getSelectedValuesList();
            if (lst.size() <= 0) {
                jlScrollableListExample.setText(text);
            } else {
                text = "Your selected items are: ";
                boolean first = true;
                for (String str : lst) {
                    if (first == false) {
                        text = text + ", ";
                    }
                    text = text + str;
                    first = false;
                } // end of for loop
                text = text + ".";
                jlScrollableListExample.setText(text);
            } // end of if else
        } // end of if source
    } // end of valueChanged

    // for JSlider, JSpinner, and JColorChooser
    public void stateChanged(ChangeEvent ce) {
        String text = "";
        Object source = ce.getSource();
        if (source == jsSliderExample) {
            JSlider jsSource = (JSlider)source;
            if (!jsSource.getValueIsAdjusting()) {
                int value = (int)(jsSource.getValue());
                text = "The current value from slider is: " + value;
                jlSliderExample.setText(text);
            }
        } else if (source == jsSpinnerExample) {
            JSpinner jspnSource = (JSpinner)source;
            SpinnerModel sm = jspnSource.getModel();
            if (sm instanceof SpinnerNumberModel) {
                text = "The current value from spinner is: " + ((SpinnerNumberModel)(sm)).getNumber().intValue();
                jlSpinnerExample.setText(text);
            } else {
                text = "Something went wrong.";
                jlSpinnerExample.setText(text);
            }
        } else if (source == jccColorChooserExample.getSelectionModel()) {
            Color newColor = jccColorChooserExample.getColor();
            jlColorChooserExample.setBackground(newColor);
        }
    } // end of stateChanged

    // for JFormattedTextField
    public void propertyChange(PropertyChangeEvent pce) {
        Object source = pce.getSource();
        if (source == jftfFormattedTextFieldExample) {
            double amount = ((Number)(jftfFormattedTextFieldExample.getValue())).doubleValue();
            String text = "You entered amount: " + amount;
            JOptionPane.showMessageDialog(jf, text, "Info", JOptionPane.INFORMATION_MESSAGE);
        }
    } // end of propertyChange

    // for JTree
    public void valueChanged(TreeSelectionEvent tse) {
        String text = "";
        Object source = tse.getSource();
        if (source == jtreeScrollableTreeExample) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode)(((JTree)source).getLastSelectedPathComponent());
            if (node == null) {
                jlScrollableTreeExample.setText(text);
            } else {
                text = "You selected: " + node.getUserObject().toString();
                jlScrollableTreeExample.setText(text);
            }
        }
    } // end of valueChanged

    // for JPopupMenu to show up
    public void mousePressed(MouseEvent e) {
        //JOptionPane.showMessageDialog(jf, "Mouse Pressed", "Info", JOptionPane.INFORMATION_MESSAGE);
        if (e.isPopupTrigger()) {
            jpmPopupMenuExample.show(e.getComponent(), e.getX(), e.getY());
        }
    } // end of mousePressed

    // for JPopupMenu to show up
    public void mouseReleased(MouseEvent e) {
        //JOptionPane.showMessageDialog(jf, "Mouse Released", "Info", JOptionPane.INFORMATION_MESSAGE);
        if (e.isPopupTrigger()) {
            jpmPopupMenuExample.show(e.getComponent(), e.getX(), e.getY());
        }
    } // end of mouseReleased

    //void createJFrameExample() {
        //jf = SwingLibrary.setupJFrameAndGet("Learn Java Swing GUI Programming By Examples", screenWidth, screenHeight);
        //jf = SwingLibrary.setupJFrameAndGet("Learn Java Swing GUI Programming By Examples", 0, 0);
        //jf.setVisible(true);
    //} // end of addJFrameExample

    void createScrollableJFrameExample() {
        ArrayList<Object> a = SwingLibrary.setupScrollableJFrameAndGetFrameAndPanel("Learn Java Swing GUI Programming By Examples", screenWidth, screenHeight + 4500);
        jf = (JFrame)(a.get(0));
        jp = (JPanel)(a.get(1));
    } // end of addScrollableJFrameExample

    void addJButtonExample() {
        currentYPos = currentYPos + vGap;
        addHeadingLabel("Button Example", 0, currentYPos, screenWidth, componentHeight);
        currentYPos = currentYPos + componentHeight + vGap;
        jbButtonExample = SwingLibrary.setupJButtonAndGet("Click Me!", this, true, midScreenWidth - 50, currentYPos, buttonWidth, componentHeight);
        jp.add(jbButtonExample);
        currentYPos = currentYPos + componentHeight + vGap;
        addLineLabel(currentYPos);
    } // end of addJButtonExample

    void addJLabelExample() {
        currentYPos = currentYPos + vGap;
        addHeadingLabel("Label Example", 0, currentYPos, screenWidth, componentHeight);
        currentYPos = currentYPos + componentHeight + vGap;
        JLabel jl = SwingLibrary.setupJLabelAndGet("This is a label!", true, Color.GREEN, SwingConstants.CENTER, SwingConstants.CENTER, true, midScreenWidth - 100, currentYPos, 200, componentHeight);
        jp.add(jl);
        currentYPos = currentYPos + componentHeight + vGap;
        addLineLabel(currentYPos);
    } // end of addJLabelExample

    void addToolTipExample() {
        currentYPos = currentYPos + vGap;
        addHeadingLabel("Tool Tip Example", 0, currentYPos, screenWidth, componentHeight);
        currentYPos = currentYPos + componentHeight + vGap;
        JLabel jl = SwingLibrary.setupJLabelAndGet("Hover the mouse over me to see the tool tip!", true, Color.GREEN, SwingConstants.CENTER, SwingConstants.CENTER, true, midScreenWidth - 150, currentYPos, 300, componentHeight);
        jl.setToolTipText("This is a tool tip!");
        // show tool tip immediately
        ToolTipManager.sharedInstance().setInitialDelay(0);
        // keep tool tip showing
        ToolTipManager.sharedInstance().setDismissDelay(Integer.MAX_VALUE);
        jp.add(jl);
        currentYPos = currentYPos + componentHeight + vGap;
        addLineLabel(currentYPos);
    } // end of addToolTipExample

    void addJTextFieldExample() {
        currentYPos = currentYPos + vGap;
        addHeadingLabel("Text Field Example", 0, currentYPos, screenWidth, componentHeight);
        currentYPos = currentYPos + componentHeight + vGap;
        JLabel jl = SwingLibrary.setupJLabelAndGet("Enter some text in below field:", false, null, SwingConstants.CENTER, SwingConstants.CENTER, true, midScreenWidth - 100, currentYPos, 200, componentHeight);
        jp.add(jl);
        currentYPos = currentYPos + componentHeight;
        jtfTextFieldExample = SwingLibrary.setupJTextFieldAndGet(xMargin, currentYPos, screenWidth - (xMargin*2), componentHeight);
        jp.add(jtfTextFieldExample);
        currentYPos = currentYPos + componentHeight + vGap;
        jbTextFieldExample = SwingLibrary.setupJButtonAndGet("Submit", this, true, midScreenWidth - 50, currentYPos, buttonWidth, componentHeight);
        jp.add(jbTextFieldExample);
        currentYPos = currentYPos + componentHeight + vGap;
        addLineLabel(currentYPos);
    } // end of addJTextFieldExample

    void addJFormattedTextFieldExample() {
        currentYPos = currentYPos + vGap;
        addHeadingLabel("Formatted Text Field Example", 0, currentYPos, screenWidth, componentHeight);
        currentYPos = currentYPos + componentHeight + vGap;
        JLabel jl = SwingLibrary.setupJLabelAndGet("Enter some number in below formatted field (it accepts numbers only):", false, null, SwingConstants.CENTER, SwingConstants.CENTER, true, midScreenWidth - 200, currentYPos, 400, componentHeight);
        jp.add(jl);
        currentYPos = currentYPos + componentHeight;
        jftfFormattedTextFieldExample = SwingLibrary.setupJFormattedTextFieldAndGet(NumberFormat.getNumberInstance(), 1000, this, "value", xMargin, currentYPos, screenWidth - (xMargin*2), componentHeight);
        jp.add(jftfFormattedTextFieldExample);
        currentYPos = currentYPos + componentHeight + vGap;
        jbFormattedTextFieldExample = SwingLibrary.setupJButtonAndGet("Click to shift focus away from formatted field", this, true, midScreenWidth - 200, currentYPos, 400, componentHeight);
        jp.add(jbFormattedTextFieldExample);
        currentYPos = currentYPos + componentHeight + vGap;
        addLineLabel(currentYPos);
    } // end of addJFormattedTextFieldExample

    void addJCheckBoxExample() {
        currentYPos = currentYPos + vGap;
        addHeadingLabel("Check Box Example", 0, currentYPos, screenWidth, componentHeight);
        currentYPos = currentYPos + componentHeight + vGap;
        JLabel jl = SwingLibrary.setupJLabelAndGet("Select/Unselect a checkbox", true, Color.GREEN, SwingConstants.CENTER, SwingConstants.CENTER, true, midScreenWidth - 100, currentYPos, 200, componentHeight);
        jp.add(jl);
        currentYPos = currentYPos + componentHeight;
        jcb1CheckBoxExample = SwingLibrary.setupJCheckBoxAndGet("One", false, this, midScreenWidth - 100, currentYPos, 200, componentHeight);
        jp.add(jcb1CheckBoxExample);
        currentYPos = currentYPos + componentHeight;
        jcb2CheckBoxExample = SwingLibrary.setupJCheckBoxAndGet("Two", false, this, midScreenWidth - 100, currentYPos, 200, componentHeight);
        jp.add(jcb2CheckBoxExample);
        currentYPos = currentYPos + componentHeight;
        jcb3CheckBoxExample = SwingLibrary.setupJCheckBoxAndGet("Three", false, this, midScreenWidth - 100, currentYPos, 200, componentHeight);
        jp.add(jcb3CheckBoxExample);
        currentYPos = currentYPos + componentHeight;
        jcb4CheckBoxExample = SwingLibrary.setupJCheckBoxAndGet("Four", false, this, midScreenWidth - 100, currentYPos, 200, componentHeight);
        jp.add(jcb4CheckBoxExample);
        currentYPos = currentYPos + componentHeight;
        jlCheckBoxExample = SwingLibrary.setupJLabelAndGet("", true, Color.GREEN, SwingConstants.CENTER, SwingConstants.CENTER, true, midScreenWidth - 100, currentYPos, 200, componentHeight);
        jp.add(jlCheckBoxExample);
        currentYPos = currentYPos + componentHeight + vGap;
        addLineLabel(currentYPos);
    } // end of addJCheckBoxExample

    void addJRadioButtonExample() {
        currentYPos = currentYPos + vGap;
        addHeadingLabel("Radio Button Example", 0, currentYPos, screenWidth, componentHeight);
        currentYPos = currentYPos + componentHeight + vGap;
        JLabel jl = SwingLibrary.setupJLabelAndGet("Select a radio button", true, Color.GREEN, SwingConstants.CENTER, SwingConstants.CENTER, true, midScreenWidth - 100, currentYPos, 200, componentHeight);
        jp.add(jl);
        currentYPos = currentYPos + componentHeight;
        jrb1RadioButtonExample = SwingLibrary.setupJRadioButtonAndGet("A", false, this, midScreenWidth - 100, currentYPos, 200, componentHeight);
        jp.add(jrb1RadioButtonExample);
        currentYPos = currentYPos + componentHeight;
        jrb2RadioButtonExample = SwingLibrary.setupJRadioButtonAndGet("B", false, this, midScreenWidth - 100, currentYPos, 200, componentHeight);
        jp.add(jrb2RadioButtonExample);
        currentYPos = currentYPos + componentHeight;
        jrb3RadioButtonExample = SwingLibrary.setupJRadioButtonAndGet("C", false, this, midScreenWidth - 100, currentYPos, 200, componentHeight);
        jp.add(jrb3RadioButtonExample);
        currentYPos = currentYPos + componentHeight;
        jrb4RadioButtonExample = SwingLibrary.setupJRadioButtonAndGet("D", false, this, midScreenWidth - 100, currentYPos, 200, componentHeight);
        jp.add(jrb4RadioButtonExample);

        // add all radio buttons to a button group so that only one radio button
        // can be selected at a time
        ButtonGroup bg = SwingLibrary.setupButtonGroupAndGet();
        bg.add(jrb1RadioButtonExample);
        bg.add(jrb2RadioButtonExample);
        bg.add(jrb3RadioButtonExample);
        bg.add(jrb4RadioButtonExample);

        currentYPos = currentYPos + componentHeight;
        jlRadioButtonExample = SwingLibrary.setupJLabelAndGet("", true, Color.GREEN, SwingConstants.CENTER, SwingConstants.CENTER, true, midScreenWidth - 100, currentYPos, 200, componentHeight);
        jp.add(jlRadioButtonExample);
        currentYPos = currentYPos + componentHeight + vGap;
        addLineLabel(currentYPos);
    } // end of addJRadioButtonExample

    void addJFileChooserExample() {
        currentYPos = currentYPos + vGap;
        addHeadingLabel("File Chooser Example", 0, currentYPos, screenWidth, componentHeight);
        currentYPos = currentYPos + componentHeight + vGap;
        JLabel jl = SwingLibrary.setupJLabelAndGet("Select a file by clicking Browse button below:", true, Color.GREEN, SwingConstants.CENTER, SwingConstants.CENTER, true, midScreenWidth - 150, currentYPos, 300, componentHeight);
        jp.add(jl);
        currentYPos = currentYPos + componentHeight + 4;
        jbFileChooserExample = SwingLibrary.setupJButtonAndGet("Browse", this, true, midScreenWidth - 50, currentYPos, buttonWidth, componentHeight);
        jp.add(jbFileChooserExample);
        currentYPos = currentYPos + componentHeight + vGap;
        jl = SwingLibrary.setupJLabelAndGet("The path to file that you choose will appear below:", true, Color.GREEN, SwingConstants.CENTER, SwingConstants.CENTER, true, midScreenWidth - 150, currentYPos, 300, componentHeight);
        jp.add(jl);
        currentYPos = currentYPos + componentHeight + 4;
        jtfFileChooserExample = SwingLibrary.setupJTextFieldAndGet(xMargin, currentYPos, screenWidth - (xMargin*2), componentHeight);
        jp.add(jtfFileChooserExample);
        currentYPos = currentYPos + componentHeight + vGap;
        addLineLabel(currentYPos);
    } // end of addJFileChooserExample

    void addJPasswordFieldExample() {
        currentYPos = currentYPos + vGap;
        addHeadingLabel("Password Field Example", 0, currentYPos, screenWidth, componentHeight);
        currentYPos = currentYPos + componentHeight + vGap;
        JLabel jl = SwingLibrary.setupJLabelAndGet("Enter some text (password) in below field:", false, null, SwingConstants.CENTER, SwingConstants.CENTER, true, midScreenWidth - 150, currentYPos, 300, componentHeight);
        jp.add(jl);
        currentYPos = currentYPos + componentHeight;
        jpfPasswordFieldExample = SwingLibrary.setupJPasswordFieldAndGet(xMargin, currentYPos, screenWidth - (xMargin*2), componentHeight);
        jp.add(jpfPasswordFieldExample);
        currentYPos = currentYPos + componentHeight + vGap;
        jbPasswordFieldExample = SwingLibrary.setupJButtonAndGet("Submit", this, true, midScreenWidth - 50, currentYPos, buttonWidth, componentHeight);
        jp.add(jbPasswordFieldExample);
        currentYPos = currentYPos + componentHeight + vGap;
        addLineLabel(currentYPos);
    } // end of addJPasswordFieldExample

    void addScrollableJTextAreaExample() {
        currentYPos = currentYPos + vGap;
        addHeadingLabel("Scrollable Text Area Example", 0, currentYPos, screenWidth, componentHeight);
        currentYPos = currentYPos + componentHeight + vGap;
        JLabel jl = SwingLibrary.setupJLabelAndGet("Write something in Text Area below:", false, null, SwingConstants.CENTER, SwingConstants.CENTER, true, midScreenWidth - 150, currentYPos, 300, componentHeight);
        jp.add(jl);
        currentYPos = currentYPos + componentHeight;
        jtaTextAreaExample = SwingLibrary.setupJTextAreaAndGet("", 10, 100, true, true, true, false, 0, 0, 0, 0);
        jspScrollableTextAreaExample = SwingLibrary.setupScrollableJTextAreaAndGet(jtaTextAreaExample, (xMargin*5)/2, currentYPos, screenWidth - (xMargin*5), componentHeight*4);
        jp.add(jspScrollableTextAreaExample);
        currentYPos = currentYPos + componentHeight*4 + vGap;
        jbScrollableTextAreaExample = SwingLibrary.setupJButtonAndGet("Submit", this, true, midScreenWidth - 50, currentYPos, buttonWidth, componentHeight);
        jp.add(jbScrollableTextAreaExample);
        currentYPos = currentYPos + componentHeight + vGap;
        addLineLabel(currentYPos);
    } // end of addScrollableJTextAreaExample

    void addScrollableJListExample() {
        currentYPos = currentYPos + vGap;
        addHeadingLabel("Scrollable List Example", 0, currentYPos, screenWidth, componentHeight);
        currentYPos = currentYPos + componentHeight + vGap;
        JLabel jl = SwingLibrary.setupJLabelAndGet("Select/Unselect item(s) from list", true, Color.GREEN, SwingConstants.CENTER, SwingConstants.CENTER, true, midScreenWidth - 100, currentYPos, 200, componentHeight);
        jp.add(jl);
        currentYPos = currentYPos + componentHeight + 4;

        // add items to default list model
        DefaultListModel<String> dlm = new DefaultListModel<String>();
        dlm.addElement("One");
        dlm.addElement("Two");
        dlm.addElement("Three");
        dlm.addElement("Four");
        dlm.addElement("Five");
        dlm.addElement("Six");
        dlm.addElement("Seven");
        dlm.addElement("Eight");
        dlm.addElement("Nine");
        dlm.addElement("Ten");

        jlistScrollableListExample = SwingLibrary.setupJListAndGet(dlm, ListSelectionModel.MULTIPLE_INTERVAL_SELECTION, 3, -1, this, false, 0, 0, 0, 0);
        jspScrollableListExample = SwingLibrary.setupScrollableJListAndGet(jlistScrollableListExample, xMargin*4, currentYPos, screenWidth - (xMargin*8), componentHeight*4);
        jp.add(jspScrollableListExample);
        currentYPos = currentYPos + componentHeight*4 + vGap;
        jlScrollableListExample = SwingLibrary.setupJLabelAndGet("", true, Color.GREEN, SwingConstants.CENTER, SwingConstants.CENTER, true, xMargin, currentYPos, screenWidth - (xMargin*2), componentHeight);
        jp.add(jlScrollableListExample);
        currentYPos = currentYPos + componentHeight + vGap;
        addLineLabel(currentYPos);
    } // end of addScrollableJListExample

    void addJComboBoxExample() {
        currentYPos = currentYPos + vGap;
        addHeadingLabel("Combo Box Example", 0, currentYPos, screenWidth, componentHeight);
        currentYPos = currentYPos + componentHeight + vGap;
        JLabel jl = SwingLibrary.setupJLabelAndGet("Select an item from combo box", true, Color.GREEN, SwingConstants.CENTER, SwingConstants.CENTER, true, midScreenWidth - 100, currentYPos, 200, componentHeight);
        jp.add(jl);
        currentYPos = currentYPos + componentHeight + 4;

        // add items to default list model
        DefaultComboBoxModel<String> dcbm = new DefaultComboBoxModel<String>();
        dcbm.addElement("");
        dcbm.addElement("A");
        dcbm.addElement("B");
        dcbm.addElement("C");
        dcbm.addElement("D");
        dcbm.addElement("E");
        dcbm.addElement("V");
        dcbm.addElement("W");
        dcbm.addElement("X");
        dcbm.addElement("Y");
        dcbm.addElement("Z");

        jcbComboBoxExample = SwingLibrary.setupJComboBoxAndGet(dcbm, 0, this, true, midScreenWidth - 100, currentYPos, 200, componentHeight);
        jp.add(jcbComboBoxExample);
        currentYPos = currentYPos + componentHeight*4 + vGap;
        jlComboBoxExample = SwingLibrary.setupJLabelAndGet("", true, Color.GREEN, SwingConstants.CENTER, SwingConstants.CENTER, true, midScreenWidth - 100, currentYPos, 200, componentHeight);
        jp.add(jlComboBoxExample);
        currentYPos = currentYPos + componentHeight + vGap;
        addLineLabel(currentYPos);
    } // end of addJComboBoxExample

    void addJProgressBarExample() {
        currentYPos = currentYPos + vGap;
        addHeadingLabel("Progress Bar Example", 0, currentYPos, screenWidth, componentHeight);
        currentYPos = currentYPos + componentHeight + vGap;
        jlProgressBarExample = SwingLibrary.setupJLabelAndGet("Task not started.", false, null, SwingConstants.CENTER, SwingConstants.CENTER, true, midScreenWidth - 100, currentYPos, 200, componentHeight);
        jp.add(jlProgressBarExample);
        currentYPos = currentYPos + componentHeight;
        jpbProgressBarExample = SwingLibrary.setupJProgressBarAndGet(SwingConstants.HORIZONTAL, 0, 100, 0, true, true, true, xMargin, currentYPos, screenWidth - (xMargin*2), componentHeight);
        jp.add(jpbProgressBarExample);
        currentYPos = currentYPos + componentHeight + vGap;
        jbProgressBarExample = SwingLibrary.setupJButtonAndGet("Start Task", this, true, midScreenWidth - 100, currentYPos, 200, componentHeight);
        jp.add(jbProgressBarExample);
        currentYPos = currentYPos + componentHeight + vGap;
        addLineLabel(currentYPos);
    } // end of addJProgressBarExample

    void addJSliderExample() {
        currentYPos = currentYPos + vGap;
        addHeadingLabel("Slider Example", 0, currentYPos, screenWidth, componentHeight);
        currentYPos = currentYPos + componentHeight + vGap;
        jsSliderExample = SwingLibrary.setupJSliderAndGet(SwingConstants.HORIZONTAL, 0, 100, 20, 1, 10, true, true, this, true, xMargin, currentYPos, screenWidth - (xMargin*2), componentHeight*2);
        jp.add(jsSliderExample);
        currentYPos = currentYPos + componentHeight*2 + vGap;
        jlSliderExample = SwingLibrary.setupJLabelAndGet("The current value from slider is: 20", true, Color.GREEN, SwingConstants.CENTER, SwingConstants.CENTER, true, midScreenWidth - 110, currentYPos, 220, componentHeight);
        jp.add(jlSliderExample);
        currentYPos = currentYPos + componentHeight + vGap;
        addLineLabel(currentYPos);
    } // end of addJSliderExample

    void addScrollableJTreeExample() {
        currentYPos = currentYPos + vGap;
        addHeadingLabel("Scrollable Tree Example", 0, currentYPos, screenWidth, componentHeight);
        currentYPos = currentYPos + componentHeight + vGap;
        JLabel jl = SwingLibrary.setupJLabelAndGet("You can select any node in the tree.", true, Color.GREEN, SwingConstants.CENTER, SwingConstants.CENTER, true, midScreenWidth - 110, currentYPos, 220, componentHeight);
        jp.add(jl);
        currentYPos = currentYPos + componentHeight + 4;

        // create tree nodes
        DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Names");

        DefaultMutableTreeNode j = new DefaultMutableTreeNode("J");
        DefaultMutableTreeNode james = new DefaultMutableTreeNode("James");
        DefaultMutableTreeNode jerrod = new DefaultMutableTreeNode("Jerrod");
        j.add(james);
        j.add(jerrod);
        rootNode.add(j);

        DefaultMutableTreeNode n = new DefaultMutableTreeNode("N");
        DefaultMutableTreeNode nathan = new DefaultMutableTreeNode("Nathan");
        DefaultMutableTreeNode nicholas = new DefaultMutableTreeNode("Nicholas");
        n.add(nathan);
        n.add(nicholas);
        rootNode.add(n);

        DefaultMutableTreeNode v = new DefaultMutableTreeNode("V");
        DefaultMutableTreeNode vincent = new DefaultMutableTreeNode("Vincent");
        v.add(vincent);
        rootNode.add(v);

        jtreeScrollableTreeExample = SwingLibrary.setupJTreeAndGet(rootNode, TreeSelectionModel.SINGLE_TREE_SELECTION, this, false, 0, 0, 0, 0);
        jspScrollableTreeExample = SwingLibrary.setupScrollableJTreeAndGet(jtreeScrollableTreeExample, xMargin*4, currentYPos, screenWidth - (xMargin*8), componentHeight*4);
        jp.add(jspScrollableTreeExample);
        currentYPos = currentYPos + componentHeight*4 + vGap;
        jlScrollableTreeExample = SwingLibrary.setupJLabelAndGet("", true, Color.GREEN, SwingConstants.CENTER, SwingConstants.CENTER, true, midScreenWidth - 110, currentYPos, 220, componentHeight);
        jp.add(jlScrollableTreeExample);
        currentYPos = currentYPos + componentHeight + vGap;
        addLineLabel(currentYPos);
    } // end of addScrollableJTree example

    void addJSpinnerExample() {
        currentYPos = currentYPos + vGap;
        addHeadingLabel("Spinner Example", 0, currentYPos, screenWidth, componentHeight);
        currentYPos = currentYPos + componentHeight + vGap;
        JLabel jl = SwingLibrary.setupJLabelAndGet("Click on up arrow or down arrow of the spinner to set a value.", true, Color.GREEN, SwingConstants.CENTER, SwingConstants.CENTER, true, midScreenWidth - 200, currentYPos, 400, componentHeight);
        jp.add(jl);
        currentYPos = currentYPos + componentHeight*2;
        SpinnerNumberModel snm = new SpinnerNumberModel(20, 1, null, 1);
        jsSpinnerExample = SwingLibrary.setupJSpinnerAndGet(snm, false, this, xMargin*4, currentYPos, screenWidth - (xMargin*8), componentHeight);
        jp.add(jsSpinnerExample);
        currentYPos = currentYPos + componentHeight + vGap;
        jlSpinnerExample = SwingLibrary.setupJLabelAndGet("The current value from spinner is: 20", true, Color.GREEN, SwingConstants.CENTER, SwingConstants.CENTER, true, midScreenWidth - 150, currentYPos, 300, componentHeight);
        jp.add(jlSpinnerExample);
        currentYPos = currentYPos + componentHeight + vGap;
        addLineLabel(currentYPos);
    } // end of addJSpinnerExample

    void addJColorChooserExample() {
        currentYPos = currentYPos + vGap;
        addHeadingLabel("Color Chooser Example", 0, currentYPos, screenWidth, componentHeight);
        currentYPos = currentYPos + componentHeight + vGap;
        jlColorChooserExample = SwingLibrary.setupJLabelAndGet("Select a color and the background of this label will change to that color.", true, Color.GREEN, SwingConstants.CENTER, SwingConstants.CENTER, true, midScreenWidth - 250, currentYPos, 500, componentHeight);
        jp.add(jlColorChooserExample);
        currentYPos = currentYPos + componentHeight*2;
        jccColorChooserExample = SwingLibrary.setupJColorChooserAndGet(Color.GREEN, true, "Choose a color", this, xMargin*2, currentYPos, screenWidth - (xMargin*4), componentHeight*12);
        jp.add(jccColorChooserExample);
        currentYPos = currentYPos + componentHeight*12 + vGap;
        addLineLabel(currentYPos);
    } // end of addJColorChooserExample

    void addJOptionPaneExamples() {
        currentYPos = currentYPos + vGap;
        addHeadingLabel("Option Pane Examples", 0, currentYPos, screenWidth, componentHeight);

        currentYPos = currentYPos + componentHeight + vGap;
        jbOptionPaneExamplesMessage = SwingLibrary.setupJButtonAndGet("Click to see the Message Dialog Box", this, true, midScreenWidth - 150, currentYPos, 300, componentHeight);
        jp.add(jbOptionPaneExamplesMessage);

        currentYPos = currentYPos + componentHeight + vGap;
        jbOptionPaneExamplesInput = SwingLibrary.setupJButtonAndGet("Click to see the Input Dialog Box", this, true, midScreenWidth - 150, currentYPos, 300, componentHeight);
        jp.add(jbOptionPaneExamplesInput);

        currentYPos = currentYPos + componentHeight + vGap;
        jbOptionPaneExamplesConfirm = SwingLibrary.setupJButtonAndGet("Click to see the Confirm Dialog Box", this, true, midScreenWidth - 150, currentYPos, 300, componentHeight);
        jp.add(jbOptionPaneExamplesConfirm);

        currentYPos = currentYPos + componentHeight + vGap;
        jbOptionPaneExamplesOption = SwingLibrary.setupJButtonAndGet("Click to see the Option Dialog Box", this, true, midScreenWidth - 150, currentYPos, 300, componentHeight);
        jp.add(jbOptionPaneExamplesOption);

        currentYPos = currentYPos + componentHeight + vGap;
        addLineLabel(currentYPos);
    } // end of addJOptionPaneExample

    void addJDialogExample() {
        int dialogWidth = 336;
        int dialogHeight = 350;
        int midDialogWidth = dialogWidth/2;
        int jdXPos = 10;
        int jdYPos = 10;
        currentYPos = currentYPos + vGap;
        addHeadingLabel("Dialog Example", 0, currentYPos, screenWidth, componentHeight);
        currentYPos = currentYPos + vGap;

        jdDialogExample = SwingLibrary.setupJDialogAndGet(jf, "Details", true, dialogWidth, dialogHeight);

        JLabel jl = SwingLibrary.setupJLabelAndGet("Select your month of birth from combo box:", false, null, SwingConstants.LEFT, SwingConstants.CENTER, true, jdXPos, jdYPos, 300, componentHeight);
        jdDialogExample.add(jl);
        DefaultComboBoxModel<String> dcbm = new DefaultComboBoxModel<String>();
        dcbm.addElement("");
        dcbm.addElement("Jan");
        dcbm.addElement("Feb");
        dcbm.addElement("Mar");
        dcbm.addElement("Apr");
        dcbm.addElement("May");
        dcbm.addElement("Jun");
        dcbm.addElement("Jul");
        dcbm.addElement("Aug");
        dcbm.addElement("Sep");
        dcbm.addElement("Oct");
        dcbm.addElement("Nov");
        dcbm.addElement("Dec");
        jdYPos = jdYPos + componentHeight;
        jcbDialogExample = SwingLibrary.setupJComboBoxAndGet(dcbm, 0, null, true, jdXPos, jdYPos, 300, componentHeight);
        jdDialogExample.add(jcbDialogExample);

        jdYPos = jdYPos + componentHeight*2;
        jl = SwingLibrary.setupJLabelAndGet("Enter your year of birth in text field (4 digits):", false, null, SwingConstants.LEFT, SwingConstants.CENTER, true, jdXPos, jdYPos, 300, componentHeight);
        jdDialogExample.add(jl);
        jdYPos = jdYPos + componentHeight;
        NumberFormat format = NumberFormat.getIntegerInstance();
        format.setGroupingUsed(false);
        format.setMinimumIntegerDigits(0);
        format.setMaximumIntegerDigits(4);
        jftfDialogExample = SwingLibrary.setupJFormattedTextFieldAndGet(format, null, null, null, jdXPos, jdYPos, 300, componentHeight);
        jdDialogExample.add(jftfDialogExample);

        jdYPos = jdYPos + componentHeight*2;
        jl = SwingLibrary.setupJLabelAndGet("Select your country of birth from list:", false, null, SwingConstants.LEFT, SwingConstants.CENTER, true, jdXPos, jdYPos, 300, componentHeight);
        jdDialogExample.add(jl);
        DefaultListModel<String> dlm = new DefaultListModel<String>();
        dlm.addElement("USA");
        dlm.addElement("Outside USA");
        jdYPos = jdYPos + componentHeight;
        jlistDialogExample = SwingLibrary.setupJListAndGet(dlm, ListSelectionModel.SINGLE_SELECTION, 2, -1, null, true, jdXPos, jdYPos, 300, componentHeight*2 - 14);
        jdDialogExample.add(jlistDialogExample);

        jdYPos = jdYPos + componentHeight*3;
        jbDialogExample1 = SwingLibrary.setupJButtonAndGet("Submit", this, true, midDialogWidth - 50, jdYPos, 100, componentHeight);
        jdDialogExample.add(jbDialogExample1);

        currentYPos = currentYPos + componentHeight;
        jbDialogExample2 = SwingLibrary.setupJButtonAndGet("Click to see the dialog box", this, true, midScreenWidth - 150, currentYPos, buttonWidth*3, componentHeight);
        jp.add(jbDialogExample2);
        currentYPos = currentYPos + componentHeight + vGap;
        addLineLabel(currentYPos);
    } // end of addJDialogExample

    void addScrollableJDialogExample() {
        int dialogWidth = 400;
        int dialogHeight = 400;
        int midDialogWidth = dialogWidth/2;
        int panelWidth = 420;
        int panelHeight = 570;
        int jdYPos = 10;
        currentYPos = currentYPos + vGap;
        addHeadingLabel("Scrollable Dialog Example", 0, currentYPos, screenWidth, componentHeight);
        currentYPos = currentYPos + vGap;

        ArrayList<Object> a = SwingLibrary.setupScrollableJDialogAndGetDialogAndPanel(jf, "List of Labels", true, dialogWidth, dialogHeight, panelWidth, panelHeight);
        jdScrollableDialogExample = (JDialog)(a.get(0));
        jpScrollableDialogPanel = (JPanel)(a.get(1));

        JLabel jl = null;
        String text = null;
        for (int i = 0; i < 15; i++) {
            text = "This is label " + (i + 1);
            jl = SwingLibrary.setupJLabelAndGet(text, true, Color.BLACK, SwingConstants.LEFT, SwingConstants.CENTER, true, 10, jdYPos, 400, componentHeight);
            jl.setForeground(Color.WHITE);
            jdYPos = jdYPos + 35;
            jpScrollableDialogPanel.add(jl);
        }

        jbCloseScrollableDialog = SwingLibrary.setupJButtonAndGet("Close", this, true, midDialogWidth - 50, jdYPos, 100, componentHeight);
        jpScrollableDialogPanel.add(jbCloseScrollableDialog);

        currentYPos = currentYPos + componentHeight;
        jbDisplayScrollableDialog = SwingLibrary.setupJButtonAndGet("Click to see the scrollable dialog box", this, true, midScreenWidth - 150, currentYPos, buttonWidth*3, componentHeight);
        jp.add(jbDisplayScrollableDialog);
        currentYPos = currentYPos + componentHeight + vGap;
        addLineLabel(currentYPos);
    } // end of addScrollableJDialogExample

    void addJPopupMenuExample() {
        currentYPos = currentYPos + vGap;
        addHeadingLabel("Popup Menu Example", 0, currentYPos, screenWidth, componentHeight);
        currentYPos = currentYPos + componentHeight + vGap;
        JLabel jlPopupMenuExample = SwingLibrary.setupJLabelAndGet("Right click anywhere in the frame to the see the popup menu.", true, Color.GREEN, SwingConstants.CENTER, SwingConstants.CENTER, true, midScreenWidth - 250, currentYPos, 500, componentHeight);
        jp.add(jlPopupMenuExample);
        jpmPopupMenuExample = new JPopupMenu("Popup Menu");
        jmiPopupMenuExample1 = SwingLibrary.setupJMenuItemAndGet("Show current time", this, null, null, null);
        jpmPopupMenuExample.add(jmiPopupMenuExample1);
        jpmPopupMenuExample.addSeparator();
        jmiPopupMenuExample2 = SwingLibrary.setupJMenuItemAndGet("Show today's date", this, null, null, null);
        jpmPopupMenuExample.add(jmiPopupMenuExample2);

        // Add mouse listener on JPanel so that clicking anywhere on JPanel will bring up the popup menu.
        jp.addMouseListener(this);
        currentYPos = currentYPos + componentHeight + vGap;
        addLineLabel(currentYPos);
    } // end of addJPopupMenuExample

    void addJToggleButtonExample() {
        currentYPos = currentYPos + vGap;
        addHeadingLabel("Toggle Button Example", 0, currentYPos, screenWidth, componentHeight);
        currentYPos = currentYPos + componentHeight + vGap;
        jtbToggleButtonExample = SwingLibrary.setupJToggleButtonAndGet("OFF", this, true, Color.YELLOW, true, midScreenWidth - 50, currentYPos, buttonWidth, componentHeight);
        jp.add(jtbToggleButtonExample);
        currentYPos = currentYPos + componentHeight + vGap;
        addLineLabel(currentYPos);
    } // end of addJToggleButtonExample

    void addJSeparatorExample() {
        currentYPos = currentYPos + vGap;
        addHeadingLabel("Separator Example", 0, currentYPos, screenWidth, componentHeight);
        currentYPos = currentYPos + componentHeight + vGap;
        JLabel jlSeparatorExample = SwingLibrary.setupJLabelAndGet("Below are the separators.", true, Color.GREEN, SwingConstants.CENTER, SwingConstants.CENTER, true, midScreenWidth - 100, currentYPos, 200, componentHeight);
        jp.add(jlSeparatorExample);
        currentYPos = currentYPos + componentHeight + vGap;
        JSeparator sep1 = SwingLibrary.setupJSeparatorAndGet(SwingConstants.HORIZONTAL, Color.YELLOW, true, xMargin*2, currentYPos, screenWidth - (xMargin*4), componentHeight);
        jp.add(sep1);
        currentYPos = currentYPos + componentHeight + vGap;
        JSeparator sep2 = SwingLibrary.setupJSeparatorAndGet(SwingConstants.HORIZONTAL, Color.GREEN, true, xMargin*2, currentYPos, screenWidth - (xMargin*4), componentHeight);
        jp.add(sep2);
        currentYPos = currentYPos + componentHeight + vGap;
        addLineLabel(currentYPos);
    } // end of addJSeparatorExample

    void addJToolBarExample() {
        JToolBar jtbToolBarExample = new JToolBar("Tool Bar", SwingConstants.HORIZONTAL);
        jbToolBarExample = SwingLibrary.setupJButtonAndGet("Button 1", this, false, 0, 0, 0, 0);
        jtbToolBarExample.add(jbToolBarExample);
        jtbToolBarExample.addSeparator();
        // Now, add a combo box to tool bar
        DefaultComboBoxModel<String> dcbm = new DefaultComboBoxModel<String>();
        dcbm.addElement("");
        dcbm.addElement("Item 1");
        dcbm.addElement("Item 2");
        dcbm.addElement("Item 3");
        dcbm.addElement("Item 4");
        jcbToolBarExample = SwingLibrary.setupJComboBoxAndGet(dcbm, 0, this, false, 0, 0, 0, 0);
        jtbToolBarExample.add(jcbToolBarExample);
        //jtbToolBarExample.setBorderPainted(true);
        jf.add(jtbToolBarExample, BorderLayout.NORTH);
    } // end of addJToolBarExample

    void addJMenuItemsExample() {
        KeyStroke k = KeyStroke.getKeyStroke(KeyEvent.VK_I, InputEvent.CTRL_DOWN_MASK);
        jmb = SwingLibrary.setupJMenuBarAndGet(null, Color.GREEN);
        jm = SwingLibrary.setupJMenuAndGet("Help", Color.BLACK, Color.YELLOW);
        jmSubMenu = SwingLibrary.setupJMenuAndGet("More Help", Color.BLUE, Color.YELLOW);
        jmiMenuItemExample = SwingLibrary.setupJMenuItemAndGet("About", this, k, null, Color.WHITE);
        jcbmiCheckBoxMenuItemExample = new JCheckBoxMenuItem("Show a message dialog box");
        jcbmiCheckBoxMenuItemExample.addActionListener(this);
        jrbmiRadioButtonMenuItem1 = new JRadioButtonMenuItem("Change background color to white");
        jrbmiRadioButtonMenuItem1.addActionListener(this);
        jrbmiRadioButtonMenuItem2 = new JRadioButtonMenuItem("Change background color to default");
        jrbmiRadioButtonMenuItem2.addActionListener(this);
        ButtonGroup bg = SwingLibrary.setupButtonGroupAndGet();
        bg.add(jrbmiRadioButtonMenuItem1);
        bg.add(jrbmiRadioButtonMenuItem2);
        jmSubMenu.add(jmiMenuItemExample);
        jmSubMenu.addSeparator();
        jmSubMenu.add(jcbmiCheckBoxMenuItemExample);
        jmSubMenu.addSeparator();
        jmSubMenu.add(jrbmiRadioButtonMenuItem1);
        jmSubMenu.add(jrbmiRadioButtonMenuItem2);
        jm.add(jmSubMenu);
        jmb.add(jm);
        jf.setJMenuBar(jmb);
    } // end of addJMenuItemsExample

    void createAndShowSwingGUIExamples() {
        //createJFrameExample();
        createScrollableJFrameExample();
        addJButtonExample();
        addJLabelExample();
        addToolTipExample();
        addJTextFieldExample();
        addJFormattedTextFieldExample();
        addJCheckBoxExample();
        addJRadioButtonExample();
        addJFileChooserExample();
        addJPasswordFieldExample();
        addScrollableJTextAreaExample();
        addScrollableJListExample();
        addJComboBoxExample();
        addJProgressBarExample();
        addJSliderExample();
        addScrollableJTreeExample();
        addJSpinnerExample();
        addJColorChooserExample();
        addJOptionPaneExamples();
        addJDialogExample();
        addScrollableJDialogExample();
        addJPopupMenuExample();
        addJToggleButtonExample();
        addJSeparatorExample();
        addJToolBarExample();
        addJMenuItemsExample();

        // Frame has been created, all examples have been added, now show the frame
        jf.setVisible(true);
    } // end of createAndShowSwingExamples

    void addHeadingLabel(String text, int xpos, int ypos, int width, int height) {
        //JLabel jl = SwingLibrary.setupJLabelAndGet(text, true, Color.YELLOW, SwingConstants.CENTER, SwingConstants.CENTER, true, xpos, ypos, width, height);
        JLabel jl = SwingLibrary.setupJLabelAndGet(text, true, lightBlue, SwingConstants.CENTER, SwingConstants.CENTER, true, xpos, ypos, width, height);
        jp.add(jl);
    } // end of addHeadingLabel

    void addLineLabel(int ypos) {
        JLabel jl = SwingLibrary.setupJLabelAndGet("", true, Color.BLACK, SwingConstants.CENTER, SwingConstants.CENTER, true, 0, ypos, screenWidth, lineLabelHeight);
        jp.add(jl);
        currentYPos = currentYPos + lineLabelHeight;
    } // end of addLineLabel

    public static void main(String[] args) {
        Examples_Of_Many_Swing_Components_In_One_Program eomsciop = new Examples_Of_Many_Swing_Components_In_One_Program();
        eomsciop.createAndShowSwingGUIExamples();
    } // end of main

} // end of Examples_Of_Many_Swing_Components_In_One_Program

The components whose examples are given in the code are:

  • JButton
  • JLabel
  • ToolTip
  • JTextField
  • JFormattedTextField
  • JCheckBox
  • JRadioButton
  • JFileChooser
  • JPasswordField
  • Scrollable JTextArea
  • Scrollable JList
  • JComboBox
  • JProgressBar
  • JSlider
  • Scrollable JTree
  • JSpinner
  • JColorChooser
  • JOptionPane
  • JDialog
  • Scrollable JDialog
  • JPopupMenu
  • JToggleButton
  • JSeparator
  • JToolBar
  • JMenuBar
  • JMenu
  • JMenuItem
  • JCheckBoxMenuItem
  • JRadioButtonMenuItem
\$\endgroup\$
4
  • \$\begingroup\$ Is this a real code review or a trick question? all your code is named example - is it for real? \$\endgroup\$ Nov 29 '21 at 7:01
  • \$\begingroup\$ Why do you think this is a trick question? All method names contain the word "example" but does this mean that the code is not real? The method names contain the word example because they show how to draw that swing component on the jframe and how to use that swing component. For example, "addScrollableJListExample()" method contains code that will draw a scrollable jlist on the jframe and also how to use the scrollable jlist (means how to get value that the user selected). \$\endgroup\$
    – Amit
    Dec 6 '21 at 9:25
  • \$\begingroup\$ codereview.stackexchange.com/help/how-to-ask : be on topic Do not ask questions about broken code, hypothetical code, or non-existent code, as such questions will be closed as off-topic. so i'm not sure if this is really an existing code... \$\endgroup\$ Dec 6 '21 at 14:44
  • \$\begingroup\$ @Martin, I don't know what you are talking about. Have you seen the whole code? Actually, I want to know whether you are able to see any code or not. \$\endgroup\$
    – Amit
    Dec 6 '21 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.