Examples Of Many Java Swing Components In One Program.
Amit
amitchoudhary0523 at gmail.com
Tue Oct 11 06:38:33 UTC 2022
Code for many Java Swing Components In One Program in case someone needs it.
Description: The program
Examples_Of_Many_Swing_Components_In_One_Program contains examples of
many swing components - that is, how to initialize them and use them.
You can use code from this program in your code. Where applicable,
appropriate listeners are also implemented.
Components covered are: JButton, JLabel, ToolTip, JTextField,
JFormattedTextField, JCheckBox, JRadioButton, JFileChooser,
JPasswordField, Scrollable JTextArea, Scrollable JList, JComboBox,
JProgressBar, JSlider, Scrollable JTree, JSpinner, JColorChooser,
JOptionPane, JDialog, JPopupMenu, JToggleButton, JSeparator, JToolBar,
JMenuBar, JMenu, JMenuItem, JCheckBoxMenuItem, JRadioButtonMenuItem.
-------------------------------------------------------------------------------------------
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 xMargin = screenWidth/10;
// vertical gap between components
int vGap = 25;
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;
@Override
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() {
@Override
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
@Override
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
@Override
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
@Override
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
@Override
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
@Override
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
@Override
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
@Override
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<>();
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<>();
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<>();
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<>();
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<>();
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
class SwingLibrary {
// if width is 0 then the frame is maximized horizontally
// if height is 0 then the frame is maximized vertically
public static JFrame setupJFrameAndGet(String title, int width,
int height) {
int state = 0;
JFrame tmpJF = new JFrame(title);
if (width == 0) {
state = state | JFrame.MAXIMIZED_HORIZ;
}
if (height == 0) {
state = state | JFrame.MAXIMIZED_VERT;
}
if ((width != 0) || (height != 0)) {
tmpJF.setSize(width, height);
}
tmpJF.setExtendedState(tmpJF.getExtendedState() | state);
tmpJF.setLocationRelativeTo(null);
tmpJF.setLayout(null);
tmpJF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return tmpJF;
} // end of setupJFrameAndGet
// width and height are the preferred width and height of JPanel
public static ArrayList<Object>
setupScrollableJFrameAndGetFrameAndPanel(String title, int width, int
height) {
JFrame tmpJF = new JFrame(title);
tmpJF.setExtendedState(tmpJF.getExtendedState() |
JFrame.MAXIMIZED_BOTH);
tmpJF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//tmpJF.setLayout(null);
JPanel tmpJP = new JPanel();
//tmpJP.setBounds(xpos, ypos, width + 1000, height + 1000);
tmpJP.setPreferredSize(new Dimension(width, height));
tmpJP.setLayout(null);
JScrollPane tmpJSPFrame = new JScrollPane(tmpJP,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
tmpJSPFrame.getHorizontalScrollBar().setUnitIncrement(10);
tmpJSPFrame.getVerticalScrollBar().setUnitIncrement(10);
tmpJF.add(tmpJSPFrame);
ArrayList<Object> tmpA = new ArrayList<>();
tmpA.add((Object) (tmpJF));
tmpA.add((Object) (tmpJP));
return tmpA;
} // end of setupScrollableJFrameAndGetFrameAndPanel
// actLisObj: object which implements action listener
public static JButton setupJButtonAndGet(String text, Object
actLisObj, boolean setBoundsFlag, int xpos, int ypos, int width, int
height) {
JButton tmpJB = new JButton(text);
if (setBoundsFlag == true) {
tmpJB.setBounds(xpos, ypos, width, height);
}
tmpJB.addActionListener((ActionListener) actLisObj);
return tmpJB;
} // end of setupJButtonAndGet
// halign: horizontal alignment of text, valign: vertical alignment of text
public static JLabel setupJLabelAndGet(String text, boolean
opaque, Color bg, int halign, int valign, boolean setBoundsFlag, int
xpos, int ypos, int width, int height) {
JLabel tmpJL = new JLabel(text);
if (setBoundsFlag == true) {
tmpJL.setBounds(xpos, ypos, width, height);
}
tmpJL.setOpaque(opaque);
if (bg != null) {
tmpJL.setBackground(bg);
}
tmpJL.setHorizontalAlignment(halign);
tmpJL.setVerticalAlignment(valign);
return tmpJL;
} // end of setupJlabelAndGet
public static JTextField setupJTextFieldAndGet(int xpos, int ypos,
int width, int height) {
JTextField tmpJTF = new JTextField();
tmpJTF.setBounds(xpos, ypos, width, height);
return tmpJTF;
} // end of setupJTextFieldAndGet
public static JFormattedTextField
setupJFormattedTextFieldAndGet(Format fmt, Object initialVal, Object
propertyChangeLis, String propertyToListenFor, int xpos, int ypos, int
width, int height) {
JFormattedTextField tmpJFTF = new JFormattedTextField(fmt);
tmpJFTF.setValue(initialVal);
tmpJFTF.addPropertyChangeListener(propertyToListenFor,
(PropertyChangeListener) propertyChangeLis);
tmpJFTF.setBounds(xpos, ypos, width, height);
return tmpJFTF;
} // end of setupJFormattedTextFieldAndGet
// itemLisObj: object which implements item listener
public static JCheckBox setupJCheckBoxAndGet(String text, boolean
state, Object itemLisObj, int xpos, int ypos, int width, int height) {
JCheckBox tmpJCB = new JCheckBox(text, state);
tmpJCB.setBounds(xpos, ypos, width, height);
tmpJCB.addItemListener((ItemListener) itemLisObj);
return tmpJCB;
} // end of setupJCheckBoxAndGet
// actLisObj: object which implements action listener
public static JRadioButton setupJRadioButtonAndGet(String text,
boolean state, Object actLisObj, int xpos, int ypos, int width, int
height) {
JRadioButton tmpJRB = new JRadioButton(text, state);
tmpJRB.setBounds(xpos, ypos, width, height);
tmpJRB.addActionListener((ActionListener) actLisObj);
return tmpJRB;
} // end of setupJRadioButtonAndGet
public static ButtonGroup setupButtonGroupAndGet() {
return new ButtonGroup();
} // end of setupButtonGroupAndGet
public static JPasswordField setupJPasswordFieldAndGet(int xpos,
int ypos, int width, int height) {
JPasswordField tmpJPF = new JPasswordField();
tmpJPF.setBounds(xpos, ypos, width, height);
return tmpJPF;
} // end of setupJPasswordFieldAndGet
public static JTextArea setupJTextAreaAndGet(String text, int
rows, int columns, boolean setEditableFlag, boolean setLineWrapFlag,
boolean setWrapStyleWordFlag, boolean setBoundsFlag, int xpos, int
ypos, int width, int height) {
JTextArea tmpJTA = new JTextArea(text, rows, columns);
tmpJTA.setEditable(setEditableFlag);
tmpJTA.setLineWrap(setLineWrapFlag);
tmpJTA.setWrapStyleWord(setWrapStyleWordFlag);
if (setBoundsFlag == true) {
tmpJTA.setBounds(xpos, ypos, width, height);
}
return tmpJTA;
} // end of setupJTextAreaAndGet
public static JScrollPane setupScrollableJTextAreaAndGet(JTextArea
jta, int xpos, int ypos, int width, int height) {
JScrollPane tmpJSP = new JScrollPane(jta);
tmpJSP.setBounds(xpos, ypos, width, height);
return tmpJSP;
} // end of setupScrollableJTextAreaAndGet
public static JList<String> setupJListAndGet(ListModel<String> lm,
int selectionMode, int visibleRowCount, int initialSelectedIndex,
Object listSelLisObj, boolean setBoundsFlag, int xpos, int ypos, int
width, int height) {
JList<String> tmpJList = new JList<>(lm);
tmpJList.setSelectionMode(selectionMode);
tmpJList.setVisibleRowCount(visibleRowCount);
if (initialSelectedIndex >= 0) {
tmpJList.setSelectedIndex(initialSelectedIndex);
}
tmpJList.addListSelectionListener((ListSelectionListener)
listSelLisObj);
if (setBoundsFlag == true) {
tmpJList.setBounds(xpos, ypos, width, height);
}
return tmpJList;
} // end of setupJListAndGet
public static JScrollPane setupScrollableJListAndGet(JList jlist,
int xpos, int ypos, int width, int height) {
JScrollPane tmpJSP = new JScrollPane(jlist);
tmpJSP.setBounds(xpos, ypos, width, height);
return tmpJSP;
} // end of setupScrollableJListAndGet
public static JComboBox<String>
setupJComboBoxAndGet(ComboBoxModel<String> cbm, int
initialSelectedIndex, Object actLisObj, boolean setBoundsFlag, int
xpos, int ypos, int width, int height) {
JComboBox<String> tmpJComboBox = new JComboBox<>(cbm);
if (initialSelectedIndex >= 0) {
tmpJComboBox.setSelectedIndex(initialSelectedIndex);
}
tmpJComboBox.addActionListener((ActionListener) actLisObj);
if (setBoundsFlag == true) {
tmpJComboBox.setBounds(xpos, ypos, width, height);
}
return tmpJComboBox;
} // end of setupJComboBoxAndGet
public static JProgressBar setupJProgressBarAndGet(int
orientation, int min, int max, int initialVal, boolean
borderPaintedFlag, boolean stringPaintedFlag, boolean setBoundsFlag,
int xpos, int ypos, int width, int height) {
JProgressBar tmpJPB = new JProgressBar(orientation, min, max);
tmpJPB.setValue(initialVal);
tmpJPB.setBorderPainted(borderPaintedFlag);
tmpJPB.setStringPainted(stringPaintedFlag);
if (setBoundsFlag == true) {
tmpJPB.setBounds(xpos, ypos, width, height);
}
return tmpJPB;
} // end of setupJProgressBarAndGet
public static JSlider setupJSliderAndGet(int orientation, int min,
int max, int initialVal, int minorTickSpacing, int majorTickSpacing,
boolean paintTicksFlag, boolean paintLabelsFlag, Object changeLisObj,
boolean setBoundsFlag, int xpos, int ypos, int width, int height) {
JSlider tmpJS = new JSlider(orientation, min, max, initialVal);
tmpJS.setMinorTickSpacing(minorTickSpacing);
tmpJS.setMajorTickSpacing(majorTickSpacing);
tmpJS.setPaintTicks(paintTicksFlag);
tmpJS.setPaintLabels(paintLabelsFlag);
tmpJS.addChangeListener((ChangeListener) changeLisObj);
if (setBoundsFlag == true) {
tmpJS.setBounds(xpos, ypos, width, height);
}
return tmpJS;
} // end of setupJSliderAndGet
public static JTree setupJTreeAndGet(DefaultMutableTreeNode
rootNode, int selectionMode, Object treeSelLisObj, boolean
setBoundsFlag, int xpos, int ypos, int width, int height) {
JTree tmpJTree = new JTree(rootNode);
tmpJTree.getSelectionModel().setSelectionMode(selectionMode);
tmpJTree.addTreeSelectionListener((TreeSelectionListener)
treeSelLisObj);
if (setBoundsFlag == true) {
tmpJTree.setBounds(xpos, ypos, width, height);
}
return tmpJTree;
} // end of setupJTreeAndGet
public static JScrollPane setupScrollableJTreeAndGet(JTree jtree,
int xpos, int ypos, int width, int height) {
JScrollPane tmpJSP = new JScrollPane(jtree);
tmpJSP.setBounds(xpos, ypos, width, height);
return tmpJSP;
} // end of setupScrollableJTreeAndGet
public static JSpinner setupJSpinnerAndGet(SpinnerModel sm,
boolean editableFlag, Object spinnerChangeLisObj, int xpos, int ypos,
int width, int height) {
JSpinner tmpJSPN = new JSpinner(sm);
tmpJSPN.addChangeListener((ChangeListener) spinnerChangeLisObj);
if (editableFlag == false) {
JComponent editor = tmpJSPN.getEditor();
if (editor instanceof JSpinner.DefaultEditor) {
((JSpinner.DefaultEditor)
editor).getTextField().setEditable(editableFlag);
} else {
System.out.println("Error: Could not set editableFlag
for JSpinner.");
}
}
tmpJSPN.setBounds(xpos, ypos, width, height);
return tmpJSPN;
} // end of setupJSpinnerAndGet
public static JColorChooser setupJColorChooserAndGet(Color
initialColor, boolean borderTitleFlag, String borderTitle, Object
colorChooserChangeLisObj, int xpos, int ypos, int width, int height) {
JColorChooser tmpJCC = new JColorChooser(initialColor);
tmpJCC.getSelectionModel().addChangeListener((ChangeListener)
colorChooserChangeLisObj);
if (borderTitleFlag == true) {
tmpJCC.setBorder(BorderFactory.createTitledBorder(borderTitle));
}
tmpJCC.setBounds(xpos, ypos, width, height);
return tmpJCC;
} // end of setupJColorChooserAndGet
public static JDialog setupJDialogAndGet(Frame owner, String
title, boolean modal, int width, int height) {
JDialog tmpJD = new JDialog(owner, title, modal);
tmpJD.setSize(width, height);
tmpJD.setLocationRelativeTo(null);
tmpJD.setLayout(null);
tmpJD.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
return tmpJD;
} // end of setupJDialogAndGet
public static ArrayList<Object>
setupScrollableJDialogAndGetDialogAndPanel(Frame owner, String title,
boolean modal, int dialogWidth, int dialogHeight, int panelWidth, int
panelHeight) {
JDialog tmpJD = new JDialog(owner, title, modal);
tmpJD.setSize(dialogWidth, dialogHeight);
tmpJD.setLocationRelativeTo(null);
tmpJD.setLayout(null);
tmpJD.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
JPanel tmpJP = new JPanel();
tmpJP.setPreferredSize(new Dimension(panelWidth, panelHeight));
tmpJP.setLayout(null);
ScrollPane sp = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
sp.getHAdjustable().setUnitIncrement(10);
sp.getVAdjustable().setUnitIncrement(10);
sp.add(tmpJP);
tmpJD.getRootPane().setContentPane(sp);
ArrayList<Object> tmpA = new ArrayList<>();
tmpA.add((Object) (tmpJD));
tmpA.add((Object) (tmpJP));
return tmpA;
} // end of setupScrollableJDialogAndGetDialogAndPanel
public static JToggleButton setupJToggleButtonAndGet(String text,
Object itemLisObj, boolean opaque, Color bgcolor, boolean
setBoundsFlag, int xpos, int ypos, int width, int height) {
JToggleButton tmpJTB = new JToggleButton(text);
if (setBoundsFlag == true) {
tmpJTB.setBounds(xpos, ypos, width, height);
}
tmpJTB.addItemListener((ItemListener) itemLisObj);
tmpJTB.setOpaque(opaque);
tmpJTB.setBackground(bgcolor);
return tmpJTB;
} // end of setupJToggleButtonAndGet
public static JSeparator setupJSeparatorAndGet(int orientation,
Color bgcolor, boolean setBoundsFlag, int xpos, int ypos, int width,
int height) {
JSeparator tmpJS = new JSeparator(orientation);
tmpJS.setBackground(bgcolor);
if (setBoundsFlag == true) {
tmpJS.setBounds(xpos, ypos, width, height);
}
return tmpJS;
} // end of setupJSeparatorAndGet
public static JMenuBar setupJMenuBarAndGet(Color fgcolor, Color bgcolor) {
JMenuBar tmpJMB = new JMenuBar();
tmpJMB.setOpaque(true);
tmpJMB.setForeground(fgcolor);
tmpJMB.setBackground(bgcolor);
return tmpJMB;
} // end of setupJMenuBarAndGet
public static JMenu setupJMenuAndGet(String text, Color fgcolor,
Color bgcolor) {
JMenu tmpJM = new JMenu(text);
tmpJM.setOpaque(true);
tmpJM.setForeground(fgcolor);
tmpJM.setBackground(bgcolor);
return tmpJM;
} // end of setupJMenuAndGet
public static JMenuItem setupJMenuItemAndGet(String text, Object
actLisObj, KeyStroke k, Color fgcolor, Color bgcolor) {
JMenuItem tmpJMI = new JMenuItem(text);
tmpJMI.setOpaque(true);
tmpJMI.setForeground(fgcolor);
tmpJMI.setBackground(bgcolor);
tmpJMI.setAccelerator(k);
if (actLisObj != null) {
tmpJMI.addActionListener((ActionListener) actLisObj);
}
return tmpJMI;
} // end of setupJMenuItemAndGet
} // end of SwingLibrary
-------------------------------------------------------------------------------------------
More information about the core-libs-dev
mailing list