# Inhalt in JTable suchen



## IDE (17. Feb 2006)

hallo!

Ich schreibe Daten von einer MySql Datenbank in ein JTable. es soll auch eine suchfunktion geben. wie suche ich jetzt nach einem bestimmten wort in einem jtable und makiere dann die zeile, in der sich das wort befindet?
die tabelle wurde mit dem netbeans gui builder erstellt:


```
jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Nummer", "Nachname", "Vorname", "PLZ", "Ort", "Strasse", "Telefon", "E-Mail", "Altersgruppe", "Bankeverbindung", "Sonstiges"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false, false, false
            };
```

danke im voraus!


----------



## Guest (17. Feb 2006)

tschuldigung. hab da noch was im code vergessen. gehört einfach hinten dran:


```
public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
```


----------



## André Uhres (18. Feb 2006)

```
/*
 * Suche.java
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class Suche extends JFrame {
    public Suche() {
        super("Suche");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(500,600);
        setLocationRelativeTo(null);
        //JComponents erzeugen:
        toolbar = new JToolBar();
        suchFeld = new JTextField("test12345");
        suchBtn = new JButton("Suche");
        jTable1 = new JTable();
        //Layout:
        getRootPane().setDefaultButton(suchBtn);
        toolbar.add(suchFeld);
        toolbar.add(suchBtn);
        getContentPane().add(toolbar, BorderLayout.NORTH);
        jTable1.setModel(new DefaultTableModel(
                new Object [][] {
        },
                new String [] {
            "Title 1", "Title 2", "Title 3"
        }
        ));
        model = (DefaultTableModel)jTable1.getModel();
        getContentPane().add(new JScrollPane(jTable1), BorderLayout.CENTER);
        fuelle();
        //Listener:
        suchBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                suchBtnActionPerformed(evt);
            }
        });
    }
    private void suchBtnActionPerformed(ActionEvent evt) {
        such(suchFeld.getText().trim());
    }
    private void such(String string) {
        String str = "";
        for (int i = 0; i < jTable1.getRowCount(); i++) {
            str = jTable1.getValueAt(i,0).toString();
            if(str.equalsIgnoreCase(string)){
                jTable1.changeSelection(i,0,false,false);
            }
        }
    }
    public static void main(String args[]) { new Suche().setVisible(true); }
    private void fuelle(){
        for (int i = 0; i < 50000; i++) model.addRow(new Object[]{"test"+i});
    }
    private DefaultTableModel model ;
    private JTable jTable1;
    private JButton suchBtn;
    private JTextField suchFeld;
    private JToolBar toolbar;
}
```


----------



## Guest (18. Feb 2006)

das funktioniert jetzt auch super.

nur wenn ich jetzt mehrmals einen eintrag habe, dann springt es immer nur zum letzten. gibt es da eine möglichkeit alle diese felder zu makieren?


----------



## André Uhres (18. Feb 2006)

Nach changeSelection "break;" einfügen, dann stoppt er beim ersten Treffer. 
Die Suchaktion beginnst du immer erst hinter der selektierten Zeile --> jTable1.getSelectedRow()+1. 
Wenn der Zeilenindex grösser als  (jTable1.getRowCount()-1) ist, dann beginnst du die Suche bei 0.


----------



## Guest (23. Feb 2006)

ich hab den code jetzt ein wenig umgeändert. leider bekomme ich, seit dem ich java 6 beta installiert habe eine java.lang.ArrayIndexOutOfBoundsException: -1.

Früher hats funktioniert, aber ich bin mir nicht ganz sicher ob ich vorher was verändert habe.



```
private void such() 
    {
        int x = -1;
        int y = -1;
        String str = "";

        try
        {
            for (int i = 0; i <= jTable1.getRowCount(); i++) 
            {
                for(int j = 0; j <= jTable1.getColumnCount(); j++) // -1
                {
                    str = jTable1.getValueAt(i,y).toString();
                    if( str.equalsIgnoreCase( jTextField1.getText() ) )
                    {
                        x = i;
                        y = j;
                    }   
                }
            } 
            if(x == -1 && y == -1) {
                javax.swing.JOptionPane.showMessageDialog(null,"Eintrag nicht gefunden",
                            "Nicht gefunden",javax.swing.JOptionPane.ERROR_MESSAGE);
            } else {
                jTable1.changeSelection(x,y,false,false);
                
            } 
        } catch(Exception e) { 
             System.out.println("Fehler: " + e);
        } 
    }
```

danke im voraus!


----------



## André Uhres (23. Feb 2006)

```
private void such() {
        boolean gefunden = false;
        String str = "";
        int i = jTable1.getSelectedRow()+1;
        for (; i < jTable1.getRowCount(); i++) {
            for (int j = 0; j < jTable1.getColumnCount(); j++) {
                str = jTable1.getValueAt(i,j)==null?"":jTable1.getValueAt(i,j).toString();
                if(str.startsWith(jTextField1.getText().trim())){
                    jTable1.changeSelection(i,j,false,false);
                    gefunden = true;
                    break;
                }
            }
            if(gefunden) break;
        }
        if(!gefunden) {
            JOptionPane.showMessageDialog(null,"Eintrag nicht gefunden",
                    "Nicht gefunden",JOptionPane.ERROR_MESSAGE);
            jTable1.clearSelection();
        }
    }
```


----------



## Guest (24. Feb 2006)

Danke, es gibt aber irgendwie trotzdem ein problem.

Wenn ich das zweite mal was suchen will, oder einen nichtbestehenden Eintrag suche, dann bekomme ich wieder eine java.lang.NullPointerException

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at GUIH.such(GUIH.java:1356)
        at GUIH.jButton7ActionPerformed(GUIH.java:1081)
        at GUIH.access$1000(GUIH.java:11)
        at GUIH$11.actionPerformed(GUIH.java:370)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1957)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2278)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:377)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:232)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
        at java.awt.Component.processMouseEvent(Component.java:5955)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3285)
        at java.awt.Component.processEvent(Component.java:5720)
        at java.awt.Container.processEvent(Container.java:1960)
        at java.awt.Component.dispatchEventImpl(Component.java:4365)
        at java.awt.Container.dispatchEventImpl(Container.java:2018)
        at java.awt.Component.dispatchEvent(Component.java:4195)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4222)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3886)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3816)
        at java.awt.Container.dispatchEventImpl(Container.java:2004)
        at java.awt.Window.dispatchEventImpl(Window.java:2210)
        at java.awt.Component.dispatchEvent(Component.java:4195)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)


Zeile 1356:
str = jTable1.getValueAt(i,j).toString();

danke im voraus.


----------



## André Uhres (25. Feb 2006)

str = jTable1.getValueAt(i,j)==null?"":jTable1.getValueAt(i,j).toString();


----------

