Loading specific data onto a jtable on a frame - java

The screenshot above shows the current design of a frame, and this concept still is difficult for me to grasp: Adding data onto a table.
I've searched about the topic before, but I always saw that the code included the use of Vectors. I however, wish to use no vectors, and for this table, to be able to insert only a specific set of data.
For example, if the selected Activity is Bowling
the method will run through a file called students.dat, which is in this format:
StudentName
Grade
Email
ParentName
ParentPhone
ParentEmail
Sunday
SundayActivity
Monday
MondayActivity
Tuesday
TuesdayActivity
Wednesday
WednesdayActivity
Thursday
ThursdayActivity
The method will return only the StudentNames Grades and Parent Contact& ParentEmail of the students who are involved in Bowling, as seen in the screenshot, into an ArrayList. From the moment the items are entered into the Arraylist, I do not know what to do onwards.
-Also, when loading into the ArrayList, I plan to do something like this:
StudentNames into NamesList, Grades into GradeList, etc to seperate each array

If you don't want to use Vectors then you need to create a custom TableModel because the DefaultTableModel was written to support Vectors or 2Dimensional arrays.
One way to do this is to copy the DefaultTableModel and change all the occurrences of Vector to ArrayList.
Or if you want to get fancy then you can use my version of a List Table Model.

Related

How can I add a List to the JLabel?

I'm writing Java game right now, I have a problem that I have a List which type is Ranking, with the fields name and score, and I would like to add that fields to the JLabel. Method setText() unfortunately doesn't fix. What should I do?
with the fields name and score
A JLabel is not designed to display multiple lines of text.
You can format the text using HTML.
Or I would suggest a better approach is to use a JTable, which is designed to display data in a row/column format.
Read the section from the Swing tutorial on How to Use Tables for more information and working examples.
I have a List which type is Ranking
You may also want to check out Row Table Model for an example of a custom TableModel show how you can display your Ranking object in a table.

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?

Create a two dimensional array of different data types (Date and double)

I am trying to make a program to do calculations of doubling time based on multiple readings at different points in time.
I am using a Netbeans JFrame with a JTextfield and a DateChooser with an add button. I intend to add the values in the JTextfield and the DateChooser to a two dimensional array of the data type [Date,double] each time the add button is clicked.
Once all the data is entered I have another button that then plots a graph based on these data points and calculates the doubling time and generates a report.
I am facing the following problems.
I will not know the number of elements I will have it can be 2 it can be 20 it will depend on the observations, I am unsure of how I can set this up in the context of a JTextField, JButton and DateChooser
Any help will be appreciated.
I will not know the number of elements I will have it can be 2 it can
be 20 it will depend on the observations, I am unsure of how I can set
this up in the context of a JTextField, JButton and DateChooser
I wouldn't. It would be eaiser to manage with a JTable.
This way you won't need to care how many observations need to be made, they can simply keep adding new rows as they want.
I would also make a simple Object that contains the Date and double value, maybe called Observation, which can then simply be managed by the JTable's model and if required, transferred to something like a List or array.
This links the Date and value together in a obvious manner which is not easily disconnected - IMHO
Instead of using an Array, use a List. That way you can use List.add() and you don't need to worry about how many elements are going to be in that list.
Elements in a list can be access similar to an array by using List.get(index) to get the objects or a more generic for( element : List ) {}.
You can have complex user type object with 2 fields and then have array of that object.

Saving a layout for later view

I've created an app in which a user can can add or delete rows in a tableLayout. Inside of these rows, a user inputs an Academic class name, the letter grade received in the class, and the number of units the class is worth. Then, it calculates the user's gpa.
I'm not sure how I would go about doing this, but I need to save an exact copy of the layout when the user presses the 'save' button that will save every current specific thing. (Eg. number of editTexts, text in the editTexts, number of rows, etc.)
I've researched using SharedPreferences and using SQLite. I'm not sure which one is better for saving multiple specific layouts. Also, I know nothing of using SQLite because I've never used it before, so if that is the best choice, could you post a link or something to a good tutorial? Thanks!
Avoid database access you can just use simple 2d array to Store all your data
Ex:
//Saving info of 1st Layout
String[][] data= new String[4][4];
data[0][0] =editTextno;
data[0][1] =editText;
data[0][2] =noofrows;
data[0][3] =field4;
For saving info of second,third you can similarly change the index no. like data[1] for layout 1,data[2] for layout 2
later you can set this array inside SharedPreference or can create setter getter to store this array and can retrieve the Array for info inputted by user wherever you want

sorting a JTable using a data vector

I got stuck with a performance problem while writing my program and I need your help! :)
I'm using a JTable to view test results taken from a vector I made and it has 4 columns in it. When I click on a row the details from a saved txt file of that test are shown in a child window. Also, when I click on the columns header the event sends the vector to a function that sorts it according to the pressed column. Every time a new value needs to be entered the sorting function is called again.
My program works fine with a small number of rows. But, when I enter say, 150 rows, every time I enter a new row the Table flicks (the sort probably takes a lot of time), but I have to keep the vector synchronized with the jable because of the "push to view the result" option.
I would really appreciate some help with this.
thanks
You shouldn't have to do any sorting yourself. JTable supports sorting natively, and has the convertRowIndexToModel and convertRowIndexToView methods to go from the view index to the model index and vice-versa.
See http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting.
Use JTable's internal sorter (DefaultRowSorter). Do not re-create the vector which holds the data - use Vector's add() method to add new records. In many years of Java GUI development I haven't seen a single case where I had to keep records in the TableModel sorted. Make sure getColumnClass() returns a proper type, so the default sorter knows how to sort the column, and that is all.

Categories