This question already has answers here:
How to add JCheckBox in JTable?
(2 answers)
Closed 3 years ago.
I am currently working on an attendance system where I'll use jTable and add jCheckbox in it. However, I have no idea how to do this.
What should I do to add jCheckBox in my jTable. The data in my jTable is acquired from a database.
I have tried using this code but the table doesn't show the data from my database and still doesn't have a checkbox on it.
public void Update_table(int Column, int ColumnBoolean, DefaultTableModel model) {
try {
String sql = "select * from student_info";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
Attendance.setModel(DbUtils.resultSetToTableModel(rs));
Object[] files = new Object[Column];
while (rs.next()) {
for (int i = 1; i <= Column; i++) {
if (i == ColumnBoolean) {
files[ColumnBoolean - 1] = Boolean.FALSE;
} else {
files[i - 1] = rs.getObject(i - 1);
}
model.addRow(files);
}
Attendance.updateUI();
rs.close();
}
JCheckBox check = new JCheckBox();
Attendance.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(check));
Attendance.getColumnModel().getColumn(0).setCellRenderer(new DefaultTableCellRenderer());
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
First of all you have all kinds of issues with the posted code:
Variable names should NOT start with an upper case character. Some are correct, other are not. Be consistent and follow Java conventions
Method names should NOT start with an upper case character. Again follow Java conventions.
Why are you passing a "DefaultTableModel" as a parameter to the method. You never use that variable. Get rid of the parameter if it is not needed!
There is no need to invoke updateUI(). The method is invoked internally on a Look and Feel change. The JTable will repaint itself when the model is added to the table.
Did you debug your while loop? I don't believe it will work. When using the DBUtils it will loop through the ResultSet to add the data to the TableModel. So when you execute that code you are already at the end of the ResultSet.
In any case I don't understand what you are attempting to do with the loop. All the data has already been added to the TableModel. I don't know why you would attempt to add more rows.
but the table doesn't show the data from my database
Well that is your first step. Display the data from the database and forget about the custom renderer/editor. Solve one problem at a time.
Once you get the data displayed, there is still no reason to use a custom renderer/editor because Swing already provides defaults. The problem is that you need to override the getColumnClass(...) method of your JTable to return the Class of each column so the table can choose the apppriate renderer/editor for the column.
For an example of how to do this see: Java, changing boolean column to checkbox in jTable when using rs2xml for populating jTable
Related
I am currently using a JTable to display a few patient details. I have a JCheckBox, that when ticked adds a new column to the table and adds the data to the new column. When it is unticked, it should remove the column, sort of like adding extra filters to the table. However, when I tick the box again after unticking it, it duplicates the columns in the table.
I tried to use the fireTableStructureChanged() method, but it would never update the table and so the columns stay there even if the checkbox was unticked. But if I remove it, then when I untick the the checkbox it works, but then the duplication problem comes back. I pasted my actionPerformed() method for when the checkbox is ticked. The first image is of my table before I click my Checkbox. The second is when I click the Checkbox and the column is added. Lastly the third is when I untick the checkbox and tick again. Any help will be much appreciated.
#Override
public void actionPerformed(ActionEvent e)
{
if (gui.getChkCholesterol().isSelected() == true)
{
try {
gui.getRightTableModel().addColumn("Cholesterol");
newColumnIndeces.put("2093-3",gui.getRightTable().getColumnCount()-1);
gui.getRightTableModel().addColumn("Cholesterol Effective Date/Time");
newColumnIndeces.put("2093-3e",gui.getRightTable().getColumnCount()-1);
if(gui.getRightTable().getRowCount()>0)
{
int columnNumberValue = newColumnIndeces.get("2093-3");
int columnNumberDate = newColumnIndeces.get("2093-3e");
for (int i = 0; i<gui.getRightTable().getRowCount(); i++)
{
String value = op.getPatientObservationValue(gui.getRightTable().getValueAt(i,0).toString(),"2093-3");
String date = op.getPatientObservationEffectiveDate(gui.getRightTable().getValueAt(i,0).toString(),"2093-3");
gui.getRightTableModel().setValueAt(value, i, columnNumberValue);
gui.getRightTableModel().setValueAt(date, i, columnNumberDate);
}
}
} catch (ParseException parseException) {
parseException.printStackTrace();
}
setCellColourForTable();
timer.schedule(new RefreshTable(), 0, seconds);
}
else
{
gui.getRightTable().removeColumn(gui.getRightTable().getColumnModel().getColumn(newColumnIndeces.get("2093-3")));
gui.getRightTable().removeColumn(gui.getRightTable().getColumnModel().getColumn(newColumnIndeces.get("2093-3e")-1));
newColumnIndeces.remove("2093-3");
newColumnIndeces.remove("2093-3e");
timer.cancel();
timer = new Timer();
}
gui.getRightTableModel().fireTableStructureChanged();
}
You add a column to the model by using:
gui.getRightTableModel().addColumn("Cholesterol");
This will notify the view that the data has changed and the table will also be updated.
However, you remove the column from the table using:
gui.getRightTable().removeColumn(…);
This only removes the column from the "table view". The column has not been removed from the DefaultTableModel. So the next time you add a column it just gets added to the end of the DefaultTableModel.
So the solution is to remove the column from the DefaultTableModel, not the table.
Unfortunately there is no removeColumn(…) method.
However because your requirement is to only add/remove columns from the end of the DefaultTableModel you can use:
model.setColumnCount(model.getColumnCount() - 2);
which will effectively remove the last two columns from the model.
The other option is to not keep adding columns to the DefaultTableModel, but instead you can just add/remove TableColumns from the TableColumnModel directly. So this would mean the data for the Cholestoral column would always be in the model, but the view would just not display the columns.
So instead of using the model.addColumn(…) you would use the table.addColumn(…) method.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Load/refresh jTable everytime I inserted new set of data to arrayList
Already tried those lines I've put on comments but still nothing changed. Consider that adding of those data to arrayList are performed in separate class.So here, Action performed in a jButton so everytime I add set of data from jTextfields and stores it as Object to arrayList, only the first set of columns and rows I inserted to the arrayList are the only displayed data in jTable. Although the second set of data I inserted after clicking the jButton twice was saved to arrayList, the jTable didn't even refresh or load the new data I inserted. Any help will be appreciated
//BUTTON ACTION PERFORMED
DefaultTableModel tableModel = (DefaultTableModel) MyJTable.getModel();
tableModel.fireTableDataChanged();
//tableModel.setRowCount(0);
//tableModel.repaint();
//int rowCount = tableModel.getRowCount();
//for(int i = rowCount -1; i >= 0; i--){
//tableModel.removeRow(i);
//}
Object[][] displayOnTable = new Object[Data.data.size()][4];
for(int x=0; x < Data.data.size(); x++){
displayOnTable[x] = Data.data.get(x);
}
MyJTable.setModel(new javax.swing.table.DefaultTableModel(
disp,
new String [] {
"ID", "Name", "Gender", "Age"
} ));
// --- Declared in Separated class----
//public class Data {
// public static List<Object[]> data = new ArrayList<>();
// }
// ------------------------
Already tried those lines I've put on comments but still nothing changed.
Well start with something really simple.
Add two lines to your ActionListener:
DefaultTableModel tableModel = (DefaultTableModel) MyJTable.getModel();
tableModel.setRowCount(0);
If nothing happens to the data in the table, then it means you don't have a reference to the table that is visible on the frame. So you need to solve this problem.
Did the data in the table clear? If so this is a good sign as it means your code is referencing the TableModel of the JTable that has been added to the frame.
So now you can add data from the ArrayList.
The basic logic would be:
System.out.println( data.size() ); // to verify you actually have data in the List
for (Object[] row: data)
model.addRow( row );
That's it. No need to create a new TableModel. No need to create a 2D array. You will simply iterate through the ArrayList adding one row of data at a time to the model. Then the table will automatically repaint() itself.
Of course this will only work if you have data in the ArrayList.
well i am making this system that has a table, and i have to put buttons in the last column. i've been researching but all the codes i saw are really confusing. there is one tho, but there are still some parts that i didn't understand. here's the site where i got it http://www.javaquery.com/2013/05/how-to-implement-jbutton-in-jtable.html
String[] InvoiceArray = new String[20];
//Declare above variable globally. Used by two-three methods. Change variable name as per your need.
/*
* import the ButtonColumn class if you are not working in IDE
* I used formWindowOpened event to load content in Jtable but you can use other event.
* All you need is put the code with in that event.
*/
private void formWindowOpened(java.awt.event.WindowEvent evt) {
Object[][] rowData = new Object[4][2]; // 4: is number of row ; 2: is number of column
Object columnNames[] = {"Invoice No", "View Report"}; // Name of columns
for (int i = 0; i < 4; i++) {
InvoiceArray[i] = i + "-2345";
rowData[i][0] = i + "-2345";
rowData[i][1] = "View Order " + i; // Can change the text of button.
}
DefaultTableModel tm = new DefaultTableModel(rowData, columnNames);
jTable1.setModel(tm);
ButtonColumn buttonColumn = new ButtonColumn(jTable1, showOrder, 1); // 1: is column number. column count starts with 0,1,2...
}
what's the InvoiceArray for? and should i make the showOrder from the last line? and also, i didn't understand the code he posted on how to make a listener on it. here it is:
Action showOrder = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
//JTable table = (JTable) e.getSource(); // If you have multiple component following the ActionEvent
int modelRow = Integer.valueOf(e.getActionCommand());
if (InvoiceArray[modelRow] != null) {
/* We are placing invoice no in array
* And track the button click index
* And fetch index in invoice no
*/
System.out.println("Your Invoice No:" + InvoiceArray[modelRow]);
} else {
JOptionPane.showMessageDialog(rootPane, "No records found!");
}
}
};
i know there are some explanations already. i understand some of them but not all. just a simplier way to add jbutton on jtable and also listeners for the jbutton. thank you so much
just a simplier way to add jbutton on jtable and also listeners for the jbutton.
There is no simple way. You need to understand how renderers and editors work in a JTable. Read the section from the Swing tutorial on Concepts: Renderers and Editors for the basics.
Then you can check out Table Button Column which does the hard work for you. You only need to provide the Action to be invoked when you click on the button.
what's the InvoiceArray for?
It is used to load data into the JTable. This is basic usage of a JTable and has absolutely nothing to do with adding a button to a column of the table.
After the data is loaded you should forget about the invoiceArray. The Action you write should access the data via the TableModel or the JTable.
i am trying to add an image that is in my database in a column in JTable
with a number and a code special to this image the problem is i got the number and the code but the image doesn't show i don't know why here is my code
String sql="SELECT id,chiffre,image FROM symbolique WHERE id BETWEEN 200 and 205";
try{
st=connexion.prepareStatement(sql);
res=st.executeQuery();
while(res.next()){
int j=0;
String [] code= new String[1000];
String [] chiffre1=new String[100];
code [j] = res.getString("id");
chiffre1[j] = Integer.toString(res.getInt("chiffre"));
Blob blob = res.getBlob("image");
is2 = new BufferedInputStream(blob.getBinaryStream());
Image raw = ImageIO.read(is2);
ImageIcon icon =new ImageIcon(raw);
Object [][] data = new Object[200][100];
data[j][1]= code [j];
data[j][2]= chiffre1[j];
data[j][3]=icon;
j++;
String title[] = {"image", "chiffre","code"};
DefaultTableModel model = new DefaultTableModel(data, title);
table.setModel(model);
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);}
}
put Icon/ImageIcon to JTable better to the TableModel directly
you have to override getColumnClass for Icon/ImageIcon in XxxTableModel for showing this Object as image in the JTables view
for detailed informations (incl. working code examples) to read official Oracle tutorial - How to use Tables
This won't help solve the image problem but right now your code recreates the TableModel for every row in the ResultSet, which means you will only ever see one row of data.
Since you have looping code you need to create an empty DefaultTableModel before the loop starts.
Inside the loop you then use the addRow(...) method to add another row of data to the TableModel for every row in the ResultSet
When you finish the loop then you use the model to create the JTable.
Did you add any debug code? Did you attempt to display the width/height of the image to confirm that you are reading the image properly? Why don't you do a simple test that tries to read and display an image in a JLabel first since using a JTable is more complicated than using a JLabel. Then once you get that working you can apply the knowledge to using a JTable.
I have an ArrayList called conditionList which stores condition names.
Whenever one is added/edited or deleted, the Lists on my GUI update no problems.
You can see below that i use 2 models... a DefaultListModel called condListModel and a DefaultComboBoxModel called conditionModel.
The code i have below is for the method editCondition(), at this stage the text is already changed on the GUI and is being submitted here. On my GUI, after ive submitted the change, the ComboBox and JList change no problem so im sure that the model changes are correct.
HOWEVER MY PROBLEM IS: When i save the ArrayList conditionList through serialization, and then load it back up, the change is gone. SO I think there is problem in my code with changing the String Value in the ArrayList(named conditionList), can anyone have a look and see if u notice an issue
String conString = jListCondition.getSelectedValue().toString();
for(String c: conditionList)
{
if(conString.compareTo(c) == 0)
{
String temp = entConName.getText();
c = temp;
//edit the Condition jList model
int x = condListModel.indexOf(conString);
condListModel.setElementAt(temp, x);
jListCondition.setModel(condListModel);
//edit the Condition comboBox model
int i = conditionModel.getIndexOf(conString);
conditionModel.insertElementAt(temp, i);
conditionModel.removeElement(conString);
entCondition.setModel(conditionModel);
//reset buttons
editConConfirm.setEnabled(false);
editCon.setEnabled(false);
deleteCon.setEnabled(false);
entConName.setText("");
addCon.setEnabled(true);
}
}