• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Displaying ArrayList data in a JTable

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, while I take a break from that Accept/Reject problem, I'm trying to display the team rosters in a JTable that automatically updates with every player selection. I'm trying to embed all the code for the table into the DraftSimulation class. I've whittled it down to only one error.


The error is "RosterTable cannot be resolved to a type". I don't see anything wrong with it, so I'm kind of stumped.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
weirdest bit of code I've seen for a while

in the middle of a class that extends JDialog, you're creating multiple JFrames?

in the method (that you think is a class), you add the table/scrollpane to the extended JDialog,
but try to set that as a JFrame's contentPane.
 
Jason Guyton
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Dunn wrote:weirdest bit of code I've seen for a while

in the middle of a class that extends JDialog, you're creating multiple JFrames?

in the method (that you think is a class), you add the table/scrollpane to the extended JDialog,
but try to set that as a JFrame's contentPane.


As I explained in another thread, I'm a java beginner being asked to create a rather complex program for a class project. I'm trying to cobble something together that functions. I've scoured the internet for examples and tried to frankenstein them together with only a pittance of understanding. That's why that extra JFrame showed up, along with numerous other WTFs. Please, enlighten me. I am willing to learn.

If you have a great method for displaying arraylist data in a jtable that updates as the objects in the arraylist change, I'd love to see it.

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> I'm a java beginner being asked to create a rather complex program for a class project

the purpose of such projects is to get you to learn/understand something, and submitting
something that works by accident won't get you many points.


> If you have a great method for displaying arraylist data in a jtable that updates as the objects in the arraylist change, I'd love to see it.

how do the objects in the arraylist change?
 
Jason Guyton
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, this is not even a java class. The purpose of the project is to understand information modeling and knowledge based systems. They just want us to implement a knowledge system using java. Java really should have been a prerequisite.

The ArrayList that I want to display is a list of Team objects. The Team object includes QB1, RB1, RB2, etc. These are initially empty. As my system runs thru a draft simulation, these positions are filled with a player name. So, as each player is placed on a team, I'd like to see the jTable pop in the name at the correct position. I believe this can be done with fireTableCellUpdated. But, since I can't get the table to display, I can't tell if that is working.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here's a (very) simple demo of working with the table's model.

the table/model loads initially with some data from an arraylist (presumably arraylist is loaded from a file)

click the 'Add Player' button, fill in the 3 textfields, click 'OK', and the table will update (via the model)
(extra code required if new player is to be saved to file, for next time loading)

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi There please do not use an Array for setting Value in Jtable you can use directly from a List to a Cell.
you can use in this way

getValueAt(int row){
VO vo = list.get(int row);
if col==1
return vo.getPropertyValue();
}

setValueAT(int row){
similaryly you can set the Value in the List
the FireTablecellUpdate()

}
 
Rohit Kedia
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NOte this changes need to be done in the TableModel which will be extended by your customized Table Model
 
Jason Guyton
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, that's pretty slick. But, that's not quite the functionality I was looking for. Let me be more specific.

I have a class called Autodrafter that has the main. This starts a window that has a button for doing a draft simulation. That button starts a window that takes user input to initialize the team names and a few other attributes. Hitting the enter button creates 10 team objects. Each team object also has an attribute for each roster spot that are initially set to an empty string. There is also a "Draft Simulation" button that calls a class to do the sim. Right now, this function has no GUI component, all the info is displayed in the console. I'm trying to add this table that shows all 10 teams and all the roster spots (that would initially be empty). Then as the sim function goes thru the simulation adding players into these roster positions, the table would update the appropriate fields as they are assigned. From what I've read about tables, this should be possible, since the cells just reference the objects. the fireTableCellUpdated should enable this.

So, the DraftSimulation method is passed the playerlist and the teamlist. The simulation function includes the Accept/Reject functionality that I just got working. So, the main GUI element of the Simulation is the roster table.
 
Jason Guyton
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I coded a new class called RosterTable that is called by two other functions of the program. It actually displays a table! But I have two problems.

Problem 1: The table is displayed very small. All the data in the cells is truncated, showing only a few characters of the player's names. I can resize the window and all the columns expand equally, but i have to make it about 3 times the original width to see the names well enough. Also, there is gray space below the table that double the height of the window. How can I size the window to my liking?

