i was setting the number of rows of my Table in java using properties of the table but how can i add new row inside the code because i don't know the number of inputs that should be entered?
but how can i add new row inside the code
This will depend on the implementation of the TableModel, for example, the DefaultTableModel provides two addRow methods.
The TableModel itself doesn't provide this functionality directly and is dependent on the physical implementation to provide this functionality if and when required.
See How to Use Tables for more details
first, i set the number of rows equal "0" in the properties of the table
second, write this code in the for loop with your condition
ArrayList arr = new ArrayList();
for (int i = 0; i < shipmain.files.length; i++) {
arr.add(shipmain.files[i]);
arr.add(shipmain.fabricName[i]);
arr.add(shipmain.color[i]);
DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
model.addRow(arr.toArray());
jTable1.setModel(model);
arr.remove(shipmain.files[i]);
arr.remove(shipmain.fabricName[i]);
arr.remove(shipmain.color[i]);
}
AddRow() function must take an object and ArrayList provided that
Related
I would like to populate more than one JTable using the code below. The amount of columns in the 2D array can be variable. So I would like to use a method that accepts an integer to indicate the number of columns to add in the following line:
tableModel.addRow(new Object[]{arr[i][0], arr[i][1]});
I'm trying to figure a way to add (for each new JTable) a specified number of arr[i][0], arri, to a table determined by a method argument.
DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
for (int i = 0; i < arr.length; i++) {
tableModel.addRow(new Object[]{arr[i][0], arr[i][1]});
}
Is this possible?
This is linked in a way to my previous question: How to populate a JComboBox using a method
In order for data to be displayed in a JTable, you must have a column defined in the TableColumnModel of the table.
So you need to define your DefaultTableModel with the maximum number of columns that is possible.
Then you just use the addRow(...) method. The DefaultTableModel will automatically pad the row with null values up to the maximum number of columns.
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.
i hav a ArreayList which is contain PRIvariable(name of the class) class data. Below shows my part of the java code. So now i want to put this Arraylist data to Jtable. how can i do that. Here i already added pri.dateText , pri.sum , pri.count to ar(arraylist)
PRIvariable pri=new PRIvariable();
while (reader.ready()) {
String line = reader.readLine();
String[] values = line.split(",");
if(values[2].equals(pri.incDate)){
if(values[4].equals("KI")){
pri.dateText=values[2]+" "+values[4];
pri.count=pri.count+1;
pri.sum = pri.sum+Integer.parseInt(values[7]);
}
}
}
System.out.println(pri.dateText+" "+pri.sum+" "+pri.count);
ar.add(pri);
Like all Swing components, a JTable relies upon the MVC pattern (at multiple levels, but that's not the subject).
You have one view (the JTable), one model (I'll come back on it later), and a controller (implemented here as a set of event listeners : one controller for each kind of control).
The array you have could be a good model starting point. However, Swing provides far better way to inject your data in JTable. Indeed, a JTable uses as model an implementation of TableModel. Hopefully, there already exist an implementation : DefaultTableModel.
So, here is what I suggest to you : create DefaultTableModel, put in its rows/columns all the data you want to display in your table, then call JTable#setModel(TableModel) to have thze table display your data.
Obviously, you'll soon find various misfits between DefaultTableModel and what you want to do. It will then be time for you to create our very own table model. But that's another question.
Besides, don't forget to take a look at Swing tutorial, it's usually a good thing when dealing with Swing components.
I don't see where you have an ArrayList anywhere.
First you create a PRIvariable object. Then you keep looping as you read a line of data from the file. For each line of data you split it into individual tokens and then add some of the token data to the PRIvariable ojbect. The problem is that you only have a single PRIVariable object. So every time you read a new line of data you change the values of the PRIvariable object. After all the looping you add this single PRIvariable object to your ArrayList, but you will only ever have one object in the ArrayList.
The easier solution is to update the TableModel as you get the data. Something like:
DefaultTableModel model = new DefaultTableModel(...);
JTable table = new JTable( model );
...
...
while (reader.ready())
{
String line = reader.readLine();
String[] values = line.split(",");
String[] row = new String[3];
row[0] = values[?];
row[1] = values[?];
row[2] = values[?];
model.addRow( row );
}
Look into GlazedLists. They make it extremely easy to create a suitable TableModel object from your list.
ArrayList< PRIvariable> myList = new ArrayList<PRIvariable>();
... fill up the list ...
// This assumes your object has a getName() and getAge() methods
String[] propertyNames = {"name","age"};
String[] columnLabels = {"Name","Age"};
// Are these columns editable?
boolean[] writeable = {false, false};
EventList<PRIvariable> eventList = GlazedLists.eventList(myList);
EventTableModel<PRIvariable> tableModel = new EventTableModel<PRIvariable>(eventList,propertyNames,columnLabels,writable);
JTable table = new JTable(tableModel);
I have designed one GUI in which I have used one JTable from which I have to make 2 columns invisible . How should I do that ?
Remove the TableColumn from the TableColumnModel.
TableColumnModel tcm = table.getColumnModel();
tcm.removeColumn( tcm.getColumn(...) );
If you need access to the data then you use table.getModel().getValueAt(...).
For a more complex solution that allows the user to hide/show columns as they wish check out the Table Column Manager.
First remove the column from the view
table.removeColumn(table.getColumnModel().getColumn(4));
Then retrieve the data from the model.
table.getModel().getValueAt(table.getSelectedRow(),4);
One thing to note is that when retrieving the data, it must be retrieve from model not from the table.
I tried 2 possible solutions that both work, but got some issue with the 1st solution.
table.removeColumn(table.getColumnModel().getColumn(4));
or
table.getColumnModel().getColumn(4).setMinWidth(0);
table.getColumnModel().getColumn(4).setMaxWidth(0);
table.getColumnModel().getColumn(4).setWidth(0);
In my recent case, I preferred the 2nd solution because I added a TableRowSorter.
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
table.setRowSorter(sorter);
When using table.removeColumn(table.getColumnModel().getColumn(4)), it will physically remove the column from the view/table so you cannot use table.getValueAt(row, 4) - it returns ArrayIndexOutOfBounds. The only way to get the value of the removed column is by calling table.getModel().getValueAt(table.getSelectedRow(),4). Since TableRowSorter sorts only what's on the table but not the data in the DefaultTableModel object, the problem is when you get the value after sorting the records - it will retrieve the data from DefaultModelObject, which is not sorted.
So I used the 2nd solution then used table.getValueAt(table.getSelectedRow(),4);
The only issue I see with the 2nd approach was mentioned by #camickr: "When you set the width to 0, try tabbing, when you hit the hidden column focus disappears until you tab again. This will confuse users."
i had the same problem and because of i am using TableColumnModel removColumn(); does'not help me so i used this
table.getColumnModel().getColumn(0).setWidth(0);
table.getColumnModel().getColumn(0).setMinWidth(0);
table.getColumnModel().getColumn(0).setMaxWidth(0);
and worked fine for me it hide a column 0 and i still able to get value from it
Set the min, max and "normal" width to 0:
jTable.getColumn("ABC").setMinWidth(0); // Must be set before maxWidth!!
jTable.getColumn("ABC").setMaxWidth(0);
jTable.getColumn("ABC").setWidth(0);
Note: Since you can't set a maxWidth < minWidth, you need to change minWidth, first (javadoc). Same is true for width.
The second approach is to extend TableColumnModel and override all the methods to create the illusion (for the JTable) that your model doesn't have those two columns.
So when you hide a column, you must return one less when the table asks for the number of columns and when it asks for the column X, you may have to add +1 to the column index (depending on whether it is to the left or right of the hidden column), etc.
Let your new table model forward all method calls (with the corrected indexes, etc) to the actual column model and use the new table model in the JTable.
If you remove the column from the JTable the column is still present in the TableModel.
For example to remove the first ID column:
TableColumnModel tcm = table.getColumnModel();
tcm.removeColumn(tcm.getColumn(0));
If you want to access the value of the removed column, you have to access it through the getValueAt function of the TableModel, not the JTable. But you have to convert to rowIndex back to rowIndex in the model.
For example if you want to access the first column of the selected row:
int modelRow = table.convertRowIndexToModel(table.getSelectedRow());
int value = (Integer)table.getModel().getValueAt(modelRow,0);
I have tried them all: they don't help. The BEST way ever is to make a new table model without the column you want to delete. This is how you do it:
table = (DefaultTableModel) <table name>.getModel();
DefaultTableModel table1 = new DefaultTableModel();
Vector v = table.getDataVector();
Vector v1 = newvector(v,<column index you want to delete>);
Vector newvector(Vector v,int j){
Vector v1= new Vector();
try{
Vector v2;
Object[] o = v.toArray();
int i =0;
while(i<o.length){
v2 = (Vector) o[i];
v2.remove(j);
v1.add(v2);
i++;
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"Error in newvector \n"+e);
}
return v1;
}
Vector getColumnIdentifiers(int i) {
Vector columnIdentifiers = new Vector();
int j=0;
while(j<i){
columnIdentifiers.add(("c"+(j+1)));
j++;
}
return columnIdentifiers;
}
table1.setDataVector(v1,getColumnIdentifiers((<column count>-1)));
<table name>.setModel(table1);
You could create a subclass of the model, and override TableModel.getColumnCount as follows:
int getColumnCount() {
return super.getColumnCount()-2;
}
The last two columns would then not be displayed in the JTable.