473,406 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

JTabel, Model, and Searching Records

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
Expand|Select|Wrap|Line Numbers
  1.     //SEARCH RECORDS
  2.     private void doSearch()
  3.     {
  4.         int rows = 0;
  5.         String search = tSearch.getText();
  6.         int index = cbSearch.getSelectedIndex();
  7.         String iSearch = "";
  8.  
  9.         for(int i=0;i<tData.length;i++)
  10.         {
  11.             iSearch = tData[i][index];
  12.             if (iSearch.indexOf(search) > 0)
  13.             {
  14.                 model.insertRow(rows,tData[i]);
  15.                 rows++;
  16.             }
  17.         }
  18.     }
This is called before doSearch on button event too:

Expand|Select|Wrap|Line Numbers
  1.             if(source == bSearch)
  2.             {
  3.                 rc = model.getRowCount();
  4.                 for(int i=(rc-1);i>-1;i--)
  5.                 {
  6.                     model.removeRow(i);
  7.                 }
  8.                 doSearch();
  9.             }
It should be adding the rows from String[][] tData that meet the comparison...
Aug 7 '08 #1
2 1526
Whole class just in case
Expand|Select|Wrap|Line Numbers
  1.  
  2. import javax.swing.*;
  3. import javax.swing.border.*;
  4. import java.awt.*;
  5. import javax.swing.table.*;
  6. import java.awt.event.*;
  7. import java.util.*;
  8. import java.text.*;
  9. import java.io.*;
  10.  
  11. public class Sampol extends JFrame
  12. {
  13.     private JPanel pMain, pMenu, pCont, pAddRecords, pSearch;
  14.     private JTable table;
  15.     private JButton bViewRecords, bAddRecords, bAddRecord, bExit, bSaveRecord, bDelete, bSearch, bSort, bShowAll;
  16.     private JScrollPane spCont;
  17.     private JTextField tSearch,tID,tName,tStock,tPrice,tDate,tGroup;
  18.     private JComboBox cbSearch, cbSort;
  19.     private String tData[][];
  20.     private DefaultTableModel model;
  21.     private Inventory inv = new Inventory();
  22.  
  23.     public Sampol(String title) throws IOException
  24.     {
  25.         super(title);
  26.         setSize(800,600);
  27.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  28.         setResizable(false);
  29.  
  30.         pMain = new JPanel();
  31.         pMain.setLayout(new BorderLayout());
  32.         pMenu = new JPanel();
  33.         pMenu.setLayout(new GridLayout(10,1,0,5));
  34.         add(pMain);
  35.         pMain.add(pMenu, BorderLayout.WEST);
  36.         Border line = new LineBorder(Color.GRAY, 1);
  37.         pMenu.setBorder(new TitledBorder(line,"Navigation"));
  38.  
  39.         bViewRecords = new JButton("Record(s) Table");
  40.         bViewRecords.addActionListener(new Listen());
  41.         bAddRecords = new JButton("Add Records");
  42.         bAddRecords.addActionListener(new Listen());
  43.         bExit = new JButton("Exit");
  44.         bExit.addActionListener(new Listen());
  45.         bSaveRecord = new JButton("Save Changes");
  46.         bSaveRecord.addActionListener(new Listen());
  47.         bDelete = new JButton("Delete Record");
  48.         bDelete.addActionListener(new Listen());
  49.  
  50.         pMenu.add(new JLabel(""));
  51.         pMenu.add(bViewRecords);
  52.         pMenu.add(bAddRecords);
  53.         pMenu.add(new JLabel(""));
  54.         pMenu.add(bExit);
  55.         pMenu.add(new JLabel(""));
  56.         pMenu.add(bSaveRecord);
  57.         pMenu.add(bDelete);
  58.  
  59.         String[] col = {"ID","Name","# In Stock","Price ($)","Added","Group"};
  60.         tData = inv.readData();
  61.         model = new DefaultTableModel(tData,col);
  62.         table = new JTable(model)
  63.         {
  64.             public boolean isCellEditable(int row, int column)
  65.             {
  66.                 if(column == 0 || column == 4)
  67.                 {
  68.                     return false;
  69.                 }
  70.                 return true;
  71.             }
  72.         };
  73.         table.getColumnModel().getColumn(1).setPreferredWidth(120);
  74.         table.getColumnModel().getColumn(4).setPreferredWidth(120);
  75.  
  76.         spCont = new JScrollPane(table);
  77.         pCont = new JPanel(new CardLayout());
  78.         pCont.add(spCont,"0");
  79.         pMain.add(pCont, BorderLayout.CENTER);
  80.  
  81.         pSearch = new JPanel();
  82.         pSearch.setLayout(new GridLayout(2,3,1,1));
  83.         tSearch = new JTextField();
  84.         cbSearch = new JComboBox();
  85.         cbSearch.addItem("  ID");
  86.         cbSearch.addItem("  Name");
  87.         cbSearch.addItem("  Stock");
  88.         cbSearch.addItem("  Price");
  89.         cbSearch.addItem("  Date");
  90.         cbSearch.addItem("  Group");
  91.         bSearch = new JButton("Search");
  92.         bSearch.addActionListener(new Listen());
  93.         cbSort = new JComboBox();
  94.         cbSort.addItem("Name Alpha-numeric");
  95.         cbSort.addItem("By Price");
  96.         bSort = new JButton("Sort");
  97.         bShowAll = new JButton("Show All");
  98.         bShowAll.addActionListener(new Listen());
  99.         bSort.addActionListener(new Listen());
  100.         pSearch.add(bShowAll);
  101.         pSearch.add(cbSort);
  102.         pSearch.add(bSort);
  103.         pSearch.add(tSearch);
  104.         pSearch.add(cbSearch);
  105.         pSearch.add(bSearch);
  106.         pMain.add(pSearch, BorderLayout.NORTH);
  107.  
  108.         pAddRecords = new JPanel(new GridLayout(20,1,5,5));
  109.         tID = new JTextField();
  110.         tID.setEnabled(false);
  111.         tName = new JTextField();
  112.         tStock = new JTextField();
  113.         tPrice = new JTextField();
  114.         tDate = new JTextField();
  115.         tDate.setEnabled(false);
  116.         tGroup = new JTextField();
  117.         bAddRecord = new JButton("Add Record");
  118.         bAddRecord.addActionListener(new Listen());
  119.  
  120.         pAddRecords.add(new JLabel("")); pAddRecords.add(new JLabel("ID")); pAddRecords.add(tID);
  121.         pAddRecords.add(new JLabel("Name")); pAddRecords.add(tName);
  122.         pAddRecords.add(new JLabel("# in Stock")); pAddRecords.add(tStock);
  123.         pAddRecords.add(new JLabel("Price ($)")); pAddRecords.add(tPrice);
  124.         pAddRecords.add(new JLabel("Date Added")); pAddRecords.add(tDate);
  125.         pAddRecords.add(new JLabel("Group")); pAddRecords.add(tGroup);
  126.         pAddRecords.add(new JLabel("")); pAddRecords.add(bAddRecord);
  127.  
  128.         pCont.add(pAddRecords,"1");
  129.         //END
  130.         setVisible(true);
  131.     }
  132.  
  133.     //ADD/WRITE RECORD
  134.     private void addRec()
  135.     {
  136.         String[][] temp = new String[tData.length+1][6];
  137.         for(int i=0;i<tData.length;i++)
  138.         {
  139.             temp[i][0] = tData[i][0];
  140.             temp[i][1] = tData[i][1];
  141.             temp[i][2] = tData[i][2];
  142.             temp[i][3] = tData[i][3];
  143.             temp[i][4] = tData[i][4];
  144.             temp[i][5] = tData[i][5];
  145.         }
  146.         temp[tData.length][0] = tID.getText();
  147.         temp[tData.length][1] = tName.getText();
  148.         try{int test1 = Integer.parseInt(tStock.getText()); temp[tData.length][2] = tStock.getText();}catch(Exception e){temp[tData.length][2] = "0";}
  149.         try{double test2 = Double.parseDouble(tPrice.getText()); temp[tData.length][3] = tPrice.getText();}catch(Exception e){temp[tData.length][3] = "0.0";}
  150.         temp[tData.length][4] = tDate.getText();
  151.         temp[tData.length][5] = tGroup.getText();
  152.  
  153.         tData = temp;
  154.         try{inv.writeData(tData);}catch(Exception e){}
  155.  
  156.         tID.setText(""+(tData.length));
  157.         tName.setText("");
  158.         tStock.setText("");
  159.         tPrice.setText("");
  160.         tGroup.setText("");
  161.  
  162.         model.insertRow(model.getRowCount(),tData[tData.length-1]);
  163.     }
  164.  
  165.     //SAVE/WRITE RECORD
  166.     private void saveRec()
  167.     {
  168.         String temp[][] = new String[model.getRowCount()][6];
  169.         int index;
  170.  
  171.         for(int i=0;i<model.getRowCount();i++)
  172.         {
  173.             temp[i][0] = (String)model.getValueAt(i,0);
  174.             temp[i][1] = (String)model.getValueAt(i,1);
  175.             temp[i][2] = (String)model.getValueAt(i,2);
  176.             temp[i][3] = (String)model.getValueAt(i,3);
  177.             temp[i][4] = (String)model.getValueAt(i,4);
  178.             temp[i][5] = (String)model.getValueAt(i,5);
  179.         }
  180.         for(int i=0;i<temp.length;i++)
  181.         {
  182.             tData[i][1] = temp[i][1];
  183.             tData[i][2] = temp[i][2];
  184.             tData[i][3] = temp[i][3];
  185.             tData[i][5] = temp[i][5];
  186.         }
  187.         try{inv.writeData(tData);}catch(Exception e){}
  188.     }
  189.  
  190.     //DELETE/WRITE RECORD
  191.     private void delRecord(int sr)
  192.     {
  193.         String[][] temp = new String[tData.length-1][6];
  194.         for(int i=0;i<sr;i++)
  195.         {
  196.             temp[i] = tData[i];
  197.         }
  198.         for(int i=sr+1;i<tData.length;i++)
  199.         {
  200.             temp[i-1] = tData[i];
  201.         }
  202.         tData = temp;
  203.         try{inv.writeData(tData);}catch(Exception e){}
  204.     }
  205.  
  206.     //SEARCH RECORDS
  207.     private void doSearch()
  208.     {
  209.         int rows = 0;
  210.         String search = tSearch.getText();
  211.         int index = cbSearch.getSelectedIndex();
  212.         String iSearch = "";
  213.  
  214.         for(int i=0;i<tData.length;i++)
  215.         {
  216.             iSearch = tData[i][index];
  217.             if (iSearch.indexOf(search) > 0)
  218.             {
  219.                 model.insertRow(rows,tData[i]);
  220.                 rows++;
  221.             }
  222.         }
  223.     }
  224.  
  225.     //** EVENT HANDLES ***********************************************************
  226.     private class Listen implements ActionListener
  227.     {
  228.         public void actionPerformed(ActionEvent e)
  229.         {
  230.             Object source = e.getSource();
  231.             CardLayout cl = (CardLayout)(pCont.getLayout());
  232.             Date d = new Date();
  233.             DateFormat df =  DateFormat.getDateInstance(DateFormat.LONG);
  234.             int rc;
  235.  
  236.             if(source == bAddRecords)
  237.             {
  238.                 pSearch.setVisible(false);
  239.                 cl.show(pCont,"1");
  240.                 tID.setText(""+(Integer.parseInt(tData[tData.length-1][0])+1));
  241.                 tDate.setText(df.format(d));
  242.                 repaint();
  243.             }
  244.             if(source == bViewRecords)
  245.             {
  246.                 pSearch.setVisible(true);
  247.                 cl.show(pCont,"0");
  248.                 repaint();
  249.             }
  250.             if(source == bExit)
  251.             {
  252.                 dispose();
  253.             }
  254.             if(source == bSaveRecord)
  255.             {
  256.                 saveRec();
  257.             }
  258.             if(source == bDelete)
  259.             {
  260.                 int sr = table.getSelectedRow();
  261.                 if(sr != -1)
  262.                 {
  263.                     model.removeRow(sr);
  264.                     delRecord(sr);
  265.                 }
  266.             }
  267.             if(source == bAddRecord)
  268.             {
  269.                 addRec();
  270.             }
  271.             if(source == bSearch)
  272.             {
  273.                 rc = model.getRowCount();
  274.                 for(int i=(rc-1);i>-1;i--)
  275.                 {
  276.                     model.removeRow(i);
  277.                 }
  278.                 doSearch();
  279.             }
  280.             if(source == bSort)
  281.             {
  282.                 //doSort();
  283.             }
  284.             if(source == bShowAll)
  285.             {
  286.                 rc = model.getRowCount();
  287.                 for(int i=(rc-1);i>-1;i--)
  288.                 {
  289.                     model.removeRow(i);
  290.                 }
  291.                 for(int i=0;i<tData.length;i++)
  292.                 {
  293.                     model.insertRow(i,tData[i]);
  294.                 }
  295.             }
  296.         }
  297.     }
  298.     //****************************************************************************
  299.  
  300.     //** MAIN METHOD *************************************************************
  301.     public static void main(String[] args) throws IOException
  302.     {
  303.             Sampol app = new Sampol("Sampol CIS");
  304.     }
  305.     //****************************************************************************
  306. }
Aug 7 '08 #2
LOL ah man, solved. changed indexOf() > 0 to >=0
Aug 7 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
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...
10
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...
4
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...
5
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...
0
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... ...
7
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...
4
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...
0
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
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...
0
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...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.