Put values in JTable, using setModel(); - java

I created JTable, using Design View - Table.
But, I want the values to come from SQL query, not to be inserted manulally.
So I select on table: model -> Custom Code, then there is: jTable1.setModel();
What I can put there? I tried with methods to return value, but NetBeans tell me, String cannot to be converted to TableModel.
I can't modify the code initComponents(), which is generated from NetBeans, I can only put method there.
So how to get values from SQL or what I can use here setModel() to retrieve the result from SQL.
Table: 3 columns x 7 lines
The code:
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTable1.setModel(resultsTable());
jTable1.setToolTipText("");
jScrollPane1.setViewportView(jTable1);
... more code for the frame .....

You could also make your table model by extending AbstractTableModel class and overriding appropriate methods.
Take a look at this https://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data

I found very easy solution:
https://www.youtube.com/watch?v=fbYxThOFsLI
This is exactly what I want, using Table, generated from NetBeans.
Thanks to everyone!

Related

Is there a way to create pivot table with model table name using Apache POI?

I need to add an extra column using a custom formula which has to refer to the generated pivot table field value. So, I'm looking for a way to name the pivot table(Data model name) and then refer the generated value using that name.
I have tried to set value for name field of CTCacheField, but it doesn't create/name the model table name.
Here is the code that I have tried,
public void getScoreInPivotTable(String fieldName) {
CTCacheFields ctCacheFields = pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().getCacheFields();
CTCacheField ctCacheField = ctCacheFields.addNewCacheField();
ctCacheField.setName(fieldName);
ctCacheField.setDatabaseField(false);
ctCacheField.setFormula("'SUM(APPHNDL)'");
ctCacheField.setNumFmtId(1);
ctCacheFields.setCount(ctCacheFields.sizeOfCacheFieldArray());
}
I'm attaching screenshot of what I'm trying to achieve.
PS: I want to set model table name for the pivot table.
Please let me know if there is any other way to achieve this?
Please feel free to improve this question.

Netbeans MVC Swing JTable ResultSet data insert

I'm new to Java, Swing and programming, but I need to make an application. I'm using NetBeans for development and here's my question:
If I drag and drop the JTable to my frame and select custom code for the table, I get this code:
appView.java
tblViewData = new javax.swing.JTable();
tblViewData.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"ID", "Reference No", "Name", "Description"
}
));
jScrollPane1.setViewportView(tblViewData);
What I want to do is populate new Objects[][] with data that I fetch from the appController using ResultSet and the values I set in the appModel for variables ID, Ref, Name and Desc.
Here's a sample code of an insert I attempted to see if I could display a row of data:
new Object [][] {
{ new Integer(model.getID()), model.getRef(), model.getName(), model.getDesc() }
},
This code returns a 0 in the first column of the row.
Am I approaching this problem wrong?
Thanks for the help.
Well I have no idea what the "appController" or "appModel" are. Maybe these are IDE dependent classes? Instead of using the IDE to generate your SQL code I suggest you write your own code and populate the table yourself.
There are plenty of examples on the forum showing how to load a JTable with data from a ResultSet. Just search for examples using those two class names as your keywords.
Or you can check out Table From Database which shows how you can use the DefaultTableModel to load rows of data from the ResultSet.

nattable multiple row selection

I am using NatTable and I want to select some rows in my table. Additional I want to select some other rows after this (CTRL + left mouse-click).
There is no problem with the first selection, but when I try to select some additional rows, my first selection got lost. This happens only if I do my second selection in dragmode. When I select every additional row by single clicking everything works fine.
I used the RowSelectionModel with the DefaultRowSelectionLayerConfiguration:
selectionLayer.setSelectionModel(new RowSelectionModel<Entry>(selectionLayer, bodyDataProvider, new IRowIdAccessor<Entry>() {
#Override
public Serializable getRowId(Entry rowObject) {
return rowObject.getStartLine();
}
}));
selectionLayer.addConfiguration(new DefaultRowSelectionLayerConfiguration());
Maybe I just did a silly mistake and you guys can help me.
It looks like an issue in the NatTable code. I created a ticket for this: https://bugs.eclipse.org/bugs/show_bug.cgi?id=494392

CassandraTemplate.execute Batch statements help needed in Spring

I am having difficult time in finding a sample program that uses the execute of batch statement as argument for org.springframework.data.cassandra.core.CassandraTemplate;
Basically I am trying to do multiple insert as a batch.
CqlTemplate cqltemplate = new CqlTemplate(session);
cqltemplate.execute(Batch arg0);
How does it all come together? Also batch has issues in dealing with inserting multiple records to any unknown table (not linked to entity class). My project requires a method to do multiple insert for a given table and hashmap of key and values (row data) - which does not have an equivalent POJO Class. Any suggestions on how to achieve this?
I was able to work it out. Thank you for the guidance. Sorry for posting it late:
Insert insert1 = QueryBuilder.insert...
Batch batch = QueryBuilder.batch(insert1);
Insert insert2 = QueryBuilder.insert...
batch.add(insert2);
CassandraOperations cassandraOperations = new CassandraTemplate(session);
WriteOptions options = new WriteOptions();
options.setTtl(60);
options.setConsistencyLevel(ConsistencyLevel.ONE);
options.setRetryPolicy(RetryPolicy.DOWNGRADING_CONSISTENCY);
cassandraOperations.execute(batch.toString(), options);
According to the reference you need to create an object of class com.datastax.driver.core.querybuilder.Batch.
You can create with com.datastax.driver.core.querybuilder.QueryBuilder batch method. CQLTemplate should not be created in the code, it should be injected in the configuration:
CQLTemplate cqlTemplate=new CQLTemplate();
yourServiceBean.setCQLTemplate(cqlTemplate);
And in your service/dao it would be something like:
Batch batch=QueryBuilder.batch (...)
cqlTemplate.execute(batch);

SmartGWT ListGrid - exclude field from selection

I would like to ask for your help to solve the following problem.
I have a SmartGwt ListGrid wich has multiple rows in it.
This ListGrid has a SelectionChangedHandler which works just fine.
I added a special column (ListGridField) to this ListGrid, on which basically I want to prevent the selectionChangeEvent to trigger when clicked.
This special column has its own recordclickHandler.
I only want to EXCLUDE this column form changing the selected record in the ListGrid.
Is there any way to do so in your knowledge?
Thanks in advance.
Since the event for row selection doesn't tell you which cell you clicked on, hence no way of telling which column, I think you'll need to make cells selectable, and ignore the event if the cell is in the excluded column.
myGrid.setCanSelectCells(true);
myGrid.addCellSelectionChangedHandler(new CellSelectionChangedHandler() {
public void onCellSelectionChanged(CellSelectionChangedEvent event) {
CellSelection selection = countryGrid.getCellSelection();
//use to determine if excluded column is clicked:
int[][] selectedCells = selection.getSelectedCells();
//use to get selected row:
ListGridRecord record = selection.getSelectedRecord();
//etc...
}
}

Categories