JTabel, Model, and Searching Records

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Toothpick
    New Member
    • Aug 2008
    • 5

    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
    Code:
    	//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:

    Code:
    			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...
  • Toothpick
    New Member
    • Aug 2008
    • 5

    #2
    Whole class just in case
    Code:
    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");
    	}
    	//****************************************************************************
    }

    Comment

    • Toothpick
      New Member
      • Aug 2008
      • 5

      #3
      LOL ah man, solved. changed indexOf() > 0 to >=0

      Comment

      Working...