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

    Default SAMPLE CODE! (considered IT AS SMALL!)

    I can't update the list of the typeCB.
    Though, the Vector did update itself after action[Submit, Delete]
    The problem will focus more on the refresh method located in the Controller class.
    (As much as possible can you guide me with the Model thing some suggested)

    Here is the code. (I have removed the JDBC connection and I hope I have closed the problem regarding the SSCCE)

    The main method is located in the Model class.

    Java Code:
    package demo;
    
    import javax.swing.JFrame;
    import javax.swing.SwingConstants;
    import javax.swing.GroupLayout;
    import javax.swing.LayoutStyle;
    import javax.swing.JLabel;
    import javax.swing.JButton;
    import javax.swing.JTextField;
    import javax.swing.JComboBox;
    import javax.swing.JCheckBox;
    
    public class View extends JFrame {
    
        private final JLabel typeLabel = new JLabel("Type", SwingConstants.LEFT);
        private final JLabel nameLabel = new JLabel("Name", SwingConstants.LEFT);
    
        public JButton createButton = new JButton("Create");
        public JButton editButton = new JButton("Edit");
        public JButton deleteButton = new JButton("Delete");
        public JTextField nameField = new JTextField(10);
        public JCheckBox exceptionCB = new JCheckBox("Exception", false);
        public JCheckBox cashcountCB = new JCheckBox("Cash Count", false);
        public JCheckBox adminCB = new JCheckBox("Administrator", false);
        public JComboBox typeCB = null;
    
        public View() {
            setTitle("Employee Type");
            setResizable(false);        
            defaultSet(false);
        }
    
        private void defaultSet(boolean flag) {        
            editButton.setEnabled(flag);
            deleteButton.setEnabled(flag);
            nameField.setEnabled(flag);
            exceptionCB.setEnabled(flag);
            cashcountCB.setEnabled(flag);
            adminCB.setEnabled(flag);
        }
    
        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()
                            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                .addComponent(nameLabel)
                                .addComponent(typeLabel))
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                .addComponent(typeCB, 0, 146, Short.MAX_VALUE)
                                .addComponent(nameField, GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(createButton)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(editButton)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(deleteButton)))
                    .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(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)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(createButton)
                        .addComponent(editButton)
                        .addComponent(deleteButton))
                    .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
        }
    }
    Java Code:
    package demo;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JComboBox;
    import javax.swing.JOptionPane;
    
    import java.util.Vector;
    
    public class Model extends View {
    
        private buttonListener listener = new buttonListener();
        private Controller database = new Controller();
    
        private int id = 0;
        private boolean edit = false;
    
        public Model() {
            load();
            pack();
            setVisible(true);
        }
    
        private void load() {
            typeCB = new JComboBox(database.loadTypes());
            loadListener();
            loadLayout();
            loadData(typeCB.getSelectedIndex());
            switchBox();
        }
    
        private void loadListener() {
            createButton.addActionListener(listener);
            editButton.addActionListener(listener);
            deleteButton.addActionListener(listener);
            typeCB.addActionListener(new comboListener());
        }
    
        private void switchBox() {
            createButton.setEnabled(true);
            if(database.count()!=0) {
                editButton.setEnabled(true);
                deleteButton.setEnabled(true);
            }
        }
    
        private void loadData(int index) {
            Vector data = database.loadData(index);
            this.id = Integer.parseInt(data.elementAt(0).toString());
            nameField.setText(data.elementAt(1).toString());
            exceptionCB.setSelected(Boolean.parseBoolean(data.elementAt(2).toString()));
            cashcountCB.setSelected(Boolean.parseBoolean(data.elementAt(3).toString()));
            adminCB.setSelected(Boolean.parseBoolean(data.elementAt(4).toString()));
        }
    
        private void editMode(boolean flag) {
            typeCB.setEnabled(!flag);
            nameField.setEnabled(flag);
            exceptionCB.setEnabled(flag);
            cashcountCB.setEnabled(flag);
            adminCB.setEnabled(flag);
            if(!edit) {
                exceptionCB.setSelected(false);
                cashcountCB.setSelected(false);
                adminCB.setSelected(false);
                nameField.setText("");
            }
            if(!flag) {
                createButton.setText("Create");
                editButton.setText("Edit");
                deleteButton.setText("Delete");
                switchBox();
                loadData(typeCB.getSelectedIndex());
            } else deleteButton.setText("Cancel");
        }
    
        private void refresh() {
            typeCB.repaint();
        }
    
        private class buttonListener implements ActionListener {
            public void actionPerformed(ActionEvent event) {
                String action = event.getActionCommand();
                if(action.equals("Create")) {
                    edit = false;
                    editMode(true);
                    editButton.setEnabled(false);
                    createButton.setText("Submit");
                } else if(action.equals("Edit")) {
                    edit = true;
                    editMode(true);
                    createButton.setEnabled(false);
                    editButton.setText("Submit");
                } else if(action.equals("Delete")) {
                    int option = JOptionPane.showConfirmDialog(null, "Do you wish to delete '" + nameField.getText() + "'?", "Confirmation", JOptionPane.YES_NO_OPTION);
                    if(option==0) database.delete(id-1);
                    refresh();
                } else if(action.equals("Submit")) {
                    if(!edit) database.create(nameField.getText(), exceptionCB.isSelected(), cashcountCB.isSelected(), adminCB.isSelected());
                    else database.edit(id-1, nameField.getText(), exceptionCB.isSelected(), cashcountCB.isSelected(), adminCB.isSelected());
                    refresh();
                    edit = false;
                    editMode(false);
                } else if(action.equals("Cancel")) {
                    edit = false;
                    editMode(false);
                }
            }
        }
    
        private class comboListener implements ActionListener {
            public void actionPerformed(ActionEvent event) { loadData(typeCB.getSelectedIndex()); }
        }
    
        public static void main(String[] args) {
            new Model();
        }
    }
    Java Code:
    package demo;
    
    import java.util.Vector;
    
    public class Controller {
    
        public Vector data = new Vector(); //the Vector substitutes the JDBC contents ONLY!!!
    
        public Controller() {
            loadTempData();
        }
    
        private void loadTempData() {
            Vector data1 = new Vector();
                data1.addElement(new Integer(1));
                data1.addElement(new String("ADMIN"));
                data1.addElement(new String("Y"));
                data1.addElement(new String("Y"));
                data1.addElement(new String("Y"));            
    
            Vector data2 = new Vector();
                data2.addElement(new Integer(2));
                data2.addElement(new String("INMATE"));
                data2.addElement(new String("Y"));
                data2.addElement(new String("Y"));
                data2.addElement(new String("N"));
    
            data.addElement(data1);
            data.addElement(data2);;
        }
    
        private String convert(String result) {
            if(result.equals("Y")) return "true";
            else return "false";
        }
    
        private String convert(boolean result) {
            if(result) return "Y";
            else return "N";
        }
    
        private int newID() {
            Vector lastrow = (Vector)data.elementAt(data.size()-1);
            int id = Integer.parseInt(lastrow.elementAt(0).toString());
            return id+1;
        }
    
        public void create(String name, boolean exception, boolean cashcount, boolean admin) {
            Vector data = new Vector();
                data.addElement(new Integer(newID()));
                data.addElement(new String(name));
                data.addElement(new String(convert(exception)));
                data.addElement(new String(convert(cashcount)));
                data.addElement(new String(convert(admin)));
            this.data.addElement(data);
        }
    
        public void edit(int id, String name, boolean exception, boolean cashcount, boolean admin) {
            Vector data = (Vector)this.data.elementAt(id);
            data.set(1, new String(name));
            data.set(2, new String(convert(exception)));
            data.set(3, new String(convert(cashcount)));
            data.set(4, new String(convert(admin)));
            this.data.set(id, data);
        }
    
        public void delete(int id) {
            this.data.remove(id);
        }
    
        public Vector loadTypes() {
            Vector data = new Vector();
            for(int i=0; i<this.data.size(); i++) {
                Vector row = (Vector)this.data.elementAt(i);
                data.addElement(row.elementAt(1));
            }
            return data;
        }
    
        public int count() {
            return data.size();
        }
    
        public Vector loadData(int i) {
            Vector data = (Vector)this.data.elementAt(i);
            Vector result = new Vector();
            result.addElement(data.elementAt(0).toString());
            result.addElement(data.elementAt(1).toString());
            result.addElement(convert(data.elementAt(2).toString()));
            result.addElement(convert(data.elementAt(3).toString()));
            result.addElement(convert(data.elementAt(4).toString()));
            return result;
        }
    }
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  2. #42
    Michael Dunn is offline Member
    Join Date
    Jul 2008
    Posts
    62
    Rep Power
    0

    Default

    Java Code:
    private void refresh() {
        //typeCB.repaint();
        typeCB.setModel(new javax.swing.DefaultComboBoxModel(database.loadTypes()));
    }
    > SAMPLE CODE! (considered IT AS SMALL!)

    again you've missed the point:
    your checkboxes have nothing to do with 'refreshing' a comboBox
    you only needed one button, not all three
    you didn't need to use GroupLayout and its (what) 100 lines of code,
    the comboBox and single button could be anywhere in the frame,
    being 'pretty' is irrelevant to the problem

    the above is intended as constructive criticism, for when you next post a problem

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

    Default

    Alas. The thread is finally considered SOLVE.
    Thanks everyone and especially to Michael Dunn.
    Last edited by chyrl; 04-16-2010 at 01:34 PM.
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

Page 3 of 3 FirstFirst 123

Similar Threads

  1. How to reload form after saving
    By bikkerss in forum NetBeans
    Replies: 0
    Last Post: 02-14-2010, 06:10 PM
  2. Reload the page when any changes in the database
    By Kayal in forum Web Frameworks
    Replies: 0
    Last Post: 03-28-2009, 07:17 AM
  3. How to force Firefox to reload applet
    By JordashTalon in forum Java Applets
    Replies: 3
    Last Post: 02-20-2009, 11:19 AM
  4. reload form problem
    By porta325 in forum New To Java
    Replies: 2
    Last Post: 11-01-2007, 04:07 PM
  5. how to reload a jsp page
    By Heather in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-14-2007, 11:49 PM

Posting Permissions

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