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

    Default

    Quote Originally Posted by camickr View Post
    You've been told (multiple times) we can't answer the question because we have no idea what you mean by "refresh" and you haven't posted your SSCCE showing what you are attempting to do.
    I apologize for my ignorance.
    But here's the SSCCE you've been asking of me.

    When I click the createButton, the record is displayed on the list.
    But when I add a new record again, the records on the list won't display anymore.

    What I mean by "refresh" was I want to refresh or (reload) the JInternalFrame or not the JComboBox of this Frame. Because, I have a JIF on this application that uses two JTables, and at least 3 JComboBox. The JTables on that JIF would refresh automatically every 1second (I am to code it later) and the JComboBox(will be updated after every CUD function).

    I know it seems confusing but, that what the application is required to do.

    Java Code:
    package test;
    
    import java.util.Vector;
    
    import java.awt.GridLayout;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import javax.swing.JButton;
    import javax.swing.JTextField;
    import javax.swing.JComboBox;
    import javax.swing.JCheckBox;
    
    public class Test extends JFrame{
    
        private Vector data = new Vector();
        private Vector types = new Vector();
    
        private JTextField nameField = new JTextField(10);
        private JComboBox typesCB = new JComboBox(types);
        private JCheckBox adminCB = new JCheckBox("Administrator", false);
        private JButton createButton = new JButton("Create");
        private JButton editButton = new JButton("Edit");
        private JButton deleteButton = new JButton("Delete");
    
        public Test() {
            loadLayout();
        }
    
        private void loadLayout() {
            setTitle("Demo");
            getContentPane().setLayout(new GridLayout(4,1,3,3));
            JPanel typePanel = new JPanel(new GridLayout(1,2,3,3));
            JPanel namePanel = new JPanel(new GridLayout(1,2,3,3));
            JPanel buttonPanel = new JPanel(new GridLayout(1,3,3,3));
    
            typePanel.add(new JLabel("Type: "));
            typePanel.add(typesCB);
            namePanel.add(new JLabel("Name: "));
            namePanel.add(nameField);
            buttonPanel.add(createButton);
            buttonPanel.add(editButton);
            buttonPanel.add(deleteButton);
    
            getContentPane().add(typePanel);
            getContentPane().add(namePanel);
            getContentPane().add(adminCB);
            getContentPane().add(buttonPanel);
            
            Handler listener = new Handler();
            
            createButton.addActionListener(listener);
            editButton.addActionListener(listener);
            deleteButton.addActionListener(listener);
    
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(300,200);
            setResizable(false);
            setVisible(true);
        }
    
        private class Handler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
                String action = event.getActionCommand();
                if(action.equals("Create")) {                
                    data.addElement(nameField.getText());
                    data.addElement(adminCB.isSelected());
                    types.addElement(data);
                    typesCB.repaint();
                } else if(action.equals("Edit")) {
                    System.out.println("E");
                } else if(action.equals("Delete")) {
                    System.out.println("D");
                }
            }
        }
    
        public static void main(String[] args) {
            new Test();
        }
    }

  2. #22
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    It doesn't work because you don't understand a fundamental concept of how Swing is designed. Swing uses a MVC (Model-View-Controller) design. Bascally this means data is stored in a model and when a model changes the model tells the view to repaint itself.

    So, a Vector is only ever use to populate the model with initial data. After that changes are made directly to the model. Most Swing components have a getModel() method that allows you to get the model so you can update it.

    However, in the case of a JComboBox it has a convenience method which allows you to update the model directly. So as a simple example you can change your code to:

    Java Code:
    //                data.addElement(nameField.getText());
    //                data.addElement(adminCB.isSelected());
    //                types.addElement(data);
    //                typesCB.repaint();
    typesCB.addItem(nameField.getText());

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

    Default

    But is there a code that you could manipulate the Model of the JComboBox?

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

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

    Default

    Thanks... I will try to apply it to my application.

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

    Default

    Sir Fubarable,

    I can't find a way to implement the JComboBox with a DefaultComboBoxModel.
    All I did was

    DefaultComboBoxModel model = new DefaultComboBoxModel(types);
    JComboBox typeCB = new JComboBox(model);

    I can't have the model updated after CUD instruction.
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  7. #27
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    after CUD instruction
    What is that supposed to mean? Why do you use acronyms (if that is what it is)? Its been hours since you posted the question, now we will have to wait for you to explain your requirement and then you will have to wait again until we answer.

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

    Default

    CUD means Create, Update, Delete of item in the DefaultComboBoxModel
    Meaning. The JComboBox will update its elements after any of the CUD button(instruction) is press
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  9. #29
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    CUD means Create, Update, Delete of item in the DefaultComboBoxModel
    Well, if you are doing what you are saying (and what I suggested earlier), that is, updating the model directly then you should not have a problem. I told you the model can be updated indirectly by using methods found in the JComboBox API. Or you can access the model from the combobox and then update the model directly.

    No where did I suggest you create a new model, which is what your posted code is doing. However, even if you do that, there is no need to create a new combobox, you just replace the model used by the combobox. Again the combo box API has a method for this.

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

    Default

    Quote Originally Posted by camickr View Post
    Well, if you are doing what you are saying (and what I suggested earlier), that is, updating the model directly then you should not have a problem. I told you the model can be updated indirectly by using methods found in the JComboBox API. Or you can access the model from the combobox and then update the model directly.

    No where did I suggest you create a new model, which is what your posted code is doing. However, even if you do that, there is no need to create a new combobox, you just replace the model used by the combobox. Again the combo box API has a method for this.
    Can you post a sample code about the model thing. I can't visualize how it must be coded though I got the logic.
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  11. #31
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    I don't understand your confusion. You know how to add an element to a Vector. It no different when updating the combo box or the model. All you need to do is invoke the correct method.

    What method of the JComboBox have you tried to use and what problems did you have?

    What method of the DefaultComboBoxModel have you tried to use and what problems did you have?

    The API even has a link to the Swing tutorial on "How to Use Combo Boxes" that highlights some of these methods.

    Post your SSCCE if you need help. Show me that you have attempted to actually read the API.

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

    Default

    So meaning, I have to do the following for each CUD Instruction: (using the JComboBox and the DCBModel i'll still use this)

    Create: addItem(Object itemName)

    Edit: removeItem(Object prevItemName) then addItem(Object newItemName)

    Delete: removeItem(Object itemName)

    I need to be sure of this implementation and logic.
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

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

    Default

    Try it and see. But note that if you're not creating a new DefaultComboBoxModel, you don't even need a reference to the JComboBox itself; just work with it's model.

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

    Default

    How is that possible? (using the getModel() method)
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  15. #35
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Depending on how you design your class you need a reference to either the combo box or the model but not both. If you have a reference to the combo box you can get the model.

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

    Default

    Ok. Meaning if I have these code do i need to have a use the getModel() method then?

    Java Code:
    JComboBox typeCB = new JComboBox(database.loadTypes());
    database.loadTypes() is a Vector method that returns the ResultSet (one column) of a particular statement.

    I used addElement(result.getObject(1).toString)); to add the row of the ResultSet to the Vector which the database.loadTypes() returns.
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  17. #37
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    I have no idea what you are asking and since you haven't posted a SSCCE, I will not be responding any further. Its a waste of my time and yours.

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

    Default

    Here the code I have used.

    The DBUtilities object is the application JDBC.
    This is just a small code, and it will be easy for to understand.

    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 buttonListener listener = new buttonListener();
        private DBEmployeeType database = new DBEmployeeType();
    
        private int id = 0;
        private boolean edit = false;
    
        public ControllerType() {
            load();
            pack();
            setVisible(true);
        }
    
        private void load() {        
            typeCB = new JComboBox(database.loadTypes());
            loadListener();        
            loadLayout();
            loadData(typeCB.getSelectedItem().toString());
            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(String name) {
            Vector data = database.loadData(name);
            this.id = Integer.parseInt(data.elementAt(0).toString());
            nameField.setText(name);
            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 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.getSelectedItem().toString());
            } else deleteButton.setText("Cancel");
        }
    
        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);
                } else if(action.equals("Submit")) {
                    if(!edit) database.create(nameField.getText(), exceptionCB.isSelected(), cashcountCB.isSelected(), adminCB.isSelected());
                    else database.edit(id, nameField.getText(), exceptionCB.isSelected(), cashcountCB.isSelected(), adminCB.isSelected());                
                    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.getSelectedItem().toString()); }
        }
    }
    Java Code:
    package Database;
    
    import java.util.Vector;
    
    public class DBEmployeeType {
    
        private DBUtilities database = new DBUtilities();
            
        private final String typesSQL = "SELECT NAME FROM EMPLOYEE_TYPE ORDER BY EMPLOYEE_TYPE_ID";
        private final String loadSQL = "SELECT EMPLOYEE_TYPE_ID, EXCEPTIONS, CASHCOUNT, ADMIN FROM EMPLOYEE_TYPE WHERE NAME = ?";
        private final String countSQL = "SELECT COUNT(*) FROM EMPLOYEE_TYPE";
        private final String idSQL = "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 EMPLOYEE_TYPE_ID = ?";   
    
        public DBEmployeeType() { }
    
        public void create(String name, boolean exception, boolean cashcount, boolean admin) {
            String parameter = database.count(idSQL)+1 + "," + name + "," + convert(exception) + "," + convert(cashcount) + "," + convert(admin);
            database.execute(createSQL, parameter);
        }
    
        public void edit(int id, String name, boolean exception, boolean cashcount, boolean admin) {
            String parameter = name + "," + convert(exception) + "," + convert(cashcount) + "," + convert(admin) + "," + id;
            database.execute(editSQL, parameter);
        }
    
        public void delete(int id) {
            database.execute(deleteSQL, Integer.toString(id));        
        }
    
        public int count() {
            return database.count(countSQL);
        }
    
        public Vector loadTypes() {
            Vector types = database.load(typesSQL);
            return types;
        }
    
        public Vector loadData(String name) {
            Vector data = database.loadSet(loadSQL, name);
            Vector result = (Vector)data.elementAt(0);
            for(int index=0; index<result.size(); index++) {
                   if(index>0) result.set(index, convert(result.elementAt(index).toString()));
            }
            return result;
        }
    
        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";
        }
    }
    Java Code:
    package Database;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.Statement;
    
    import javax.swing.JOptionPane;
    
    import java.util.Vector;
    
    public class DBUtilities {
    
        private final Connection conn = connect();
    
        private final String className = "DBUtilities";
        private final String driver = "oracle.jdbc.OracleDriver";
        private final String url = "jdbc:oracle:thin:@localhost:1521:xe";
        private final String username = "********;
        private final String password = "********";
    
        public DBUtilities() {
            connect();
        }
    
        private Connection connect() {
            Connection con = null;
            try {
                Class.forName(driver).newInstance();
                con = DriverManager.getConnection(url, username, password);
            } catch (Exception e) {
                error(className, "connect", e.getMessage());
            }
    
            return con;
        }
    
        public void execute(String statement, String parameter) {        
            try {
                Statement stmt = conn.createStatement();
                stmt.executeUpdate(convert(statement, parameter));
                stmt.close();
            } catch (Exception e) {
                error(className, "executeParameter", e.getMessage());
            }
        }
    
        public int count(String statement) {
            int count = 0;
    
            try {
                Statement stmt = conn.createStatement();
                ResultSet result = stmt.executeQuery(statement);
                if(result.next()) count = result.getInt(1);
                result.close();
                stmt.close();
            } catch (Exception e) {
                error(className, "count", e.getMessage());
            }
    
            return count;
        }   
    
        public Vector load(String statement) {
            Vector loadData = new Vector();
    
            try {
                Statement stmt = conn.createStatement();
                ResultSet result = stmt.executeQuery(statement);
                while(result.next()) {
                    if(!(result.getObject(1).equals(null))) loadData.addElement(result.getObject(1));
                    else loadData.addElement("");
                }
                result.close();
                stmt.close();
            } catch (Exception e) { error(className, "load", e.getMessage()); }
    
            return loadData;
        }
    
        public Vector loadSet(String statement) {
            Vector loadData = new Vector();
    
            try {
                Statement stmt = conn.createStatement();
                ResultSet result = stmt.executeQuery(statement);
                ResultSetMetaData metaData = result.getMetaData();
                while(result.next()) {
                    Vector rowData = new Vector();
                    for(int index=1; index<=metaData.getColumnCount(); index++) {
                        if(!(result.getObject(index).equals(null))) rowData.addElement(result.getObject(index));
                        else rowData.addElement("");
                    }
                    loadData.addElement(rowData);
                }
                result.close();
                stmt.close();
            } catch (Exception e) {
                error(className, "loadSet", e.getMessage());
            }
    
            return loadData;
        }
    
        public Vector loadSet(String statement, String parameter) {
            Vector loadData = new Vector();
    
            try {
                Statement stmt = conn.createStatement();
                ResultSet result = stmt.executeQuery(convert(statement, parameter));
                ResultSetMetaData metaData = result.getMetaData();
                while(result.next()) {
                    Vector rowData = new Vector();
                    for(int index=1; index<=metaData.getColumnCount(); index++) {
                        if(!(result.getObject(index).equals(null))) rowData.addElement(result.getObject(index));
                        else rowData.addElement("");
                    }
                    loadData.addElement(rowData);
                }
                result.close();
                stmt.close();
            } catch (Exception e) {
                error(className, "loadSet", e.getMessage());
            }
    
            return loadData;
        }
    
        public String convert(String statement, String parameter) {
            String[] token = parameter.split(",");
            int idx =0, index = 0, ctr=0;
            String executeSQL = "";
            while(index < statement.length()-1) {
                index = statement.indexOf("?", idx);
                if(index >- 1) {
                    executeSQL = executeSQL + statement.substring(idx, index) + "'" + token[ctr] + "'";
                    idx = index + 1;
                    ctr++;
                } else {
                    executeSQL = executeSQL + statement.substring(idx, statement.length());
                    break;
                }
            }
    
            return executeSQL;
        }
    
        public void error(String className, String methodName, String exception) {
            JOptionPane.showMessageDialog(null, methodName + ": " + exception, className, JOptionPane.ERROR_MESSAGE);
        }
    
    }
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

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

    Default

    despite repeated suggestions to post a small demo program,
    you post something we can't compile, and most certainly won't read.

    do you want further replies in braille?

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

    Default

    The demo program is the code posted above.
    I have where to create a demo program.

    ALSO, if I was to create a SMALL DEMO of the PROGRAM. It will destroy the program flow.
    Due to the JDBC. It will be hard for me and you to be flexible to my progam.

    Well if you insist I will post the small demo program if this.
    Last edited by chyrl; 04-16-2010 at 10:49 AM.
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. How to reload form after saving
    By bikkerss in forum NetBeans
    Replies: 0
    Last Post: 02-14-2010, 07: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, 08:17 AM
  3. How to force Firefox to reload applet
    By JordashTalon in forum Java Applets
    Replies: 3
    Last Post: 02-20-2009, 12:19 PM
  4. reload form problem
    By porta325 in forum New To Java
    Replies: 2
    Last Post: 11-01-2007, 05:07 PM
  5. how to reload a jsp page
    By Heather in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-15-2007, 12:49 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
  •