I want to add db(MySql) data to a single specific column in jTable when there is three columns. From this below coding the data automatically add to the 1st column, but I want to add it to the 2nd column in Jtable. Please help me ..i'm new to netbeans !!!
Connection con = Driver.connect();
ResultSet rst = Handler.getData(con, "select lec_name from lecturer");
DefaultTableModel dtm = (DefaultTableModel)jTable1.getModel();
while (rst.next()) {
Object ob []= {rst.getString(1)};
dtm.addRow(ob);
}
Each element in the Object array is a column. This means, you simply means you just need to fill the row array with the correct values
Object ob []= {rst.getString(1), rst.getString(2), rst.getString(3)}};
dtm.addRow(ob);
This of course assumes you've added the appropriate columns to the model in the first place
Related
I saw this Need to add JCheckBox In JTable Dynamically but it didn't help at all seeing my situation is similar but I am not sure how to add the JCheckBox after I have take the raw data from my database.
public void fillAnyTable(ResultSet resultSet, JTable table)throws SQLException{
//Create new table model
DefaultTableModel tableModel = new DefaultTableModel();
//Retrieve meta data from ResultSet
ResultSetMetaData metaData = resultSet.getMetaData();
//Get number of columns from metadata
int columnCount = metaData.getColumnCount();
//Get all column names from metadata and add columns to table
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++){
tableModel.addColumn(metaData.getColumnLabel(columnIndex));
}
//this is when I assume I can create a new column but now how to add the checkbox
tableModel.addColumn("Check");
//Create Array of Objects with size of Column count from metadata
Object[] row = new Object[columnCount];
//Scroll through Result Set
while (resultSet.next()){
//Get object from column with specific index of result set to array of objects
for (int i = 0; i < columnCount; i++){
row[i] = resultSet.getObject(i+1);
}
System.out.println(Arrays.toString(row));
//Now add row to table model with that array of objects as an argument
tableModel.addRow(row);
}
//Now we add the table model to the table
table.setModel(tableModel);
}
}
This is what the view looks like
I am not sure how to add the JCheckBox after I have take the raw data from my database.
You don't add a JCheckBox. The TableModel holds data, not components. So you need to:
override the getColumnClass(...) method of the TableModel to return Boolean.class for the column where you want the check box. Then the table will use the proper renderer/editor for a check box.
add Boolean.FALSE to the TableModel when you add the data from the ResultSet.
So you just add the Boolean value when you are finished adding each column value to your row array:
//Now add row to table model with that array of objects as an argument
row[columnCount].add( Boolean.FALSE );
tableModel.addRow(row);
Of course you will also need to make your "row" array larger to hold this value.
I tried to create a jTable by adding a column of type boolean, to tick the wanted rows. And get them into another similar jTable.
I used jTable1.getModel().setValueAt(int, int, int); but can't put selected String values to the value parameter.
Can someone help me please?
I used jTable1.getModel().setValueAt(int, int, int); but can't put selected String values to the value parameter.
Sure you can, the signature is setValueAt(Object, int, int). The first parameter is Object, not "int" so you can put any Object into a TableModel.
If you are talking about adding new rows of data to the second table then you need to use the addRow(...) method of the DefaultTableModelof your table. That is, the DefaultTableModel will initially contain no data so you can't just use the setValueAt(...) method. Instead you need to add a new row of data for every row that is selected in the first table.
If you need more help than post your SSCCE that demonstrates the problem.
JTable is very flexible and could add and delete any row you desire.
The number of columns that constitute a row may vary from table to another so JTable has a model object through which you can manipulate the rows of the table.
The JTable will have a DefaultTableModel. You can add rows to the model with your data.
If you get the column values of a selected row of a JTable for example:
String obj1 for row1, String obj2 for row2, String obj3 for row3, ...ect
OR IF YOU NEED BOOLEAN :
Boolean obj1 for row1, Boolean obj2 for row2, Boolean obj3 for row3, ...ect
Then you can make the following:
Object[] row = { obj1 , obj2 , obj3 };
You get the object model of the destination table as follows:
DefaultTableModel model = ( DefaultTableModel ) jTable1.getModel();
Then add the manually created row to it:
model.addRow( row );
And that's it!
the size of table limit is set as static limit and i want to change that to dynamic
here jtable & object declared.
public JTable issuetable = null;
static Object[][] data;
here is my jtable
public JTable getIssues() {
issuetable = new JTable();
String[] colName = {"Member", "Book", "Issue Date", "Return Date ",
"Remarks" };
List<Issue>issues=ServiceFactory.getIssueServiceImpl().findAllIssue();
the size of issuedata is limited to 100000 .. i want to change the limit to dynamic..
data=new Object[issues.size()][100000];
for(Issue issue:issues){
data[i][0]=issue.getMemberId().getName();
data[i][1]=issue.getBookId().getName();
data[i][2]=issue.getIssueDate();
data[i][3]=issue.getReturnDate();
data[i][4]=issue.getRemark();
data[i][5]=issue;
i++;
}
if u know answer please share here..
In your previous question, You were using a DefaultTableModel. Keep in mind, a TableModel is a data structure in itself. There is no need at to store the data in two data structures, i.e. your data[][] and the DefaultTableModel. The underlying structure of DefaultTableModel is a dynamic Vector of Vectors.
What you can do is this. Just declare your DefaultTableModel with 0 rows, using this constructor
public DefaultTableModel(Object[] columnNames, int rowCount)
Then just add rows dynamically to the structure with
public void addRow(Object[] rowData) - Adds a row to the end of the model. The new row will contain null values unless rowData is specified. Notification of the row being added will be generated.
So basically, your declaration will be like this
String[] colName = {"Member", "Book", "Issue Date", "Return Date ", "Remarks" };
DefaultTableModel model = new DefaultTableModel(colName, 0);
JTable table = new JTable(model);
Then just add rows like
String member = "Stack";
String book = "overflow";
Data issueDate = date;
....
Object[] row = { member, book, issueDate, returnDate, remarks };
DefaultTableModel model = (DefaultTableModel)table.getModel();
model.addRow(row);
Please read the DefaultTableModel api documentation to see more constructors and methods available
Instead of an array, use a dynamically resizable data structure in your implementation of AbstractTableModel. This EnvDataModel is an example that contains a Map<String, String>.
Instead of copying all the data from your List to the DefaultTableModel you can use your List as the data structure for a custom TableModel. Then you can add/remove Issue object from this TableModel.
See the JButtonTableModel.java example from Row Table Model for an simple example of how the RowTableModel can be extended to give you this functionality.
Using this approach the data is only ever in one place and you can access Issue objects directly from the TableModel.
I have a JTable, which is a DefaultTableModel. The data in the table is accessed from the ArrayList CarList. The problem I am having, is that after I delete the row, it is only deleted temporarily. To delete the row in the project, the user has to select a row and then press the button delete. When I use the coding that I am using, the row is deleted from the jTable, but the data is not removed from my Arraylist, so when I open up the JTable again, the row that I deleted is still there. Can anyone please help me? I have some coding here :
ArrayList CarList = CarRentalSystem.CarList;
UsingFiles CarFile = CarRentalSystem.CarFile; //ArrayLists accessed from the whole project
/**
* Creates new form ViewCars
*/
public ViewCars() { //creating the table and accessing the data from the arraylists
initComponents();
this.CarList=CarList;
DefaultTableModel model = (DefaultTableModel) carTable.getModel();
for (int i=0; i<CarList.size(); i++){
Car car = (Car) CarList.get(i);
model.addRow(new Object[]{car.getCarNum(), car.getManufacturer(), car.getModel(), car.getBooked(), car.getAircondition()});
btnEdit.setVisible(true);
btnDelete.setVisible(false);
btnSave.setVisible(false);
}
//delete button coding
private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel model = (DefaultTableModel) this.carTable.getModel();
int[] rows = carTable.getSelectedRows();
for(int i=0;i<rows.length;i++){
model.removeRow(rows[i]-i);
JOptionPane.showMessageDialog(null, "Row Deleted"); //the row is deleted but the data isn't
}
}
}
Each time you create the JTable model you are taking all of item in your car list. You delete them from the table model, but you never remove the items from your array list. You need to modify your deletion code to remove from the carList.
You should also be naming your variables using lower camel case conventions.
I would create your own table model that contains the car list so that the table model itself can directly remove things from the car list. Unless you have use cases for the car list not being in sync with what you display in your JTable.
Here you are failing to remove the items from ArrayList, therefore every time you create the JTable model all the items from CarList are added to JTable.
You need to add
CarList.remove(Object_To_Remove);
to your delete code.
For more help on how to delete objects from ArrayList see this. Remove Item from ArrayList this question on StackOverflow may help you. Or better way you can directly go for Docs.
JAVA
NETBEANS
// resultsTable, myModel
JTable resultsTable;
DefaultTableModel myModel; //javax.swing.table.DefaultTableModel
myModel = (DefaultTableModel) resultsTable.getModel();
// event of clicking on item of table
String value = (String) myModel.getValueAt(resultsTable.getSelectedRow(), columnIndex)
I use JTable and DefaultTableModel to view a table of various info
and I want to get a value of a certain column of the selected index of the table.
The code I wrote above works fine except when:
I use the sort of the GUI (click on the field name I want to sort on the table)
The table is properly sorted but after that when I select a row, it gets
the value of the row that was there before the sort.
This means that after sorting (using the JTable's GUI)
the 'myModel' and 'resultsTable' objects have different row indexes.
How do I synchronize those two?
You need to use the 'convertXXX' methods on the JTable see the JavaDoc
int row = resultsTable.getSelectedRow();
if (row != -1) {
row = table.convertRowIndexToModel(row);
String value = (String) myModel.getValueAt(row, columnIndex)
A problem with using the JTable.getValueAt() is to get the column you want. When the columns are moved around in the GUI the indexes "change" to match the view. By using the AbstractTableModel.getValueAt() and the JTable.convertXXX() (as outlined by Guillaume) it's just a matter of using the column indexes for the model when retrieving data.
Except from the solution Guillaume gave (Thanks)
I did this:
// resultsTable, myModel
JTable resultsTable;
DefaultTableModel myModel; //javax.swing.table.DefaultTableModel
myModel = (DefaultTableModel) resultsTable.getModel();
// event of clicking on item of table
String value = (String) **resultsTable**.getValueAt(resultsTable.getSelectedRow(), columnIndex)
I used the resultsTable Object instead of the myModel Object to get the value.