Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Search Form Doesn't work!

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Search Form Doesn't work!

    need help here plss.. and thanks in advance>>

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class frmSearch extends JDialog
    {
    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    JPanel jpnlMain = new JPanel();

    JButton bttnSearch = new JButton("Search", new ImageIcon("images/i16x16/search.png"));
    JButton bttnExit = new JButton("Cancel", new ImageIcon("images/i16x16/exit.png"));

    JLabel lblHeader = new JLabel();
    JLabel lblIcon = new JLabel();
    JLabel lblCaption = new JLabel("IMPORTANT: All Fields are required.");
    JLabel lblSearchFor = new JLabel("Search For:");
    JLabel lblSearchIn = new JLabel("Look In:");

    JTextField txtSearchFor = new JTextField();
    JComboBox cbSearchIn;

    String sFilter;

    clsSettings settings = new clsSettings();

    public frmSearch(JFrame OwnerForm, String sSearch)
    {
    super(OwnerForm,true);

    sFilter = sSearch;

    String sCustomers[] ={"CustID", "TradeName", "TypeOfBus"};
    String sSuppliers[] ={"SupID", "TradeName", "TypeOfBus"};
    String sStockCat [] ={"stk_clas_Code", "stk_clas_Name","stk_clas_Desc"};
    String sStockDesc[] ={"stk_ds_Code", "stk_ds_Desc","stk_clas_Name"};
    String sBank[] ={"Acct_Code", "Bnk_Code", "Bnk_Code","Bnk_Code"};
    String sUsers[] ={"Username", "EmployeeID", "Fullname", "Title"};
    String sLogDetails[]={"Username", "Fullname", "Month", "Year"};

    if(sFilter == "Customers") {cbSearchIn = new JComboBox(sCustomers);}
    else if(sFilter == "Suppliers") {cbSearchIn = new JComboBox(sSuppliers);}
    else if(sFilter == "Stock Category") {cbSearchIn = new JComboBox(sStockCat);}
    else if(sFilter == "Stock Description") {cbSearchIn = new JComboBox(sStockDesc);}
    else if(sFilter == "Bank Accounts") {cbSearchIn = new JComboBox(sBank);}
    else if(sFilter == "Users") {cbSearchIn = new JComboBox(sUsers);}
    else if(sFilter == "Log Details") {cbSearchIn = new JComboBox(sLogDetails);}

    settings.setJComboBox(cbSearchIn,80,72,225,20);
    settings.setJTextField(txtSearchFor,80,50,225,20);

    //Set the Settings of JLable
    lblHeader.setIcon(new ImageIcon("images/iCustom/iTools/Banner-Header.png"));
    lblIcon.setIcon(new ImageIcon("images/i32x32/search.png"));
    lblCaption.setFont(new Font("Dialog", Font.BOLD, 12));
    lblCaption.setForeground(new Color(255,255,255,255));
    settings.setJLabel(lblCaption,80,15,200,40);
    settings.setJLabel(lblHeader,0,0,500,40);
    settings.setJLabel(lblIcon,5,2,50,40);
    settings.setJLabel(lblSearchFor,5,50,105,20);
    settings.setJLabel(lblSearchIn,5,72,105,20);

    //Set the Settings of JButton
    settings.setJButton(bttnExit,200,100,100,24,"exit" ,"Unload Form");
    settings.setJButton(bttnSearch,100,100,100,24,"sea rch","Search");
    bttnExit.setMnemonic(KeyEvent.VK_C);
    bttnSearch.setMnemonic(KeyEvent.VK_S);
    bttnExit.addActionListener(JBActionListener);
    bttnSearch.addActionListener(JBActionListener);

    //Add ComboBox to JPanel
    jpnlMain.add(cbSearchIn);
    //Add TextField to JPanel
    jpnlMain.add(txtSearchFor);
    //Add JLabel to JPanel
    jpnlMain.add(lblCaption);
    jpnlMain.add(lblIcon);
    jpnlMain.add(lblHeader);
    jpnlMain.add(lblSearchFor);
    jpnlMain.add(lblSearchIn);
    //Add JButton to JPanel
    jpnlMain.add(bttnExit);
    jpnlMain.add(bttnSearch);

    jpnlMain.setBackground(Color.gray);
    jpnlMain.setLayout(null);

    getContentPane().setLayout(new BorderLayout(0,0));
    getContentPane().add(BorderLayout.CENTER, jpnlMain);

    setSize(350,160);
    setResizable(false);
    setLocation((screen.width - 350)/2,((screen.height-160)/2));

    }
    	ActionListener JBActionListener = new ActionListener()
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			String srcObj = e.getActionCommand();
    			if(srcObj=="search")
    			{
    				 if (sFilter == ("Supliers"));
    				{
    					frmSuppliers.reloadRecord("SELECT * FROM tblSuppliers WHERE " +   cbSearchIn.getSelectedItem().toString().replaceAll(" ", "") + " LIKE '%" + txtSearchFor.getText() + "%' ORDER BY SupplierID ASC");
    					dispose();
    				}					
    			}
     
    			else if(srcObj=="exit"){dispose();}
    }
    };
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Search Form Doesn't work!

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    Please explain what the problem is.

    One problem I see is that the code uses == to compare Strings. You should use the equals() method for comparing String objects.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Search Form Doesn't work!

    sir I've tried the .equals() method. but it does not display the result here's the new code for search button

    [code = java]
    ActionListener JBActionListener = new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    String srcObj = e.getActionCommand();
    if(srcObj.equals("search"))
    {
    if(sFilter.equals("Supliers"))
    {
    frmSuppliers.reloadRecord("SELECT * FROM tblSuppliers WHERE " + cbSearchIn.getSelectedItem().toString().replaceAll ("","") + " LIKE '%" + txtSearchFor.getText() + "%' ORDER BY SupplierID ASC");
    dispose();
    }
    }
    else if(srcObj.equals("exit"))
    {dispose();}
    }
    };
    }
    [/code]

    --- Update ---

    sir I've tried the .equals() method. but it does not display the result here's the new code for search button

    	ActionListener JBActionListener = new ActionListener()
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			String srcObj = e.getActionCommand();
    			if(srcObj.equals("search"))
    			{
    				 if(sFilter.equals("Supliers"))
    				{
    					frmSuppliers.reloadRecord("SELECT * FROM tblSuppliers WHERE " + cbSearchIn.getSelectedItem().toString().replaceAll("","") + " LIKE '%" + txtSearchFor.getText() + "%' ORDER BY SupplierID ASC");
    					dispose();
    				}					
    			}
    			else if(srcObj.equals("exit"))
    				{dispose();}
    		}
    	};
    }

  4. #4
    Member
    Join Date
    Feb 2014
    Location
    India
    Posts
    47
    My Mood
    Bored
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default Re: Search Form Doesn't work!

    frmSuppliers.reloadRecord("SELECT * FROM tblSuppliers WHERE " + cbSearchIn.getSelectedItem().toString().replaceAll ("","") + " LIKE '%" + txtSearchFor.getText() + "%' ORDER BY SupplierID ASC");
    What do you mean by

    cbSearchIn.getSelectedItem().toString().replaceAll ("","")

    Why are you doing that?
    you are replacing nothing.

    I think there is some problem with your sql query formation.
    The quotations ( " ) in the query might be causing some problem.

    Try it once by hardcoding the values in place of the variables.

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Search Form Doesn't work!

    well sir im expecting
    cbSearchIn.getSelectedItem().toString().replaceAll ("","")
    would replace the data(s) in the table. but it ends up in logical error it made the whole table blank or i thinks its more precise to describe as the whole table is gone...

  6. #6
    Member
    Join Date
    Feb 2014
    Location
    India
    Posts
    47
    My Mood
    Bored
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default Re: Search Form Doesn't work!

    Can you once try running the program by hardcoding the values in place of the variables.
    e.g.
    replace
    cbSearchIn.getSelectedItem().toString().replaceAll ("","")
    and
    txtSearchFor.getText()
    with some real values from your side and then check what is your output.

Similar Threads

  1. My JPanel-form doesn´t show up
    By iHank in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 11th, 2013, 11:03 AM
  2. Program doesn't work!
    By Mrcinica in forum What's Wrong With My Code?
    Replies: 15
    Last Post: May 16th, 2013, 10:23 AM
  3. [SOLVED] .equals doesn't work?
    By Purple01 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 14th, 2012, 03:20 AM
  4. Why doesn't this work!
    By Alex-Green in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2012, 03:25 AM
  5. Why doesn't this work?
    By mailman in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 9th, 2012, 10:19 PM

Tags for this Thread