Java get field values from DefaultTableModel and format into array - arrays

I am having trouble getting two of my fields from DefaultTableModel and format them into String[].
Here is how I get data from desired fields.
model = new DataTable().Table();
int rowcount = model.getRowCount();
int[] columns = {0,1};
String[][] name = new String[columns.length][rowcount];
for (i = 0; i < columns.length; i++)
{
for (row = 0; row < rowcount; row++)
{
int column = columns[i];
name[i][row] = model.getValueAt(row, column).toString();
}
}
name[0] is firstname, name[1] is lastname. Now, would you please guide me how to combine these two fields as firstname, lastname and put it into String[] addressee?
Any inputs will be much appreciated.

I am not sure if I understand your problem. Do you need just to combine two fields?
You could create addressee directly from table.
String[] addressee = new String [rowcount];
for (row = 0; row < rowcount; row++){
addressee [row]= model.getValueAt(row, 0).toString()+" "+model.getValueAt(row, 1).toString()
}

Related

Can I create array based on field names that exist in PDF?

I am trying to count the number of a specific field which is not empty and would like to use an array to store the field names based on whether they exist in the PDF form.
My code looks like this so far and it works as long as the field names in the array exists, if that field does not exist then the code breaks.
The SiteDeficiency_DeficiencyNumber_# could have 0 instances or anywhere up to 50 instances or so.
I did find that you can check if a name exists with this code but I can't seem to get it to work.
Any help is appreciated. I have coded simple things but maybe this is just too advanced for me.
This is what my code looks like so far:
var aFields = new Array("SiteDeficiency_DeficiencyNumber_1",
"SiteDeficiency_DeficiencyNumber_2",
"SiteDeficiency_DeficiencyNumber_3",
"SiteDeficiency_DeficiencyNumber_4",
"SiteDeficiency_DeficiencyNumber_5",
"SiteDeficiency_DeficiencyNumber_6");
var count = 0;
for (i = 0; i < aFields.length; i++) {
if (this.getField(aFields[i]).valueAsString != "") count++
}
event.value = count;
I tried incorporating this too
if(document.forms[0].SiteDeficiency_DeficiencyNumber_1)
{
...
}
I would really like the array at the beginning to populate with only the SiteDeficiency_DeficiencyNumber_# fields that exist and then have the array looped through to count which fields are not blank.
I tried doing this but still does not work.
var aFields = [];
for(i = 1; i <101; i++)
{
if(document.forms[0].("SiteDeficiency_DeficiencyNumber_"+i).valueAsString.length > 0) //Checks if field exists
{
aFields.push("SiteDeficiency_DeficiencyNumber_"+i); //adds field name to array
}
}
var count = 0;
for (a = 0; a < aFields.length; a++) {
if (this.getField(aFields[a]).valueAsString != "") count++ //counts field if not empty
}
event.value = count;
Try this code:
var count = 0;
for(var i = 0; i < this.numFields; i++)
{
if (this.getNthFieldName(i).indexOf("SiteDeficiency_DeficiencyNumber")!= -1){
if (this.getField(this.getNthFieldName(i)).valueAsString != "")
count = count +1
}
}
event.value = count;
The first if statement will check if name of the field contain string SiteDeficiency_DeficiencyNumber. If SiteDeficiency_DeficiencyNumber is present in the field naem then it will check the field value and increment accordingly.
Don't forget to upvote! let me know if you need anything else

Ignore blank values in WPF chart control

