Results 1 to 3 of 3
  1. #1
    ppreeti is offline Member
    Join Date
    Mar 2014
    Posts
    17
    Rep Power
    0

    Default make search result appear in the same page (Swing program)

    So, this is a program in swing for implementing a search functionality. It runs perfectly well. No problems there. My requirement is to make the search results appear beneath the same page. In this code, I have made the results to appear in a new Jframe that opens a new window. I basically don't want this. I want to make the search result appear in the same page. So, should I modify the code ? Any form of help is appreciated. :) Thanks !

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.io.File;
    import java.sql.*;
    import java.util.Vector;
    
    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;
    
    public class r_search_1 extends JFrame implements ActionListener {
    
        JFrame frame1;
        JLabel l0, l1, l2;
        JComboBox c1;
        JButton b1;
        Connection con;
        ResultSet rs, rs1;
        Statement st, st1;
        PreparedStatement pst;
        String ids;
        static JTable table;
        String[] columnNames = {"SECTION NAME", "REPORT NAME", "CONTACT", "LINK"};
        String from;
        Vector v = new Vector();
    
        r_search_1() 
        {
            l0 = new JLabel("Fetching Search Results...");
            l0.setForeground(Color.blue);
            l0.setFont(new Font("Serif", Font.BOLD, 20));
            l1 = new JLabel("Search");
            b1 = new JButton("submit");
    
            l0.setBounds(100, 50, 350, 40);
            l1.setBounds(75, 110, 75, 20);
            b1.setBounds(150, 150, 150, 20);
            b1.addActionListener(this);
    
            setTitle("Search Executive Reports  ");
            setLayout(null);
            //setVisible(true);
            setSize(500, 500);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    
            add(l0);
            add(l1);
            add(b1);
    
            try 
            {
    
                File dbFile = new File("executive_db.accdb");
                String path = dbFile.getAbsolutePath();
                con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ= " + path);
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                st = con.createStatement();
                rs = st.executeQuery("select index_name from Index1");
               while (rs.next())
               {
                    ids = rs.getString(1);
                    v.add(ids);
    
                }
                c1 = new JComboBox(v);
                c1.setEditable(true);c1.setSelectedItem("");
                c1.setBounds(150, 110, 150, 20);
    
                add(c1);
                st.close();
                rs.close();
            } catch (Exception e) {
            }
            setVisible(true);
        }
    
        public void actionPerformed(ActionEvent ae) {
            if (ae.getSource() == b1) {
                showTableData();
            }
         }
    
        public void showTableData()
        {
            frame1 = new JFrame("Database Search Result");
            frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame1.setLayout(new BorderLayout());
            DefaultTableModel model = new DefaultTableModel();
            model.setColumnIdentifiers(columnNames);
            table = new JTable();
            table.setModel(model);
            table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
            table.setFillsViewportHeight(true);
            JScrollPane scroll = new JScrollPane(table);
            scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            scroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
            from = (String) c1.getSelectedItem();
    
            String section_name = "";
            String report_name = "";
            String contact_name = "";
            String link = "";
    
    
            try
            {
    
            pst = con.prepareStatement("select distinct Section.Section_Name,Report.Report_Name,Report.Link,Contact.Contact_Name "
                            + "FROM (( Section INNER JOIN Report ON Report.Section_ID=Section.Section_ID ) INNER JOIN Contact ON Contact.Contact_ID=Report.Contact_ID )  LEFT JOIN Metrics ON Metrics.Report_ID=Report.Report_ID  "
                            + " WHERE Section.Section_Name LIKE '%"+from+"%' OR Report.Report_Name LIKE '%"+from+"%' OR Metrics.Metric_Name LIKE '%"+from+"%' OR Contact.Contact_Name LIKE '%"+from+"%' ");
                ResultSet rs = pst.executeQuery();
                int i = 0;
                while (rs.next()) {
                    section_name = rs.getString("Section_Name");
                    report_name = rs.getString("Report_Name");
                    contact_name = rs.getString("Contact_Name");
                    link = rs.getString("Link");
                    model.addRow(new Object[]{section_name, report_name, contact_name, link});
                    i++;
                }
                if (i < 1) {
                    JOptionPane.showMessageDialog(null, "No Record Found", "Error", JOptionPane.ERROR_MESSAGE);
                }
                if (i == 1) {
                    System.out.println(i + " Record Found");
                } else {
                    System.out.println(i + " Records Found");
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
            frame1.add(scroll);
            frame1.setVisible(true);
            frame1.setSize(1000, 400);
        }
    
        public static void main(String args[]) {
            new r_search_1();
        }
    }

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: make search result appear in the same page (Swing program)

    Hi,

    can you give more details where you have the problem?

    You could add a JPanel to your frame which will get the result. That is more or less straight forward. Instead of creating a new JFrame and adding stuff to it, you add the stuff to the JPanel (and remove stuff from it first). That way you can keep almost all of your code the same.

    With kind regards,

    Konrad

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: make search result appear in the same page (Swing program)

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. How to get a html result in a same page using servlet
    By sathishvegeta in forum New To Java
    Replies: 6
    Last Post: 05-20-2013, 04:42 PM
  2. Search result not optimal...
    By Sebbal in forum Lucene
    Replies: 0
    Last Post: 05-08-2012, 04:38 PM
  3. Search Result and Highlighting
    By sprateek in forum Lucene
    Replies: 0
    Last Post: 10-08-2011, 02:50 PM
  4. Adding result from jsp page to html
    By renuka_anil in forum Java Servlet
    Replies: 0
    Last Post: 03-23-2009, 08:33 AM
  5. Replies: 5
    Last Post: 08-26-2008, 04:43 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •