Results 1 to 11 of 11
  1. #1
    DJCali is offline Member
    Join Date
    Oct 2009
    Posts
    10
    Rep Power
    0

    Default How to pass a variable to another frame.

    Hello All,

    I am trying to develop a database front end and I came across a problem which I've spent 2 days trying to figure out.

    I have a main jFrame(MainUI) which just has 2 menu items Add a client(class called Clients) and Manage Clients (class called ManageClients).

    When a user selects Add a client it opens up the Clients frame and the user can save Client information to the database.

    When a user selects Manage Clients it opens up the ManageClients frame which displays all the clients from the database in a jTable.

    When the user clicks a row from the jTable I store the ClientID which is in column 1 in the jTable into a variable.

    The user can then click on a jButton called EditClient within the ManageClients frame.

    When the user does this I want to be able to Close the ManageClients frame and open the Clients frame so the user can edit the client information.

    I just need to figure out how to pass the variable ClientID to the Clients frame so I can execute a query to display the client information.

    First I tried creating a class ClientID..

    Java Code:
    package pms;
    
    public class ClientID {
        private int clientID;
    public ClientID(){
        
    }
    public ClientID(int clientID){
        this.clientID = clientID;
    
    }
    public int getID(){
        return clientID;
    }
    public void setID(int clientID){
        this.clientID = clientID;
    }
    }
    Then I tried storing the ClientID from the ManageClients Class...

    Java Code:
    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {                                        
            ClientID clientid = new ClientID();
            clientid.setID(50);
    
            /**Close the ManageClients frame*/
            this.dispose();
    
        }
    When I try to do a test to get the Client ID it returns 0..
    I just put a jButton on the Main form to do a test to get the ClientID and change the jButton text to the ClientID.

    Java Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            ClientID clientid = new ClientID();
            String theid = Integer.toString(clientid.getID());
            jButton1.setText(theid);
        }
    I think whats happening is Im creating a new instance of ClientID.

    any suggestions to get this to work?

    Thank you.

  2. #2
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default

    Make your ClientID class static and make all the methods static. Then you can do ClientID.getID from any of your classes and it should contain the variable.

    You wouldn't have to instantiate the ClientID class in your main method then.
    Last edited by toymachiner62; 10-13-2009 at 09:55 PM. Reason: Forgot something.

  3. #3
    DJCali is offline Member
    Join Date
    Oct 2009
    Posts
    10
    Rep Power
    0

    Default

    When I try to make the methods static I get an error:
    non-static variable ClientID cannot be refrenced from a static context.

  4. #4
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default

    did you make the class static as well?

  5. #5
    DJCali is offline Member
    Join Date
    Oct 2009
    Posts
    10
    Rep Power
    0

    Default

    Yes. I get "Modifier static not allowed here."


    Thanks.

  6. #6
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default

    Can you post your code?

  7. #7
    DJCali is offline Member
    Join Date
    Oct 2009
    Posts
    10
    Rep Power
    0

    Default

    It's probaby me incorrectly doing it but here it is:

    Java Code:
    package pms;
    
    static class ClientID {
        private int clientID;
    static ClientID(){
        
    }
    static ClientID(int clientID){
        this.clientID = clientID;
        
    }
    static int getID(){
        return clientID;
    }
    static void setID(int clientID){
        this.clientID = clientID;
    }
    }

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

    Default

    Quote Originally Posted by toymachiner62 View Post
    Make your ClientID class static and make all the methods static. Then you can do ClientID.getID from any of your classes and it should contain the variable.

    You wouldn't have to instantiate the ClientID class in your main method then.
    I would strongly advise against using static anything here as it is very anti-OOPs and completely unnecessary. What is a stand-alone static class anyway?

    Instead the classes that display your info and that add new or change it should have public methods that the main class can use to extract info as needed. Also, you shouldn't be displaying new JFrames here, but rather dialogs such as JDialogs.

    Perhaps I can show a small example...
    Last edited by Fubarable; 10-14-2009 at 01:19 AM.

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

    Default

    OK, a bloated example that uses a JList instead of a JTable and a HashMap instead of a database for simplicity (and cuts other corners too), but it does illustrate the point. Sorry for the lack of comments, but it was composed quickly...

    Client.java
    Java Code:
    // simple class has only 2 fields, id and text
    class Client {
      private String id;
      private String text;
    
      Client(String id, String text) {
        this.id = id;
        this.text = text;
      }
    
      public String getId() {
        return id;
      }
    
      public String getText() {
        return text;
      }
    
      public void setText(String text) {
        this.text = text;
      }
      
      @Override
      public String toString() {
        return "ID: " + id + ", Text: " + text;
      }
    
    }
    ClientMain.java
    Java Code:
    import java.awt.event.*;
    import java.util.HashMap;
    import java.util.Map;
    import javax.swing.*;
    
    // the main GUI
    public class ClientMain {
      private JPanel mainPanel = new JPanel();
      private JFrame frame;
      // instead of a database, using a Map for simplicity
      private Map<String, Client> clientMap = new HashMap<String, Client>();
      private AddClientPanel addClientPanel = new AddClientPanel();
      private ManageClientPanel manageClientPanel = new ManageClientPanel();
    
      public ClientMain(JFrame frame) {
        this.frame = frame;
        JButton addClient = new JButton("Add Client");
        JButton manageClient = new JButton("Manage Client");
    
        mainPanel.add(addClient);
        mainPanel.add(manageClient);
    
        addClient.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent arg0) {
            addClientAction();
          }
        });
        manageClient.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent arg0) {
            manageClientAction();
          }
        });
      }
    
      private void addClientAction() {
        int response = JOptionPane.showConfirmDialog(mainPanel,
            addClientPanel.getPanel(), "Add Client",
            JOptionPane.OK_CANCEL_OPTION);
        if (response == JOptionPane.OK_OPTION) {
          String id = addClientPanel.getID().trim();
          String text = addClientPanel.getText().trim();
          if (!id.isEmpty()) {
            clientMap.put(id, new Client(id, text));
          }
        }
        addClientPanel.setID("");
        addClientPanel.setText("");
      }
    
      private void manageClientAction() {
        manageClientPanel.addClients(clientMap.values());
        
        JDialog dialog = new JDialog(frame, "Manage Clients", true);
        dialog.getContentPane().add(manageClientPanel.getPanel());
        dialog.pack();
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
        
        Client selectedClient = (Client) manageClientPanel.getSelectedValue();
        if (selectedClient != null) {
          addClientPanel.setID(selectedClient.getId());
          addClientPanel.setText(selectedClient.getText());
          addClientAction();
        }
      }
    
      public JComponent getPanel() {
        return mainPanel;
      }
    
      private static void createAndShowGUI() {
        JFrame frame = new JFrame("Main");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new ClientMain(frame).getPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            createAndShowGUI();
          }
        });
      }
    }
    AddClientPanel.java
    Java Code:
    import javax.swing.*;
    
    class AddClientPanel {
      private JPanel mainPanel = new JPanel();
      private JTextField idField = new JTextField(5);
      private JTextField textField = new JTextField(15);
    
      public AddClientPanel() {
        mainPanel.add(new JLabel("ID:"));
        mainPanel.add(idField);
        mainPanel.add(Box.createHorizontalStrut(15));
        mainPanel.add(new JLabel("Text:"));
        mainPanel.add(textField);
      }
    
      public void setID(String id) {
        idField.setText(id);
      }
    
      public void setText(String text) {
        textField.setText(text);
      }
    
      public String getID() {
        return idField.getText();
      }
    
      public String getText() {
        return textField.getText();
      }
    
      public JComponent getPanel() {
        return mainPanel;
      }
    
    }
    ManageClientPanel.java
    Java Code:
    import java.awt.Window;
    import java.util.Collection;
    import javax.swing.*;
    import javax.swing.event.*;
    
    class ManageClientPanel {
      private JPanel mainPanel = new JPanel();
      private DefaultListModel listModel = new DefaultListModel();
      private JList list = new JList(listModel);
      
      public ManageClientPanel() {
        mainPanel.add(new JScrollPane(list));
        list.addListSelectionListener(new ListSelectionListener() {
          public void valueChanged(ListSelectionEvent arg0) {
            Window window = SwingUtilities.getWindowAncestor(list);
            window.dispose();  // close window on list selection
          }
          
        });
      }
      
      // allow main class to get the value selected from the list
      public Object getSelectedValue() {
        return list.getSelectedValue();
      }
      
      // whenever display, first initialize with Clients
      public void addClients(Collection<Client> clients) {
        listModel.clear();
        for (Client client : clients) {
          listModel.addElement(client);
        }
      }
    
      public JComponent getPanel() {
        return mainPanel;
      }
    
    }

  10. #10
    DJCali is offline Member
    Join Date
    Oct 2009
    Posts
    10
    Rep Power
    0

    Default

    I just created a new project to run your code.

    This is exactly what I needed to do!

    Now let me learn from this and I'll post back.


    Thanks so much for the help it is greatly appreciated that you took the time out.

    DJCali

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

Similar Threads

  1. How pass variable from jsp to servelet
    By shiva in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 04-22-2009, 02:55 PM
  2. How can I pass a boolean to notifyObservers()
    By dumb_ass in forum New To Java
    Replies: 6
    Last Post: 03-05-2009, 09:15 PM
  3. Pass by ref. A work around?
    By diRisig in forum New To Java
    Replies: 0
    Last Post: 02-05-2008, 08:25 PM
  4. How to pass an html variable as an applet value
    By fred in forum Java Applets
    Replies: 1
    Last Post: 08-06-2007, 04:28 AM
  5. Replies: 1
    Last Post: 08-03-2007, 11:52 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
  •