Problem2: The table displays an initial state of the data in the arraylist, which is mostly empty strings. As the popups for selecting players come and go, the table does not update. I tried the public void fireTableDataChanged() in the table model, but it didn't work.

The call from the parent...

The RosterTable class..
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First some small improvements for your class:
1) use a switch statement in the getValueAt method

2) you have no setValueAt method which indicates that all values are read-only. If this should be the case you should override isCellEditable to return false for all read-only columns. Simply return false to make all cells read-only.

Jason Guyton wrote:Problem 1: The table is displayed very small. All the data in the cells is truncated, showing only a few characters of the player's names. I can resize the window and all the columns expand equally, but i have to make it about 3 times the original width to see the names well enough.


Use the JTable and its TableColumnModel to get hold of all TableColumn objects, then give each column a minimum size.

Also, there is gray space below the table that double the height of the window. How can I size the window to my liking?


After you've created the window and sized it (using pack()), you can call setSize:

Problem2: The table displays an initial state of the data in the arraylist, which is mostly empty strings. As the popups for selecting players come and go, the table does not update. I tried the public void fireTableDataChanged() in the table model, but it didn't work.


Do you call fireTableDataChanged() after you've added a team to your ArrayList? Because the model needs to have changed before you can notify the table. If that still does not work try calling fireTableRowsInserted instead. The new index is of course getRowCount() - 1 or tl.size() - 1 (which are of course the same).
 
Jason Guyton
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to add in some of your suggestions.

1 - Not sure what you mean by switch statement.

2 - setValueAt - Got it.

3 - On the TableColumnModel for setting column size, do I have to create another inner class for TableColumnModel to do that?

4 - Setting window size did not seem to have any effect. I was doing this to the contentPane object.

5 - The set of teams is fixed at 10. there is no adding of columns or rows. The only the values in the cells are changed by the draft algorithm. A roster slot is changed from " " to "Drew Brees" for example. The table will fill in all the new info as soon as I manually resize it, but it doesn't do anything when the picks are happening.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Guyton wrote:I'm trying to add in some of your suggestions.

1 - Not sure what you mean by switch statement.


3 - On the TableColumnModel for setting column size, do I have to create another inner class for TableColumnModel to do that?


Just retrieve the one from the JTable:

4 - Setting window size did not seem to have any effect. I was doing this to the contentPane object.


The content pane is not the window, is it? The frame is your window.

5 - The set of teams is fixed at 10. there is no adding of columns or rows. The only the values in the cells are changed by the draft algorithm. A roster slot is changed from " " to "Drew Brees" for example. The table will fill in all the new info as soon as I manually resize it, but it doesn't do anything when the picks are happening.


Then call fireTableCellUpdated.
 
Jason Guyton
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will implement those changes when I get home. Thanks for the input.

I think I'm still confused about the fireTableCellUpdated call. I found an example that has this in the table model..

Is that correct?

So, theoretically, if I have a line in the parent class (the one that called the RosterTable class to draw the table) that changes a roster position from " " to "Joe Schmo", the table cell will immediately update in the table? I don't need any other listeners or other tricksies. I hope so, because that's the functionality that I want.


 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JTable is a listener itself, so calling this method will make the JTable repaint this cell.
 
Jason Guyton
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, I keep getting stuck. Passing these arraylists around is hard work (and the source of many errors).

So, to put in this code...

I need the values to be defined by a 2d array called data. But, I had my values defined by this....


So, then I tried to define the data array by using this....

Eclipse shows no errors until I run it. It bombs out with an out of bounds error on the line marked above. So, I can only assume one of two things...

Thing 1. The arraylist isn't getting passed to this table model class. But, it works for the other methods in the table model.
Thing 2. I'm not declaring this data array properly. It seems I can't just declare

It demands a brace block after that presumably to define the values. Anyway, I'm stuck again.

Funny, I read on one of these many sites that writing your own table model is simple. Yeah, maybe for a pro.
 
Jason Guyton
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, and the column width stuff didn't have any effect.

Nor did the window sizing.

I suck at this.

Here's what I have so far....


I get a table out of it. But it's still small, and won't update. Does somebody make a GUI plugin to make my GUI?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> Oh, and the column width stuff didn't have any effect.

add this line
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
 
