Results 1 to 7 of 7
  1. #1
    tashimoto is offline Member
    Join Date
    Sep 2010
    Location
    Oregon, usa
    Posts
    69
    Rep Power
    0

    Default JTable vs JTextField - which to use for creating "Search" screen

    Hi all,

    I have a work project that needs a GUI for users to input their search criteria that the program will create the database search queries from. Our search GUI needs the capability for multiple search criteria under any particular field.

    I know that JTables are used for displaying the data returned from a database. Could a JTable also be used as an interface to give users a place to input their search criteria? I am stuck on whether I should use a JTable with headings such as "First Name", "Last Name", "SomeSearchable Field" , and then retrieving the users input from each row/column to create the database queries. (columns in same row would be "and"s / each row would be "or"s)

    OR Would it be better to use JTextFields and JLabels and lay them out using GridLayout to create the database queries??

    OR ??? What have people done/used??

    I've viewed other websites that have user search interfaces, but I'm still not sure which would be best.

    Any thoughts/comments/critiques??
    Thanks in advance! Chris
    Last edited by tashimoto; 11-12-2010 at 06:22 PM. Reason: corrected spelling of critiques.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by tashimoto View Post
    I know that JTables are used for displaying the data returned from a database. Could a JTable also be used as an interface to give users a place to input their search criteria?
    Absolutely, yes.


    I am stuck on whether I should use a JTable with headings such as "First Name", "Last Name", "SomeSearchable Field" , and then retrieving the users input from each row/column to create the database queries. (columns in same row would be "and"s / each row would be "or"s)

    OR Would it be better to use JTextFields and JLabels and lay them out using GridLayout to create the database queries??
    I'm no pro at this, but I've seen both of these types work. My uneducated take on this is if you need a few "rows" of information and you know how many rows you'll need, then the JTextFields are probably the way to go. If on the other hand you need many or a variable number of rows of information, then perhaps go with a JTable.

    If you create your program logic separate from your GUI code, you'll be able to use the same logic code with either GUI solution to see which works better for you.

    Much luck!

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    About this -
    (columns in same row would be "and"s / each row would be "or"s)
    I rather think the rows in each column should be ORed before ANDing the columns.

    If I were to enter John, Jane in the First Name column and Smith, Brown in the Last Name column I think I would expect John Brown and Jane Smith to be included in the result.

    As far as your main question is concerned, laying out text fields and labels may not give a very intuitive search form. A JTable on the other hand could be confusing to users in the sense that it may not be apparent that a column can be left blank or that each column may not need the same number of rows filled. A third possibility could be to provide a label with a "+" button that adds a search field for that data field each time it is clicked, provided all fiedls thus added have content. OK I know that's not clear so here's a mockup that doesn't do any searching (I'm sure you can improve on the layout, this is just for concept).
    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.*;
    import javax.swing.border.LineBorder;
    
    public class SearchForm {
    
      JPanel panel = new JPanel();
    
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
    
          @Override
          public void run() {
            new SearchForm().makeUI();
          }
        });
      }
    
      public void makeUI() {
        panel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = GridBagConstraints.RELATIVE;
        gbc.weightx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        String[] searchFields = {"First Name", "Last Name", "SomeSearchable Field"};
        for (String searchField : searchFields) {
          panel.add(new SearchPanel(searchField), gbc);
        }
        gbc.weighty = 0.1;
        panel.add(Box.createVerticalGlue(), gbc);
    
        JFrame frame = new JFrame();
        frame.add(new JScrollPane(panel));
    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      class SearchPanel extends JPanel {
    
        JPanel textPanel = new JPanel();
        List<JTextField> entryFields = new ArrayList<JTextField>();
    
        public SearchPanel(String searchField) {
          setBorder(new LineBorder(Color.BLACK));
          setLayout(new GridBagLayout());
          GridBagConstraints gbc = new GridBagConstraints();
          gbc.gridx = GridBagConstraints.RELATIVE;
          gbc.gridy = 0;
          gbc.fill = GridBagConstraints.NONE;
          gbc.anchor = GridBagConstraints.NORTHWEST;
          add(new SearchLabel(searchField), gbc);
          JButton button = new JButton("+");
          button.addActionListener(new ActionListener() {
    
            @Override
            public void actionPerformed(ActionEvent e) {
             addTextField();
            }
          });
          add(button, gbc);
          textPanel.setLayout(new GridLayout(0, 1));
          gbc.weightx = 1;
          add(textPanel, gbc);
        }
    
        private void addTextField() {
          for (JTextField entryField : entryFields) {
            if (entryField.getText().trim().isEmpty()) {
             entryField.requestFocusInWindow();
             return;
            }
          }
          JTextField entryField = new JTextField(20);
          entryFields.add(entryField);
          textPanel.add(entryField);
          textPanel.revalidate();
          panel.revalidate();
          entryField.requestFocusInWindow();
        }
      }
    }
    
    class SearchLabel extends JLabel {
    
      private static Dimension preferredSize = new Dimension();
    
      SearchLabel(String text) {
        super(text);
        Dimension d = super.getPreferredSize();
        preferredSize.width = Math.max(preferredSize.width, d.width);
        preferredSize.height = Math.max(preferredSize.height, d.height);
      }
    
      @Override
      public Dimension getPreferredSize() {
        return preferredSize;
      }
    }
    db
    Last edited by DarrylBurke; 11-12-2010 at 08:56 PM.

  4. #4
    tashimoto is offline Member
    Join Date
    Sep 2010
    Location
    Oregon, usa
    Posts
    69
    Rep Power
    0

    Default

    Thank you both for your ideas and suggestions!

    Quote Originally Posted by Fubarable View Post
    If on the other hand you need many or a variable number of rows of information, then perhaps go with a JTable.
    I was thinking this might be the way to go, but I'm having trouble trying to figure out if/how to add more rows. I cannot find a direct addRow method in JTable, although I did come across a thread with with a post from you in it. (Post:Returning a ResultSet to custom JTable Model) You directed the OP to Rob Camick's blog about JTables. I read through it and found he has information posted about a List Table Model (List Table Model Java Tips Weblog) and it has an addRow method. I'm researching more about his ListTableModel.

    Quote Originally Posted by Darryl.Burke View Post
    I rather think the rows in each column should be ORed before ANDing the columns.

    If I were to enter John, Jane in the First Name column and Smith, Brown in the Last Name column I think I would expect John Brown and Jane Smith to be included in the result.
    Good Point! Something I hadn't thought of, and it is definitely how they would expect the search results!

    Quote Originally Posted by Darryl.Burke View Post
    A third possibility could be to provide a label with a "+" button that adds a search field for that data field each time it is clicked
    I like the + idea! I've seen it on other search forms and, I think it keeps the form clean looking yet flexible. I might try modifying your mock up and place the Field Labels horizontal and the Search TextFields vertical to see how that looks and works. Currently, the program this group is using has it's search fields laid out like a spread sheet which is why I was thinking of using a JTable. Plus, some enter multiple entries for one searchable field (like report id numbers) so they can view and modify multiple reports at once.

    Thank you Both again for your responses!!
    Chris
    Last edited by tashimoto; 11-12-2010 at 09:47 PM. Reason: added "quote by"

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If your JTable uses a DefaultTableModel, then it has an addRow method. This will work well with DefaultTableModel subclasses too. If instead you subclass the AbstractTableModel, then you'll have to create your own addRow method, but it's not too hard to do so long as your remember to call fireTableRowsInserted after the insertion.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    I might try modifying your mock up and place the Field Labels horizontal and the Search TextFields vertical to see how that looks and works.
    Yup, that does sound sensible. Maybe something like this.
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.*;
    
    public class SearchForm {
    
      private String[] searchFields = {"First Name", "Last Name", "SomeSearchable Field"};
      private List<List<JTextField>> textFieldList = new ArrayList<List<JTextField>>();
      private JPanel[] searchFieldPanels = new JPanel[searchFields.length];
    
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
    
          @Override
          public void run() {
            new SearchForm().makeUI();
          }
        });
      }
    
      public void makeUI() {
        final JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.NORTH;
        gbc.weightx = 1;
    
        for (int i = 0; i < searchFields.length; i++) {
          JButton button = new SameSizeButton(searchFields[i]);
          final int j = i;
          button.addActionListener(new ActionListener() {
    
            @Override
            public void actionPerformed(ActionEvent e) {
              List<JTextField> textFields = textFieldList.get(j);
              for (JTextField textField : textFields) {
                if (textField.getText().trim().isEmpty()) {
                  textField.requestFocusInWindow();
                  return;
                }
              }
              JTextField textField = new JTextField();
              textFields.add(textField);
              searchFieldPanels[j].add(textField);
              panel.revalidate();
              textField.requestFocusInWindow();
            }
          });
          gbc.gridx = i;
          gbc.gridy = 0;
          gbc.weighty = 0;
          panel.add(button, gbc);
    
          textFieldList.add(new ArrayList<JTextField>());
          searchFieldPanels[i] = new JPanel();
          searchFieldPanels[i].setLayout(new BoxLayout(searchFieldPanels[i], BoxLayout.Y_AXIS));
          gbc.gridy = 1;
          gbc.weighty = 1;
          panel.add(searchFieldPanels[i], gbc);
        }
        JFrame frame = new JFrame();
        frame.add(new JScrollPane(panel), BorderLayout.CENTER);
    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    }
    
    class SameSizeButton extends JButton {
    
      private static Dimension preferredSize = new Dimension();
    
      public SameSizeButton(String text) {
        super(text);
        Dimension d = super.getPreferredSize();
        preferredSize.width = Math.max(preferredSize.width, d.width);
        preferredSize.height = Math.max(preferredSize.height, d.height);
      }
    
      @Override
      public Dimension getPreferredSize() {
        return preferredSize;
      }
    }
    db
    Last edited by DarrylBurke; 11-13-2010 at 12:02 PM. Reason: changed a couple of variable names

  7. #7
    tashimoto is offline Member
    Join Date
    Sep 2010
    Location
    Oregon, usa
    Posts
    69
    Rep Power
    0

    Default

    Thanks again for both your input! I am going to mark this as solved since you both gave me feedback that has helped me figure out a direction to proceed in.

    Sorry I didn't respond sooner. I got side tracked on another portion of this project. :)

    Much Thanks!
    Chris

Similar Threads

  1. "Array Map" with Binary Search...
    By kreyszig in forum Advanced Java
    Replies: 5
    Last Post: 10-14-2010, 02:23 PM
  2. Replies: 0
    Last Post: 12-29-2009, 03:10 PM
  3. Replies: 0
    Last Post: 12-26-2009, 10:55 PM
  4. Replies: 2
    Last Post: 03-18-2009, 08:36 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

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