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:
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?
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
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..
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
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:
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:
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();'):
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 :(
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):
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):
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).
Re: CardLayout - opening 'new instance' each time a button is pressed
still havent fixed this problem, noone can help?
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.
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:
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:
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'.
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?
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
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.