create a method where the variable is a jTable - java

I have 3 different buttons on java, depending on with I press I want to take a value from one of 3 different Tables I have so:
public MethodName (String Table) {
String value=Table.getValueAt(1,1);
Return value;
}
how can this be done so Table is substitute with jTable1, jTable2, or jTable3?
is this possible?

Related

DynamicReports remove row if column is null

I am a little bit stuck with such an easy problem. I am using DynamicReports and I want to hide whole row if column value is null. As I know, DynamicReports is based on JasperReports and it's possible there to do that by checking TextField's option "Remove line when blank". How can I do that in Dynamic?
Components, which I use:
TextColumnBuilder, ColumnGroupBuilder, JasperReportBuilder
I want to hide whole row if any of my TextColumns would be null.
OK, after some thoughts, I found that this problem could be resolved in other way.
We gonna use column, group etc property setPrintWhenExpression(DRIExpression expression)
1. Create class, which will handle, print or not to print the row. Dynamic has the abstract class for doing this:
public class ShowExpressionDynamicReports extends AbstractSimpleExpression<Boolean> {
private String fieldName;
public ShowExpressionDynamicReports(String fieldName) {
this.fieldName = fieldName;
}
#Override
public Boolean evaluate(net.sf.dynamicreports.report.definition.ReportParameters reportParameters) {
return reportParameters.getValue(fieldName) != null;
}
}
You should extend AbstractSimpleExpression in order to pass it as parameter to methods which are listed below.
So, the column value is printed if evaluate(ReportParameters rp) returns true.
I've also added field fieldName which allows me to print (or not) column due to other's column state.
2. Add property to your
column:
setPrintWhenExpression(DRIExpression expression)
group:
.setPrintSubtotalsWhenExpression(DRIExpression expression)
or
setFooterPrintWhenExpression(DRIExpression expression)
or
setHeaderPrintWhenExpression(DRIExpression expression)
Depends on what you want to hide.
Example:
We have 2 columns in our report: Product and ProductCount columns. I would like to hide Product column if ProductCount is null (we have no information for this Product).
So, to do that i will add property PrintWhenExpression to Product column
TextColumnBuilder<String> productColumn = col.column("Product", "Product", type.stringType())
.setPrintWhenExpression(new ShowExpressionDynamicReports("ProductCount"));

Want to convert java struts 2 static code into dynamic

I have been assign to one struts2 project and its one of jsp contains more than 100 radio buttons and they have handled in statically not dynamically. As jsp contains 100 radio buttons so I am able to see the below list of radio buttons catches in actions with their getter and setter
List selectRadioList001
List selectRadioList002
List selectRadioList003
List selectRadioList004
etc
List selectRadioList100
I want to add these radio button in a list dynamically iterating through 1 to 100 something like below but when I try to access the variable like "searchBoxSelectRadioList"+i then it is pretending like a simple string. I want it to be like a List as shown above.
public class SelectRadioListPOJO {
private List<TicketDesignUtil> selectRadioList;
public List<TicketDesignUtil> getSelectRadioList() {
return selectRadioList;
}
public void setSelectRadioList(List<TicketDesignUtil> selectRadioList) {
this.selectRadioList = selectRadioList;
}
}
Action code:
List<SelectRadioListPOJO> selectRadioListPOJOList = new ArrayList<>();
SelectRadioListPOJO selectRadioListPOJO;
for (int i = 1; i <= 100; i++) {
selectRadioListPOJO = new SelectRadioListPOJO();
selectRadioListPOJO.setSelectRadioList("searchBoxSelectRadioList"+i);// ERROR
selectRadioListPOJOList.add(selectRadioListPOJO);
}
It's not clear what you're asking.
You can't pass arbitrary values to methods; setSelectRadioList takes a list of TicketDesignUtil.
If your action doesn't have getters and setters for all of those radio buttons then you should resort to accessing the request parameters directly, for example, via ParameterAware.
You would then access the radio button parameters by name from the injected parameter map.
Notes:
It's not "pretending" to be a simple string, it is a simple string, because... well, because it is.
Your for loop is wrong; I corrected it in your question to avoid others commenting on it. The POJO should be added to the POJOList inside the loop.
Naming is funky; just call it selectRadioListPojos. Better yet, name it something domain-specific: variables should be semantically meaningful, not just a description of the class(es) involved.
These shouldn't be static in the first place, but a map or array.

GXT 3 Use ListStore to display one row per value of a Set

