For some reason the searching isn't comparing strings right, according to my debugging they should be, something else must be wrong?
The actual search method when user clicks search - //SEARCH RECORDS
-
private void doSearch()
-
{
-
int rows = 0;
-
String search = tSearch.getText();
-
int index = cbSearch.getSelectedIndex();
-
String iSearch = "";
-
-
for(int i=0;i<tData.length;i++)
-
{
-
iSearch = tData[i][index];
-
if (iSearch.indexOf(search) > 0)
-
{
-
model.insertRow(rows,tData[i]);
-
rows++;
-
}
-
}
-
}
This is called before doSearch on button event too: - if(source == bSearch)
-
{
-
rc = model.getRowCount();
-
for(int i=(rc-1);i>-1;i--)
-
{
-
model.removeRow(i);
-
}
-
doSearch();
-
}
It should be adding the rows from String[][] tData that meet the comparison...
2 1526
Whole class just in case -
-
import javax.swing.*;
-
import javax.swing.border.*;
-
import java.awt.*;
-
import javax.swing.table.*;
-
import java.awt.event.*;
-
import java.util.*;
-
import java.text.*;
-
import java.io.*;
-
-
public class Sampol extends JFrame
-
{
-
private JPanel pMain, pMenu, pCont, pAddRecords, pSearch;
-
private JTable table;
-
private JButton bViewRecords, bAddRecords, bAddRecord, bExit, bSaveRecord, bDelete, bSearch, bSort, bShowAll;
-
private JScrollPane spCont;
-
private JTextField tSearch,tID,tName,tStock,tPrice,tDate,tGroup;
-
private JComboBox cbSearch, cbSort;
-
private String tData[][];
-
private DefaultTableModel model;
-
private Inventory inv = new Inventory();
-
-
public Sampol(String title) throws IOException
-
{
-
super(title);
-
setSize(800,600);
-
setDefaultCloseOperation(EXIT_ON_CLOSE);
-
setResizable(false);
-
-
pMain = new JPanel();
-
pMain.setLayout(new BorderLayout());
-
pMenu = new JPanel();
-
pMenu.setLayout(new GridLayout(10,1,0,5));
-
add(pMain);
-
pMain.add(pMenu, BorderLayout.WEST);
-
Border line = new LineBorder(Color.GRAY, 1);
-
pMenu.setBorder(new TitledBorder(line,"Navigation"));
-
-
bViewRecords = new JButton("Record(s) Table");
-
bViewRecords.addActionListener(new Listen());
-
bAddRecords = new JButton("Add Records");
-
bAddRecords.addActionListener(new Listen());
-
bExit = new JButton("Exit");
-
bExit.addActionListener(new Listen());
-
bSaveRecord = new JButton("Save Changes");
-
bSaveRecord.addActionListener(new Listen());
-
bDelete = new JButton("Delete Record");
-
bDelete.addActionListener(new Listen());
-
-
pMenu.add(new JLabel(""));
-
pMenu.add(bViewRecords);
-
pMenu.add(bAddRecords);
-
pMenu.add(new JLabel(""));
-
pMenu.add(bExit);
-
pMenu.add(new JLabel(""));
-
pMenu.add(bSaveRecord);
-
pMenu.add(bDelete);
-
-
String[] col = {"ID","Name","# In Stock","Price ($)","Added","Group"};
-
tData = inv.readData();
-
model = new DefaultTableModel(tData,col);
-
table = new JTable(model)
-
{
-
public boolean isCellEditable(int row, int column)
-
{
-
if(column == 0 || column == 4)
-
{
-
return false;
-
}
-
return true;
-
}
-
};
-
table.getColumnModel().getColumn(1).setPreferredWidth(120);
-
table.getColumnModel().getColumn(4).setPreferredWidth(120);
-
-
spCont = new JScrollPane(table);
-
pCont = new JPanel(new CardLayout());
-
pCont.add(spCont,"0");
-
pMain.add(pCont, BorderLayout.CENTER);
-
-
pSearch = new JPanel();
-
pSearch.setLayout(new GridLayout(2,3,1,1));
-
tSearch = new JTextField();
-
cbSearch = new JComboBox();
-
cbSearch.addItem(" ID");
-
cbSearch.addItem(" Name");
-
cbSearch.addItem(" Stock");
-
cbSearch.addItem(" Price");
-
cbSearch.addItem(" Date");
-
cbSearch.addItem(" Group");
-
bSearch = new JButton("Search");
-
bSearch.addActionListener(new Listen());
-
cbSort = new JComboBox();
-
cbSort.addItem("Name Alpha-numeric");
-
cbSort.addItem("By Price");
-
bSort = new JButton("Sort");
-
bShowAll = new JButton("Show All");
-
bShowAll.addActionListener(new Listen());
-
bSort.addActionListener(new Listen());
-
pSearch.add(bShowAll);
-
pSearch.add(cbSort);
-
pSearch.add(bSort);
-
pSearch.add(tSearch);
-
pSearch.add(cbSearch);
-
pSearch.add(bSearch);
-
pMain.add(pSearch, BorderLayout.NORTH);
-
-
pAddRecords = new JPanel(new GridLayout(20,1,5,5));
-
tID = new JTextField();
-
tID.setEnabled(false);
-
tName = new JTextField();
-
tStock = new JTextField();
-
tPrice = new JTextField();
-
tDate = new JTextField();
-
tDate.setEnabled(false);
-
tGroup = new JTextField();
-
bAddRecord = new JButton("Add Record");
-
bAddRecord.addActionListener(new Listen());
-
-
pAddRecords.add(new JLabel("")); pAddRecords.add(new JLabel("ID")); pAddRecords.add(tID);
-
pAddRecords.add(new JLabel("Name")); pAddRecords.add(tName);
-
pAddRecords.add(new JLabel("# in Stock")); pAddRecords.add(tStock);
-
pAddRecords.add(new JLabel("Price ($)")); pAddRecords.add(tPrice);
-
pAddRecords.add(new JLabel("Date Added")); pAddRecords.add(tDate);
-
pAddRecords.add(new JLabel("Group")); pAddRecords.add(tGroup);
-
pAddRecords.add(new JLabel("")); pAddRecords.add(bAddRecord);
-
-
pCont.add(pAddRecords,"1");
-
//END
-
setVisible(true);
-
}
-
-
//ADD/WRITE RECORD
-
private void addRec()
-
{
-
String[][] temp = new String[tData.length+1][6];
-
for(int i=0;i<tData.length;i++)
-
{
-
temp[i][0] = tData[i][0];
-
temp[i][1] = tData[i][1];
-
temp[i][2] = tData[i][2];
-
temp[i][3] = tData[i][3];
-
temp[i][4] = tData[i][4];
-
temp[i][5] = tData[i][5];
-
}
-
temp[tData.length][0] = tID.getText();
-
temp[tData.length][1] = tName.getText();
-
try{int test1 = Integer.parseInt(tStock.getText()); temp[tData.length][2] = tStock.getText();}catch(Exception e){temp[tData.length][2] = "0";}
-
try{double test2 = Double.parseDouble(tPrice.getText()); temp[tData.length][3] = tPrice.getText();}catch(Exception e){temp[tData.length][3] = "0.0";}
-
temp[tData.length][4] = tDate.getText();
-
temp[tData.length][5] = tGroup.getText();
-
-
tData = temp;
-
try{inv.writeData(tData);}catch(Exception e){}
-
-
tID.setText(""+(tData.length));
-
tName.setText("");
-
tStock.setText("");
-
tPrice.setText("");
-
tGroup.setText("");
-
-
model.insertRow(model.getRowCount(),tData[tData.length-1]);
-
}
-
-
//SAVE/WRITE RECORD
-
private void saveRec()
-
{
-
String temp[][] = new String[model.getRowCount()][6];
-
int index;
-
-
for(int i=0;i<model.getRowCount();i++)
-
{
-
temp[i][0] = (String)model.getValueAt(i,0);
-
temp[i][1] = (String)model.getValueAt(i,1);
-
temp[i][2] = (String)model.getValueAt(i,2);
-
temp[i][3] = (String)model.getValueAt(i,3);
-
temp[i][4] = (String)model.getValueAt(i,4);
-
temp[i][5] = (String)model.getValueAt(i,5);
-
}
-
for(int i=0;i<temp.length;i++)
-
{
-
tData[i][1] = temp[i][1];
-
tData[i][2] = temp[i][2];
-
tData[i][3] = temp[i][3];
-
tData[i][5] = temp[i][5];
-
}
-
try{inv.writeData(tData);}catch(Exception e){}
-
}
-
-
//DELETE/WRITE RECORD
-
private void delRecord(int sr)
-
{
-
String[][] temp = new String[tData.length-1][6];
-
for(int i=0;i<sr;i++)
-
{
-
temp[i] = tData[i];
-
}
-
for(int i=sr+1;i<tData.length;i++)
-
{
-
temp[i-1] = tData[i];
-
}
-
tData = temp;
-
try{inv.writeData(tData);}catch(Exception e){}
-
}
-
-
//SEARCH RECORDS
-
private void doSearch()
-
{
-
int rows = 0;
-
String search = tSearch.getText();
-
int index = cbSearch.getSelectedIndex();
-
String iSearch = "";
-
-
for(int i=0;i<tData.length;i++)
-
{
-
iSearch = tData[i][index];
-
if (iSearch.indexOf(search) > 0)
-
{
-
model.insertRow(rows,tData[i]);
-
rows++;
-
}
-
}
-
}
-
-
//** EVENT HANDLES ***********************************************************
-
private class Listen implements ActionListener
-
{
-
public void actionPerformed(ActionEvent e)
-
{
-
Object source = e.getSource();
-
CardLayout cl = (CardLayout)(pCont.getLayout());
-
Date d = new Date();
-
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
-
int rc;
-
-
if(source == bAddRecords)
-
{
-
pSearch.setVisible(false);
-
cl.show(pCont,"1");
-
tID.setText(""+(Integer.parseInt(tData[tData.length-1][0])+1));
-
tDate.setText(df.format(d));
-
repaint();
-
}
-
if(source == bViewRecords)
-
{
-
pSearch.setVisible(true);
-
cl.show(pCont,"0");
-
repaint();
-
}
-
if(source == bExit)
-
{
-
dispose();
-
}
-
if(source == bSaveRecord)
-
{
-
saveRec();
-
}
-
if(source == bDelete)
-
{
-
int sr = table.getSelectedRow();
-
if(sr != -1)
-
{
-
model.removeRow(sr);
-
delRecord(sr);
-
}
-
}
-
if(source == bAddRecord)
-
{
-
addRec();
-
}
-
if(source == bSearch)
-
{
-
rc = model.getRowCount();
-
for(int i=(rc-1);i>-1;i--)
-
{
-
model.removeRow(i);
-
}
-
doSearch();
-
}
-
if(source == bSort)
-
{
-
//doSort();
-
}
-
if(source == bShowAll)
-
{
-
rc = model.getRowCount();
-
for(int i=(rc-1);i>-1;i--)
-
{
-
model.removeRow(i);
-
}
-
for(int i=0;i<tData.length;i++)
-
{
-
model.insertRow(i,tData[i]);
-
}
-
}
-
}
-
}
-
//****************************************************************************
-
-
//** MAIN METHOD *************************************************************
-
public static void main(String[] args) throws IOException
-
{
-
Sampol app = new Sampol("Sampol CIS");
-
}
-
//****************************************************************************
-
}
LOL ah man, solved. changed indexOf() > 0 to >=0
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Iain Bishop |
last post by:
I'm trying to model objects for the following problem:
A building site contains assemblies, each of which can contain other
assemblies and/or materials.
I have modelled this using a Site...
|
by: sffan |
last post by:
I am new to database programming and was curious how others solve the
problem of storing encrypted in data in db table columns and then
subsequently searching for these records.
The particular...
|
by: Itai |
last post by:
I need to develop an internal messaging sub-system that is similar to
a web mail application but without SMTP support (e.g message routes
are confined to the webapp domain). The requirements are...
|
by: Digital |
last post by:
Hi,
I'm looking for links to the JS object model for the not-as-popular
properties and methods. I've found things like hasOwnProperty,
isEnumerable, ===, prototype, and others by searching for...
|
by: Chris Thomasson |
last post by:
<wkaras@yahoo.comwrote in message
news:1156863246.290976.191710@i3g2000cwc.googlegroups.com...
volatile has nothing to do with the memory model... Well, except in
Microsoft...
...
|
by: john |
last post by:
In my form I have a master table and a details table linked 1xM. I can
search through the whole parent table but I also like to be able to search
through the child table fields to find parent...
|
by: Hunk |
last post by:
Hi
I have a binary file which contains records sorted by Identifiers
which are strings. The Identifiers are stored in ascending order. I
would have to write a routine to give the record given...
|
by: skumar2008 |
last post by:
This may not be the right forum for this question so my appologies in advance. But I turn to this group as a last resort...
To give an example of my problem/question:
Suppose I have a database...
|
by: remya1000 |
last post by:
I’m using VB.net 2003 application program.
I am trying to do a select statement whereby I'm searching between 2 datetime values that are being stored as datetime. records are stored inside...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| | |