How to fix jtable rendering always only (and at least) 4 rows? - java

My program should translate the pairs of key/value of a table to a escaped JSON for shell execution. This JTable is inside a JSplitPane, but only 4 rows are displayed and when there is less than 4 a white background appears where the other 1-3 rows should be. (There are more than 4 registers)
e.g.:
With data:
Without data:
As you can see the JSON on the bottom has all the table data, but the JTable is not rendering it right. Table filling code:
DefaultTableModel dtm = ((DefaultTableModel) tblParameters.getModel());
dtm.setRowCount(0);
currentFileParameters = (ReplacerLinkedHashMap<String, String>) FileParametersDAO.getInstance().getParametersForFile(file); // get parameters from database for specific file
for (Map.Entry<String, String> entry : currentFileParameters.entrySet()) {
dtm.addRow(new Object[]{entry.getKey(), entry.getValue()}); // add row
}
dtm.addRow(new Object[]{"", ""}); // add blank row for adding more parameters
tblParameters.revalidate(); // failure trying to resolve the problem [part 1]
tblParameters.repaint(); // failure trying to resolve the problem [part 2]
LocalProperties.setLastAccessedFile(file); // saves the accessed file
loadSavedFiles(); // load all saved files to other table (that has the same problem)
Table was working before and stopped suddenly. Thank you in advance.

Related

Zkoss-8.5.1 Problem creating data binding programmatically

I'm creating a zk component for mount easy grid from view database. One of its powerful features is the automatic generation of filters by column. These filters appear automatically on the top column inside the auxheader tag and all are generated programmatically in java with the column info. Example of database view TIMESTAMP column (sorry not css right now):
https://prnt.sc/ZK9nMws2l68p
My problem now is that I don't know how to bind these datebox generated programmatically in java with the view for recovering the filter values.
I was searching inside the zkoss wiki and only found this:
create data binding programmatically,
where you can find how bind zk components of the zul, or how bind components generated programatically if the parent has an item render option.
But i need generate N datebox, numberbox, textbox depending on the number and type of the columns and recover the values of it.
Anyone knows how do it?
The component has a loot of code and scrap, but more or less the idea is:
https://pastebin.com/Q5VX472L
cols.setParent(gridDatatable);
// create filters inside AuxHeads
Auxhead auxhead1 = new Auxhead();
// create a line with the same number of AuxHeaders as Columns we have
for (int indexColumn = 0; indexColumn < columnsList
.size(); indexColumn++) {
if (columnsList.get(indexColumn).getVisible()) {
// i have definned the diferent Auxheader with a patron factory
Auxheader auxheader = gridDatatableFilterFactory.generateFilter(
columnsList.get(indexColumn).getTerms().getType(),
columnsList.get(indexColumn).getTerms().getKey());
auxheader.setParent(auxhead1);
}
}
// set the Auxhead line between col names and rows
auxhead1.setParent(gridDatatable);
// we add the rowrender for print the rows
gridDatatable.setRowRenderer(
new DatatableRowRender(columnsList, gridDatatableCellFactory));
search();
Thank you in advance, regards.

Column stretch type issue when data overflows to next page

I created a report with dynamic columns using dynamic reports. If any column in the last row is overflows then the only the overflowing column is stretched and printed on next page. Rest of the columns is not stretched.
The printed report is look like this:
Following section of code is used for creating report with dynamic columns.
JasperReportBuilder jasperReportBuilder=DynamicReports.report();
for(Field field:fields){
for (Entry<String, String> entry : dynamicTableColumns.entrySet()) {
if ( entry.getKey().equals(field.getName())){
jasperReportBuilder.columns(DynamicReports.col.column(entry.getValue(), field.getName().toString(), DynamicReports.type.stringType()).setStretchWithOverflow(true));
}
}
}
I haven't seen any option to set the column's stretch type as RELATIVE_TO_TALLEST_OBJECT. Is there any other way to fix this ?
Setting detail's split type as 'PREVENT' will prevent the row from stretching to next page and move the entire row to next page.
jasperReportBuilder.setDetailSplitType(SplitType.PREVENT);

JTable with updated values after a file search is performed

