MYSQL boolean return checkbox, not return tinyint - java

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.

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.

Connecting two columns in two different comboBoxes

I connected MySql database with Java app, I see data from two different columns (Items, Price) but I want to connect them somehow, for example, if choose in first combobox item "Desk" the other combobox should automatic find "Desk" price and display it in second combobox. Anyone have idea how to make that ?
You could write a changelistener for the first box, then have it go into another sql query which will populate the second box. Check out this guide, it'll explain to you how to do it
https://docs.oracle.com/javase/tutorial/uiswing/events/changelistener.html
First, I think that you don't want to ComboBoxes being correlated in such a way. There must be a better design for your gui.
Is your ComboBox from JFace? There are JFace Data Bindings. Normally, you use them to automatically update your model if someone changes values in the user interface. If your model contains an id from your database, both comboboxes can have a binding to this id.

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());

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

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.

JTable model column

I have a JTable and a Model for that table.
Now I want to change the order of the columns and to hide or show some columns (e.g. like Windows Explorer in "Detail View" via Menu on rightclick).
My first problem here is, the getColumnName function. Do I have to keep track of, which column is at which place and then return the right columnName or is this already part of the model?
Same for the getValueAt function. Can I always return the value for the first column if I get columnIndex = 0, even if the user has dragged this column to the end of the table?
And nearly the same problem for adding/removing columns. If I do that, of course I have to fireTableStructureChanged, but do I also have to adapt e.g. the getColumnName function?
I haven't found a tutorial for that. All tutorials stop at "you can use a model".
I'd really like to see an example of such a dynamic model.
Thanks a lot.
You should use the getColumn(int) method of the model, and for accessing the model, you'll need to convert the row and column view indices with JTable's convertRowIndexToModel(int), convertColumnIndexToModel(int) and the equivalents for converting the model indices to view indices.
You need to understand the difference between the "View" and the "Model". When you reorder the columns in the JTable (view), this does not change the order of the data in the model.
If you want to access the first column that is displayed in the table you use:
table.getValueAt(row, 0);
if you want to access the first column in the model, then you use:
table.getModel().getValueAt(row, 0);
I want to hide or show some columns
See Table Column Manager.

Categories

Resources