0

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!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Bassinator
  • 1,682
  • 3
  • 23
  • 50
  • Remember that, in Java, *everything* (or almost) is subclass of `Object`, so you can pass `String` arrays as data. I suggest you check [The Java Tutorials: Swing - How to use tables](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html). Besides that, I think that you should also store your data in a database. Check also [JDBC](http://docs.oracle.com/javase/tutorial/jdbc/index.html) (and [Using JDCB with GUI API](http://docs.oracle.com/javase/tutorial/jdbc/basics/jdbcswing.html)) – Barranka Jul 07 '14 at 20:35
  • This might help a bit: http://stackoverflow.com/questions/2146488/converting-an-arraylist-into-a-2d-array – TFischer Jul 07 '14 at 20:43
  • @Barranka Okay, 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? – Bassinator Jul 07 '14 at 21:19

1 Answers1

1

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • This is a great answer man! Thanks! Exactly what I needed. I don't know why, but this all just suddenly clicked. Do I need to do anything besides accept this answer? I want to make sure you get the credit you deserve mate. Thanks again! My solution is essentially to use his RowTableModel class and extend it. I'm still working, but I think it should work. – Bassinator Jul 11 '14 at 00:42