Results 1 to 2 of 2
  1. #1
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default searching a row using text fields

    Java Code:
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class InventoryTEST2 extends JFrame {
    
      private JTextField searchField;
      private JTable table;
      private JPanel panel;
      private JScrollPane scroll;
    
      public InventoryTEST2() {
    
        initializeInventory();
      }
    
    
      private void initializeInventory() {
    
          panel = new JPanel();
          searchField = new JTextField();
          
          panel.setLayout(null);
    
          final String[] columnNames = {"Name", "Surname", "Age"};
    
          final Object[][] data = {{"Jhon", "Java", "23"}, {"Stupid", "Stupido", "500"},
                                   {"Max", "Dumbass", "10"}, {"Melanie", "Martin", "23"},
                                   {"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
                                   {"Jollibe", "Mcdonalds", "15"}};
    
          table = new JTable(data, columnNames);
          table.setColumnSelectionAllowed(true);
          table.setRowSelectionAllowed(true);
    
          searchField.setBounds(20, 150, 180, 20);
    
    
          scroll = new JScrollPane(table);
          scroll.setBounds(0, 0, 900, 400);
    
          panel.add(searchField);
          panel.add(scroll);
          getContentPane().add(panel);
    
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          setTitle("Inventory Window");
          setSize(900, 400);
          setLocationRelativeTo(null);
          setVisible(true);
      }
      
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            new InventoryTEST2();
          }
        });
      }
    }
    hello. how can i highlight a specific "row" by searching(typing the value in a text field?)..

    for example I search by "name", It will highlight on the row that has the equivalent searched name...

    by the way, i have an image background in my real JTable, I can only focus on the selection foreground not the highlight effect. something like.. the colors of the text will change.
    Last edited by bigj; 02-03-2010 at 09:58 AM.

  2. #2
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default

    I made some changes, and I got a little bit answers in google
    Java Code:
      private void initializeInventory() {
    
          panel = new JPanel();
          searchField = new JTextField();
          
          panel.setLayout(null);
    
          final String[] columnNames = {"Name", "Surname", "Age"};
    
          final Object[][] data = {{"Jhon", "Java", "23"}, {"Stupid", "Stupido", "500"},
                                   {"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
                                   {"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
                                   {"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
                                   {"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
                                   {"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
                                   {"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
                                   {"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
                                   {"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
                                   {"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
                                   {"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
                                   {"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
                                   {"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
                                   {"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
                                   {"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
                                   {"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
                                   {"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
                                   {"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
                                   {"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
                                   {"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
                                   {"Max", "Dumbass", "10"}, {"Melanie", "Martin", "23"},
                                   {"Jollibe", "Mcdonalds", "15"}};
    
          table = new JTable(data, columnNames);
          table.setColumnSelectionAllowed(true);
          table.setRowSelectionAllowed(true);
          
          scroll = new JScrollPane(table);
          scroll.setBounds(0, 200, 900, 150);
    
          searchField.setBounds(10, 100, 150, 20);
          searchField.addActionListener(new ActionListener() {
    
             public void actionPerformed(ActionEvent e) {
    
                 String value = searchField.getText();
                 String tableValue = "";
    
                 System.out.println(value);
                 
                 for (int x = 0; x <= table.getRowCount() - 1; x++) {
    
                     if (value.equals(table.getValueAt(x, 0))) {
    
                         table.setRowSelectionInterval(x, x);
                   
                     }
                 }
             }
          });
    
          panel.add(searchField);
          panel.add(scroll);
          getContentPane().add(panel);
    
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          setTitle("Inventory Window");
          setSize(900, 400);
          setLocationRelativeTo(null);
          setVisible(true);
      }
      
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            new InventoryTEST2();
          }
        });
      }
    }

    now the problems are.

    1.) when I run the program the table doesnt have the selection focus (if that is the right term to say). I have to click first in any of the cells in the table, so the selection will be on focus.
    (is there any way to make a default selection focus in a JTable?)
    2.) when the program searches for the equivalent "name" input in the text field, it highlights it but the "scroll" doesnt scroll down to view the part where that value resides.. in this changes Im focusing on the name "Max". because its in the bottom of the JTable... please I need more help with this...
    Last edited by bigj; 02-03-2010 at 11:30 AM.

Similar Threads

  1. Searching in several fields
    By ivaneggel in forum Lucene
    Replies: 0
    Last Post: 11-11-2009, 09:24 AM
  2. Problem With Text Fields!
    By freshoreo in forum AWT / Swing
    Replies: 3
    Last Post: 08-04-2008, 09:52 PM
  3. Demonstration of text fields in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 02:20 PM
  4. Help with text fields in Java
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:29 AM
  5. Special characters in text fields
    By Felissa in forum Web Frameworks
    Replies: 0
    Last Post: 06-27-2007, 04:47 PM

Posting Permissions

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