I want to display data about a Java Object in a JTable in my GUI. In a traditional JTable the rows are Objects (or whatever data representation) and the columns describe the data of the objects. I want to do the opposite. I want the rows to list the name of the data of the object and the column to show the value of the data. I will only have 1 object in the table so I want the table to display data vertically instead of horizontally if that makes sense.
It appears that JTable is not designed to flip the functionality of rows and columns so my question is what is the best way to implement this? I know all of the names of the data I want to include as rows so I think I could just hardcode everything, but that doesn't seem very elegant.
Related
I'm learning about JDBC with GUI. I can do that the GUI display the data in SQL one by one. Then i want to display the data in table just like the result when i execute in SQL. I saw that many people use two Vector to add data to JTable. I wonder is there anything else apart from Vector that I can do to add data to JTable.I really want to add some images in my post but i can't so sorry if it's uncovinient for everyone. Thanks for reading.
I wonder is there anything else apart from Vector that I can do to add data to JTable.
The DefaultTableModel uses Vectors because it makes the TableModel dynamic. That is you can easily add or remove rows/columns from the model.
If you don't want to use a Vector then you can create a custom TableModel to store the data any way that you want.
Check out Row Table Model. It gives an example
of how to create a custom TableModel using an ArrayList for the data
of to write a generic TableModel that is reusable
I have a model(AbstracTableModel) which I use to build a JTable.
The thing is that the table cell values seen in the GUI are displayed from a database.
How can I add a new column with checboxes for each row of the table?
Is there a concrete answer to this?
The thing is that the table cell values seen in the GUI are displayed from a database.
Use a DefaultTableModel to store the data from the database.
See the TableFromDatabaseExample.java code found in Table From Database for simple code to load the DefaultTableModel.
How can I add a new column with checboxes for each row of the table?
You can modify the above code to add an extra column to the "columnNames" Vector. Then in the looping code you add a Boolean.FALSE object to the "row" Vector.
Or, after creating the DefaultTableModel with the data from the database you can use the addColumn(...) method of the DefualtTableModel to create your column of check boxes.
I have a JTable, which has comboboxes and text fields. I want to have a save button, which saves all the data from the jtable into an arraylist. I've found some examples but they were all unhelpful. The Table is using the defaultTableModel. How do I read the inputs after the push of the save button?
DefaultTableModel#getValueAt(col, row), if you want to read each cell individually...
DefaultTableModel#getDataVector(), which returns a Vector of Vectors, where the outter is the rows and the inner are the columns, if you want to process the rows/columns in some speical way...
I am using JTable for displaying the information. After rendering the information if I drag the columns to reorder them, the information is displayed in the same fashion in the session. But when I try to capture the changes by checking the column names by iterating the column names, the sequence is same as the older one. Why is the latest view not available from the API?
As commented by Hovercraft Full Of Eels, the column indices in the view change independently of the column indices in the model. JTable's JavaDoc has this to say about it:
By default, columns may be rearranged in the JTable so that the view's columns appear in a different order to the columns in the model. This does not affect the implementation of the model at all: when the columns are reordered, the JTable maintains the new order of the columns internally and converts its column indices before querying the model.
JTable offers the methods convertColumnIndexToModel() and convertColumnIndexToView() that you can use to translate column numbers from one to the other. You can use these to figure out if (and how) the columns were rearranged.
To be notified of column changes as they happen, use a TableColumnModelListener:
myTable.getColumnModel().addColumnModelListener( new TableColumnModelListener() {
//etc.
} );
Here is a typical input .txt file (also called as fasta file):
>contig00001 length=586 numreads=4
CGGGAAATTATCcGCGCCTTCACCGCCGCCGGTTCCACCGACGAACGGATACTGCGtGaa
ggCCGCGATCCCGTCggaCGGAAAaCGCCcTGGCCCGGGAaCATACCGTTCGGGCCGCCA
AGTGTTATAGCCGGACCACTTGTCAGAACATTTCCaaTCCGAAGATGTGAGTtCGGAAGg
TAAAAGCCCGACAAGTTGCGCGgTGAATTTACCTTtACcGCACGATATGCGTCCGTATTA
AaGAAAaGTTCGAAATTATCAGTAAGGCCGACCTGAAaGCTGACCGGGAGTTCAACAAAA
TCTGCATCACCcGGgTCACGGTCGAAATTGCTGTACGCGGCGCTGAACGTAAATTCACCC
TTTcTAAGGGTGTCGCcGTCGTAAACCGTAAaCAaGCCGGTAGCGCCGCCCATCGGGCCG
CCGGTACCAACCGTCGGTGCCGTGTTTCTtGCATCATTGTCCGATCGAGCGTTCTCGTCC
GCTTGTGCAAaTCCTGCAaTAGCTAACGTGAAAACGATCAGAGCTGTTGTAAATACTCTA
TAAGCGAGATTCATCACATTCCTCcGCCGAAATAAAAAGTTAATTt
>contig00002 length=554 numreads=4
TGCGCCAaCCGCGCTCTtCATAAaTGGGCACTGCTCCCGATGGCCgACTCGGGCGGTTCG
CCATGAGATCTTTGCCtACCcAGgAaCtCACcACCAAGTCTGATTGCTGTGTGTTTtCTT
CAAGTCCCTATTTCTATTCtCTTtAATGGAACCCGTAGGAAACCCGTGTAGGACGCGGGA
aCCGCACTTgAAGGGGGAGGCGCGGGGTACCGGtCCGGGAACGTACGGGTACCGGCGGGG
gAGGGGAGGGGGACCgCTCCGGGAAGGCCAGGGGACGGATTGGGGAAGGgCGGGTACCGA
AGCGGGgAAaTGGGggAaCcGGCGAGAGGGTTCCTCGCTAAGTGGGGGAAATaGGGGAAA
GGTTGACCAGTGGTtCCCcGCTCTCGTAACATGCCTCAGATAGCGCCATCCGCTGTACCT
GGtcaggtcGctggcaacttcggccgagcaggtgaacccgaaaggtgagggtcagtgtga
cacaccaaccgaacaccgacgaggcaagcgtaggagccggcgtggccgcgcccggcggcg
ctgaggactcctcg
Code to read the sequence may be found here.
It gives the proper output, as shown below with tab seperation:
contig00001 586 52.38
contig00002 554 62.45
The problem is that I developed a form in NetBeans that consists of a JTable having 5 columns i.e:
"contigID","Description","Organism","Sequence_length","Gc_percentage"
and a JTextArea. I want to display the above output in the JTable columns, while the other columns remain empty; and when I click 'contig00001' in JTable, then respective sequence like "CGGGAAAT...." should be displayed in the JTextArea.
How can I do that? Any suggestion would be appreciated.
I'm not exactly sure what you're stuck on. If it's adding data to the JTable, I'd consider creating a DefaultTableModel object, constructing it with the correct column header Strings in an array, with 0 rows of data, and then adding rows of data as you read through your files. The JTable tutorial should help you do all of this. Once you have your table model created, you can add it to your JTable easily via its setModel method.
One approach is to extend AbstractTableModel, as discussed in Creating a Table Model.
Addendum: By listening for a user selection, you can determine which row was selected and update your JTextArea accordingly.
Addendum: Because data retrieval may be prtotracted, SwingWorker offers a safe way to mutate the TableModel. Here's a simple example.