JStar2.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Text;

/**
 *
 * @author info
 */
public class JStar2 extends javax.swing.JFrame {

    /**
     * Creates new form JStar2
     */
    public JStar2() {
        initComponents();
        taText.setCaretPosition(0);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        taText = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        taMessage = new javax.swing.JTextArea();
        tfSuchen = new javax.swing.JTextField();
        buSuchen = new javax.swing.JButton();
        cbIgnoreCase = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("JStar2 (und noch ein Texteditor)");

        taText.setColumns(20);
        taText.setRows(5);
        taText.setText("Dieser Text ist eine \nTexteingabe damit ich \nmeinen tollen Editor leichter Testen kann.");
        jScrollPane1.setViewportView(taText);

        taMessage.setEditable(false);
        taMessage.setBackground(new java.awt.Color(0, 255, 255));
        taMessage.setColumns(20);
        taMessage.setRows(5);
        taMessage.setText("Meldungen");
        jScrollPane2.setViewportView(taMessage);

        tfSuchen.setText("Suchtext");
        tfSuchen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfSuchenActionPerformed(evt);
            }
        });

        buSuchen.setBackground(new java.awt.Color(255, 255, 255));
        buSuchen.setText("Suchen");
        buSuchen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buSuchenActionPerformed(evt);
            }
        });

        cbIgnoreCase.setText("Ignore Case");
        cbIgnoreCase.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbIgnoreCaseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(256, 256, 256)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 626, Short.MAX_VALUE))
                    .addComponent(jScrollPane2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tfSuchen)
                    .addComponent(buSuchen, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cbIgnoreCase)
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 328, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tfSuchen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buSuchen)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbIgnoreCase)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(268, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tfSuchenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfSuchenActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfSuchenActionPerformed

    private void buSuchenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buSuchenActionPerformed
        suchen();
    }//GEN-LAST:event_buSuchenActionPerformed

    private void cbIgnoreCaseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbIgnoreCaseActionPerformed
        
    }//GEN-LAST:event_cbIgnoreCaseActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JStar2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JStar2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JStar2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JStar2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JStar2().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buSuchen;
    private javax.swing.JCheckBox cbIgnoreCase;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea taMessage;
    private javax.swing.JTextArea taText;
    private javax.swing.JTextField tfSuchen;
    // End of variables declaration//GEN-END:variables

    private void suchen() {
        // Suchtext bestimmen
        String su = tfSuchen.getText();

        // Text in dem gesucht wird bestimmen 
        String text = taText.getText();

        // Position ab der gesucht wird bestimmen
        int from = taText.getCaretPosition();
        if(cbIgnoreCase.isSelected())
        {
            
        }

        //// Suche durchfuehren /////////////////////////
        int pos = text.indexOf(su, from);

        //Ergebnis der Suche anzeigen 
        if (pos < 0) {
            taMessage.setText("Das wahr wohl nichts");
        } else {
            taMessage.setText("'" + su + "' an Position" + pos + " gefunden.");
        }

        // Falls erfolgreich, den Suchtext im Text markieren
        if (pos > -1) {
            taText.setSelectionStart(pos);
            taText.setSelectionEnd(pos + su.length());
        }

        taText.requestFocusInWindow();
    }

}