Results 1 to 3 of 3
  1. #1
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default How to refresh my JInternalFrame upon CUD

    Greetings to eveyone,

    I've been studying Java Swing already for the past days yet I still cannot figure out on how to reload (or refresh) my JIF upon CUD (Create, Update, Delete). The system works fine, all transactions are functioning well. I just can't figure how to work the refresh function.

    Here my code(s).

    View.java
    Java Code:
    package Employee;
    
    import java.awt.Dimension;
    
    import javax.swing.GroupLayout;
    import javax.swing.LayoutStyle;
    import javax.swing.SwingConstants;
    import javax.swing.JInternalFrame;
    import javax.swing.JLabel;
    import javax.swing.JButton;
    import javax.swing.JTextField;
    import javax.swing.JComboBox;
    import javax.swing.JCheckBox;
    
    public class ViewType extends JInternalFrame{
    
        private final JLabel typeLabel = new JLabel("Type", SwingConstants.LEFT);
        private final JLabel nameLabel = new JLabel("Type Name", SwingConstants.LEFT);
        
        public JComboBox typeCB = null;
        public JButton createButton = new JButton("Create");
        public JButton editButton = new JButton("Edit");
        public JButton deleteButton = new JButton("Delete");
        public JTextField nameField = new JTextField(8);
        public JCheckBox exceptionCB = new JCheckBox("Excpetion", false);
        public JCheckBox cashcountCB = new JCheckBox("Cash Count", false);
        public JCheckBox adminCB = new JCheckBox("Administrator", false);
    
        public ViewType() {
            defaultSet();
    
            setTitle("Employee Type");
            setSize(new Dimension(275,250));
            setResizable(true);
            setClosable(true);
            setIconifiable(true);
        }
    
        protected void loadLayout() {
            GroupLayout layout = new GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addComponent(adminCB)
                        .addComponent(cashcountCB)
                        .addComponent(exceptionCB)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(typeLabel)
                            .addGap(18, 18, 18)
                            .addComponent(typeCB, 0, 149, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(createButton)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(editButton)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(deleteButton))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(nameLabel)
                            .addGap(18, 18, 18)
                            .addComponent(nameField, GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(typeLabel)
                        .addComponent(typeCB, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(createButton)
                        .addComponent(editButton)
                        .addComponent(deleteButton))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(nameLabel)
                        .addComponent(nameField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addComponent(exceptionCB)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(cashcountCB)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(adminCB)
                    .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
        }
    
        private void defaultSet() {
            createButton.setEnabled(true);
            editButton.setEnabled(false);
            deleteButton.setEnabled(false);
            nameField.setEditable(false);
            exceptionCB.setEnabled(false);
            cashcountCB.setEnabled(false);
            adminCB.setEnabled(false);
        }
    }
    DBEmployeeType
    Java Code:
    package Database;
    
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.sql.PreparedStatement;
    
    import java.util.Vector;
    
    public class DBEmployeeType extends DBConnect {
    
        private Connection conn = connect();
    
        private final String className = "DBEmployeeType";
        private final String typesSQL = "SELECT ET.NAME FROM EMPLOYEE_TYPE ET ORDER BY ET.EMPLOYEE_TYPE_ID";
        private final String countSQL = "SELECT COUNT(*) FROM EMPLOYEE_TYPE";
        private final String loadSQL = "SELECT EMPLOYEE_TYPE_ID, EXCEPTIONS, CASHCOUNT, ADMIN FROM EMPLOYEE_TYPE WHERE NAME = ";
        private final String nextIDSQL = "SELECT MAX(EMPLOYEE_TYPE_ID) FROM EMPLOYEE_TYPE";
        private final String createSQL = "INSERT INTO EMPLOYEE_TYPE VALUES (?, ?, ?, ?, ?)";
        private final String editSQL = "UPDATE EMPLOYEE_TYPE SET NAME = ?, EXCEPTIONS = ?, CASHCOUNT = ?, ADMIN = ? WHERE EMPLOYEE_TYPE_ID = ?";
        private final String deleteSQL = "DELETE FROM EMPLOYEE_TYPE WHERE NAME = ?";
        private final String getIDSQL = "SELECT EMPLOYEE_TYPE_ID FROM EMPLOYEE_TYPE WHERE NAME = ?";
    
        public DBEmployeeType() {
            
        }
    
        public Vector loadTypes() {
            Vector<String> types = new Vector();
                  
            try {
                Statement stmt = conn.createStatement();
                ResultSet result = stmt.executeQuery(typesSQL);
                while(result.next()) types.addElement(result.getString(1));            
                result.close();
                stmt.close();
            } catch(Exception e) { report(className, "loadTypes", e.getMessage()); }
    
            return types;
        }
    
        public Vector loadData(String name) {
            Vector data = new Vector();
    
            try {
                Statement stmt = conn.createStatement();
                ResultSet result = stmt.executeQuery(loadSQL + "'" + name + "'");
                if(result.next()) {
                    for(int index=1; index<=4; index++) {
                        if(index>1) data.addElement(convert(result.getString(index).charAt(0)));
                        else data.addElement(result.getString(index));
                    }
                }
                result.close();
                stmt.close();
            } catch(Exception e) { report(className, "loadData", e.getMessage()); }
    
            return data;
        }
    
        public void create(String name, boolean exception, boolean cashcount, boolean admin) {
            try {
                PreparedStatement stmt = conn.prepareCall(createSQL);
                stmt.setInt(1, newID());
                stmt.setString(2, name);
                stmt.setString(3, convert(exception));
                stmt.setString(4, convert(cashcount));
                stmt.setString(5, convert(admin));
                stmt.executeUpdate();
                stmt.close();
            } catch (Exception e) { report(className, "create", e.getMessage()); }
        }
    
        public void edit(String name, String newName, boolean exception, boolean cashcount, boolean admin) {
            try {
                PreparedStatement stmt = conn.prepareCall(editSQL);
                stmt.setInt(5, getID(name));
                stmt.setString(1, newName);
                stmt.setString(2, convert(exception));
                stmt.setString(3, convert(cashcount));
                stmt.setString(4, convert(admin));
                stmt.executeUpdate();
                stmt.close();
            } catch (Exception e) { report(className, "edit", e.getMessage()); }
        }
    
        public void delete(String name) {
            try {
                PreparedStatement stmt = conn.prepareCall(deleteSQL);
                stmt.setString(1, name);
                stmt.executeUpdate();
                stmt.close();
            } catch (Exception e) { report(className, "delete", e.getMessage()); }
        }
    
        public int count() {
            int count = 0;
    
            try {
                Statement stmt = conn.createStatement();
                ResultSet result = stmt.executeQuery(countSQL);
                if(result.next()) count = result.getInt(1);
            } catch (Exception e) { report(className, "count", e.getMessage()); }
    
            return count;
        }
    
        private int getID(String name) {
            int id = 0;
    
            try {
                PreparedStatement stmt = conn.prepareCall(getIDSQL);
                stmt.setString(1, name);
                ResultSet result = stmt.executeQuery();
                if(result.next()) id = result.getInt(1);
                result.close();
                stmt.close();
            } catch (Exception e) { report(className, "getID", e.getMessage()); }
    
            return id;
        }
    
        private int newID() {
            int newID = 0;
    
            try {
                Statement stmt = conn.createStatement();
                ResultSet result = stmt.executeQuery(nextIDSQL);
                if(result.next()) newID = result.getInt(1) + 1;
            } catch(Exception e) { report(className, "newID", e.getMessage()); }
    
            return newID;
        }
    
        private String convert(char result) {
            if(result == 'Y') return "true";
            else return "false";
        }
    
        private String convert(boolean result) {
            String data = "N";
            if(result) data = "Y";
            return data;
        }
    }
    ControllerType.java
    Java Code:
    package Employee;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JComboBox;
    import javax.swing.JOptionPane;
    
    import java.util.Vector;
    
    import Database.DBEmployeeType;
    
    public class ControllerType extends ViewType {
    
        private DBEmployeeType database = new DBEmployeeType();
        private CommandListener listener = new CommandListener();
    
        private boolean editMode = false;
    
        public ControllerType() {
            load();
            setVisible(true);
        }
    
        private void load() {
            createButton.addActionListener(listener);
            editButton.addActionListener(listener);
            deleteButton.addActionListener(listener);
            typeCB = new JComboBox(database.loadTypes());
            typeCB.addActionListener(new ComboListener());
            switchBox();
            loadData();
            loadLayout();
        }
    
        private void loadData() {
            if(count()) {
                Vector data  = database.loadData(typeCB.getSelectedItem().toString());
                nameField.setText(typeCB.getSelectedItem().toString());
                exceptionCB.setSelected(Boolean.parseBoolean(data.elementAt(1).toString()));
                cashcountCB.setSelected(Boolean.parseBoolean(data.elementAt(2).toString()));
                adminCB.setSelected(Boolean.parseBoolean(data.elementAt(3).toString()));
            }
        }
    
        private boolean count() {
            boolean flag = true;
            if(database.count()==0) flag = false;
            return flag;
        }
    
        private void switchBox() {
            editButton.setEnabled(typeCB.getItemCount() != 0);
            deleteButton.setEnabled(typeCB.getItemCount() != 0);        
        }
    
        private void editMode(boolean flag) {
            typeCB.setEnabled(!flag);
            nameField.setEditable(flag);
            exceptionCB.setEnabled(flag);
            cashcountCB.setEnabled(flag);
            adminCB.setEnabled(flag);
            if(flag) {
                deleteButton.setText("Cancel");
            } else {
                deleteButton.setText("Delete");
                createButton.setText("Create");
                editButton.setText("Edit");
            }
            if(!editMode) {
                exceptionCB.setSelected(!flag);
                cashcountCB.setSelected(!flag);
                adminCB.setSelected(!flag);
            }
        }
        
        private class CommandListener implements ActionListener {
            String name = "";
            public void actionPerformed(ActionEvent event) {
                if(event.getActionCommand().equals("Create")) {
                    editButton.setEnabled(false);
                    createButton.setText("Submit");
                    nameField.setText("");
                    editMode = false;
                    editMode(true);                
                } else if(event.getActionCommand().equals("Edit")) {
                    name = nameField.getText();
                    createButton.setEnabled(false);
                    editButton.setText("Submit");
                    editMode = true;
                    editMode(true);                
                } else if(event.getActionCommand().equals("Delete")) {
                    int option = JOptionPane.showConfirmDialog(null, "Do you wish to delete '" + nameField.getText() + "'?", "Confirmation", JOptionPane.YES_NO_OPTION);
                    if(option==0) database.delete(nameField.getText());
                } else if(event.getActionCommand().equals("Submit")) {
                    String typeName = nameField.getText().replaceAll(" ", "").trim();               
                    if(editMode) {
                        if(!typeName.equals("")) {
                            database.edit(name, typeName, exceptionCB.isSelected(), cashcountCB.isSelected(), adminCB.isSelected());
                        }                    
                    } else {
                        if(!typeName.equals("")) {
                            database.create(typeName, exceptionCB.isSelected(), cashcountCB.isSelected(), adminCB.isSelected());
                        }                    
                    }                
                    editMode(false);
                    editMode = false;
                    switchBox();
                    loadData();
                } else if(event.getActionCommand().equals("Cancel")) {
                    editMode(false);
                    editMode = false;
                    switchBox();
                    loadData();
                }
            }
        }
    
        private class ComboListener implements ActionListener {
            public void actionPerformed(ActionEvent event) { loadData(); }
        }
    }
    Could someone help me figure this out.
    I can't move on from this :(

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Why the double post?
    How to reload JInternalFrame

    db

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. JInternalFrame Help
    By collin389 in forum New To Java
    Replies: 2
    Last Post: 03-16-2010, 01:23 AM
  2. JInternalFrame Help
    By collin389 in forum AWT / Swing
    Replies: 1
    Last Post: 03-15-2010, 10:24 PM
  3. Control JInternalFrame ?
    By h9h in forum AWT / Swing
    Replies: 1
    Last Post: 10-11-2009, 04:58 AM
  4. Problem with JInternalFrame
    By hameem in forum AWT / Swing
    Replies: 1
    Last Post: 12-11-2008, 05:23 PM
  5. [SOLVED] jScrool in JInternalFrame
    By gustio in forum New To Java
    Replies: 2
    Last Post: 07-22-2008, 11:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •