0

I trying to solve a this problem from 3 days and its difficult and I can't find how to solve it. I tried to compare two dimension array from another class to ActionListener class with JtextField that the user input in search box then view the result in a JTable. Any one can help and I am thankful for that.

    the class how contain the array 

    public class SerachClassic extends JFrame{

         String[]columnNames2 = { "Car Name", "Color", "Miles", "Year Model"," Prise"};

         String [][]info2 = {{"Ford","Black","1000","1923","34000$"},
                            {"Ford Mustang","red","1300","1969","58500$"},
                            {"Dodge","Sliver","6000","1972","38000$"},
                            {"Chevrolet Corvette","red & Whiat","500","1960","89000$"},
                            {"Buick Electra","blu","2000","1969","24000$"},
                            {"Pontiac Bonneville","blu","700","1963","26900$"},
                            {"Lincoln Continental","Black","1500","1964","79000$"}, 
                            {"Cadillac Fleetwood","Black","1200","1959","90000$"},
                            {"Ford Thunderbird","Sliver","500","1957","75000$"}};




    public String[] getColumnNames2() {
            return columnNames2;
        }

        public String[][] getInfo2() {
            return info2;
        }

    } 

this the class that create a search box

static class Action1 implements ActionListener{
           @Override
           public void actionPerformed(ActionEvent e){
               SwingUtilities.invokeLater(new Runnable() { //using  this  method to update UI

       @Override
       public void run() {
           TextFile textFile = new TextFile();// create a new object to write somthing
       }
   });  
           }
       }

     public static class TextFile{

     JFrame ftext = new JFrame();
     JPanel panel = new JPanel();
     JTextField text = new JTextField("",30); 
     JButton button = new JButton("Serach");     


     public TextFile(){


         text.addFocusListener(new FocusListener(){
  @Override


        public void focusGained(FocusEvent arg0){
           text.setText("");
  }
  @Override

        public void focusLost(FocusEvent arg0){
        text.setText("Please enter something");

       }

         });

panel.add(text);
panel.add(button);

ftext.add(panel);
ftext.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
ftext.pack();
ftext.setVisible(true);

     }

     }

I tried here to do somthing by compare the JTextfile from the search box with string but its not working

static class Action8 implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            SerachClassic findt = new SerachClassic();
            TextFile find1 = new TextFile();

            String fromUser = find1.text.getText();

            if(Arrays.asList(findt.info2).contains(fromUser)){

                JFrame searchType = new JFrame("Car Type");
                JPanel panel = new JPanel();
                searchType.setVisible(true);
                searchType.setSize(400, 200);
                JTable table = new JTable();

                panel.add(table);
                searchType.add(panel);
Hemdip
  • 410
  • 6
  • 29
JOJO
  • 25
  • 1
  • 3

1 Answers1

4

Since I don't fully understand what exactly you want to achieve, I can just guess. I will now list the important things that I've changed in the code:

  1. Fixed the focusGained() & focusedLost() methods. E.g. it will only change it to "Please enter something" if no text is inside the textfield.
  2. Arrays.asList() will still return a '2D Array' in your case (something like List<List<String>> for example). So I've created an own method that turns a 2D array into a 1D list (called to1DList())
  3. Avoid the use of mutliple JFrames, use JDialogs instead.
  4. I added an ActionListener to the button with the text "Search"
  5. A new JTable() won't magically display your info2. You have to use this constructor: JTable(Object[][] rowData, Object[] columnNames).
  6. I think you want the row in which the searched item appears to be highlighted, so I've implemented the method findRow(). Then you can just do table.setRowSelectionInterval(findRow(), findRow());.
  7. I fixed a few spelling mistakes and made the variable names a bit more clearer.

Output:

enter image description here

Code:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class SearchClassic {

    private String[] columnNames2 = { "Car Name", "Color", "Miles", "Year Model", "Price" };

    private String[][] info2 = { { "Ford", "Black", "1000", "1923", "34000$" },
            { "Ford Mustang", "Red", "1300", "1969", "58500$" }, { "Dodge", "Silver", "6000", "1972", "38000$" },
            { "Chevrolet Corvette", "Red & White", "500", "1960", "89000$" },
            { "Buick Electra", "Blue", "2000", "1969", "24000$" },
            { "Pontiac Bonneville", "Blue", "700", "1963", "26900$" },
            { "Lincoln Continental", "Black", "1500", "1964", "79000$" },
            { "Cadillac Fleetwood", "Black", "1200", "1959", "90000$" },
            { "Ford Thunderbird", "Silver", "500", "1957", "75000$" } };

    public String[] getColumnNames2() {
        return columnNames2;
    }

    public String[][] getInfo2() {
        return info2;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                TextFile textFile = new TextFile();
            }
        });
    }
}

class TextFile {

    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    JTextField textField = new JTextField("", 30);
    JButton button = new JButton("Search");

    public TextFile() {

        textField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent arg0) {
                if (textField.getText().equals("Please enter something")) {
                    textField.setText("");
                }
            }

            @Override
            public void focusLost(FocusEvent arg0) {
                if (textField.getText().equals("")) {
                    textField.setText("Please enter something");
                }
            }

        });

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SearchClassic searchClass = new SearchClassic();
                String input = textField.getText();
                if (to1DList(searchClass.getInfo2()).contains(input)) {
                    JTable table = new JTable(searchClass.getInfo2(), searchClass.getColumnNames2());
                    table.setRowSelectionInterval(findRow(searchClass.getInfo2(), input),
                            findRow(searchClass.getInfo2(), input));
                    JPanel tablePanel = new JPanel(new BorderLayout());
                    tablePanel.add(table.getTableHeader(), BorderLayout.NORTH);
                    tablePanel.add(table);
                    JOptionPane.showMessageDialog(frame, tablePanel);
                }
            }
        });

        panel.add(textField);
        panel.add(button);

        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }

    private ArrayList<Object> to1DList(Object[][] array) {
        ArrayList<Object> newArray = new ArrayList<Object>();
        for (int row = 0; row < array.length; row++) {
            for (int column = 0; column < array[row].length; column++) {
                newArray.add(array[row][column]);
            }
        }
        return newArray;
    }

    private Integer findRow(Object[][] array, Object target) {
        for (int row = 0; row < array.length; row++) {
            for (int column = 0; column < array[row].length; column++) {
                if (array[row][column].equals(target)) {
                    return row;
                }
            }
        }
        return null;
    }

}
Community
  • 1
  • 1
Lukas Rotter
  • 4,158
  • 1
  • 15
  • 35