I am working on WPF application which includes WPF chart. I am facing a situation.
I want to draw the chart only for values by ignoring blank values.
In application the data is contained by a datagrid and same data will be reflected in the graph, but datagrid having blank values(DBNull.Value).
So, I want to generate graph with only values by ignoring the blank values.
Here is my code for generating graph.
for (int col = 1; col < dtGeneric.Columns.Count; col++)
{
valueList = new List<KeyValuePair<string, double>>();
gLineSeries= new System.Windows.Controls.DataVisualization.Charting.LineSeries();
for (int row = 0; row < dtGeneric.Rows.Count - 1; row++)
{
if (string.IsNullOrEmpty(XaxisValue))
{
XaxisValue = "0";
}
YAxisValue = dtGeneric.Rows[row][col].ToString();
if (string.IsNullOrEmpty(YAxisValue))
{
YAxisValue = "0";
}
valueList.Add(new KeyValuePair<string, double>(XaxisValue, Convert.ToDouble(YAxisValue)));
}
gLineSeries.DependentValuePath = "Value";
gLineSeries.Style = gLineSeries.PolylineStyle;
gLineSeries.IndependentValuePath = "Key";
gLineSeries.ItemsSource = valueList;
gLineSeries.Title = dtGeneric.Columns[col].Caption.Replace('_', '.').ToString();
gLineSeries.AnimationSequence = AnimationSequence.FirstToLast;
chartControl.Series.Add(gLineSeries);
}
}
As you can see in the code, I have used keyvaluepair to draw the graph. So I am unable to add null value in the Value of keyvaluepair. I have tried with double.NaN but that is not working.
I have iterated all the columns because all the column will have its separate graph.
I have tried one logic to create the graph which is:
for (int col = 1; col < dtGeneric.Columns.Count; col++)
{
valueList = new List<KeyValuePair<string, double>>();
gPositionLineSeries = new System.Windows.Controls.DataVisualization.Charting.LineSeries();
for (int row = 0; row < dtGeneric.Rows.Count - 1; row++)
{
if (!string.IsNullOrEmpty(dtGeneric.Rows[row][0].ToString()) && !string.IsNullOrEmpty(dtGeneric.Rows[row][col].ToString())) //Null values will be ignored for graph generation...
{
XaxisValue = dtGeneric.Rows[row][0].ToString();
YAxisValue = dtGeneric.Rows[row][col].ToString();
valueList.Add(new KeyValuePair<string, double>(XaxisValue, Convert.ToDouble(YAxisValue)));
}
else
{
continue;
}
}
}
Above code is working fine but the X-axis values in the graph is not in order.
Please tell me some solution.
use below code in if block
string.IsNullOrEmpty(row.Cells[clm.Index].Value.ToString())

Items and Subitems in List-View control

I'm want to use a List-View control to display results of an LDAP search in a "grid". I've written some test code to see how it works, but it's not being displayed as I want. As I understand it, each Item is equivalent to a "row" (using LVS_REPORTstyle), and the Subitem is equivalent to a "column" (e.g. for each item I can display a number of subitems, each in a separate column on the same row).
Here's my test code, currently set to create four columns, with a single Item and four Subitems (corresponding to the four columns). Two functions: one to create the columns, the other to insert items.
int CreateColumns(HWND *hwndlistbox)
{
wchar_t *cnames[100];
LVCOLUMN lvc;
int i;
cnames[0] = L"column1";
cnames[1] = L"column2";
cnames[2] = L"column3";
cnames[3] = L"column4";
cnames[4] = NULL;
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
for (i = 0; cnames[i] != NULL; i++)
{
lvc.iSubItem = i;
lvc.pszText = cnames[i];
lvc.cx = 100;
lvc.fmt = LVCFMT_LEFT;
ListView_InsertColumn(*hwndlistbox, i, &lvc);
}
return i;
}
void InsertItems(HWND *hwndlistbox, int *columncount)
{
LVITEM lvi;
wchar_t *items[100];
int i, j;
items[0] = L"text1";
items[1] = L"text2";
items[2] = L"text3";
items[3] = L"text4";
items[4] = NULL;
lvi.mask = LVIF_TEXT;
lvi.iItem = 0;
for (i = 0; i < *columncount; i++)
{
lvi.pszText = items[i];
lvi.iSubItem = i;
ListView_InsertItem(*hwndlistbox, &lvi);
}
}
I expect this to generate a single row (lvi.iItem = 0;) with a text string under each column (lvi.iSubItem = i;). This is what it displays instead:
Changing lvi.iSubItem = i to lvi.iSubItem = 0 results in each text string being displayed as a new row in the first column:
I've played around with it, hardcoding the numbers on both iItem and iSubItem, changing both to i, but I can't get it to display the text anywhere other than the first column. What am I doing wrong?
First of all, your cnames and items arrays are declared as array of pointers, but you are not allocating memory for them; you would need to declare them as an array of strings, like wchar_t cnames[100][40];.
Secondly, you need to use ListView_InsertItem to insert an item and set the value for the first column, then use ListView_SetItem to add additional columns, like
lvi.pszText = items[0];
lvi.iSubItem = 0;
ListView_InsertItem(*hwndlistbox, &lvi);
for (i = 1; i < *columncount; i++)
{ lvi.pszText = items[i];
lvi.iSubItem = i;
ListView_SetItem(*hwndlistbox, &lvi);
}
Each row shows a single item so you cannot populate the columns by adding items.
As the documentation says:
"You cannot use ListView_InsertItem or LVM_INSERTITEM to insert subitems. The iSubItem member of the LVITEM structure must be zero. See LVM_SETITEM for information on setting subitems."
The LVM_SETITEM documentation explains how to set the text of a sub-item.

