Implementing few excel functionality using JTable - java

I am trying to implement Summation function of MS EXCEL but not getting a way to get the values from the JTable cells. Can you suggest me some way to get the values from the cells and use it for my function?

For example, how can you get the value -
String data[][] = {{"Value1", "Value2", "Value3"},{"Value4", "Value5", "Value6"},
{"Value7", "Value8", "Value9"},{"Value10", "Value11", "Value12"}};
String col[] = {"Column1", "Column2", "Column3"};
DefaultTableModel model = new DefaultTableModel(data, col);
JTable table = new JTable(model);
...
System.out.println(table.getModel().getValueAt(2, 2)); // row index and column index
...
it gives -
Value9
To get the values ​​from the table, you can implement the interface MouseListener in the anonymous inner class, for example -
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
JTable target = (JTable) e.getSource();
int rowIndex = target.getSelectedRow();
int columnIndex = target.getSelectedColumn();
System.out.println(target.getModel().getValueAt(rowIndex, columnIndex));
}
});
See also:
How to Write a Mouse Listener
How to Write a List Selection Listener

Related

Remove highlighted columns and data from jtable

Is there a way I can delete the highlighted selected columns in this jtable using the remove button? I know there's a way for rows but I'm not sure how to do this for selected columns.
private void RemoveColBActionPerformed(java.awt.event.ActionEvent evt) {
// Removes the highlighted column
}
private void AddBActionPerformed(java.awt.event.ActionEvent evt) {
//Add Data
lMessage.setText("");
DefaultTableModel model = (DefaultTableModel) JtableData.getModel();
if (!ProdNameTF.getText().trim().equals("")) {
model.addRow(new Object[] {
ProdNameTF.getText(), CategoryCB.getSelectedItem().toString(), PriceTF.getText()
});
} else {
lMessage.setText("Message Left Blank");
}
}
You can remove columns from the JTable view. The data will still be contained in the TableModel, it just won't be displayed in the JTable.
So the basic code would be:
TableColumnModel tcm = table.getColumnModel();
tcm.removeColumn( tcm.getColumn(...) );
For a more complex solution that allows the user to hide/show columns as they wish check out the Table Column Manager.

How to Insert row data from database into specific columns of a JTable?

I have written a GUI Java program that manages a MySQL database. The user selects which columns (which tables and columns will be selected from the database) he/she wants to populate the JTable with.
I hard-coded the column names for the JTable so even if the user chooses to only display the data from a subset of columns, all of the column-names will be visible.
The problem is that when the user chooses columns in a different order than my JTable is anticipating, the data gets displayed in the wrong column.. It's a bit hard to explain so here's a screenshot of the genre data being loaded into the length column:
tableGenerator class:
package gui;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
public class TableGenerator
{
private ArrayList columnNames = new ArrayList();
private ArrayList data = new ArrayList();
private Vector columnNamesVector = new Vector();
private Vector dataVector = new Vector();
private int columns = 0;
private int rows = 0;
#SuppressWarnings("unchecked")
public TableGenerator(ResultSet rs)
{
try{
ResultSetMetaData md = rs.getMetaData();
columns = md.getColumnCount();
// Get column names
columnNames.add("Title");
columnNames.add("Year");
columnNames.add("Length");
columnNames.add("Genre");
columnNames.add("Actor");
columnNames.add("Producer");
columnNames.add("Director");
columnNames.add("Writer");
// Get row data
while (rs.next())
{
ArrayList row = new ArrayList(columnNames.size());
for (int i = 1; i <= columns; i++)
{
row.add(rs.getObject(i));
}
data.add( row );
rows++;
}
}
catch (SQLException e)
{
System.out.println( e.getMessage() );
}
// Create Vectors and copy over elements from ArrayLists to them
// Vector is deprecated but I am using them in this example to keep
// things simple - the best practice would be to create a custom defined
// class which inherits from the AbstractTableModel class
for (int i = 0; i < data.size(); i++)
{
ArrayList subArray = (ArrayList)data.get(i);
Vector subVector = new Vector();
for (int j = 0; j < subArray.size(); j++)
{
subVector.add(subArray.get(j));
}
dataVector.add(subVector);
}
for (int i = 0; i < columnNames.size(); i++ )
columnNamesVector.add(columnNames.get(i));
}
public Vector getColumns(){
return columnNamesVector;
}
public Vector getData(){
return dataVector;
}
public ArrayList getColumnNames(){
return columnNames;
}
public int getNumberOfRows(){
return rows;
}
}
I'm using the DefaultTableModel with some modifications.. :
model = new DefaultTableModel(rows, columns){
private static final long serialVersionUID = 1L;
#Override
public boolean isCellEditable(int row, int column) {
return false;
}
#Override
public Class<?> getColumnClass(int column) {
if (column < classes.length)
return classes[column];
return super.getColumnClass(column);
};};
Your query should always return the data for all columns. This means the data will be stored in the same manner in the TableModel.
You can then change the view for the columns to be displayed. That is you can remove TableColumns from the TableColumnModel of the JTable and only the data the user want to view will be displayed, even though it is still available in the model. Then means the user can click on any check box at any time and you don't need to redo the database query, only add the TableColumn back to the table.
Check out Table Column Manager for an example of this approach. This class uses a popup menu to manage the columns, but you can still use your check boxes. You just need to invoke the appropriate method of the TableColumnManager to hide/show a column. That is, assuming the labels of the check boxes match the headings in the table you can just use the check box text to hide/show a column.
The other approach is to NOT hard code the column names (if you build your query to only get specific columns) but instead get the column names from the meta data of the ResultSet. The TableFromDatabaseExample.java from Table From Database shows how this can be done. The code is generic so that appropriate renderers are used for Dates, Integers etc.

