How to bind ArrayList data from database into JComboBox in JTable?
up vote
0
down vote
favorite
I have successfully fetched data from database into JTable. But I'm having a little trouble to populate in JComboBox and then add it to JTable. Like I have stored ArrayList in String format in Documents Column. Now, I want a JComboBox in each row of Documents Column using that ArrayList in JTable.
Basically, replacing ArrayList String to JComboBox with that Array items.
In the documents column, the data is in form of array string:
Jtable Code:
private static final DatabaseConnections databaseConnections = new DatabaseConnections();
private Thread thread;
Vector<Object> columnNames = new Vector<Object>();
Vector<Object> data = new Vector<Object>();
public ConnectionRecordsPanel() {
initComponents();
SetData();
}
private void SetData(){
columnNames.addElement("Customer Id");
columnNames.addElement("Name");
columnNames.addElement("DOB");
columnNames.addElement("Gender");
columnNames.addElement("Current Address");
columnNames.addElement("Permanent Address");
columnNames.addElement("Mobile Number");
columnNames.addElement("Email Id");
columnNames.addElement("Documents");
columnNames.addElement("Gas Type");
columnNames.addElement("Status");
thread = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("Inside run");
data = databaseConnections.getData(data);
System.out.println("Inside run");
DefaultTableModel model = new DefaultTableModel(data, columnNames)
{
@Override
public Class getColumnClass(int column)
{
for (int row = 0; row < getRowCount(); row++)
{
Object o = getValueAt(row, column);
if (o != null)
{
return o.getClass();
}
}
return Object.class;
}
};
jTable1.setModel(model);
JComboBox jcb = new JComboBox();
jTable1.getColumnModel().getColumn(9).setCellEditor(new DefaultCellEditor(jcb));
} catch (ParseException ex) {
Logger.getLogger(ConnectionRecordsPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
thread.start();
}
Reading from Database
public Vector getData(Vector data) throws ParseException{
try {
resultSet = statement.executeQuery("select * from connection_records");
while (resultSet.next()){
Vector<Object> row = new Vector<Object>();
for (int i = 1; i <= 11 ; i++)
{
row.addElement(resultSet.getString(i));
}
data.addElement( row );
}
} catch (SQLException ex) {
Logger.getLogger(DatabaseConnections.class.getName()).log(Level.SEVERE, null, ex);
}
return data;
}
java database swing jtable jcombobox
|
show 1 more comment
up vote
0
down vote
favorite
I have successfully fetched data from database into JTable. But I'm having a little trouble to populate in JComboBox and then add it to JTable. Like I have stored ArrayList in String format in Documents Column. Now, I want a JComboBox in each row of Documents Column using that ArrayList in JTable.
Basically, replacing ArrayList String to JComboBox with that Array items.
In the documents column, the data is in form of array string:
Jtable Code:
private static final DatabaseConnections databaseConnections = new DatabaseConnections();
private Thread thread;
Vector<Object> columnNames = new Vector<Object>();
Vector<Object> data = new Vector<Object>();
public ConnectionRecordsPanel() {
initComponents();
SetData();
}
private void SetData(){
columnNames.addElement("Customer Id");
columnNames.addElement("Name");
columnNames.addElement("DOB");
columnNames.addElement("Gender");
columnNames.addElement("Current Address");
columnNames.addElement("Permanent Address");
columnNames.addElement("Mobile Number");
columnNames.addElement("Email Id");
columnNames.addElement("Documents");
columnNames.addElement("Gas Type");
columnNames.addElement("Status");
thread = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("Inside run");
data = databaseConnections.getData(data);
System.out.println("Inside run");
DefaultTableModel model = new DefaultTableModel(data, columnNames)
{
@Override
public Class getColumnClass(int column)
{
for (int row = 0; row < getRowCount(); row++)
{
Object o = getValueAt(row, column);
if (o != null)
{
return o.getClass();
}
}
return Object.class;
}
};
jTable1.setModel(model);
JComboBox jcb = new JComboBox();
jTable1.getColumnModel().getColumn(9).setCellEditor(new DefaultCellEditor(jcb));
} catch (ParseException ex) {
Logger.getLogger(ConnectionRecordsPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
thread.start();
}
Reading from Database
public Vector getData(Vector data) throws ParseException{
try {
resultSet = statement.executeQuery("select * from connection_records");
while (resultSet.next()){
Vector<Object> row = new Vector<Object>();
for (int i = 1; i <= 11 ; i++)
{
row.addElement(resultSet.getString(i));
}
data.addElement( row );
}
} catch (SQLException ex) {
Logger.getLogger(DatabaseConnections.class.getName()).log(Level.SEVERE, null, ex);
}
return data;
}
java database swing jtable jcombobox
Don't use bold highlighting on the text of your question. Bold highlighting is used to emphasize a word or sentence, not the entire question.
– camickr
Nov 11 at 18:51
How do you expect this to work? A JTable uses renderers to display data. If you use the combo box as a renderer then you will only ever see one item. A JTable also uses editors. The editor is how you change the data. So when you edit the cell a combo box can be used to display multiple items, but then when you select an item only that item is store in the model.
– camickr
Nov 11 at 18:58
Sorry for posting it in a wrong format.
– Yagnesh Vakharia
Nov 11 at 19:16
So what is the solution according to you? Because I have multiple data (ArrayList) in column which I want to display in an efficient manner in JTable that is why I have chosen JComboBox. But now you are telling me renderers are used to display data.
– Yagnesh Vakharia
Nov 11 at 19:23
If you want to see all the items at once you do what you are currently doing. You could limit the column with and use a tooltip on the cell to see all the values. Or you could create a custom render. Maybe something like: stackoverflow.com/questions/9955595/…. Or maybe you change your database to have a single column for each type of Document and then you just store a Boolean value to indicate which Documents you have. Then the JTable would also just have a column with a check box for each document.
– camickr
Nov 12 at 16:31
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have successfully fetched data from database into JTable. But I'm having a little trouble to populate in JComboBox and then add it to JTable. Like I have stored ArrayList in String format in Documents Column. Now, I want a JComboBox in each row of Documents Column using that ArrayList in JTable.
Basically, replacing ArrayList String to JComboBox with that Array items.
In the documents column, the data is in form of array string:
Jtable Code:
private static final DatabaseConnections databaseConnections = new DatabaseConnections();
private Thread thread;
Vector<Object> columnNames = new Vector<Object>();
Vector<Object> data = new Vector<Object>();
public ConnectionRecordsPanel() {
initComponents();
SetData();
}
private void SetData(){
columnNames.addElement("Customer Id");
columnNames.addElement("Name");
columnNames.addElement("DOB");
columnNames.addElement("Gender");
columnNames.addElement("Current Address");
columnNames.addElement("Permanent Address");
columnNames.addElement("Mobile Number");
columnNames.addElement("Email Id");
columnNames.addElement("Documents");
columnNames.addElement("Gas Type");
columnNames.addElement("Status");
thread = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("Inside run");
data = databaseConnections.getData(data);
System.out.println("Inside run");
DefaultTableModel model = new DefaultTableModel(data, columnNames)
{
@Override
public Class getColumnClass(int column)
{
for (int row = 0; row < getRowCount(); row++)
{
Object o = getValueAt(row, column);
if (o != null)
{
return o.getClass();
}
}
return Object.class;
}
};
jTable1.setModel(model);
JComboBox jcb = new JComboBox();
jTable1.getColumnModel().getColumn(9).setCellEditor(new DefaultCellEditor(jcb));
} catch (ParseException ex) {
Logger.getLogger(ConnectionRecordsPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
thread.start();
}
Reading from Database
public Vector getData(Vector data) throws ParseException{
try {
resultSet = statement.executeQuery("select * from connection_records");
while (resultSet.next()){
Vector<Object> row = new Vector<Object>();
for (int i = 1; i <= 11 ; i++)
{
row.addElement(resultSet.getString(i));
}
data.addElement( row );
}
} catch (SQLException ex) {
Logger.getLogger(DatabaseConnections.class.getName()).log(Level.SEVERE, null, ex);
}
return data;
}
java database swing jtable jcombobox
I have successfully fetched data from database into JTable. But I'm having a little trouble to populate in JComboBox and then add it to JTable. Like I have stored ArrayList in String format in Documents Column. Now, I want a JComboBox in each row of Documents Column using that ArrayList in JTable.
Basically, replacing ArrayList String to JComboBox with that Array items.
In the documents column, the data is in form of array string:
Jtable Code:
private static final DatabaseConnections databaseConnections = new DatabaseConnections();
private Thread thread;
Vector<Object> columnNames = new Vector<Object>();
Vector<Object> data = new Vector<Object>();
public ConnectionRecordsPanel() {
initComponents();
SetData();
}
private void SetData(){
columnNames.addElement("Customer Id");
columnNames.addElement("Name");
columnNames.addElement("DOB");
columnNames.addElement("Gender");
columnNames.addElement("Current Address");
columnNames.addElement("Permanent Address");
columnNames.addElement("Mobile Number");
columnNames.addElement("Email Id");
columnNames.addElement("Documents");
columnNames.addElement("Gas Type");
columnNames.addElement("Status");
thread = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("Inside run");
data = databaseConnections.getData(data);
System.out.println("Inside run");
DefaultTableModel model = new DefaultTableModel(data, columnNames)
{
@Override
public Class getColumnClass(int column)
{
for (int row = 0; row < getRowCount(); row++)
{
Object o = getValueAt(row, column);
if (o != null)
{
return o.getClass();
}
}
return Object.class;
}
};
jTable1.setModel(model);
JComboBox jcb = new JComboBox();
jTable1.getColumnModel().getColumn(9).setCellEditor(new DefaultCellEditor(jcb));
} catch (ParseException ex) {
Logger.getLogger(ConnectionRecordsPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
thread.start();
}
Reading from Database
public Vector getData(Vector data) throws ParseException{
try {
resultSet = statement.executeQuery("select * from connection_records");
while (resultSet.next()){
Vector<Object> row = new Vector<Object>();
for (int i = 1; i <= 11 ; i++)
{
row.addElement(resultSet.getString(i));
}
data.addElement( row );
}
} catch (SQLException ex) {
Logger.getLogger(DatabaseConnections.class.getName()).log(Level.SEVERE, null, ex);
}
return data;
}
java database swing jtable jcombobox
java database swing jtable jcombobox
edited Nov 11 at 18:47
camickr
273k15126239
273k15126239
asked Nov 11 at 17:10
Yagnesh Vakharia
1
1
Don't use bold highlighting on the text of your question. Bold highlighting is used to emphasize a word or sentence, not the entire question.
– camickr
Nov 11 at 18:51
How do you expect this to work? A JTable uses renderers to display data. If you use the combo box as a renderer then you will only ever see one item. A JTable also uses editors. The editor is how you change the data. So when you edit the cell a combo box can be used to display multiple items, but then when you select an item only that item is store in the model.
– camickr
Nov 11 at 18:58
Sorry for posting it in a wrong format.
– Yagnesh Vakharia
Nov 11 at 19:16
So what is the solution according to you? Because I have multiple data (ArrayList) in column which I want to display in an efficient manner in JTable that is why I have chosen JComboBox. But now you are telling me renderers are used to display data.
– Yagnesh Vakharia
Nov 11 at 19:23
If you want to see all the items at once you do what you are currently doing. You could limit the column with and use a tooltip on the cell to see all the values. Or you could create a custom render. Maybe something like: stackoverflow.com/questions/9955595/…. Or maybe you change your database to have a single column for each type of Document and then you just store a Boolean value to indicate which Documents you have. Then the JTable would also just have a column with a check box for each document.
– camickr
Nov 12 at 16:31
|
show 1 more comment
Don't use bold highlighting on the text of your question. Bold highlighting is used to emphasize a word or sentence, not the entire question.
– camickr
Nov 11 at 18:51
How do you expect this to work? A JTable uses renderers to display data. If you use the combo box as a renderer then you will only ever see one item. A JTable also uses editors. The editor is how you change the data. So when you edit the cell a combo box can be used to display multiple items, but then when you select an item only that item is store in the model.
– camickr
Nov 11 at 18:58
Sorry for posting it in a wrong format.
– Yagnesh Vakharia
Nov 11 at 19:16
So what is the solution according to you? Because I have multiple data (ArrayList) in column which I want to display in an efficient manner in JTable that is why I have chosen JComboBox. But now you are telling me renderers are used to display data.
– Yagnesh Vakharia
Nov 11 at 19:23
If you want to see all the items at once you do what you are currently doing. You could limit the column with and use a tooltip on the cell to see all the values. Or you could create a custom render. Maybe something like: stackoverflow.com/questions/9955595/…. Or maybe you change your database to have a single column for each type of Document and then you just store a Boolean value to indicate which Documents you have. Then the JTable would also just have a column with a check box for each document.
– camickr
Nov 12 at 16:31
Don't use bold highlighting on the text of your question. Bold highlighting is used to emphasize a word or sentence, not the entire question.
– camickr
Nov 11 at 18:51
Don't use bold highlighting on the text of your question. Bold highlighting is used to emphasize a word or sentence, not the entire question.
– camickr
Nov 11 at 18:51
How do you expect this to work? A JTable uses renderers to display data. If you use the combo box as a renderer then you will only ever see one item. A JTable also uses editors. The editor is how you change the data. So when you edit the cell a combo box can be used to display multiple items, but then when you select an item only that item is store in the model.
– camickr
Nov 11 at 18:58
How do you expect this to work? A JTable uses renderers to display data. If you use the combo box as a renderer then you will only ever see one item. A JTable also uses editors. The editor is how you change the data. So when you edit the cell a combo box can be used to display multiple items, but then when you select an item only that item is store in the model.
– camickr
Nov 11 at 18:58
Sorry for posting it in a wrong format.
– Yagnesh Vakharia
Nov 11 at 19:16
Sorry for posting it in a wrong format.
– Yagnesh Vakharia
Nov 11 at 19:16
So what is the solution according to you? Because I have multiple data (ArrayList) in column which I want to display in an efficient manner in JTable that is why I have chosen JComboBox. But now you are telling me renderers are used to display data.
– Yagnesh Vakharia
Nov 11 at 19:23
So what is the solution according to you? Because I have multiple data (ArrayList) in column which I want to display in an efficient manner in JTable that is why I have chosen JComboBox. But now you are telling me renderers are used to display data.
– Yagnesh Vakharia
Nov 11 at 19:23
If you want to see all the items at once you do what you are currently doing. You could limit the column with and use a tooltip on the cell to see all the values. Or you could create a custom render. Maybe something like: stackoverflow.com/questions/9955595/…. Or maybe you change your database to have a single column for each type of Document and then you just store a Boolean value to indicate which Documents you have. Then the JTable would also just have a column with a check box for each document.
– camickr
Nov 12 at 16:31
If you want to see all the items at once you do what you are currently doing. You could limit the column with and use a tooltip on the cell to see all the values. Or you could create a custom render. Maybe something like: stackoverflow.com/questions/9955595/…. Or maybe you change your database to have a single column for each type of Document and then you just store a Boolean value to indicate which Documents you have. Then the JTable would also just have a column with a check box for each document.
– camickr
Nov 12 at 16:31
|
show 1 more comment
active
oldest
votes
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',
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%2f53251162%2fhow-to-bind-arraylist-data-from-database-into-jcombobox-in-jtable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53251162%2fhow-to-bind-arraylist-data-from-database-into-jcombobox-in-jtable%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
Don't use bold highlighting on the text of your question. Bold highlighting is used to emphasize a word or sentence, not the entire question.
– camickr
Nov 11 at 18:51
How do you expect this to work? A JTable uses renderers to display data. If you use the combo box as a renderer then you will only ever see one item. A JTable also uses editors. The editor is how you change the data. So when you edit the cell a combo box can be used to display multiple items, but then when you select an item only that item is store in the model.
– camickr
Nov 11 at 18:58
Sorry for posting it in a wrong format.
– Yagnesh Vakharia
Nov 11 at 19:16
So what is the solution according to you? Because I have multiple data (ArrayList) in column which I want to display in an efficient manner in JTable that is why I have chosen JComboBox. But now you are telling me renderers are used to display data.
– Yagnesh Vakharia
Nov 11 at 19:23
If you want to see all the items at once you do what you are currently doing. You could limit the column with and use a tooltip on the cell to see all the values. Or you could create a custom render. Maybe something like: stackoverflow.com/questions/9955595/…. Or maybe you change your database to have a single column for each type of Document and then you just store a Boolean value to indicate which Documents you have. Then the JTable would also just have a column with a check box for each document.
– camickr
Nov 12 at 16:31