I'm trying to create a program that searches a known file path for some files. If the search finds the file, I'd like for the program to update a JTable so that it removes that file from the table.
The point is for the table to display files that aren't yet in this certain folder.
edit: "Philly" and "L2" columns are server names. The rows are workstations that are on those servers. The two relate such that when the work has been completed on the workstations, the person who has completed that work takes a screen shot with "Snipping Tool", which is then saved as a .PNG file in a designated folder. The files are saved like... If server 1, workstation 1 was completed, it would be saved as "1-1 MyInitials.PNG" inside of the designated folder.
The idea of using an automatic TimerTask is a great idea, although I'm wholly unfamiliar with it.
You could use the DefaultTableModel of the JTable to search what is in table for a file name match.
Iterate through the table rows and read the value within the cell
column that contains the file name. You can use the the
DefaultTableModel#getRowCount() method and the
DefaultTableModel#getValueAt() methods for this.
Once you have found a match you would like to remove from table then
you can use the DefaultTableModel#removeRow() method.
A method to do this sort of thing might look something like this (Not Tested):
public void removeIfExist(JTable yourJTable, String fileName) {
// Get Table Model
DefaultTableModel dtm = (DefaultTableModel) yourJTable.getModel();
// Get Table Row Count
int rowCount = dtm.getRowCount();
// Assuming file names are in column 1
// and not in column 0.
int fileNameColumn = 1;
// Iterate through each table row and get the String
// value for what is within the row cell of column 1.
for (int i = 0; i < rowCount; i++) {
// Grab the string value in column 1 of current row.
String stringValue = dtm.getValueAt(i, fileNameColumn).toString();
// Is the cell's String value the same as the file name?
// If you want to ignore letter case for comparing then
// use equalsIgnoreCase() instead of equals().
if (stringValue.equals(fileName)) {
// Yes it is so remove the row from JTable.
dtm.removeRow(i);
// Found a match so no need to check
// further. Get out of loop.
break;
}
// No it's not so check the cell in next
// table row (if there is one).
}
}
You can of course modify this method to handle a list of file names instead of one file name at a time. Again, the code above is not tested and is just real quick off the top of my head but I think it gives you an idea of how it can be done.

Netbeans MVC Swing JTable ResultSet data insert

I'm new to Java, Swing and programming, but I need to make an application. I'm using NetBeans for development and here's my question:
If I drag and drop the JTable to my frame and select custom code for the table, I get this code:
appView.java
tblViewData = new javax.swing.JTable();
tblViewData.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"ID", "Reference No", "Name", "Description"
}
));
jScrollPane1.setViewportView(tblViewData);
What I want to do is populate new Objects[][] with data that I fetch from the appController using ResultSet and the values I set in the appModel for variables ID, Ref, Name and Desc.
Here's a sample code of an insert I attempted to see if I could display a row of data:
new Object [][] {
{ new Integer(model.getID()), model.getRef(), model.getName(), model.getDesc() }
},
This code returns a 0 in the first column of the row.
Am I approaching this problem wrong?
Thanks for the help.
Well I have no idea what the "appController" or "appModel" are. Maybe these are IDE dependent classes? Instead of using the IDE to generate your SQL code I suggest you write your own code and populate the table yourself.
There are plenty of examples on the forum showing how to load a JTable with data from a ResultSet. Just search for examples using those two class names as your keywords.
Or you can check out Table From Database which shows how you can use the DefaultTableModel to load rows of data from the ResultSet.

How to remove item in JTable also remove from Database?

I am pretty basic to link from Java to Database.
I link item from Database (I use MS.Access) to Java table (JTable)
but when I delete in JTable using this code
int numRows = tblweng.getSelectedRows().length;
for(int i=0; i<numRows ; i++ )
((DefaultTableModel)tblweng.getModel()).removeRow(tblweng.getSelectedRow());
it deletes only in Table but not in Database. So how I can remove both of them at the same time I click "Remove Button".
Please guide me but don't go too deep I am just a basic here. Thank in advance.
1. click Jtable // row selected
2. get data form dep_Table like
int a = dep_Table.getSelectedRow();
String b = String.valueOf(dep_Table.getValueAt(a, 1));
3. What data you want in you get and store in String
4. Connect Database
5. use Delete Query and Delete Data From Database
6. Reload Table Again
These Few basic Step is Enough for You.. I Think..

Categories