jCombobox giving incremental value error on jTable

I am having problems with my code below, the code below shows a jComboBox being populated, when i select an item from this list it is added to the jTable below it.
There is alos code to check for duplicate entries ont he table. If a duplicate entry is found it should increase the qty column by one and not create a seperate entry.
This is where the problem comes in, when I press the back button on this screen and go to a different screen and then come back via same route as the first time, I get an incrementally different qty added to the table row/cell.
I have also included the code that populates the Round Details depending on Round Drop selected from table, for reference, but Im fairly certain the problem lies in the below code. The navigation is as follows...
To get to the below screen... Round Drop panel table of round drops) >> click on table row and taken to associated round details panel >> pressing the Till button takes user to screen with code below...
Test results:
First pass through below code using navigation above gives results as expected
Second pass gives an initial value of 2 (instead of one), and duplicate row increases qty by 2 instead of one
Third pass gives an initial value of 3 (instead of one), and duplicate row increases qty by 3 instead of one
Fourth pass gives an initial value of 4 (instead of one), and duplicate row increases qty by 4 instead of one
...and so on.
Any help, guidance on solution or a better design would be hugely appreciated.
Thanks
/*************Code sample ********************************/
public void tillOperations(String sourceCall) {
final DefaultTableModel model = (DefaultTableModel)main.tillPanel.tblTillSale.getModel();
if (main.tillPanel.cmbTillProdSelect.getItemCount() < 1) {
for (int d = 0; d < roundStockObj.length ; d++) {
main.tillPanel.cmbTillProdSelect.addItem(roundStockObj[d].getDescription());
}}
main.tillPanel.tblTillSale.removeRowSelectionInterval(0, model.getRowCount() - 1);
main.tillPanel.cmbTillProdSelect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent f)
{
int qty = 1;
for (int index = 0; index < 4; index++) {
addSelectedItem[index] = "";
}
int row;
selectedItem = null;
main.tillPanel.tblTillSale.removeRowSelectionInterval(0, model.getRowCount() - 1);
selectedItem = main.tillPanel.cmbTillProdSelect.getSelectedItem();
for (int d = 0; d < roundStockObj.length; d++) {
if (selectedItem.equals(roundStockObj[d].getDescription())) {
addSelectedItem[0] = roundStockObj[d].getDescription();
addSelectedItem[1] = Integer.toString(qty);
addSelectedItem[2] = Double.toString(roundStockObj[d].getPrice()).trim();
addSelectedItem[3] = Double.toString(roundStockObj[d].getPrice()).trim();
//break;
}
}
if(model.getRowCount() == 0) { //check if model is empty
model.addRow(new String[]{addSelectedItem[0], addSelectedItem[1], addSelectedItem[2], addSelectedItem[3]});
}
else { //check if there is a duplicate row
int duplicateRow = -1;
for (row = 0 ; row < model.getRowCount(); row++) {
if(addSelectedItem[0].equals(main.tillPanel.tblTillSale.getModel().getValueAt(row,0))) {
duplicateRow = row;
break;
}
}
if(duplicateRow == -1) { //if there is no duplicate row, append
model.addRow(new String[]{addSelectedItem[0], addSelectedItem[1], addSelectedItem[2], addSelectedItem[3]});
}
else { //if there is a duplicate row, update
main.tillPanel.jLabel1.setText(addSelectedItem[1]);
DecimalFormat fmtObj = new DecimalFormat("####0.00");
int currentValue = Integer.parseInt(main.tillPanel.tblTillSale.getValueAt(row, 1).toString().trim());
int newValue = currentValue + 1;
Integer newValueInt = new Integer(newValue);
model.setValueAt(newValueInt, row, 1);
double unitPrice = Double.parseDouble(main.tillPanel.tblTillSale.getValueAt(row, 2).toString().trim());
double newPrice = newValue * unitPrice;
Double newPriceDbl = new Double(newPrice);
main.tillPanel.tblTillSale.setValueAt(fmtObj.format(newPriceDbl), row, 3);
}
}
main.tillPanel.tblTillSale.removeRowSelectionInterval(0, model.getRowCount() - 1);
for (int index = 0; index < 4; index++) {
addSelectedItem[index] = "";
}
}
});
//This code loads the specific Round Details, based on the selection form the round drops table
public void displayRoundDropDetails() {
DefaultTableModel model = (DefaultTableModel)main.selectRoundDropPanel.tblSelectRoundDrop.getModel();
if (!loaded) {
for (int d = 0; d < roundDropsData.length; d++) {
if (roundDropsData[d][0].equals(defaultRoundID)) {
model.addRow(new Object[]{roundDropsData[d][3], roundDropsData[d][2],
roundDropsData[d][4], roundDropsData[d][5]});
}
}
loaded = true;
}
main.selectRoundDropPanel.tblSelectRoundDrop.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent evt)
{
int row = 0;
row = main.selectRoundDropPanel.tblSelectRoundDrop.getSelectedRow();
for (int index = 0; index < roundDropsData.length; index++) {
if (roundDropsData[index][3].equals(
main.selectRoundDropPanel.tblSelectRoundDrop.getModel().getValueAt(row, 0))) {
main.roundDetailsPanel.txtRoundDetailsAddress.setText(roundDropsData[index][6] + "\n"
+ roundDropsData[index][7] + ", " + roundDropsData[index][8] + "\n" +
roundDropsData[index][9]);
main.roundDetailsPanel.lblRoundDetailsName.setText(roundDropsData[index][2]);
main.roundDetailsPanel.txtRoundDetailsInstuct.setText(roundDropsData[index][10]);
main.roundDetailsPanel.txtDropDetailsIn.setText(roundDropsData[index][4]);
main.roundDetailsPanel.txtDropDetailsOut.setText(roundDropsData[index][5]);
main.roundDetailsPanel.txtRoundDetailsInstruct.setText(roundDropsData[index][12]);
break;
}
}
Globals.CURRENT_COMPONENT = "selectRoundDropPanel";
showRoundDetailsPanel();
}
});
}
Try changing the listener for JComboBox. try using stateChangeListener.

Best way to display and Edit a 2D array in WPF

Seems like this would be easy to do, but it appears its not so simple. I have a 2d array of floats or ints and I'd like to display it in a grid like control so it acts similar to Excel in regards to being able to move around with the arrow keys, tab keys, etc. The size of the array will vary. This comes close, but works well only for displaying:
How to populate a WPF grid based on a 2-dimensional array
I found the easiest way would be to use datatables and create one dynamically :
DataTable dt = new DataTable();
int nbColumns = 10;
int nbRows = 5;
for (int i = 0; i < nbColumns; i++)
{
dt.Columns.Add(i.ToString(), typeof(double));
}
for (int row = 0; row < nbRows; row++)
{
DataRow dr = dt.NewRow();
for (int col = 0; col < nbColumns; col++)
{
dr[col] = col;
}
dt.Rows.Add(dr);
}
myDataGrid.ItemsSource = dt.DefaultView;
Of course this is just a random table, you can load your 2d or Xd array in your DataTable. And also, you don't have to implement IEnumerable and stuff...

Resources