How to autoupdate combobox with values from SQLite in JavaFX?
I'm writing an application with JavaFX, Scene Builder and SQlite
I have combobox with values(id) that goes from SQLite database.I have 2 textArias. I have a button "Add" that has a method void addCard(ActionEvent event)
. The method add text from textArias and apply it to particular columns in SQLite.
The problem is: when I try to add values to SQLite and click a button and then I open combobox, I don't see added ID, but when I close the window and open it again combobox display my added id.
It is very annoying to close and open window every time when I want to see the added result in combobox
- Model class holds all logic
- Controller Class operate between Model and view
- Persistent Queries class holds all queries to/from SQLite
How to display new added ID after clicking on the button "Add"?
This video shows how my application work:
Video
Model class:
package src.card;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Cards {
PersistentQueries pq = new PersistentQueries();
final ObservableList OPTIONS = FXCollections.observableArrayList();
Connection connection;
PreparedStatement pst = null;
ResultSet rs = null;
public Cards() {
try {
this.connection = DbConnection.getConnection();
} catch (SQLException ex) {
ex.printStackTrace();
}
if (this.connection == null) {
System.out.println("connection is not successful!");
System.exit(1);
}
}
public ObservableList getOPTIONS() {return OPTIONS;}
//add ID of cards to combobox
void fillCombobox() {
try {
pst = connection.prepareStatement(pq.getSqlSelectID());
rs = pst.executeQuery();
while (rs.next()) {
OPTIONS.add(rs.getString("ID"));
}
pst.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//check if database is connected
public boolean isDbConnected() {
return this.connection != null;
}
}
Controller Class:
package src.card;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import src.card.Cards;
import src.card.Context;
import src.card.DbConnection;
import src.card.PersistentQueries;
import java.net.URL;
import java.sql.*;
import java.util.ResourceBundle;
public class QuestController implements Initializable {
@FXML private TextArea ta_questText, ta_answerText;
@FXML private Label questId, error;
@FXML private ComboBox<String> combobox_question;
Cards cards = new Cards();
PersistentQueries pq = new PersistentQueries();
@Override
public void initialize(URL location, ResourceBundle resources) {
//register QuestController in Context Class
Context.getInstance().setQuestController(this);
cards.fillCombobox();
combobox_question.setItems(cards.getOPTIONS());
}
//adding cards to database
@FXML
void addCard(ActionEvent event) {
if (ta_questText.getText().equals("") ||
ta_answerText.getText().equals("")) {
error.setText("All fields are required!");
} else {
try {
error.setText("");
Connection conn = DbConnection.getConnection();
PreparedStatement stmt = conn.prepareStatement(pq.getSqlInsert());
stmt.setString(1, this.ta_questText.getText());
stmt.setString(2, this.ta_answerText.getText());
ta_questText.setText("");
ta_answerText.setText("");
stmt.execute();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@FXML
void idList(ActionEvent event) {
questId.setText(combobox_question.getValue() + ".");
}
}
PersistentQueries Class
package src.card;
public class PersistentQueries {
private String sqlInsert = "INSERT INTO Cards(question, answer) VALUES
(?,?)";
private String sqlSelectID = "SELECT ID FROM Cards";
private String sqlSelect = "SELECT question and answer FROM Cards";
public String getSqlInsert() {
return sqlInsert;
}
public void setSqlInsert(String sqlInsert) {
this.sqlInsert = sqlInsert;
}
public String getSqlSelectID() {
return sqlSelectID;
}
public void setSqlSelectID(String sqlSelectID) {
this.sqlSelectID = sqlSelectID;
}
public String getSqlSelect() {
return sqlSelect;
}
public void setSqlSelect(String sqlSelect) {
this.sqlSelect = sqlSelect;
}
}
java sqlite javafx scenebuilder
add a comment |
I'm writing an application with JavaFX, Scene Builder and SQlite
I have combobox with values(id) that goes from SQLite database.I have 2 textArias. I have a button "Add" that has a method void addCard(ActionEvent event)
. The method add text from textArias and apply it to particular columns in SQLite.
The problem is: when I try to add values to SQLite and click a button and then I open combobox, I don't see added ID, but when I close the window and open it again combobox display my added id.
It is very annoying to close and open window every time when I want to see the added result in combobox
- Model class holds all logic
- Controller Class operate between Model and view
- Persistent Queries class holds all queries to/from SQLite
How to display new added ID after clicking on the button "Add"?
This video shows how my application work:
Video
Model class:
package src.card;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Cards {
PersistentQueries pq = new PersistentQueries();
final ObservableList OPTIONS = FXCollections.observableArrayList();
Connection connection;
PreparedStatement pst = null;
ResultSet rs = null;
public Cards() {
try {
this.connection = DbConnection.getConnection();
} catch (SQLException ex) {
ex.printStackTrace();
}
if (this.connection == null) {
System.out.println("connection is not successful!");
System.exit(1);
}
}
public ObservableList getOPTIONS() {return OPTIONS;}
//add ID of cards to combobox
void fillCombobox() {
try {
pst = connection.prepareStatement(pq.getSqlSelectID());
rs = pst.executeQuery();
while (rs.next()) {
OPTIONS.add(rs.getString("ID"));
}
pst.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//check if database is connected
public boolean isDbConnected() {
return this.connection != null;
}
}
Controller Class:
package src.card;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import src.card.Cards;
import src.card.Context;
import src.card.DbConnection;
import src.card.PersistentQueries;
import java.net.URL;
import java.sql.*;
import java.util.ResourceBundle;
public class QuestController implements Initializable {
@FXML private TextArea ta_questText, ta_answerText;
@FXML private Label questId, error;
@FXML private ComboBox<String> combobox_question;
Cards cards = new Cards();
PersistentQueries pq = new PersistentQueries();
@Override
public void initialize(URL location, ResourceBundle resources) {
//register QuestController in Context Class
Context.getInstance().setQuestController(this);
cards.fillCombobox();
combobox_question.setItems(cards.getOPTIONS());
}
//adding cards to database
@FXML
void addCard(ActionEvent event) {
if (ta_questText.getText().equals("") ||
ta_answerText.getText().equals("")) {
error.setText("All fields are required!");
} else {
try {
error.setText("");
Connection conn = DbConnection.getConnection();
PreparedStatement stmt = conn.prepareStatement(pq.getSqlInsert());
stmt.setString(1, this.ta_questText.getText());
stmt.setString(2, this.ta_answerText.getText());
ta_questText.setText("");
ta_answerText.setText("");
stmt.execute();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@FXML
void idList(ActionEvent event) {
questId.setText(combobox_question.getValue() + ".");
}
}
PersistentQueries Class
package src.card;
public class PersistentQueries {
private String sqlInsert = "INSERT INTO Cards(question, answer) VALUES
(?,?)";
private String sqlSelectID = "SELECT ID FROM Cards";
private String sqlSelect = "SELECT question and answer FROM Cards";
public String getSqlInsert() {
return sqlInsert;
}
public void setSqlInsert(String sqlInsert) {
this.sqlInsert = sqlInsert;
}
public String getSqlSelectID() {
return sqlSelectID;
}
public void setSqlSelectID(String sqlSelectID) {
this.sqlSelectID = sqlSelectID;
}
public String getSqlSelect() {
return sqlSelect;
}
public void setSqlSelect(String sqlSelect) {
this.sqlSelect = sqlSelect;
}
}
java sqlite javafx scenebuilder
You can either add the item to the combobox at the same time as adding it to the database, or reload the data from the database after adding it.
– Zephyr
Jan 3 at 21:34
@Zephyr how to reload data in combobox? can you give me an example?
– kentforth
Jan 4 at 7:57
add a comment |
I'm writing an application with JavaFX, Scene Builder and SQlite
I have combobox with values(id) that goes from SQLite database.I have 2 textArias. I have a button "Add" that has a method void addCard(ActionEvent event)
. The method add text from textArias and apply it to particular columns in SQLite.
The problem is: when I try to add values to SQLite and click a button and then I open combobox, I don't see added ID, but when I close the window and open it again combobox display my added id.
It is very annoying to close and open window every time when I want to see the added result in combobox
- Model class holds all logic
- Controller Class operate between Model and view
- Persistent Queries class holds all queries to/from SQLite
How to display new added ID after clicking on the button "Add"?
This video shows how my application work:
Video
Model class:
package src.card;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Cards {
PersistentQueries pq = new PersistentQueries();
final ObservableList OPTIONS = FXCollections.observableArrayList();
Connection connection;
PreparedStatement pst = null;
ResultSet rs = null;
public Cards() {
try {
this.connection = DbConnection.getConnection();
} catch (SQLException ex) {
ex.printStackTrace();
}
if (this.connection == null) {
System.out.println("connection is not successful!");
System.exit(1);
}
}
public ObservableList getOPTIONS() {return OPTIONS;}
//add ID of cards to combobox
void fillCombobox() {
try {
pst = connection.prepareStatement(pq.getSqlSelectID());
rs = pst.executeQuery();
while (rs.next()) {
OPTIONS.add(rs.getString("ID"));
}
pst.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//check if database is connected
public boolean isDbConnected() {
return this.connection != null;
}
}
Controller Class:
package src.card;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import src.card.Cards;
import src.card.Context;
import src.card.DbConnection;
import src.card.PersistentQueries;
import java.net.URL;
import java.sql.*;
import java.util.ResourceBundle;
public class QuestController implements Initializable {
@FXML private TextArea ta_questText, ta_answerText;
@FXML private Label questId, error;
@FXML private ComboBox<String> combobox_question;
Cards cards = new Cards();
PersistentQueries pq = new PersistentQueries();
@Override
public void initialize(URL location, ResourceBundle resources) {
//register QuestController in Context Class
Context.getInstance().setQuestController(this);
cards.fillCombobox();
combobox_question.setItems(cards.getOPTIONS());
}
//adding cards to database
@FXML
void addCard(ActionEvent event) {
if (ta_questText.getText().equals("") ||
ta_answerText.getText().equals("")) {
error.setText("All fields are required!");
} else {
try {
error.setText("");
Connection conn = DbConnection.getConnection();
PreparedStatement stmt = conn.prepareStatement(pq.getSqlInsert());
stmt.setString(1, this.ta_questText.getText());
stmt.setString(2, this.ta_answerText.getText());
ta_questText.setText("");
ta_answerText.setText("");
stmt.execute();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@FXML
void idList(ActionEvent event) {
questId.setText(combobox_question.getValue() + ".");
}
}
PersistentQueries Class
package src.card;
public class PersistentQueries {
private String sqlInsert = "INSERT INTO Cards(question, answer) VALUES
(?,?)";
private String sqlSelectID = "SELECT ID FROM Cards";
private String sqlSelect = "SELECT question and answer FROM Cards";
public String getSqlInsert() {
return sqlInsert;
}
public void setSqlInsert(String sqlInsert) {
this.sqlInsert = sqlInsert;
}
public String getSqlSelectID() {
return sqlSelectID;
}
public void setSqlSelectID(String sqlSelectID) {
this.sqlSelectID = sqlSelectID;
}
public String getSqlSelect() {
return sqlSelect;
}
public void setSqlSelect(String sqlSelect) {
this.sqlSelect = sqlSelect;
}
}
java sqlite javafx scenebuilder
I'm writing an application with JavaFX, Scene Builder and SQlite
I have combobox with values(id) that goes from SQLite database.I have 2 textArias. I have a button "Add" that has a method void addCard(ActionEvent event)
. The method add text from textArias and apply it to particular columns in SQLite.
The problem is: when I try to add values to SQLite and click a button and then I open combobox, I don't see added ID, but when I close the window and open it again combobox display my added id.
It is very annoying to close and open window every time when I want to see the added result in combobox
- Model class holds all logic
- Controller Class operate between Model and view
- Persistent Queries class holds all queries to/from SQLite
How to display new added ID after clicking on the button "Add"?
This video shows how my application work:
Video
Model class:
package src.card;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Cards {
PersistentQueries pq = new PersistentQueries();
final ObservableList OPTIONS = FXCollections.observableArrayList();
Connection connection;
PreparedStatement pst = null;
ResultSet rs = null;
public Cards() {
try {
this.connection = DbConnection.getConnection();
} catch (SQLException ex) {
ex.printStackTrace();
}
if (this.connection == null) {
System.out.println("connection is not successful!");
System.exit(1);
}
}
public ObservableList getOPTIONS() {return OPTIONS;}
//add ID of cards to combobox
void fillCombobox() {
try {
pst = connection.prepareStatement(pq.getSqlSelectID());
rs = pst.executeQuery();
while (rs.next()) {
OPTIONS.add(rs.getString("ID"));
}
pst.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//check if database is connected
public boolean isDbConnected() {
return this.connection != null;
}
}
Controller Class:
package src.card;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import src.card.Cards;
import src.card.Context;
import src.card.DbConnection;
import src.card.PersistentQueries;
import java.net.URL;
import java.sql.*;
import java.util.ResourceBundle;
public class QuestController implements Initializable {
@FXML private TextArea ta_questText, ta_answerText;
@FXML private Label questId, error;
@FXML private ComboBox<String> combobox_question;
Cards cards = new Cards();
PersistentQueries pq = new PersistentQueries();
@Override
public void initialize(URL location, ResourceBundle resources) {
//register QuestController in Context Class
Context.getInstance().setQuestController(this);
cards.fillCombobox();
combobox_question.setItems(cards.getOPTIONS());
}
//adding cards to database
@FXML
void addCard(ActionEvent event) {
if (ta_questText.getText().equals("") ||
ta_answerText.getText().equals("")) {
error.setText("All fields are required!");
} else {
try {
error.setText("");
Connection conn = DbConnection.getConnection();
PreparedStatement stmt = conn.prepareStatement(pq.getSqlInsert());
stmt.setString(1, this.ta_questText.getText());
stmt.setString(2, this.ta_answerText.getText());
ta_questText.setText("");
ta_answerText.setText("");
stmt.execute();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@FXML
void idList(ActionEvent event) {
questId.setText(combobox_question.getValue() + ".");
}
}
PersistentQueries Class
package src.card;
public class PersistentQueries {
private String sqlInsert = "INSERT INTO Cards(question, answer) VALUES
(?,?)";
private String sqlSelectID = "SELECT ID FROM Cards";
private String sqlSelect = "SELECT question and answer FROM Cards";
public String getSqlInsert() {
return sqlInsert;
}
public void setSqlInsert(String sqlInsert) {
this.sqlInsert = sqlInsert;
}
public String getSqlSelectID() {
return sqlSelectID;
}
public void setSqlSelectID(String sqlSelectID) {
this.sqlSelectID = sqlSelectID;
}
public String getSqlSelect() {
return sqlSelect;
}
public void setSqlSelect(String sqlSelect) {
this.sqlSelect = sqlSelect;
}
}
java sqlite javafx scenebuilder
java sqlite javafx scenebuilder
edited Jan 8 at 21:38
marc_s
583k13011241270
583k13011241270
asked Jan 3 at 11:26
kentforthkentforth
7619
7619
You can either add the item to the combobox at the same time as adding it to the database, or reload the data from the database after adding it.
– Zephyr
Jan 3 at 21:34
@Zephyr how to reload data in combobox? can you give me an example?
– kentforth
Jan 4 at 7:57
add a comment |
You can either add the item to the combobox at the same time as adding it to the database, or reload the data from the database after adding it.
– Zephyr
Jan 3 at 21:34
@Zephyr how to reload data in combobox? can you give me an example?
– kentforth
Jan 4 at 7:57
You can either add the item to the combobox at the same time as adding it to the database, or reload the data from the database after adding it.
– Zephyr
Jan 3 at 21:34
You can either add the item to the combobox at the same time as adding it to the database, or reload the data from the database after adding it.
– Zephyr
Jan 3 at 21:34
@Zephyr how to reload data in combobox? can you give me an example?
– kentforth
Jan 4 at 7:57
@Zephyr how to reload data in combobox? can you give me an example?
– kentforth
Jan 4 at 7:57
add a comment |
1 Answer
1
active
oldest
votes
PROBLEM SOLVED!
in QuestController in the method @FXML void addCard(ActionEvent event)
I added
cards.getOBS().clear();
It removes all objects from Observable list
then, I called method from Cards Class that reads all new data from SQLite and add it to Observable list
cards.fillCombobox();
and then I just close all connections:
pst.close();
rs.close();
conn.close();
My rewritten method looks like this:
/adding cards to database, update combobox and clear label text
@FXML void addCard(ActionEvent event) {
if (ta_questText.getText().equals("") || ta_answerText.getText().equals("")) {
error.setText("All fields are required!");
} else {
try {
error.setText("");
Connection conn = DbConnection.getConnection();
pst = conn.prepareStatement(pq.getSqlInsert());
pst.setString(1, this.ta_questText.getText());
pst.setString(2, this.ta_answerText.getText());
pst.execute();
pst = conn.prepareStatement(pq.getSqlSelectID());
rs = pst.executeQuery();
ta_questText.clear();
ta_answerText.clear();
cards.getOBS().clear();
questId.setText("");
cards.fillCombobox();
pst.close();
rs.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
While this may work for now, you are going to run into BIG problems in the future as your application grows. I highly recommend reading up on the MCV model...
– Zephyr
Jan 4 at 12:59
Also, on a side note, you can empty any list by callinglist.clear()
.
– Zephyr
Jan 4 at 13:00
@Zephyr I posted wrong answer, because I had some bugs in combobox listing. But your comment with list.clear(); helped me. Thanx a lot! I will rewrite my answer
– kentforth
Jan 4 at 13:18
@Zephyr by the way, I use Controller(QuestController) Model (Cards Class) and View(FXML) is this MVC? or I just did wrong code for MVC pattern?
– kentforth
Jan 4 at 13:26
Yes, you have code for updating the database along with code that updates the UI, in the same class/method. That's a violation of the MVC pattern. You basically want your datasource classes to know nothing about your UI and vice versa.
– Zephyr
Jan 4 at 14:25
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54021390%2fhow-to-autoupdate-combobox-with-values-from-sqlite-in-javafx%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
PROBLEM SOLVED!
in QuestController in the method @FXML void addCard(ActionEvent event)
I added
cards.getOBS().clear();
It removes all objects from Observable list
then, I called method from Cards Class that reads all new data from SQLite and add it to Observable list
cards.fillCombobox();
and then I just close all connections:
pst.close();
rs.close();
conn.close();
My rewritten method looks like this:
/adding cards to database, update combobox and clear label text
@FXML void addCard(ActionEvent event) {
if (ta_questText.getText().equals("") || ta_answerText.getText().equals("")) {
error.setText("All fields are required!");
} else {
try {
error.setText("");
Connection conn = DbConnection.getConnection();
pst = conn.prepareStatement(pq.getSqlInsert());
pst.setString(1, this.ta_questText.getText());
pst.setString(2, this.ta_answerText.getText());
pst.execute();
pst = conn.prepareStatement(pq.getSqlSelectID());
rs = pst.executeQuery();
ta_questText.clear();
ta_answerText.clear();
cards.getOBS().clear();
questId.setText("");
cards.fillCombobox();
pst.close();
rs.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
While this may work for now, you are going to run into BIG problems in the future as your application grows. I highly recommend reading up on the MCV model...
– Zephyr
Jan 4 at 12:59
Also, on a side note, you can empty any list by callinglist.clear()
.
– Zephyr
Jan 4 at 13:00
@Zephyr I posted wrong answer, because I had some bugs in combobox listing. But your comment with list.clear(); helped me. Thanx a lot! I will rewrite my answer
– kentforth
Jan 4 at 13:18
@Zephyr by the way, I use Controller(QuestController) Model (Cards Class) and View(FXML) is this MVC? or I just did wrong code for MVC pattern?
– kentforth
Jan 4 at 13:26
Yes, you have code for updating the database along with code that updates the UI, in the same class/method. That's a violation of the MVC pattern. You basically want your datasource classes to know nothing about your UI and vice versa.
– Zephyr
Jan 4 at 14:25
|
show 1 more comment
PROBLEM SOLVED!
in QuestController in the method @FXML void addCard(ActionEvent event)
I added
cards.getOBS().clear();
It removes all objects from Observable list
then, I called method from Cards Class that reads all new data from SQLite and add it to Observable list
cards.fillCombobox();
and then I just close all connections:
pst.close();
rs.close();
conn.close();
My rewritten method looks like this:
/adding cards to database, update combobox and clear label text
@FXML void addCard(ActionEvent event) {
if (ta_questText.getText().equals("") || ta_answerText.getText().equals("")) {
error.setText("All fields are required!");
} else {
try {
error.setText("");
Connection conn = DbConnection.getConnection();
pst = conn.prepareStatement(pq.getSqlInsert());
pst.setString(1, this.ta_questText.getText());
pst.setString(2, this.ta_answerText.getText());
pst.execute();
pst = conn.prepareStatement(pq.getSqlSelectID());
rs = pst.executeQuery();
ta_questText.clear();
ta_answerText.clear();
cards.getOBS().clear();
questId.setText("");
cards.fillCombobox();
pst.close();
rs.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
While this may work for now, you are going to run into BIG problems in the future as your application grows. I highly recommend reading up on the MCV model...
– Zephyr
Jan 4 at 12:59
Also, on a side note, you can empty any list by callinglist.clear()
.
– Zephyr
Jan 4 at 13:00
@Zephyr I posted wrong answer, because I had some bugs in combobox listing. But your comment with list.clear(); helped me. Thanx a lot! I will rewrite my answer
– kentforth
Jan 4 at 13:18
@Zephyr by the way, I use Controller(QuestController) Model (Cards Class) and View(FXML) is this MVC? or I just did wrong code for MVC pattern?
– kentforth
Jan 4 at 13:26
Yes, you have code for updating the database along with code that updates the UI, in the same class/method. That's a violation of the MVC pattern. You basically want your datasource classes to know nothing about your UI and vice versa.
– Zephyr
Jan 4 at 14:25
|
show 1 more comment
PROBLEM SOLVED!
in QuestController in the method @FXML void addCard(ActionEvent event)
I added
cards.getOBS().clear();
It removes all objects from Observable list
then, I called method from Cards Class that reads all new data from SQLite and add it to Observable list
cards.fillCombobox();
and then I just close all connections:
pst.close();
rs.close();
conn.close();
My rewritten method looks like this:
/adding cards to database, update combobox and clear label text
@FXML void addCard(ActionEvent event) {
if (ta_questText.getText().equals("") || ta_answerText.getText().equals("")) {
error.setText("All fields are required!");
} else {
try {
error.setText("");
Connection conn = DbConnection.getConnection();
pst = conn.prepareStatement(pq.getSqlInsert());
pst.setString(1, this.ta_questText.getText());
pst.setString(2, this.ta_answerText.getText());
pst.execute();
pst = conn.prepareStatement(pq.getSqlSelectID());
rs = pst.executeQuery();
ta_questText.clear();
ta_answerText.clear();
cards.getOBS().clear();
questId.setText("");
cards.fillCombobox();
pst.close();
rs.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
PROBLEM SOLVED!
in QuestController in the method @FXML void addCard(ActionEvent event)
I added
cards.getOBS().clear();
It removes all objects from Observable list
then, I called method from Cards Class that reads all new data from SQLite and add it to Observable list
cards.fillCombobox();
and then I just close all connections:
pst.close();
rs.close();
conn.close();
My rewritten method looks like this:
/adding cards to database, update combobox and clear label text
@FXML void addCard(ActionEvent event) {
if (ta_questText.getText().equals("") || ta_answerText.getText().equals("")) {
error.setText("All fields are required!");
} else {
try {
error.setText("");
Connection conn = DbConnection.getConnection();
pst = conn.prepareStatement(pq.getSqlInsert());
pst.setString(1, this.ta_questText.getText());
pst.setString(2, this.ta_answerText.getText());
pst.execute();
pst = conn.prepareStatement(pq.getSqlSelectID());
rs = pst.executeQuery();
ta_questText.clear();
ta_answerText.clear();
cards.getOBS().clear();
questId.setText("");
cards.fillCombobox();
pst.close();
rs.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
edited Jan 4 at 13:25
answered Jan 4 at 9:32
kentforthkentforth
7619
7619
While this may work for now, you are going to run into BIG problems in the future as your application grows. I highly recommend reading up on the MCV model...
– Zephyr
Jan 4 at 12:59
Also, on a side note, you can empty any list by callinglist.clear()
.
– Zephyr
Jan 4 at 13:00
@Zephyr I posted wrong answer, because I had some bugs in combobox listing. But your comment with list.clear(); helped me. Thanx a lot! I will rewrite my answer
– kentforth
Jan 4 at 13:18
@Zephyr by the way, I use Controller(QuestController) Model (Cards Class) and View(FXML) is this MVC? or I just did wrong code for MVC pattern?
– kentforth
Jan 4 at 13:26
Yes, you have code for updating the database along with code that updates the UI, in the same class/method. That's a violation of the MVC pattern. You basically want your datasource classes to know nothing about your UI and vice versa.
– Zephyr
Jan 4 at 14:25
|
show 1 more comment
While this may work for now, you are going to run into BIG problems in the future as your application grows. I highly recommend reading up on the MCV model...
– Zephyr
Jan 4 at 12:59
Also, on a side note, you can empty any list by callinglist.clear()
.
– Zephyr
Jan 4 at 13:00
@Zephyr I posted wrong answer, because I had some bugs in combobox listing. But your comment with list.clear(); helped me. Thanx a lot! I will rewrite my answer
– kentforth
Jan 4 at 13:18
@Zephyr by the way, I use Controller(QuestController) Model (Cards Class) and View(FXML) is this MVC? or I just did wrong code for MVC pattern?
– kentforth
Jan 4 at 13:26
Yes, you have code for updating the database along with code that updates the UI, in the same class/method. That's a violation of the MVC pattern. You basically want your datasource classes to know nothing about your UI and vice versa.
– Zephyr
Jan 4 at 14:25
While this may work for now, you are going to run into BIG problems in the future as your application grows. I highly recommend reading up on the MCV model...
– Zephyr
Jan 4 at 12:59
While this may work for now, you are going to run into BIG problems in the future as your application grows. I highly recommend reading up on the MCV model...
– Zephyr
Jan 4 at 12:59
Also, on a side note, you can empty any list by calling
list.clear()
.– Zephyr
Jan 4 at 13:00
Also, on a side note, you can empty any list by calling
list.clear()
.– Zephyr
Jan 4 at 13:00
@Zephyr I posted wrong answer, because I had some bugs in combobox listing. But your comment with list.clear(); helped me. Thanx a lot! I will rewrite my answer
– kentforth
Jan 4 at 13:18
@Zephyr I posted wrong answer, because I had some bugs in combobox listing. But your comment with list.clear(); helped me. Thanx a lot! I will rewrite my answer
– kentforth
Jan 4 at 13:18
@Zephyr by the way, I use Controller(QuestController) Model (Cards Class) and View(FXML) is this MVC? or I just did wrong code for MVC pattern?
– kentforth
Jan 4 at 13:26
@Zephyr by the way, I use Controller(QuestController) Model (Cards Class) and View(FXML) is this MVC? or I just did wrong code for MVC pattern?
– kentforth
Jan 4 at 13:26
Yes, you have code for updating the database along with code that updates the UI, in the same class/method. That's a violation of the MVC pattern. You basically want your datasource classes to know nothing about your UI and vice versa.
– Zephyr
Jan 4 at 14:25
Yes, you have code for updating the database along with code that updates the UI, in the same class/method. That's a violation of the MVC pattern. You basically want your datasource classes to know nothing about your UI and vice versa.
– Zephyr
Jan 4 at 14:25
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54021390%2fhow-to-autoupdate-combobox-with-values-from-sqlite-in-javafx%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You can either add the item to the combobox at the same time as adding it to the database, or reload the data from the database after adding it.
– Zephyr
Jan 3 at 21:34
@Zephyr how to reload data in combobox? can you give me an example?
– kentforth
Jan 4 at 7:57