i'm using a Liststore to display data from a DTO object into a grid.
most of the object attributes are string and can easily be displayed. But one of the parameters is a Set of strings
To sum up, my object looks as following:
public class MyObject{
private String param1; // "val1"
private String param2; // "val2"
private Set<String> param3; // param3 contains "value3-1", "value3-2" and "value3-3"
...
}
I'd like to display a row in my grid for each element in my param3.
Something like that:
param1 | param2 | param3
val1 | val2 | value3-1
val1 | val2 | value3-2
val1 | val2 | value3-3
Do you know a simple way to do this by manipulating the ListStore?
Thank you
Each item in the ListStore corresponds to a row in the grid - as such, you need to put data in the grid. You should be able to easily iterate over the list of MyObject instances and convert them into MyObjectRow instances - which could even contain a reference to the 'real' MyObject instance for easier editing/updating.
However, since it is a Set, you'll want to be careful - sets have no order! This means you might not get value3-1, value3-2 value3-3, but they could arrive in any order. Strongly consider using a List instead of order matters to you at all.
With a List then, you could have MyObjectRow look like this:
public class MyObjectRow {
private MyObject wrapped;
private int index;
//...
public String getParam1() {
return wrapped.getParam1();
}
public String getParam2() {
return wrapped.getParam2();
}
public String getParam3() {
return wrapped.getParam3().get(index);
}
}
Then, for each MyObject, make N MyObjectRow, where N is the number of items in param3.

Bind HashTable/Map to Jtable

I have a C# background and I'm pretty new to Java. Trying to port my windows application to Mac using Java.
The issue I have is how to bind a HashTable that contains a class to a JTable so that the variables in the key show up in the JTable. In C# WPF it's very easy, just binding GridView.ItemSource to dictionary.keys. But in Java it seems much more complicated. Here is what I have so far:
Map<Files, String> files = new HashMap<Files,String>();
public class Files {
public Files(String files, String duration, String status) {}
}
public void AddFiles(String addfile, String addduration, String addstatus, String path){
files.put(new Files(
addfile, addduration, addstatus),
path);
}
In C# the class would look a little different, but I can just do GridView.Itemsource = files.Keys and voila, it all shows upp perfectly. How can I achieve something similar in Java?
I know that JTable can use a multidimensional array to load the values, so I am right now trying to load the values of HashTable into Object[][] tableData and then use:
String[] columnNames = {"File","Duration", "Status"};
final JTable table = new JTable(tableData, columnNames);
The problem is that I don't know how to access the variables inside the class "Files" inside the HashMap "files".
I know there is: .getKey().getClass() but I still haven't been able to load the multidimensional array with the values of the class "Files" inside the HashMap.
Create a tableModel extending from the base class AbstractTableModel. In here you'll have to override the appropriate methods (your IDE will point them out), the most important one being getValueAt().
Override this one in a fashion similar to:
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
switch (columnIndex) {
case 0:
return data.get(rowIndex).getValueX; // Change to however you'd like to call a single value from your map
case 1:
return data.get(rowIndex).getValueY;
case 2:
return data.get(rowIndex).getValueZ;
default:
throw new IndexOutOfBoundsException();
}
}
You can call your model with your data as a parameter. Afterwards, use this model as an argument to your JTable and you should be fine.
You can get the keys of a HashMap using HashMap.keySet() method.
This will return a Set of the key objects, which you have stored in your HashMap.
HashMap<Files, String> yourHashMap = new HashMap<Files, String>();
Set<Files> keys = yourHashMap.keySet();
Once you have got the set, assuming that you have the DefaultTableModel set to your JTable, you can do,
for(Files f : keys) {
yourDefaultTableModel.addRow(new String[]{f.files, f.duration, f.status});
}
NOTE:
Its always better to use a class which overrides equals() method & which is immutable to use as HashMap key.
Overriding equals() will avoid keys being overwritten incase of Hash collision, and making a class immutable doesn't make the value associated with the key orphan (since the key will not change once its created)
So I suggest using HashMap<String, Files> instead of HashMap<Files,String> if you feel making Files class immutable breaks its purpose.

Adding a database ID to an Eclipse Combo

I'm working on a Java application in Eclipse that pulls data out of a MySQL database. I'm populating a combo box with data. So far I can get the value of a field to show up but I can't figure out how to store the database row's unique ID value. One suggestion I found was to create a custom class that could store both the display value and the id value. However, this doesn't appear to work with the Eclipse widget combo object. This is what I have
import org.eclipse.swt.widgets.Combo;
class myClass {
public static void createCombo(ResultSet rs) {
Combo c = new Combo();
while(rs.next()) {
int id = rs.getInt("id");
int display = rs.getString("display");
comboitem ci = new comboitem(id,display);
c.add(ci);
}
}
}
class comboitem {
private int _id;
private String _display;
public comboitem(int id, String display) {
this._id = id;
this._display = display;
}
public int getID(){
return _id;
}
public String toString(){
return _display;
}
}
The above errors at c.add(ci). It's expecting a string, not an object. Is there a way to do this?
No idea but, I've always felt it was a bad move anyway.
Create a collection/list of comboitems, populate the widget from comboitem.display.
Index in the combo is index in your collection.
Means you can unit test lots of things without a UI or with simple mock, and it keeps you away from desktop specific implementations in your data models.
The combo widget displays an array of String's, so simply concatenate the two values if you want to display them both. I am not sure what your end goal is from your question. If it is to select the appropriate comboitem based on the combo selection, then store the comboitems in a Map and use the combo values as the keys.
Another approach is to use a jface ComboViewer which allows you to set the input to a complex object, provide a label provider and more complex controls around the Combo widget.
You should also look up some information on java coding conventions and not access your database directly from the UI unless this is a very simple application.
You can find some examples on using most SWT widgets here.

Categories

Resources