Results 1 to 12 of 12
  1. #1
    iNko is offline Member
    Join Date
    Dec 2012
    Posts
    55
    Rep Power
    0

    Default CardLayout - opening 'new instance' each time a button is pressed

    Hey, i have a jframe witch has 2 buttons with 2 jpanel cards.
    Each time i press a button a different card shows.

    1st card displays values from database.
    2nd card has textfields that import values into database.

    My problem - when i submit values from 2nd card, and press button to switch to 1st card - it doesnt show the just imputed values. I need to make a 'update card1 thing'.
    If i close my application, and run it again, those values will show in card1.

    I think if i make it so each time i press a button, a new instance of the card would show, it would display those just imputed values. Also i think i could make a 'update' method, but that would be too complicated for me, so i think making a new instance would be easier..

    This is the part of the code that opens the cards:
    Java Code:
        private void ButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                     
           CardLayout card = (CardLayout) displayPanel.getLayout();
           card.show(displayPanel, "card1");
        }
    I think i just need to write a small code somewhere here to open a 'new' card each time, can someone help me with this code please?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,522
    Blog Entries
    7
    Rep Power
    20

    Default Re: CardLayout - opening 'new instance' each time a button is pressed

    Why did you open another thread for the same problem again?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    iNko is offline Member
    Join Date
    Dec 2012
    Posts
    55
    Rep Power
    0

    Default Re: CardLayout - opening 'new instance' each time a button is pressed

    its a different one , that one was solved:
    i needed to refresh textfields, for example - if i had a textfield in card 2 and entered something into it, then clicked to open card1 and clicked back to open card2, the textfield still had what i wrote earlier in it.. it was solved simply by adding a new method to change the textfields values into " " each time that card was opened.

    in this problem i need to refresh the content from a database (got a table that displays all the data from a database), not to change textfields..

    anyways, if u can then delete this thread ill write in that one..

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,522
    Blog Entries
    7
    Rep Power
    20

    Default Re: CardLayout - opening 'new instance' each time a button is pressed

    This is a different question alright, but it is similar to your other question; you wanted to clear certain fields in a 'card' and now you want to update them. Keep track of those fields (i.e. don't just dump them in a panel in a card layout and forget all about them) and after the update, also update the values of those fields.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    iNko is offline Member
    Join Date
    Dec 2012
    Posts
    55
    Rep Power
    0

    Default Re: CardLayout - opening 'new instance' each time a button is pressed

    I watched a tutorial where my problem is fixed by putting the method that populates table with data, in the button that writes data.

    I tried doing so but it didnt work (no errors either)..

    Heres what i did:

    1) located the method in jPanel1 (card1) that populates my combobox with data from database:
    Java Code:
        public void fillComboBox(){    
            try {
            String sql = "SELECT * FROM Table1";
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery(sql);
            while (rs.next()){
            String values_from_table1 = rs.getString("table1_name");
            ComboBox.addItem(values_from_table1 );
           }  
            } catch (Exception e) {
              JOptionPane.showMessageDialog(null, e); 
            }
        }
    2) After reading a tutorial on how to call 1 method from 1 class to another, i did this:
    Went to jPanel2 (card2), where the textfield that writes values to database is located, and created this:
    Java Code:
    jPanel1 methodsFromPanel1 = new jPanel1();
    With this i can call all methods from jPanel1.

    3) Then i located the button in jPanel2 (card2) that sends data to dabase, and called the method from jPanel1 (card1) ('methodsFromPanel1.fillComboBox();'):
    Java Code:
        private void ButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                            
        if (TextField.getText().isEmpty()) {
            infoLabel.setText("Textfield is empty");
        } else {
            try {
              String sql = "INSERT into Table1(table1_name) values (?)";
              pst = conn.prepareStatement(sql); 
              pst.setString(1, TextField.getText());
              pst.execute();
              infoLabel.setText("Data saved");
            } catch (Exception e) {
              JOptionPane.showMessageDialog(null, e);  
            }   
            methodsFromPanel1.fillComboBox();
        }    
        }
    Nothing happened, no errors , no refresh :(

  6. #6
    iNko is offline Member
    Join Date
    Dec 2012
    Posts
    55
    Rep Power
    0

    Default Re: CardLayout - opening 'new instance' each time a button is pressed

    I think my calling of methods from jPanel1 are wrong..
    How do i call methods from jPanel1 in jPanel2? the one i used above this post doest seem to work..

    I need to call this (located in jPanel1):
    Java Code:
    public void fillComboBox(){    
        try {
        String sql = "SELECT * FROM Table1";
        pst = conn.prepareStatement(sql);
        rs = pst.executeQuery(sql);
        while (rs.next()){
        String values_from_table1 = rs.getString("table1_name");
        ComboBox.addItem(values_from_table1 );
       }  
        } catch (Exception e) {
          JOptionPane.showMessageDialog(null, e); 
        }
    }
    And just put it here (located in jPanel2):
    Java Code:
    private void ButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                            
    if (TextField.getText().isEmpty()) {
        infoLabel.setText("Textfield is empty");
    } else {
        try {
          String sql = "INSERT into Table1(table1_name) values (?)";
          pst = conn.prepareStatement(sql); 
          pst.setString(1, TextField.getText());
          pst.execute();
          infoLabel.setText("Data saved");
        } catch (Exception e) {
          JOptionPane.showMessageDialog(null, e);  
        }   
        fillComboBox(); // <------- need to put this method from panel1, dont know how..
    }    
    }
    how do i do that?

    Edit:
    If i write the same method in panel2, and create a combobox there, it would refresh each time i submit a new value (tested it).
    Last edited by iNko; 12-09-2012 at 11:15 AM.

  7. #7
    iNko is offline Member
    Join Date
    Dec 2012
    Posts
    55
    Rep Power
    0

    Default Re: CardLayout - opening 'new instance' each time a button is pressed

    still havent fixed this problem, noone can help?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default Re: CardLayout - opening 'new instance' each time a button is pressed

    So jPanel2 will need a reference to jPanel1.
    Then you'd call jPanel1.fillComboBox().

    Though, from a design perspective, none of this should be in your GUI anyway.
    But that's a separate issue really, and something you can learn about later.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    iNko is offline Member
    Join Date
    Dec 2012
    Posts
    55
    Rep Power
    0

    Default Re: CardLayout - opening 'new instance' each time a button is pressed

    I think ill try to explain my problem more detailed:

    So i have jFrame1 , witch has two buttons that when i click on, they open jPanel1 (button1), or jPanel2 (button2).

    In jPanel1, is a textfield where i write value and it stores into a database.

    In jPanel2, is a combobox that reads data from a database and displays it.

    What i want - when i enter value in jPanel1, then click on button2, that value would already be displayed in jPanel2 combobox.
    What i have now - it just writes the data, and displays it only when i restart the app.
    What i tried - tried calling a method from jPanel2 that populates the combobox. I called this method in my jFrame1 in the button2 actionperformed place. (not working).

    Here is how i tried calling that method (witch is bad i think, thats why it doesnt work):
    1st) created a reference to jPanel2 in jFrame1:
    jPanel2 methodsFromJpanel2= new jPanel2 ();
    2nd) called the method that fills combobox in jPanel2 in my button2 actionperformed:
    Java Code:
    private void button2ButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                     
           CardLayout card = (CardLayout) displayPanel.getLayout();
           card.show(displayPanel, "card2");
           methodsFromJpanel2.fillsComboBox();   //<- where i called it
    So every time i press button2, it would update the combobox in jpanel2.
    Doing this didnt show any errors, it didnt do anything.

    Also i tried calling the method w/o making this (jPanel2 methodsFromJpanel2= new jPanel2 ();). I wrote this in my button2:
    Java Code:
    private void button2ButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                     
           CardLayout card = (CardLayout) displayPanel.getLayout();
           card.show(displayPanel, "card2");
           jPanel2.fillsComboBox();   //<- where i called it
    I couldnt run my app, showed this error - 'non-static method fillComboBox() cannot be referenced from a static content'.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default Re: CardLayout - opening 'new instance' each time a button is pressed

    You need the actual JPanel object.
    You can't just create another one.

    You also need to use standard naming conventions, as jPanel2 implies it is an object not a class. It should be JPanel2 if it is a class.
    And, further to that, it should have a proper, descriptive, name.

    Anyway, how are you building the JFrame and these panels?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    iNko is offline Member
    Join Date
    Dec 2012
    Posts
    55
    Rep Power
    0

    Default Re: CardLayout - opening 'new instance' each time a button is pressed

    Im using cardlayout, jFrame1 holds display panel and buttons panel. Then i create other jpanels and put them as cards..
    Also my panel names and frame names are not named jPanel1 jFrame1 etc, i just show here to make it more simple

    Edit: im reading something about revalidate(); for cardlayout, might be it
    Last edited by iNko; 12-11-2012 at 02:37 PM.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default Re: CardLayout - opening 'new instance' each time a button is pressed

    So you need the reference to the actual jPanel.
    I don't know any other way to describe this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. J2ME, stop play after pressed button
    By derata in forum CLDC and MIDP
    Replies: 3
    Last Post: 04-20-2012, 12:00 PM
  2. Do events while button is pressed
    By garnaout in forum New To Java
    Replies: 2
    Last Post: 01-24-2012, 08:43 PM
  3. How to set button to be pressed more than once
    By ajs1351 in forum New To Java
    Replies: 20
    Last Post: 12-04-2011, 06:53 AM
  4. Checking if a button was pressed in a Window.
    By Valkyrie in forum New To Java
    Replies: 2
    Last Post: 12-15-2009, 05:28 AM
  5. Waiting for a button to be pressed
    By SomeGuyOverThere in forum New To Java
    Replies: 6
    Last Post: 08-21-2008, 09:30 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
  •