Scroll to last added row in JTable

I use a HashMap to fill a JTable, which is more or less continuously updated:
public Map< Long, MyObject > tableData = new HashMap< Long, MyObject >();
Every time a new element is added to the map the table model is notified:
tableData.put(id, anObject);
AbstractTableModel atm = (AbstractTableModel)model;
atm.fireTableDataChanged();
In Addition I have a TableRowSorter which sorts the rows according to a specific criteria:
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
.
.
.
table.setRowSorter(sorter);
My goal is that the (vertical) scrollbar always jumps to the last added row, which can be somwhere in the mid
of the table because of the sorter probably using this:
table.scrollRectToVisible(table.getCellRect(row,0, true));
The problem is I do not know the index of the row :) Where can I hook in to get this index?
Scrolling to a newly inserted row in a potentially sorted table involves
listening to changes in the table model
converting the rowIndex of the event (it is in model coordiates) to view coordinates
scrolling to the view position
In code:
final JTable table = new JTable();
TableModelListener l = new TableModelListener() {
#Override
public void tableChanged(TableModelEvent e) {
if (TableUtilities.isInsert(e)) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
int viewRow = table.convertRowIndexToView(e.getFirstRow());
table.scrollRectToVisible(table.getCellRect(viewRow, 0, true));
}
});
}
}
};
table.getModel().addTableModelListener(l);
Two important aspects:
your model implemenation must fire the correct event, that is a insert, not a dataChanged
invoking both index conversion and scrolling guarantees that the table (which is listening to the model as well) has updated all internal state according to the model change.

create TableModel and populate jTable dynamically

