How to create jCheckBox depending on number of rows present in database? - java

I want to create a frame in java with variable number of checkboxes which will depend on the number of rows present in my database.
Problems that I am facing are:-
1. How to count the number of rows in database?
2. How and where to store the data present in columns of database?
3. How to create checkboxes with that data present in column of database?
Can anyone help me.??
Looking forward for solution.
Thanks in advance :)

How to count the number of rows in table?
SELECT COUNT(*) FROM table_name;
How and where to store the data present in columns of database?
I would suggest you to use JTable and look the tutorial How to use Table
How to create checkboxes with that data present in column of
database?
Read about Renderer and Editor section to know more about registered cell renderers in JTable.

Related

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

[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

MYSQL boolean return checkbox, not return tinyint

SELECT false as "Select" from Table_Name
The SQL run in derby I got this output:
The SQL run in MYSQL I got this output:
I want to create a JTable with a check box column by using the ResultSet.
It is workable when i use derby, my JTable got a column with check box.
But when I use MYSQL, my JTable got a column with 0.
The problem is found in
Which MySQL data type to use for storing boolean values
So now I'm thinking of:
Is it possible I can configure MYSQL until it can return check box like derby does?
Is there a function in MYSQL that can return boolean data type true/false? Not other data types.
Is it possible I can alter the ResultSet to get the check box?
My final objective is to create a JTable with a check box column as the first column and filled with data SELECT from database. Everyone wants the easy way.
Currently I'm using this method Using Rob Camick's ListTableModel, but JTable doesnt show up
//ListTableModel model = ListTableModel.createModelFromResultSet(rs);
model = ListTableModel.createModelFromResultSet(rs);
Thanks MadProgrammer,
The custom renderer really works, and I found a suitable custom column renderer.
CheckBoxTableCellRenderer.java
My final objective is achieved, to create a JTable with a check box column as the first column and filled with data SELECT from database. Everyone wants the easy way.

Insert JTable data into database

I am facing a problem with JTable when inserting JTable values into the database. For example I have three columns, and user enters the corresponding data into three columns, JTable has several rows and I have one submit button at bottom of the table. After filling the data into all the rows, when the user press the submit button, the data is inserted into the database. This works fine without any problems.
Now consider user enters only 10 rows and he didn't click the submit button, because of some power failure the system shutdown suddenly. Now I want to store these 10 rows in a collection and when the application is started again I want to insert these 10 rows values automatically into the database. Please consider that user information is very critical information. I don't know how to do this.
Rightly said by #madprogrammer you have to take help of temp tables,
just add a action listener to ***jtable***after insertion of row data it will transfer your data into temp tables.
-- after click on submit button, there should be check that if data is present in temp tables, first insert these data into main table.and truncate or delete data from temp tables.
here collection will not work for you.
Hope it may help you.

How to auto-refresh a JTable bound to Mysql database after insert using NetBeans

I'm working on a project in NetBeans.
I have a Jtable which i bound to my database and there is a form that I use to insert data.
I want the records in the table to be refreshed with each insert.
How can I do that?
I want the records in the table to be refreshed with each insert.
Then your "insert" logic need to do two things:
insert the data into the database
insert the data into the JTable. This is done by using the addRow(...) method of the DefaultTableModel. You get the data from the form, create a Vector to contain the data for each column then you add the Vector to the model.
I should simply select the table and go to the navigator window. Under "Other Components" there is the list that was created after the binding and contains the table records (you should know its name). Right click on the list > Properties > check "observable".
Please try the following code:
if you want update the table on button click or on event of any component
you have to put the code in the event
the list generated should be Observable checked
code
udetailsList.clear();
udetailsList.addAll(udetailsQuery.getResultList());

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