Simple Example of a Table in SWT - java

My last project I created a Singleton class and used Swing, to create my TableModel and then add the populated table to my base dialog class.
My new project however, I have to use SWT instead of Swing. I am afraid I know little to nothing about SWT.
I want to be able to create a Table class (SelectionsTable.java). In the table class I want to be able to create a simple table that has 4 colums and populates row values from a arraylist.
I will worry about how to pass the table to my BaseDialog class later.
Any Help is greatly appreciated.

There are really good code snippets directly from eclipse here. The one that is most interesting for you should be this one: create a table (columns, headers, lines). It shows how to create a table with multiple columns, headers and lines.
Since you seem fairy familiar with java, you should be able to figure out how to use this for your purpose.
If you want to have a proper TableViewer with ContentProvider, have a look at this excellent tutorial by Vogella.

ContentProvider is like a model that provides input for TableViewer. ( Model for Table)
LabelProvider is a class that provides image and text that you will display in a Table Cell. ( similar to getValueAt() in Swing)
create TableViwer in your dialog
create TableViewerColumn (each column) for tableviewer and set LabelProvider() on TableViewerColumn. LabelProvider.getImage() LabelProvider.getText() will be called for each row object that content provider provides on this column.
set viewer.setContentProvider()
This is how it works: TableViewer first gets input from its content provider. lets say your content provider is returning List of RowObjects. For each RowObject, the label providers on each TableViewerColumn will be invoked to dispaly image and text in that particular cell location ( like colIndex, RowIndex in Swing).

Related

Detecting the table of an onChanged() event with multiple JTable in a JTabbedPane

I'm currently using a JFrame to hold a JTabbedPane that contains multiple tables. In my class that extends JFrame and implements TableModelListener, I have an onChanged() method that takes a TableModelEvent as an argument. I can successfully obtain data from the event on the table that the event was fired from, but I can't determine which table it was.
From what I understand, this is not the way to do what I intend to do. I believe that I may need to write a custom TableModelListener or JTable and implement the onChanged() method there.
What do I need to do to determine which JTable was changed in the JTabbedPane? I'll need to find the table and the row that was modified.
TableModelListener and TableModelEvent won't provide information about the JTable that the model is associated with, as the model may be shared by multiple tables, in theory.
Getting the row is matter of getting the row from the event, which comes from the firstRow and lastRow properties. Once you can establish which table the model belongs to you, you can determine the view row by using JTable#convertRowIndexToView
To find the JTable you have, at least, two basic solutions
You Could...
Ask each table, stored in each JTabbedPane for their model and compare it with the model that generated the table model event
You Could...
Maintain some kind of look up between the TableModel and the JTable or JTabbedPane, depending on what it's you are ultimately after
This could be achieved by using Map of some kind, keyed to the TableModel
I believe that I may need to write a custom TableModelListener...
Check out the Table Cell Listener.
It is very similar to a TableModelListener, but you do need to specify the JTable when creating the TableCellListener, so you do have access to the table when a value is changed.

How to make filterable to jtable record using netbeans ide

There are many rows on above showing jtable. I want to filter record by short code. If I type short code like 1234 it should be display only short code 1234 associated row on jtable.
Thanks
You're going to have write the code...Start by checking out how to use tables, in particular sorting and filtering
The basic requirement would be to attach an ActionListener to both the field and button (you can do this from the form editor if you wish).
Within the actionPerformed event handler method, you need to create a RowFilter and apply it to the tables RowSorter.
A table can be configured to automatically create a row sorter by setting the autoCreateRowSorter property to true.
It's all explained nicely in the linked tutorials...
And another example

Steps to read the text file into specific columns of JTable using biojava

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.

How to add columns/rows to an existing empty JTable?

I am developing a software for which I need a basic Java Swing UI. I am using Netbeans which enables me to drag and drop UI component. However there is a final result table that I need to display using code.
In my UI using trhe IDE I created a JTabbedPane, inside which I added an empty JTable (no rows nor columns) called finalOutputTable. Now I want at runtime to fill this table, let's say with columns: x, y and rows: row1, row2 & row3.
How can I do that by coding?
You need to make a custom TableModel that will allow you to support this functionality. JTable is just there to display the table on the GUI. The TableModel has all the business logic of what the data is and how each cell should act. To do this you will have to step away from the GUI-Builder and write actual code.
Tutorial: Creating a Table Model
inside which i added an empty JTable (no rows nor columns) called "finalOutputTable".
You need to add the data to the TableModel. Then you use:
table.setModel(...);
to display the data. You may also need to revalidate() the panel containing the table if you didn't reserve space for the table when you created the form.
How can i do that by coding?
See the Laying Out Components Within a Container lesson of the Java Tutorial.
You can also use a TableModel witch does all that hard work for you.
https://github.com/MarkyVasconcelos/Towel/wiki/ObjectTableModel

Updating JTable after inline edit

I have 2 classes, one to make a frame and the other to handle and implement the interface TableModel. When editing cells inline and updating the values in the class that implements TableModel I then need to refresh the table to show the updated data (as the table needs to auto sort thus when I inline edit a cell the rows may need to be re-ordered). The problem I'm having is after updating the data I can't figure out how to refresh the table, I've tried a hacky way of refreshing it when you click off the cell or press enter but I feel there could be a more elegant solution, any ideas?
The TableModel is responsible for invoking the fireTableCellChanged(...) method when data is changed in the model. Sorting will then happen automatically.
Read the JTable API and follow the link to the Swing tutorial on How to Use Tables for more information about TableModels and sorting.
I suggest you just use the DefaultTableModel so you don't have to worry about this since it implements all the TableModel methods.

Categories