How to search and manipulate data on jTable in java in netbeans - java

[i need help in java programming in netbeans.
suppose i have 2 jTable. the first table contains the imported data from the excel file (I do not use data in the database). And the second table serves to display data calculation results from the first table earlier.
questions asked are:
how to find the value of MAXIMUM and MINIMUM in each column, starting from column two, third and so on. and displayed eg in jTextfiield?
how, for example I have a sum formula, which serves to add all the values ​​in row 1 in columns 2,3,4, and so on in the first table. then the formula will be reused on the second line, then and so on for each row in the first table, according to the number of rows in the first table. such as in excel, (which when we have a certain formula we live just copy and drag the cursor down, then automatically all the values ​​on all rows summed)?
lastly how to display all the data in the first table along with the result of the sum in each line before the previous formula, in the second jTable?
please help, just info I use java in netbeans. and for the image of his table I attach also along with this post. thanks.]1

Related

Shifting rows only in specific columns

I'm trying to shift rows down in my excel sheet and insert new data to the top of the list.
I'm able to shift rows down as:
int row = getItemsListSize(); <- which returns size of the list which contains elements
sheet.shiftRows(1, sheet.getLastRowNum(), rows);
Then I insert new data starting from row 1 and that way newest data is always on top of the list and older data gets moved down.
My problem is that I have second table also on that sheet and amount of items inside it isn't same as in first table. Currently also contents of this table gets moved down although no new items aren't added. Because of that it looks weird.
Is there way to only shift rows inside specific columns? For example: Move rows only in columns A-D.
No; from an excel perspective, there is no such thing as two separate tables. It's just one big sheet with rows and columns. Apache POI is a library that gives you access to excel's data structures. It's not a generalized 'mess with sheets' library. You'd have to write the code for such such a thing yourself.
More generally you're misusing excel a bit. The right move usually is to treat data, as, well, data. You should not be having 2 unrelated tables on the same sheet. Use the + button at the bottom left (or the appropriate POI option, which also supports sheets) to add another sheet.
If you want a sheet for visual reasons, make a third sheet, and that is a sheet that your POI code should never touch - it will render the data from the other (data) sheets. You can now add graphs, logos, whatever you want, and you won't run into issues where moving things around a bit 'because it looks nicer visually' then results in your POI-based code being broken.

How to print multiple JTables to a single page when space is available

I have a java application with 2 JTable. The first table is made up of 2 columns and 5 rows always. The second JTable is made up of 4 columns and many columns.
I want to print JTable 1 above JTable 2, then continue to print the rest of JTable. How would I do this?
From a printable page perspective it would look as follows:
Page 1: Has the first table plus as much of the second table that can fit on it.
Page 2 to N: Has the rest of the second table.
I have an application that uses the Book Class to put together the 2 JTables and use a single print Job to print it. But each table starts on a new page. Which is not what I am looking for.
After reading the comments and a bit of research on my own it appears the best route is using Jasper Reports

(Java) Jtable Operations and Exceptions

I just got this Jtable on Netbeans . First I want it to auto-calculate some grades, as you can see it has 3 columns (1st, 2nd and 3rd evaluation). I have to type between 0 and 100, in each row.
In the end, get the total and average. This can be displayed in a Jlabel, or a textfield. But, it has to be displayed real-time (as I'm typing the values).
Also, I can't type data on all the cells, just one for each row. How do I do this?
I know I can change the columns to accept only Integer values, but for everything else, I have no idea how to proceed.
In the end, get the total and average. This can be displayed in a Jlabel, or a textfield. But, it has to be displayed real-time (as I'm typing the values).
Well, you would want to update "after" the user has finished editing a cell and saves the value to the table.
So you can use a TableModelListener. You add the TableModelListener to the TableModel. Then when the data is saved an event will be generated and you can recalculate the values and update the label.
Check out the following for a simple example to get your started: TableModelListener and multiple column validation. Your logic in the listener will change based on your exact requirement.
You can do this by attaching a defaultTableModel to your JTable. After attaching TableModel to your JTable. You can use command JTable.addRow(Object[]) to add rows to table.

How to update JTableModel after the sorter has changed the rows order?

I have implemented a JTable, on which I can search entries with a textField.
When a request is made, my code gets the DefaultTableModel, looks for the element in each column and row, and then sets the selection in the table to the line where the element was found.
This works, but it's useless if I sort the table by clicking on any column, because the sorting doesn't seem to update the DefaultTableModel.
The table is part of a bigger project which is extremely complicated and full of dependencies so I cannot post a small example, but I think this will sum it up:
Given a DefaultTableModel A full of non-sorted data about a JTable B, where B.setAutoCreateRowSorter() is true , how does one update B after several/any cloumn-sortings of A?
I have read the docs and also looked into this:
http://www.codejava.net/java-se/swing/6-techniques-for-sorting-jtable-you-should-know
As well as dug a bit into TableRowSorter#addRowSorterListener, but it can only tell me that a column was sorted, not really useful. Unless of course I use what that column is to try and sort all the values in a multidimensional array, then clear the table, then assign everything back.. but clearly this is extremely slow for and not really an option in my case.
Refer to this for the info provided by a RowSorterEvent:
https://docs.oracle.com/javase/7/docs/api/javax/swing/event/RowSorterEvent.html
Can someone point me in the right direction ?
When a request is made, my code gets the DefaultTableModel, looks for the element in each column and row...
So don't search the TableModel. You can use the table.getValueAt(...) method to search for the element in each row/column of the table. The data will be accessed in the currently sorted order of the table.
because the sorting doesn't seem to update the DefaultTableModel.
Correct, only the View (JTable) is updated.
If you want to keep searching the TableModel directly then you need to convert the model indexes to the view indexes whenever you want to invoke a JTable method (ie. selecting a table row). This is done by using the following JTable methods:
int columnColumn = table.convertColumnIndexToView(modelColumn);
int row = table.convertRowIndexToView(modelRow);
There are also methods to convert the view values to the model values.

Java TableItem, TableEditor, texts and buttons - all dynamically created

I want to create a table (that will finally exist in a new eclipse view). This table has a few text columns, and a column that has a different behavior based on the other columns. It can either be a field to enter text, or to popup a dialog for users to select data from (and it has to be a popup or a dynamically created combo). Anyway, I know which of the two I need for each row in the column.
My table is built using TableViewer and a few TableViewerColumn's in it. I later fill them with String data in TableItem's up to 4 columns. The fifth is the one I have this special behavior.
I tried experimenting with TableEditor, but failed:
When I used one for each TableItem it somehow got misaligned with the table rows and columns
When I use one for the entire table, I fail to set a Text entry on specific rows
What I need help with is figuring out exactly how to achieve this:
A Table that has 4 String columns, where the data is constant (cannot be changed by the user)
A fifth column where the cell contents is either a Text for the user to enter, or a (preferably transparent) Button that has a popup as action, or a Combo that I dynamically fill with data upon table creation
Any idea would be highly appreciated.
OK, got this figured out. The example here helped a lot:
http://www.java2s.com/Tutorial/Java/0280__SWT/TableCellEditorComboTextandButton.htm
Oren

Categories

Resources