Results 1 to 7 of 7
- 11-12-2010, 06:19 PM #1
Member
- Join Date
- Sep 2010
- Location
- Oregon, usa
- Posts
- 69
- Rep Power
- 0
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! ChrisLast edited by tashimoto; 11-12-2010 at 06:22 PM. Reason: corrected spelling of critiques.
:cool: It's all here: http://download.oracle.com/javase/6/docs/api/
-
Absolutely, yes.
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.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??
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!
- 11-12-2010, 08:21 PM #3
About this -
I rather think the rows in each column should be ORed before ANDing the columns.(columns in same row would be "and"s / each row would be "or"s)
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).dbJava 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; } }Last edited by DarrylBurke; 11-12-2010 at 08:56 PM.
- 11-12-2010, 09:41 PM #4
Member
- Join Date
- Sep 2010
- Location
- Oregon, usa
- Posts
- 69
- Rep Power
- 0
Thank you both for your ideas and suggestions!
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.
Good Point! Something I hadn't thought of, and it is definitely how they would expect the search results!
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!!
ChrisLast edited by tashimoto; 11-12-2010 at 09:47 PM. Reason: added "quote by"
:cool: It's all here: http://download.oracle.com/javase/6/docs/api/
-
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.
- 11-13-2010, 12:00 PM #6
Yup, that does sound sensible. Maybe something like this.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.dbJava 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; } }Last edited by DarrylBurke; 11-13-2010 at 12:02 PM. Reason: changed a couple of variable names
- 11-19-2010, 07:44 PM #7
Member
- Join Date
- Sep 2010
- Location
- Oregon, usa
- Posts
- 69
- Rep Power
- 0
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:cool: It's all here: http://download.oracle.com/javase/6/docs/api/
Similar Threads
-
"Array Map" with Binary Search...
By kreyszig in forum Advanced JavaReplies: 5Last Post: 10-14-2010, 02:23 PM -
Using BufferedImage, WritableRaster etc. for making a "screen"
By ThemePark in forum Advanced JavaReplies: 0Last Post: 12-29-2009, 03:10 PM -
Relationship between 3D .obj file "v values" and on screen 2D coordinate?
By jiapei100 in forum Java 2DReplies: 0Last Post: 12-26-2009, 10:55 PM -
Google Error - "This site may harm your computer" on every search result?!?!
By angryboy in forum Forum LobbyReplies: 2Last Post: 03-18-2009, 08:36 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks