Reputation: 1724
So, I am trying to an inventory tracking program for my father to use at his auto shop. I am creating this to help learn Java better; his business is small and he can manage without it.
I have a class extending my main class which handles data. I will post the relevant code from this class. I need to display this data in a JTable. Yes, I do know there are more than a few highly similar questions on here. I've read through them, and still don't understand how to apply this to my situations. As much as I'd like to be able to just read those and get it, It's just not clicking for me. Please be patient with me? I'd really appreciate the help.
So, Essentially, I have a part object constructor (private class part is a subclass of class data), which holds several values.
private class Part
{
// Part constructor
Part(String partName, String Make, String PartNumber,
String altPartNumber, BigDecimal price, int quantity,
String description, boolean autoMotive, boolean marine,
boolean industrial)
{
parts.add(this);
}
The description String does not need to be displayed in the table; it just needs to be associated with the particular part (represented by a row in the table) in such a way that when a part is highlighted, a panel on the right side can read this data and display it. The booleans are just there so that I can filter the parts by application, and also do not need to be displayed.
I have several part objects stored in an ArrayList.
private ArrayList<Part> parts ;
I am trying to write a method that the main class can call to get this data and display it in the table. The problem is, JTable wants something of type Object [] []. How can I create something that will allow it to accept my data, or modify my data in such a way that I can display it?
Other somewhat relevant code:
private transient final String [] columnNames = {
"Name",
"Make",
"Part #",
"Alt. part #",
"Price",
"Qty."
};
Any help is greatly appreciated!
Upvotes: 0
Views: 357
Reputation: 324118
so say I were to create a custom table model; I don't understand something; how would I seperate the non-displayed data from the displayed data?
You control that because you implement the methods of the TableModel. You control which columns are displayed and you control how the data is accessed from your object in via the getValueAt(...)
and setValueAt(...)
methods.
Check out the JButtonTableModel.java
example found in Row Table Model. The RowTableModel
provides generic code to support any custom class. The example shows the customizations you need to display 4 specific columns of data from the JButton class.
Upvotes: 1