Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.mycompany.listBoxer.panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.ParseException; import java.util.List; import javax.swing.DefaultComboBoxModel; import javax.swing.JFormattedTextField; import javax.swing.JTextArea; import javax.swing.text.DefaultFormatterFactory; import javax.swing.text.MaskFormatter; import org.apache.commons.lang3.StringUtils; import com.mycompany.listBoxer.dto.RangeType; import com.mycompany.listBoxer.services.ListBoxerService; import com.mycompany.listBoxer.services.impl.ListBoxerServiceImpl; /** * * @author vbobina */ public class ListBoxerForm extends javax.swing.JFrame { private static final long serialVersionUID = 5507256401682059615L; private ListBoxerService service = new ListBoxerServiceImpl(); private DefaultFormatterFactory factory = new DefaultFormatterFactory(); public ListBoxerForm() throws ParseException { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. * * @throws ParseException */ @SuppressWarnings({ "unchecked", "rawtypes" }) // <editor-fold defaultstate="collapsed" // desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() throws ParseException { buttonGroup1 = new javax.swing.ButtonGroup(); buttonGroup2 = new javax.swing.ButtonGroup(); RangeComboBox = new javax.swing.JComboBox(); jLabel1 = new javax.swing.JLabel(); jTextField1 = new JFormattedTextField(); AddButton = new javax.swing.JButton(); AscendingRadioButton = new javax.swing.JRadioButton(); DescendingRadioButton = new javax.swing.JRadioButton(); jTextField2 = new JTextArea(); ClearButton = new javax.swing.JButton(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); RecordsLabel = new javax.swing.JLabel(); TotalLabel = new javax.swing.JLabel(); AlphabeticCheckBox = new javax.swing.JCheckBox(); NumericCheckBox = new javax.swing.JCheckBox(); CombinedCheckBox = new javax.swing.JCheckBox(); jLabel6 = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); MainMenu = new javax.swing.JMenuBar(); FileMenuItem = new javax.swing.JMenu(); OpenItem = new javax.swing.JMenuItem(); SaveItem = new javax.swing.JMenuItem(); ExitItem = new javax.swing.JMenuItem(); EditMenuItem = new javax.swing.JMenu(); UndoItem = new javax.swing.JMenuItem(); jSeparator2 = new javax.swing.JPopupMenu.Separator(); CutItem = new javax.swing.JMenuItem(); CopyItem = new javax.swing.JMenuItem(); PasteItem = new javax.swing.JMenuItem(); HelpMenuItem = new javax.swing.JMenu(); AboutItem = new javax.swing.JMenuItem(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); List<String> rangeList = RangeType.getStringValue(); String[] arrays = new String[rangeList.size()]; RangeComboBox.setModel(new javax.swing.DefaultComboBoxModel(rangeList.toArray(arrays))); RangeComboBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { justDoIt(); } private void justDoIt() { if (NumericCheckBox.isSelected()) { } else if (AlphabeticCheckBox.isSelected()) { } else { } } }); jLabel1.setText("Range"); jTextField1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTextField1ActionPerformed(evt); } }); jTextField2.setEditable(Boolean.FALSE); AddButton.setText("Add to list"); AddButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { AddButtonActionPerformed(evt); } }); buttonGroup1.add(AscendingRadioButton); AscendingRadioButton.setText("Ascending"); AscendingRadioButton.setBorder(javax.swing.BorderFactory.createTitledBorder("Sort Order")); AscendingRadioButton.setName("Sort Order"); // NOI18N buttonGroup1.add(DescendingRadioButton); DescendingRadioButton.setText("Descending"); DescendingRadioButton.setBorder(javax.swing.BorderFactory.createTitledBorder("Sort Order")); DescendingRadioButton.setName("Sort Order"); // NOI18N ClearButton.setText("Clear list"); ClearButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { ClearButtonActionPerformed(event); } }); jLabel2.setText("Records in list : "); jLabel3.setText("Total records : "); RecordsLabel.setText("0"); TotalLabel.setText("0"); buttonGroup2.add(AlphabeticCheckBox); AlphabeticCheckBox.setText("Alphabetic"); AlphabeticCheckBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { AlphabeticCheckBoxActionPerformed(evt); } }); buttonGroup2.add(NumericCheckBox); NumericCheckBox.setText("Numeric"); NumericCheckBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { NumericCheckBoxActionPerformed(e); } }); buttonGroup2.add(CombinedCheckBox); CombinedCheckBox.setText("Combined"); CombinedCheckBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { CombinedCheckBoxActionPerformed(e); } }); jLabel6.setText("Symbols :"); jLabel7.setText("Sort order :"); MainMenu.setName(""); // NOI18N MainMenu.setRequestFocusEnabled(false); FileMenuItem.setText("File"); OpenItem.setText("Open"); FileMenuItem.add(OpenItem); SaveItem.setText("Save as"); FileMenuItem.add(SaveItem); ExitItem.setText("Exit"); FileMenuItem.add(ExitItem); MainMenu.add(FileMenuItem); EditMenuItem.setText("Edit"); UndoItem.setText("Undo"); EditMenuItem.add(UndoItem); EditMenuItem.add(jSeparator2); CutItem.setText("Cut"); EditMenuItem.add(CutItem); CopyItem.setText("Copy"); EditMenuItem.add(CopyItem); PasteItem.setText("Paste"); EditMenuItem.add(PasteItem); MainMenu.add(EditMenuItem); HelpMenuItem.setText("Help"); AboutItem.setText("About ListBoxer"); HelpMenuItem.add(AboutItem); MainMenu.add(HelpMenuItem); setJMenuBar(MainMenu); MainMenu.getAccessibleContext().setAccessibleName(""); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(jLabel1).addGap(22, 22, 22) .addComponent(RangeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(AlphabeticCheckBox).addComponent(NumericCheckBox) .addComponent(CombinedCheckBox).addComponent(jLabel6).addComponent(jLabel7))) .addComponent(AscendingRadioButton).addComponent(DescendingRadioButton, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(44, 44, 44) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup().addComponent(jLabel3).addGap(0, 53, Short.MAX_VALUE)) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(AddButton, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE) .addComponent(ClearButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addGap(21, 21, 21)) .addGroup(layout.createSequentialGroup() .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(TotalLabel) .addGroup(layout.createSequentialGroup().addComponent(jLabel2) .addGap(18, 18, 18).addComponent(RecordsLabel))) .addContainerGap(26, Short.MAX_VALUE))))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(19, 19, 19).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(RangeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(AddButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(32, 32, 32) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(ClearButton, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2).addComponent(RecordsLabel)) .addGap(18, 18, 18) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3).addComponent(TotalLabel)) .addGap(38, 38, 38)) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(17, 17, 17) .addComponent(jLabel7).addGap(1, 1, 1) .addComponent(AscendingRadioButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(DescendingRadioButton, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(69, 69, 69).addComponent(jLabel6) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(AlphabeticCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(NumericCheckBox) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(CombinedCheckBox))) .addGap(0, 22, Short.MAX_VALUE))))); pack(); } private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) { } private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) { if (service.saveContent(jTextField1.getText())) { String text = TotalLabel.getText(); try { Integer intText = Integer.valueOf(text); intText += 1; TotalLabel.setText(intText.toString()); jTextField2.setText(service.getAll()); } catch (Exception ex) { // TODO show error message } jTextField1.setText(StringUtils.EMPTY); } else { // TODO show error message } } private void ClearButtonActionPerformed(ActionEvent event) { if (service.clearAll()) { jTextField2.setText(StringUtils.EMPTY); TotalLabel.setText("0"); } else { // TODO show error message } } private void AlphabeticCheckBoxActionPerformed(ActionEvent evt) { try { jTextField1.setValue(StringUtils.EMPTY); factory.setDefaultFormatter((new MaskFormatter("????"))); jTextField1.setFormatterFactory(factory); } catch (ParseException e) { e.getMessage(); } RangeComboBox.setModel(new DefaultComboBoxModel<String>( new String[] { RangeType.ALL.getKey(), RangeType.AM.getKey(), RangeType.NZ.getKey() })); } private void NumericCheckBoxActionPerformed(ActionEvent evt) { try { jTextField1.setValue(StringUtils.EMPTY); factory.setDefaultFormatter(new MaskFormatter("####")); jTextField1.setFormatterFactory(factory); } catch (ParseException e) { e.getMessage(); } RangeComboBox.setModel( new DefaultComboBoxModel<String>(new String[] { RangeType.ALL.getKey(), RangeType.NUM1.getKey(), RangeType.NUM2.getKey(), RangeType.NUM3.getKey(), RangeType.NUM4.getKey() })); } private void CombinedCheckBoxActionPerformed(ActionEvent evt) { try { jTextField1.setValue(StringUtils.EMPTY); factory.setDefaultFormatter(new MaskFormatter("****")); jTextField1.setFormatterFactory(factory); } catch (ParseException e) { e.getMessage(); } RangeComboBox.setModel(new DefaultComboBoxModel<String>(new String[] { RangeType.ALL.getKey(), RangeType.AM.getKey(), RangeType.NZ.getKey(), RangeType.NUM1.getKey(), RangeType.NUM2.getKey(), RangeType.NUM3.getKey(), RangeType.NUM4.getKey() })); } public static void main(String args[]) { try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(ListBoxerForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(ListBoxerForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(ListBoxerForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(ListBoxerForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { try { new ListBoxerForm().setVisible(true); } catch (ParseException e) { } } }); } private javax.swing.JMenuItem AboutItem; private javax.swing.JButton AddButton; private javax.swing.JCheckBox AlphabeticCheckBox; private javax.swing.JRadioButton AscendingRadioButton; private javax.swing.JButton ClearButton; private javax.swing.JCheckBox CombinedCheckBox; private javax.swing.JMenuItem CopyItem; private javax.swing.JMenuItem CutItem; private javax.swing.JRadioButton DescendingRadioButton; private javax.swing.JMenu EditMenuItem; private javax.swing.JMenuItem ExitItem; private javax.swing.JMenu FileMenuItem; private javax.swing.JMenu HelpMenuItem; private javax.swing.JMenuBar MainMenu; private javax.swing.JCheckBox NumericCheckBox; private javax.swing.JMenuItem OpenItem; private javax.swing.JMenuItem PasteItem; private javax.swing.JComboBox<String> RangeComboBox; private javax.swing.JLabel RecordsLabel; private javax.swing.JMenuItem SaveItem; private javax.swing.JLabel TotalLabel; private javax.swing.JMenuItem UndoItem; private javax.swing.ButtonGroup buttonGroup1; private javax.swing.ButtonGroup buttonGroup2; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JPopupMenu.Separator jSeparator2; private JFormattedTextField jTextField1; private javax.swing.JTextArea jTextField2; }