how to select an object from a jTable by a click - java

I have made a class called Clients, that it has like some simple attributes like client_id, client_name and client_age. I have programmed a small GUI with NetBeans that after I input the data from a client by pressing a button I get it displayed in a jTable.
The source code for adding this in the jTable is:
for (int i=0;i<customerV.length;i++){
jTable2.setValueAt(customerV[i].getName(), i, 0);
}
I would like that if I click one element on the jTable to be able to add some orders to that client by making using of a jButton. If I program that in a console it would be like:
Order order1=new Order("1000","41211")
in which the first field is the order id and the second is the order number, so if I want to assign that to Customer 1 it would be like.
c1.assignOrder(order1)
how I can do that by using java swing? I mean to select the whole object from the element that I click in the jTable

Set instances of your Clients to the table model instead of client names (like: jTable2.setValueAt(customerV[i], i, 0);)
Implement a custom renderer that will render the client class as needed (e.g. display the client's name) and set it to the table. Another (easier) option would be to just override toString on Clients to return client's name, or whatever you want to be displayed and do not bother about the renderer.
call getValueAt() to get Clients instance bound to particular cell in your button click handler.

Related

Select a row in TableViewer

I have a TableViewer in my application that has the following features:
It gets data from a web service, and upon selecting a row, buttons get enabled and you can make operations (all these operations are calling the webservice, so the table stays in sync with the database).
When I add a new line, I submit an "add" command to the web service, and refresh the table. Now I have a new line, and I know which line is the new one.
Now I want to select the new line by default, and I tried many commands like tableViewer.getTable().select(index); and as it is now:
public void selectAdded(int id) {
tableViewer.getTable().setSelection(id);
}
This picture shows what is the problem:
The upper one shows as it works now with this code. The row below shows how it looks when I click a row. The problem is, the buttons don't get enabled, and I have two read-only text fields that remains blank instead of showing the information I need. But when I click, everything works normally.
What should I do to achive that selectAdded(int id) acts like a click? The solution should be multi-platform (Mac/Windows), but at least on Mac.
Don't use the Table selection methods which using TableViewer. Instead use
ISelection selection = new StructuredSelection(model object);
tableViewer.setSelection(selection);
'model object' is the model object for the row you want to select (as returned by your content provider).

How can I accept a value to be passed, using a method

I am trying to select an item from the jList in one form (Home), and extract the data from the ArrayList and output the data to separate jTextFields in a different form (Details). Below is the method I'm trying to use to do this (not a lot there I know!).
public void passObjectData()
{
int i = proObjList.getSelectedIndex();
}
I know once the method is complete, I can just call it on the form load method in the next form, but I'm stuck on how to get the method correct.
I don't know what other code, if any, will be needed for your help.
I have hardcoded data into an ArrayList and have output a name to a jList. Now I want to get all of the data of one person that is stored
in the ArrayList (name, address, tel num etc) and put this information
into jTextFields.
As I understand your question this ArrayList is the undelying data structure used to fill the ListModel and you want to get the selected index from the JList to retrieve the correct object stored in that array list. In this case you can:
Have a domain class called Person to hold the person's data (name, address, etc)
Add Person objects to the ListModel.
Provide an appropriate ListCellRenderer to display the person's name.
Use JList#getSelectedValue() to get the selected Person.
Pass this selected Person object to the text field's form and update those accordingly.
Optional: attach a ListSelectionListener to the JList in order to listen for user's selection changes and do the previous step automatically.
See the first 3 points of this approach exemplified here (note: the example is using JComboBox but the same applies to JList as well).
Suggested readings
Creating a Model
Selecting Items in a List
Writing a Custom Renderer
Side note
Not sure if by forms you mean JFrames but just in case: please note that we should avoid using multiple JFrames. See this topic: The Use of Multiple JFrames, Good/Bad Practice?

bind data from jlist to appear on a jtable

I'm developing a JAVA swing application, developed using hibernate and mysql as a database.
I'm trying to bind data from a JList to appear on a JTable, when one of items in the list is clicked. For example i have different user types in my application, Supervisor, Manager, Administrator and others.
Each of the user type has users register under them. I want the application to show certain users when a certain item is clicked on the JList. Like when I click on the supervisor item then all registered supervisors must appear on the JTable. Don't know if I'm making sense, but you all allowed to reply and I will try to make you understand better. Thanks.
Like when I click on the supervisor item then all registered supervisors must appear on the JTable.
One way is to populate the table with all the data and then filter the table when you select an item from the JList. Read the section from the Swing tutorial on Sorting and Filtering.
Otherwise you would need to dynamically query you database every time a JList item is selected.
In either case you will need to add a ListSelectionListener to your JList to invoke your processing. Read the section from the Swing tutorial on How to Use Lists for an example.

Help with Java program (Swing + database)

Could somebody give me a suggestion on how to simply and effectively make GUI representation of directories which are contained inside the database. Now, getting information using SQL queries is one thing. I can do that.
In fact using separate small examples I can put a file inside the database along with his information and I can get the file out of the database. The thing is I was just doing this without GUI, just to test does it work.
Now I need a GUI of this and I really don't know where to start. DO I use JTable, JList or something third? Also, I think I need an multidimensional array because I have, for example, id of a file, name_of_file and size.
So I need different types to put them in: int, String and int.
Also, I need to obviously hide the id of a file from the user yet keep it at the same time in order to be able to reference it.
How do I hide it in a GUI component?
So, let's say that I have a database table for files with these columns:
id, name, size, binary_of_file.
My real table has a bit more of columns like, id of a parent directory, id of a owner, etc. but for now this is not important.
So, I tell database to give me all info about the file (except it's binary because I just want to list the files):
...
ResultSet rs = statementObject.executeQuery("SELECT id, name, size FROM Files;");
while(rs.next()){
//Where do I store the values in? Which GUI component and how?
...
I guess I need an JPanel that will contain this component that will show my files from the database. What component? Please help!
It sounds like you want to use a JTable, it has a TableModel interface that you can implement to adapt to your resultset.
Also follow the link at the top of the docs. to Creating a Table Model.
Perhaps a combined JList/JTable component would fit this need.
That is a screen shot of the GUI of FileBro.
My idea is that the JTree on the left would represent the 'directories' and table (names) of the DB. The JTable on the right would contain the data of the selected table. Change the Locate Open Edit Print buttons for Create Update Delete and the panel below that to show details of records, and it would be the start of a DB CRUD component.

Creating front-end (Java) user interactive table using MySQL as back-end

I'm a student, intrested in Java programming. Now im trying to create user interactive form.
Untill now, I have created a form view, using swing components, JLable, JTextfield where user enters data. JButtons, 'new' 'save' 'delete' 'edit', which listens user action through ActionListner.
A table that 'extends' AbstractTableModel. Table values are the ResultSet values. i have used MySQL connectivity.
Table is displayed. User can now add new row to it, using 'New' and 'Save' button.
Please watch this
My problem is, i need the corresponding TextFields to display the corresponding row user selects in the displayed table, so that 'edit' and 'delete' would be user interactive. users are allowed to select table's row.
Need Help.
Im sorry if any mistake in my question format and language. Thank you!
on row select event, populate the values of text fields with tables selected row values.
As discussed in How to Use Tables, JTable will supply a default editor for any cell in your TableModel that returns true from isCellEditable(). Just double click on the cell. See also User Selections for more on selection listeners.

Categories