Created
June 3, 2018 14:31
-
-
Save pieter-duplessis/86b8e7db7ad165bc2bafaaa80f6c8174 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Author: Pieter du Plessis | |
Date: 2018/06/01 | |
Description: | |
Small program to show different component with the available LookAndFeel's on the system. | |
// Command Prompt - jar cfve LookAndFeel.jar LookAndFeel LookAndFeel.class | |
*/ | |
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.*; | |
class LookAndFeel implements ActionListener, ItemListener { | |
//Class Variables - For Action Events and Item Listener Events methods to action. | |
JFrame frame = new JFrame("Showcase Available Look & Feel"); | |
JButton btn = new JButton("I show a Popup"); | |
JButton progressBar = new JButton("Progress Bar"); | |
JButton slider = new JButton("Slider"); | |
JButton radioButton = new JButton("Radio Buttons"); | |
JButton checkBoxes = new JButton("Check Boxes"); | |
JComboBox combo = new JComboBox(); | |
JMenuItem sub1_1 = new JMenuItem("Error Message"); | |
JMenuItem sub1_2 = new JMenuItem("Information Message"); | |
JMenuItem sub1_3 = new JMenuItem("Plain Message"); | |
JMenuItem sub1_4 = new JMenuItem("Question Message"); | |
JMenuItem sub1_5 = new JMenuItem("Warning Message"); | |
JMenuItem sub2_1 = new JMenuItem("Error Message"); | |
JMenuItem sub2_2 = new JMenuItem("Information Message"); | |
JMenuItem sub2_3 = new JMenuItem("Plain Message"); | |
JMenuItem sub2_4 = new JMenuItem("Question Message"); | |
JMenuItem sub2_5 = new JMenuItem("Warning Message"); | |
JMenuItem sub3_1 = new JMenuItem("Error Message"); | |
JMenuItem sub3_2 = new JMenuItem("Information Message"); | |
JMenuItem sub3_3 = new JMenuItem("Plain Message"); | |
JMenuItem sub3_4 = new JMenuItem("Question Message"); | |
JMenuItem sub3_5 = new JMenuItem("Warning Message"); | |
JMenuItem sub4_1 = new JMenuItem("Error Message"); | |
JMenuItem sub4_2 = new JMenuItem("Information Message"); | |
JMenuItem sub4_3 = new JMenuItem("Plain Message"); | |
JMenuItem sub4_4 = new JMenuItem("Question Message"); | |
JMenuItem sub4_5 = new JMenuItem("Warning Message"); | |
//Set Class Variable with LookAndFeel that is available on the system the program runs on. | |
UIManager.LookAndFeelInfo[] laf = UIManager.getInstalledLookAndFeels(); | |
LookAndFeel() { | |
frame.setBounds(200, 200, 600, 600); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
// Add available LookAndFeel's to Array for the ComboBox | |
for (int i=0;i < laf.length; i++) { | |
combo.addItem(laf[i].getName()); | |
} | |
// Create and add options to MenuBar | |
JMenuBar myMenuBar = new JMenuBar(); | |
JMenu myMenu = new JMenu("Dialog Boxes"); | |
JMenuItem myMenuItem = new JMenuItem("My Menu Item (Tooltip)"); | |
myMenuItem.setToolTipText("This button does nothing"); | |
JMenu subMenu1 = new JMenu("Confirm Dialog"); | |
JMenu subMenu2 = new JMenu("Input Dialog"); | |
JMenu subMenu3 = new JMenu("Message Dialog"); | |
JMenu subMenu4 = new JMenu("Option Dialog"); | |
subMenu1.add(sub1_1); | |
subMenu1.add(sub1_2); | |
subMenu1.add(sub1_3); | |
subMenu1.add(sub1_4); | |
subMenu1.add(sub1_5); | |
subMenu2.add(sub2_1); | |
subMenu2.add(sub2_2); | |
subMenu2.add(sub2_3); | |
subMenu2.add(sub2_4); | |
subMenu2.add(sub2_5); | |
subMenu3.add(sub3_1); | |
subMenu3.add(sub3_2); | |
subMenu3.add(sub3_3); | |
subMenu3.add(sub3_4); | |
subMenu3.add(sub3_5); | |
subMenu4.add(sub4_1); | |
subMenu4.add(sub4_2); | |
subMenu4.add(sub4_3); | |
subMenu4.add(sub4_4); | |
subMenu4.add(sub4_5); | |
myMenu.add(myMenuItem); | |
myMenu.add(subMenu1); | |
myMenu.add(subMenu2); | |
myMenu.add(subMenu3); | |
myMenu.add(subMenu4); | |
myMenuBar.add(myMenu); | |
// Create and add components to the ToolBar | |
JToolBar bar = new JToolBar(); | |
bar.add(progressBar); | |
bar.add(slider); | |
bar.add(new JToolBar.Separator()); | |
bar.add(radioButton); | |
bar.add(checkBoxes); | |
bar.add(combo); | |
// Create components for first panel | |
JTextField txtFld = new JTextField("Hello world"); | |
JTextArea txtArea = new JTextArea(10, 30); | |
JScrollPane sp = new JScrollPane(txtArea); | |
for (int i=0;i<laf.length;i++) { | |
txtArea.append("Name: "+laf[i].getName()+"\n"); | |
txtArea.append("Class Name: "+laf[i].getClassName()+"\n"); | |
txtArea.append("\n"); | |
} | |
txtArea.append("This is an JTextArea field\n\n\n\n\n\nAlmost like and text editor\n\n\n\n\n\n"); | |
// Add Action Listeners to Components | |
btn.addActionListener(this); | |
progressBar.addActionListener(this); | |
slider.addActionListener(this); | |
radioButton.addActionListener(this); | |
checkBoxes.addActionListener(this); | |
combo.addItemListener(this); | |
sub1_1.addActionListener(this); | |
sub1_2.addActionListener(this); | |
sub1_3.addActionListener(this); | |
sub1_4.addActionListener(this); | |
sub1_5.addActionListener(this); | |
sub2_1.addActionListener(this); | |
sub2_2.addActionListener(this); | |
sub2_3.addActionListener(this); | |
sub2_4.addActionListener(this); | |
sub2_5.addActionListener(this); | |
sub3_1.addActionListener(this); | |
sub3_2.addActionListener(this); | |
sub3_3.addActionListener(this); | |
sub3_4.addActionListener(this); | |
sub3_5.addActionListener(this); | |
sub4_1.addActionListener(this); | |
sub4_2.addActionListener(this); | |
sub4_3.addActionListener(this); | |
sub4_4.addActionListener(this); | |
sub4_5.addActionListener(this); | |
// Add items to the main panel to display most of the components | |
JPanel panel = new JPanel(); | |
panel.add(txtFld); | |
panel.add(btn); | |
panel.add(sp); | |
// Add components to the frame | |
frame.setJMenuBar(myMenuBar); | |
frame.add(BorderLayout.NORTH, bar); | |
frame.add(BorderLayout.SOUTH, panel); | |
frame.pack(); | |
frame.setVisible(true); | |
} | |
public void actionPerformed(ActionEvent evt) { | |
// Action Event for Clicking on the Buttonss | |
Object source = evt.getSource(); | |
if (source.equals(btn)) { | |
// Display plain popup | |
JOptionPane.showMessageDialog(null, "This is an PopUp"); | |
} else if (source.equals(progressBar)) { | |
// Display Progress Bar at 45 percent | |
JProgressBar pb = new JProgressBar(); | |
pb.setValue(45); | |
pb.setStringPainted(true); | |
JOptionPane.showMessageDialog(null, pb); | |
} else if (source.equals(slider)) { | |
// Display Slider | |
JSlider slide = new JSlider(JSlider.HORIZONTAL, -50, 50, 0); | |
slide.setMajorTickSpacing(20); | |
slide.setMinorTickSpacing(5); | |
slide.setPaintTicks(true); | |
slide.setPaintLabels(true); | |
JOptionPane.showMessageDialog(null, slide); | |
} else if (source.equals(radioButton)) { | |
// Display Grouped Radio Button on a Panel | |
JPanel pnl = new JPanel(); | |
ButtonGroup bg = new ButtonGroup(); | |
JRadioButton rb1 = new JRadioButton("Button one", true); | |
JRadioButton rb2 = new JRadioButton("Button two", false); | |
bg.add(rb1); | |
bg.add(rb2); | |
pnl.add(rb1); | |
pnl.add(rb2); | |
JOptionPane.showMessageDialog(null, pnl); | |
} else if (source.equals(checkBoxes)) { | |
// Display Check Boxes on a Panel | |
JPanel pnl = new JPanel(); | |
JCheckBox cb1 = new JCheckBox("Extra Cheese", true); | |
JCheckBox cb2 = new JCheckBox("Extra Meat", false); | |
pnl.add(cb1); | |
pnl.add(cb2); | |
JOptionPane.showMessageDialog(null, pnl); | |
} else if (source.equals(sub1_1)) { | |
int val = JOptionPane.showConfirmDialog(null, "Confirm dialog showing error message with yes no cancel options", "Confirm Dialog Error Message", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.ERROR_MESSAGE); | |
if (val == JOptionPane.YES_OPTION) { | |
JOptionPane.showMessageDialog(null, "Yes Option was selected"); | |
} else if (val == JOptionPane.NO_OPTION) { | |
JOptionPane.showMessageDialog(null, "No Option was selected"); | |
} | |
} else if (source.equals(sub1_2)) { | |
int val = JOptionPane.showConfirmDialog(null, "Confirm dialog showing information message with yes no cancel options", "Confirm Dialog Information Message", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE); | |
if (val == JOptionPane.YES_OPTION) { | |
JOptionPane.showMessageDialog(null, "Yes Option was selected"); | |
} else if (val == JOptionPane.NO_OPTION) { | |
JOptionPane.showMessageDialog(null, "No Option was selected"); | |
} | |
} else if (source.equals(sub1_3)) { | |
int val = JOptionPane.showConfirmDialog(null, "Confirm dialog showing plain message with yes no cancel options", "Confirm Dialog Plain Message", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); | |
if (val == JOptionPane.YES_OPTION) { | |
JOptionPane.showMessageDialog(null, "Yes Option was selected"); | |
} else if (val == JOptionPane.NO_OPTION) { | |
JOptionPane.showMessageDialog(null, "No Option was selected"); | |
} | |
} else if (source.equals(sub1_4)) { | |
int val = JOptionPane.showConfirmDialog(null, "Confirm dialog showing question message with yes no cancel options", "Confirm Dialog Question Message", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); | |
if (val == JOptionPane.YES_OPTION) { | |
JOptionPane.showMessageDialog(null, "Yes Option was selected"); | |
} else if (val == JOptionPane.NO_OPTION) { | |
JOptionPane.showMessageDialog(null, "No Option was selected"); | |
} | |
} else if (source.equals(sub1_5)) { | |
int val = JOptionPane.showConfirmDialog(null, "Confirm dialog showing warning message with yes no cancel options", "Confirm Dialog Warning Message", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE); | |
if (val == JOptionPane.YES_OPTION) { | |
JOptionPane.showMessageDialog(null, "Yes Option was selected"); | |
} else if (val == JOptionPane.NO_OPTION) { | |
JOptionPane.showMessageDialog(null, "No Option was selected"); | |
} | |
} else if (source.equals(sub2_1)) { | |
String val = JOptionPane.showInputDialog(null, "Enter something:", "Input Something - Error", JOptionPane.ERROR_MESSAGE); | |
if (val == null) { | |
JOptionPane.showMessageDialog(null, "Operation cancelled"); | |
} else if (val.length() == 0) { | |
JOptionPane.showMessageDialog(null, "You have not entered anything"); | |
} else if (val != null) { | |
JOptionPane.showMessageDialog(null, "You have entered: "+val); | |
} | |
} else if (source.equals(sub2_2)) { | |
String val = JOptionPane.showInputDialog(null, "Enter something:", "Input Something - Information", JOptionPane.INFORMATION_MESSAGE); | |
if (val == null) { | |
JOptionPane.showMessageDialog(null, "Operation cancelled"); | |
} else if (val.length() == 0) { | |
JOptionPane.showMessageDialog(null, "You have not entered anything"); | |
} else if (val != null) { | |
JOptionPane.showMessageDialog(null, "You have entered: "+val); | |
} | |
} else if (source.equals(sub2_3)) { | |
String val = JOptionPane.showInputDialog(null, "Enter something:", "Input Something - Plain", JOptionPane.PLAIN_MESSAGE); | |
if (val == null) { | |
JOptionPane.showMessageDialog(null, "Operation cancelled"); | |
} else if (val.length() == 0) { | |
JOptionPane.showMessageDialog(null, "You have not entered anything"); | |
} else if (val != null) { | |
JOptionPane.showMessageDialog(null, "You have entered: "+val); | |
} | |
} else if (source.equals(sub2_4)) { | |
String val = JOptionPane.showInputDialog(null, "Enter something:", "Input Something - Question", JOptionPane.QUESTION_MESSAGE); | |
if (val == null) { | |
JOptionPane.showMessageDialog(null, "Operation cancelled"); | |
} else if (val.length() == 0) { | |
JOptionPane.showMessageDialog(null, "You have not entered anything"); | |
} else if (val != null) { | |
JOptionPane.showMessageDialog(null, "You have entered: "+val); | |
} | |
} else if (source.equals(sub2_5)) { | |
String val = JOptionPane.showInputDialog(null, "Enter something:", "Input Something - Warning", JOptionPane.WARNING_MESSAGE); | |
if (val == null) { | |
JOptionPane.showMessageDialog(null, "Operation cancelled"); | |
} else if (val.length() == 0) { | |
JOptionPane.showMessageDialog(null, "You have not entered anything"); | |
} else if (val != null) { | |
JOptionPane.showMessageDialog(null, "You have entered: "+val); | |
} | |
} else if (source.equals(sub3_1)) { | |
JOptionPane.showMessageDialog(null, "There is no message", "Message Dialog - Error", JOptionPane.ERROR_MESSAGE); | |
} else if (source.equals(sub3_2)) { | |
JOptionPane.showMessageDialog(null, "There is no message", "Message Dialog - Information", JOptionPane.INFORMATION_MESSAGE); | |
} else if (source.equals(sub3_3)) { | |
JOptionPane.showMessageDialog(null, "There is no message", "Message Dialog - Plain", JOptionPane.PLAIN_MESSAGE); | |
} else if (source.equals(sub3_4)) { | |
JOptionPane.showMessageDialog(null, "There is no message", "Message Dialog - Question", JOptionPane.QUESTION_MESSAGE); | |
} else if (source.equals(sub3_5)) { | |
JOptionPane.showMessageDialog(null, "There is no message", "Message Dialog - Warning", JOptionPane.WARNING_MESSAGE); | |
} else if (source.equals(sub4_1)) { | |
JPanel panel = new JPanel(); | |
JLabel label = new JLabel("Select Something:"); | |
String[] data = new String[]{"Option 1", "Option 2", "Option 3"}; | |
JComboBox combo = new JComboBox(data); | |
panel.add(label); | |
panel.add(combo); | |
int val = JOptionPane.showOptionDialog(null, panel, "Select Something - Error Message", JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null, null, null); | |
if (val == JOptionPane.YES_OPTION) { | |
JOptionPane.showMessageDialog(null, "Yes selected on option: "+combo.getSelectedItem()); | |
} else if (val == JOptionPane.NO_OPTION) { | |
JOptionPane.showMessageDialog(null, "No selected"); | |
} else { | |
JOptionPane.showMessageDialog(null, "NOTHING!!!"); | |
} | |
} else if (source.equals(sub4_2)) { | |
JPanel panel = new JPanel(); | |
ButtonGroup bg = new ButtonGroup(); | |
JRadioButton rb1 = new JRadioButton("Male", false); | |
JRadioButton rb2 = new JRadioButton("Female", false); | |
bg.add(rb1); | |
bg.add(rb2); | |
panel.add(rb1); | |
panel.add(rb2); | |
String[] options = new String[]{"Accept Selection", "Decline Selection"}; | |
int val = JOptionPane.showOptionDialog(null, panel, "Male/Female Selection - Information Message", 0, JOptionPane.INFORMATION_MESSAGE, null, options, options[1]); | |
if (val == 0) { | |
if (rb1.isSelected() == true) { | |
JOptionPane.showMessageDialog(null, "Male was selected"); | |
} else if (rb2.isSelected() == true) { | |
JOptionPane.showMessageDialog(null, "Female was selected"); | |
} else { | |
JOptionPane.showMessageDialog(null, "Nothing was selected"); | |
} | |
} else if (val == 1) { | |
JOptionPane.showMessageDialog(null, "This selection was declined"); | |
} else if (val == -1) { | |
JOptionPane.showMessageDialog(null, "This operation was closed"); | |
} | |
} else if (source.equals(sub4_3)) { | |
String[] options = new String[]{"Dog", "Cat", "Bird", "Fish", "Other"}; | |
int val = JOptionPane.showOptionDialog(null, "What is you favourite pet?", "Favourite Pet", 0, JOptionPane.PLAIN_MESSAGE, null, options, null); | |
if (val < 0) { | |
JOptionPane.showMessageDialog(null, "Operation closed!!!"); | |
} else { | |
JOptionPane.showMessageDialog(null, "You have selected '"+options[val]+"' as your favourite pet"); | |
} | |
} else if (source.equals(sub4_4)) { | |
String[] options = new String[]{"Pizza", "Pasta", "Burger", "Steak", "Salad", "Other"}; | |
int val = JOptionPane.showOptionDialog(null, "What is you favourite food?", "Favourite Food", 0, JOptionPane.QUESTION_MESSAGE, null, options, null); | |
if (val < 0) { | |
JOptionPane.showMessageDialog(null, "Operation closed!!!"); | |
} else { | |
JOptionPane.showMessageDialog(null, "You have selected '"+options[val]+"' as your favourite food"); | |
} | |
} else if (source.equals(sub4_5)) { | |
String[] options = new String[]{"Rugby", "Netbal", "Soccer", "Hockey", "Tennis", "Chess", "Other"}; | |
int val = JOptionPane.showOptionDialog(null, "What is you favourite sport?", "Favourite Sport", 0, JOptionPane.WARNING_MESSAGE, null, options, null); | |
if (val < 0) { | |
JOptionPane.showMessageDialog(null, "Operation closed!!!"); | |
} else { | |
JOptionPane.showMessageDialog(null, "You have selected '"+options[val]+"' as your favourite sport"); | |
} | |
} | |
} | |
public void itemStateChanged(ItemEvent e) { | |
// Item State changed for Combo Box | |
Object source = e.getSource(); | |
if (source.equals(combo)) { | |
try { | |
// Get selected LookAndFeel and set as current updating components | |
UIManager.setLookAndFeel(laf[combo.getSelectedIndex()].getClassName()); | |
SwingUtilities.updateComponentTreeUI(frame); | |
frame.pack(); | |
} catch (Exception a) { | |
System.out.println("There was an error setting look and feel"); | |
} | |
} | |
} | |
public static void main(String[] args) { | |
// Run the program | |
new LookAndFeel(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment