Java tutorial
/* * The MIT License * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.yosanai.java.swing.config; import java.util.Map; import javax.swing.table.DefaultTableModel; import org.apache.commons.lang.mutable.MutableBoolean; /** * * @author Saravana Perumal Shanmugam */ @SuppressWarnings("serial") public class ConfigDialog extends javax.swing.JDialog { /** A return status code - returned if Cancel button has been pressed */ public static final int RET_CANCEL = 0; /** A return status code - returned if OK button has been pressed */ public static final int RET_OK = 1; protected CustomTableModel tableModel = new CustomTableModel(); public class CustomTableModel extends DefaultTableModel { @SuppressWarnings("rawtypes") Class[] types = new Class[] { java.lang.String.class, java.lang.String.class }; MutableBoolean[] canEdit = new MutableBoolean[] { new MutableBoolean(false), new MutableBoolean(true) }; /** * */ private CustomTableModel() { super(new Object[][] {}, new String[] { "Key", "Value" }); } @SuppressWarnings({ "unchecked", "rawtypes" }) public Class getColumnClass(int columnIndex) { return types[columnIndex]; } /* * (non-Jsdoc) * * @see javax.swing.table.DefaultTableModel#isCellEditable(int, int) */ @Override public boolean isCellEditable(int row, int column) { return canEdit[column].booleanValue(); } } /** Creates new form ConfigDialog */ public ConfigDialog(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); } public void setEditKey(boolean edit) { tableModel.canEdit[0].setValue(edit); tableModel.fireTableStructureChanged(); } public void setEditValue(boolean edit) { tableModel.canEdit[1].setValue(edit); tableModel.fireTableStructureChanged(); } /** @return the return status of this dialog - one of RET_OK or RET_CANCEL */ public int getReturnStatus() { return returnStatus; } public void loadConfig(Map<String, String> config) { cfgPanel.loadConfig(config); } public Map<String, String> getConfig() { return cfgPanel.getConfig(); } /** * 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. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" // <editor-fold defaultstate="collapsed" // <editor-fold defaultstate="collapsed" // <editor-fold defaultstate="collapsed" // <editor-fold defaultstate="collapsed" // <editor-fold defaultstate="collapsed" // <editor-fold defaultstate="collapsed" // <editor-fold defaultstate="collapsed" // desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { pnlBottom = new javax.swing.JPanel(); okButton = new javax.swing.JButton(); cancelButton = new javax.swing.JButton(); pnlTop = new javax.swing.JPanel(); cfgPanel = new com.yosanai.java.swing.config.ConfigPanel(); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { closeDialog(evt); } }); pnlBottom.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT)); okButton.setText("OK"); okButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { okButtonActionPerformed(evt); } }); pnlBottom.add(okButton); cancelButton.setText("Cancel"); cancelButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cancelButtonActionPerformed(evt); } }); pnlBottom.add(cancelButton); getContentPane().add(pnlBottom, java.awt.BorderLayout.PAGE_END); pnlTop.setLayout(new java.awt.BorderLayout()); pnlTop.add(cfgPanel, java.awt.BorderLayout.CENTER); getContentPane().add(pnlTop, java.awt.BorderLayout.CENTER); pack(); }// </editor-fold>//GEN-END:initComponents /** Closes the dialog */ private void closeDialog(java.awt.event.WindowEvent evt) {// GEN-FIRST:event_closeDialog doClose(RET_CANCEL); }// GEN-LAST:event_closeDialog private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_okButtonActionPerformed doClose(RET_OK); }// GEN-LAST:event_okButtonActionPerformed private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_cancelButtonActionPerformed doClose(RET_CANCEL); }// GEN-LAST:event_cancelButtonActionPerformed private void doClose(int retStatus) { returnStatus = retStatus; setVisible(false); dispose(); } /** * @param args * the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { ConfigDialog dialog = new ConfigDialog(new javax.swing.JFrame(), true); dialog.setEditKey(true); dialog.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0); } }); dialog.setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton cancelButton; private com.yosanai.java.swing.config.ConfigPanel cfgPanel; private javax.swing.JButton okButton; private javax.swing.JPanel pnlBottom; private javax.swing.JPanel pnlTop; // End of variables declaration//GEN-END:variables private int returnStatus = RET_CANCEL; }