How to change data in JTable cells? - java

I can set data in JTable constructor, and then user can change this data when program is running manually(typing from keyboard).
But what method should I use in case I want to change data in some column? To change column header I use TableColumn method setHeaderValue. What should I use to set value in JTable cell?

If you want to allow users to edit the data, then you need to set a TableCellEditor on the cells that you want people to edit. You probably also want to start using a TableModel instead of hard coding the data in the JTable itself.
See http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

While creating the JTable you first need to specify that the values of particular column are editable. You can obviously also provide the row basis edit functionality. but all these things you should define while ccreating the table itself. Please reply if you need any help on this.

Related

How to hide the Jtable column data?

I want to hide data of a column of Jtable, but not hide the column view,just its data.
The column contain data about profit and the customer doesn't want to show the profit but in my code I use the values of this column and get it when the user select specified row.
How do I achieve the customer need and still be able to get the values of this column when selecting it after hiding its data(displaying column as empty but still has its values)?
You need to remove the TableColumn from the TableColumnModel of the JTable. For example:
table.removeColumn( table.getColumn(...) );
Now the column will not display in the table, but the data is still available in the TableModel. to access the data you use:
table.getModel().getValueAt(...);
You could try a subclassing DefaultTableCellRenderer, and call setCellRenderer. When the relevant column number is passed to getTableCellRendererComponent, return a blank JLabel, otherwise call the default super.getTableCellRendererComponent.
This should mean the column is still visible, but each cell will be blank.
If you want to display the data when a row is selected, you will need add a listener to the selection model (from getSelectionModel) to store the selected row in a variable, and call a repaint. You can then use this value in your CellRenderer.
Why don't you just let the dataModel control what is shown? I'm assuming you are using a tableModel.
All you have to do is change the getValueAt(..) method so that it does not return a value for the column you do not want to show. Make sure that getColumnCount() method is reduced by one as well.

Getting values from JTable cell

I made a column editable in Jtable.
I want old values from a cell when I have finished editing a cell
You can get the value by using
table.getModel().getValueAt(row_index, col_index);
where table is the name of the table and it will return an Object
Go through this Getting cell value. It may be useful for you.
You can use a TableCellListener, like they show here. It uses a PropertyChangeEvent to keep track of the old and new values.
You also could create your own implementation of a TableModel and override the setValueAt method to keep track of the changes.
Add a TableModelListener to your TableModel. Whenever an event fires you can updated the contents of your text field with the new value in the cell.

Disable particular rows of a JTable

I would like to "grey out" particular rows of a JTable so that they may not be selected by any means. The other rows should still be selectable. How do I accomplish this?
You can either override JTable.changeSelection() to deselect the offending row whenever it's selected, or provide your table with a custom ListSelectionModel where you override setSelectionInterval(), addSelectionInterval(), etc. to prevent the row from being selected in the first place.
You will want to create a custom TableCellRenderer, one that will display "disabled" information greyed out. Read the Swing Table Tutorial for more on how to create these renderers, especially the section, Concepts: Editors and Renderers.
Create a temporary TableModel which has only the rows that you want to select. After the selection made and when you want to revert, change back to original TableModel

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 Mirror Edits across a Table Cell and Text Field

The desired behavior is akin to the mirrored text editing field provided in Excel when a given cell is selected, allowing more space to view the contents of the cell. I have a JTable with 5 columns and n rows. Column 2 holds expressions that can be arbitrarily long, thus I'd like to provide a separate JTextField to work with for editing the contents of the expression cell per row. The other fields are directly editable in the table. When the user clicks on a field in column 2, however, I want to send them to the text field. Any contents preexisting in the cell should be appear in the text field and additional edits in the text field should be mirrored in the table cell. Likewise, if someone double-clicks on the cell and edits it directly, I want those changes reflected in the text field. Thus, the user can choose to edit in either space and both are updated. Ideally, they are updated per keystroke, but update upon hitting return is acceptable.
So, far I've got the JTable, TableModel, TableModelListener, JTextField, ListSelectionListener, and AbstractAction, working together to provide most of the functionality described above. I'm missing the reflection of direct table cell edits to the text field and per-keystoke updates.
Are their ideas on how best to construct this behavior?
Well, if you want to get data from the table to the cell then you add the code to your TableModel's setValueAt() function, which should run when the user changes the content in an editable cell. I don't think that will update per-keystroke though.
If you want to move data from the textbox to the table cell use code like this
myJTextField.getDocument().addDocumentListener(new MyDocumentListener());
Where MyDocumentListener is an implementation of the javax.swing.event.DocumentListener interface
That will get you per-keystroke updates from the box to the table. But for the other way around it's a bit trickier.
There are two ways you might be able to go about doing it
1) Add a key listener to the table, and when the user starts typing check to see what table element is active, and intercept keystrokes as they type. That's kind of messy, though.
2) Another option might be to try to grab or replace the component that the table is using to actually let the user make the changes. I think that JTable actually allows you to change the editor component if you dig around.

Categories