Jason Guyton
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sweet. that fixed the column width. Phew. But the window is still small. How do I access the JFrame when it's not explicitly defined. It's created by having the class extend JFrame correct?

So, this...
# Dimension size = window.getSize();
# size.height = something else
# window.setSize(size); // keeps the same width

Refers to the window. How do I get window?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> window.setSize(size); // keeps the same width

shouldn't, unless it is followed by a pack()
 
Jason Guyton
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are a man of few words. But they are valuable words. I just wish I could put them all together into something that works.

That piece of code refers to the object "window". But it's not explicitly defined. contentPane and table are explicitly defined. But how do I get to the window? I tried making a new JFrame and adding the stuff in it, but the table disappeared from the window. I'm getting really frustrated now.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see what you mean now.

if you're extending JFrame (or JDialog), then the class that extends JFrame is a JFrame, so all you need to do is
setSize(w,h);

just make sure there's no pack() following
 
Jason Guyton
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what I was thinking, but the pack() at the end made that command useless. It works now!

One last hurdle, the auto updating of the table data. Did you follow the problem I'm having trying to implement the fireTableCellUpdated(row, col); command?

I tried copy and pasting the same if statements from the getvalue method in to the setvalue method, but that had no effect. I tried to switch the order of the equals statements to match the "data[row][col] = value" statement, but Mr.JavaMan didn't like that.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're not changing any data that is related to your table model. You'll need to call team.setXXX with setXXX being the method to set the value for that column.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm still not convinced updating the ArrayList is the way to go (unless you have no control over this)

here's my earlier code, now with team names/positions, but empty player info,
with a button to fill a team/position with a player name.
(the basic description I'm reading in this thread)

click the button, then fill in the team number (1 to 10), the position number (1 to 6),
and any player name. click OK and the table's model updates

 
Jason Guyton
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All right, I'll try to explain what I'm doing.

I have a system that calculates the best player to choose based on their point total and other factors that are calculated as the draft progresses. So, when the program is started, the player data is read in from an excel file. This creates multiple instances of the Player object. These objects are collected into several ArrayLists, one for all players, and one each for each position. Then the user sets up the teams. This creates 10 team objects with empty rosters. These are collected into a TeamList arraylist.

Now, when the draft function is called, it is passed all of these ArrayLists. It displays this Table to show the rosters. The user is asked what player was taken by each team in their predetermined draft order. Then, when it's the user's turn to choose, the system calculates the appropriate factors, multiplies each player's point total by the factors, and sorts the arraylist. Then the user is asked to accept or reject the top ranked player in the newly sorted list. For each selection, I call an AddPlayer class to modify the Player's team attribute, modify the Team's appropriate roster slot, and remove the player from the arraylists.

The table will always be displayed during the selection process. There are no rows or columns added to the table. Only the attributes of the Team objects are getting changed. So, after every selection, I want the player to appear in the table. Can this be done by calling a method related to the table as part of the AddPlayer routine?

Hopefully, that clears things up. Are we still one the right track with fireTableCellUpdated? Or is that not appropriate for this case?
 
Jason Guyton
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought I almost had it. So, the Draft class calls the RosterTable straight away. Then the draft logic starts with some for loops and such which include calls to AddPlayer. I modified AddPlayer to return a value to identify the correct roster slot that was filled. Then I can invoke the setValueAt method using a row and column index (team and slot). But the method is buried in the RosterTableModel within the RosterTable object.

How do I reach down into the RosterTableModel to trigger the setvalueAt?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> How do I reach down into the RosterTableModel to trigger the setvalueAt?

((RosterTableModel)table.getModel()).setValueAt(...);
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you really need that cast?
 
Jason Guyton
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Damn, I almost had it. I got it all in there without any errors. Ran it. But, no update. Booooooo.

Code from calling parent...


But, now, I'm confused about what exactly to put in the setValueAt method.

 
Jason Guyton
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Update.... I threw in a system.out line in the setValueAt method just to see if it was processing it. Seems to be working fine. So, I just need the proper code in the method to make it work. I'm wondering if the fireTableCellUpdated line is only for user edits to the table itself, but not necessarily changes in the underlying array data.
 
Space seems cool in the movies, but once you get out there, it is super boring. Now for a fascinating tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic