2012-05-02 22 views
1

TextFieldのテキストを取り込むコード部分があります(実際には2つのTextFieldがありますが、ユーザは一度に1つしか使用できません)それを端末に格納されたファイルに格納します。問題は、これがコードであるTextFieldの1 ... にテキストがある場合でも、私は常に、null文字列を取得することである(逢引は、メソッドのactionPerformed()である:JTextField.getText()は、内容が内部に入っていてもnull値を返します

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 

public class Search implements ActionListener{ 
JFrame frame; 
JButton click; 
JLabel comando, carico; 
JTextField textv, text; 
JTextArea res; 
String pathFile = "C:\\Log.txt"; 
String str= new String(); 

Search(){ 

    frame = new JFrame("Search"); 
    frame.setSize(400, 200); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setLayout(new GridLayout(1,2)); 
    frame.setResizable(false); 
    JPanel panel = new JPanel(); 
    panel.setLayout(new GridLayout(7,1)); 
    click = new JButton("Cerca"); 
    comando = new JLabel("Comando"); 
    carico = new JLabel("A carico di:"); 
    textv = new JTextField(""); 
    text = new JTextField(""); 
    res = new JTextArea(""); 
    panel.add(comando); 
    panel.add(textv); 
    panel.add(carico); 
    panel.add(text); 
    panel.add(click); 
    res.setLineWrap(true); 
    res.setWrapStyleWord(true); 
    res.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); 

    JScrollPane scroller = new JScrollPane(res); 
    scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 
    panel.add(scroller); 
    frame.add(panel); 
    click.addActionListener(this);  
    click.setSize(70, 35);  
    frame.setVisible(true); 

} 

public void actionPerformed (ActionEvent e){ 
    if(e.getSource()==click){ 
     res.setText(null); 
     if(textv != null) {cercaStringa(pathFile, textv.getText().toString());} 
     else {cercaStringa(pathFile, text.getText().toString());} 
    } 
} 

public void cercaStringa(String pathFile, String stringa){ 
    try { 
     BufferedReader in = new BufferedReader(new FileReader(pathFile)); 
     String line = new String(); 
     while((line = in.readLine())!=null) { 
      if(line.contains(stringa)){ 
       res.append(line); 
       res.append("\n"); 
       } 
     } 
    } 
    catch(IOException ex) { ex.printStackTrace();} 
    } 



public static void main (String[] args){ 
    new Search(); 
} 

}

私は本当に私は解決策は単純ですが、私はそれを得ることはできません知っているウィンドウの原因の外にすべてをスローするつもりだ...次の試験によって確認されたように

+1

私はちょうどあなたのコードをテストし、期待どおりに動作します。どのようなエラーがありますか?例外はありますか?あなたはコードをデバッグしようとしましたか? printlnステートメントを置いて、変数に期待する値があるかどうか確認しましたか?コードをそのままそのままコピーしましたか? – assylias

+0

'cercaStringa:asdad java.io.FileNotFoundException:C:\ Log.txt(指定されたファイルが見つかりません)... ' –

+0

同じです。いくつかのレイアウトとエンコーディングの問題に加えて、コードはうまくいくようです。別の質問で示唆されているように、今ではデバッガを使って学ぶ時間になるかもしれません。 –

答えて

2

、あなたのフィールドnullではありません現在actionListener呼び出し。例えば、予想通り、"Test: Carico"続い"Comando""Carico"出力"Test: Comando"の入力:

@Override 
public void actionPerformed(ActionEvent e) { 
    if (e.getSource() == click) { 
    res.setText(null); 
    System.out.println("Test: "+textv.getText()); 
    System.out.println("Test: "+text.getText()); 
    if (textv != null) { 
     cercaStringa(pathFile, textv.getText().toString()); 
    } else { 
     cercaStringa(pathFile, text.getText().toString()); 
    } 
    } 
} 

問題はif (textv != null)は、私の知る限り(最初の実行時に「他に」到達することはありませんということですクイック表情で私が目をtextvnullに設定されることはありませんようtextv = new JTextField("");textv = nullと等価ではないので、)、任意の実行時に、それは決してコードを与えた、とtextvます空ではあるが、Stringを保持している。あなたはまた、C:\Log.txtの存在について検証を欠落していた、と例外が簡単にトリガすることができ

public void actionPerformed(ActionEvent e) { 
    if (e.getSource() == click) { 
    res.setText(""); 
    if (!textv.getText().isEmpty()) { 
     cercaStringa(pathFile, textv.getText().toString()); 
    } else { 
     cercaStringa(pathFile, text.getText().toString()); 
    } 
    } 
} 

修正はこれです。ここで

は主に、応急処置である:

public static void main(String[] args) { 
    File log = new File("C:\\Log.txt"); 
    if (!log.exists()) { 
    try { 
     /* 
     * mkdirs() is not really needed when using the C root, 
     * but I like to do this because the path might be changed. 
     */ 
     if (!log.getParentFile().exists()) { 
     log.getParentFile().mkdirs(); 
     } 
     log.createNewFile(); 
    } catch (IOException ex) { 
     Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    } 
    Search search = new Search(); 
} 
+0

ファイルが見つからないとは言わなかった;) –

+0

修正済み。申し訳ありません。 Btw、あなたはあなたが得ている例外を提供できますか? – XenoRo

+0

私は例外を得ません。問題は、私が得なければならない結果が得られないということです。特定の文字列を含む行だけを取得する必要があります。代わりに、ファイルの内容全体を取得します.JTextFieldからテキストを取得すると、空であると表示されるため、すべて... –

0

使用JTextArea#read(Reader in, Object desc) throws IOException、行区切りを受け入れ、この方法E.I.

+0

以前はその関数を使用していませんでしたが、ファイル全体をテキスト領域に出力しませんでしたか?そうであれば、入力を含む行だけを表示したいので、彼の問題は残るでしょう。 REF: (line.contains(stringa)){ res.append(ライン)場合、 res.append( "\ n"); } – XenoRo

関連する問題