I want to store the results of reading lucene index into jTable, so that I can make it sortable by different columns. From index I am reading terms with different measures of their frequencies.
Table columns are these :
[string term][int absFrequency][int docFrequency][double invFrequency]
So i in AbstractTableModel I can define column names, but i dont know how to get the Object[][]data with results from the following method:
public static void FrequencyMap(Directory indexDir) throws Exception
{
List<ArrayList>redoviLista = new ArrayList<ArrayList>();
//final Map<String,TermRow> map = new TreeMap<String,TermRow>();
List<String>termList = new ArrayList<String>();
IndexReader iReader = IndexReader.open(indexDir);
FilterIndexReader fReader = new FilterIndexReader(iReader);
int numOfDocs = fReader.numDocs();
TermEnum terms = fReader.terms();
while (terms.next()){
Term term = terms.term();
String termText = term.text();
termList.add(termText);
//Calculating the frequencies
int df = iReader.docFreq(term);
double idf = 0.0F;
idf = Math.log10((double) numOfDocs / df);
double tfidf = (df*idf);
//Here comes important part
//Changes according to takoi's answer
ArrayList<Object> oneRow = new ArrayList<Object>();
oneRow.add(termText);
oneRow.add(df);
oneRow.add(idf);
oneRow.add(tfidf);
redoviLista.add(oneRow);
}
iReader.close();
// So I need something like this, and i Neeed this array to be stored out of this method
So I am kindda stuck here to proceed to implement AbstractTableModel and populate and display this table .... :/
Please help!
When you are inserting, deleting or updating data in your model, you need to notify the GUI of the changes. You can do this with the fire-methods in the AbstractTableModel.
I.e. if you add an element to your list, you also have to call fireTableRowsInserted(int firstRow, int lastRow) so that the visible layer can be updated.
Have a look at addElement(MyElement e) in the code below:
public class MyModel extends AbstractTableModel {
private static final String[] columnNames = {"column 1", "column 2"};
private final LinkedList<MyElement> list;
private MyModel() {
list = new LinkedList<MyElement>();
}
public void addElement(MyElement e) {
// Adds the element in the last position in the list
list.add(e);
fireTableRowsInserted(list.size()-1, list.size()-1);
}
#Override
public int getColumnCount() {
return columnNames.length;
}
#Override
public int getRowCount() {
return list.size();
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
switch(columnIndex) {
case 0: return list.get(rowIndex).getColumnOne();
case 1: return list.get(rowIndex).getColumnOne();
}
return null;
}
}
There is no need to create a custom TableModel for this. Just use the DefaultListModel.
The DefaultListModel allows you to dynamcially add rows to the model using the addRow(...) method and it automatically invokes the appropriate fireXXX method to tell the table to repaint itself.
The basic code would be:
DefaultTableModel model = new DefaultTableModel( columnNames );
while (...)
{
Vector row = new Vector();
row.add(...)
row.add(...)
model.addRow( row );
}
JTable table = new JTable( model );
You don't need to create a custom TableModel everytime. Sometimes you can start with the DefaultTableModel.
You dont have to create an Object[][]. Just make your redoviLista a list of lists:
redoviLista.add( new ArrayList<Object>(termText, df, idf, tfidf) ); #pseudocode
then you implement getValueAt like this:
getValueAt(int row, int column){
redoviLista.get(row).get(column);
}

Manipulating fields in the jTable Java

I have populated my jTable with the following Code. It has two columns, the first has variable name and the second is a list of its dependencies. The user can change the dependencies by selecting them from the list in the jTable.
When the user changes a value, I want to row to be added to another jTable (which would be no user editable. How would I do that?
The code to populate the table is
Vector<Vector> data = new Vector<Vector>();
for (String v : acn.getVariableNames()) {
Vector tmp = new Vector();
tmp.add(v);
ArrayList<String> temp = new ArrayList<String>();
for (String u : acn.getVariableDomain(v)) {
temp.add(u);
}
tmp.add(temp);
data.add(tmp);
}
Vector names = new Vector();
names.add("Variable");
names.add("Domain Value");
DefaultTableModel dt = new DefaultTableModel();
dt.setDataVector(data, names);
jTable2.setModel(dt);
jTable2.getColumnModel().getColumn(1).setCellEditor(new ChangeImpactEditor());
jTable2.getColumnModel().getColumn(1).setCellRenderer(new TableListRenderer());
The way i would do it is to override
public void setValueAt(Object aValue, int rowIndex, int columnIndex);
from your TableModel.
The setValue method get called by the JTable after the user has editted a value
In your overriden method you then can set the value in the other tablemodel

Categories

Resources