(Java) Jtable Operations and Exceptions - java

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.

Related

Update mysql when cell value is edited in JTable

Well I have a JTable in my app that fetches records from a MySql database adn displays them in a sorted order.
Till now I have been using a separate JTextArea and a JComboBox to allow the users to edit the table, something like
so whenever someone clicks on a row in the table the ID of the record is automatically updated below in a JLabel that lies before the JComboBox.
The question is how can I allow users to simply double click and edit values in the cells which would automatically fire a SQL Update query and update the same in the database. I want to allow this on certain Columns only not every Column.
A response with a code example would be highly appreciated.
how can I allow users to simply double click and edit values in the cells which would automatically fire a SQL Update query and update the same in the database.
You can use a TableModelListener to be notified of changes to the TableModel.
I want to allow this on certain Columns only not every Column.
The row/column is found in the TableModelEvent so you check the column that changed before invoking the SQL update.
One potential problem with this is that a TableModelEvent is generated even if the data isn't changed. That is if you place the cell in edit mode and, for instance, tab to the next cell without change the data and event will still be generated.
To get around this problem you may want to consider using the Table Cell Listener. When using this class the event is only generated if the data has actually been changed.

Update values in a cell of a JTable

I want to put subject results in a Jtable. I set DefaultTableModel to the table. In my table I have three columns.Student ID,Marks and Grade.Student ID is auto generated in column one.then user should enter marks for for second column.Then I want to automatically put the necessary grading in 3rd column.I load the grading data from the database and find the correct grading for the marks.I try this with both MouseReleaseEvent and KeyReleaseEvent.But it wasn't successful.Can any one suggest me a better way.thank you.
You don't have to listen for the input events, because the table has cell editors already. So you should be capable of editing your table. The only thing you need to do is to listen for the tableChanged event and update the database there.
See: TableModelListener
In case I was confused by your thoughts about the events and you just want to basically set values of a cell programmatically, try this method: setValueAt(Object,int,int)

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

Related to checkbox in jtable

I am using a table to display data.
I am providing checkbox to each row of a table to perform some operations based on selection. When I did like that, I am able to check multiple rows.
But my requirement is, at any point of time I should check only one checkbox. To be precise, I need the behavior of Buttongroup to all checkboxes in table.
How can I do this?
If you really want to use checkboxes, I assume your TableModel holds a boolean for those checkboxes. It should be trivial to move the logic for the single selection to the TableModel.
If you do not need the checkboxes but just want to operate on the selected rows (see JTable#getSelectedRows), you can adjust the ListSelectionModel which is present on the JTable to only allow for single selection (see ListSelectionModel#SINGLE_SELECTION)
CheckOne is a complete example that simply clears all check boxes in a specific column and sets the new value. This related example uses JRadioButton.

Removing unused Rows from jTable

How do I remove unused rows from a JTable?
I first create a table of a fixed size in a JPanel and then fill elements as needed.
Now I don't want unused rows to be displayed in my table. Please help.
I presently get a table with lots of empty rows in bottom, only top 5-6 rows being used, with rest blank. I want to hide them or remove them somehow
Work the other way around. Start with an empty DefaultTableModel. DefaultTableModel supports an addRow() method. So, as you get data to add to the model use:
model.addRow(...);
Don't let the GUI editor determine how you code your GUI. Adding cleanup code to remove rows is a bad design.
DefaultTableModel has a method removeRow which removes a row from the table. Pass the row index which you want to remove from the table.
Now I don't want unused rows to be displayed in my table.
This I am not sure please post an SSCCE to show us what is the flag that says these rows are not used.

Categories

Resources