質問編集履歴
7
コード全体を表示
title
CHANGED
File without changes
|
body
CHANGED
@@ -7,79 +7,75 @@
|
|
7
7
|
|
8
8
|
有識者の方々ご協力お願いします
|
9
9
|
```Java
|
10
|
-
public void actionPerformed(ActionEvent e) {
|
11
|
-
|
10
|
+
package logViewer;
|
12
11
|
|
12
|
+
import java.awt.BorderLayout;
|
13
|
+
import java.awt.Dimension;
|
14
|
+
import java.awt.event.ActionEvent;
|
13
|
-
|
15
|
+
import java.awt.event.ActionListener;
|
16
|
+
import java.io.BufferedReader;
|
17
|
+
import java.io.File;
|
18
|
+
import java.io.FileReader;
|
19
|
+
import java.io.IOException;
|
14
20
|
|
21
|
+
import javax.swing.JButton;
|
22
|
+
import javax.swing.JFileChooser;
|
23
|
+
import javax.swing.JOptionPane;
|
24
|
+
import javax.swing.JPanel;
|
25
|
+
import javax.swing.JScrollPane;
|
26
|
+
import javax.swing.JTextArea;
|
27
|
+
import javax.swing.JTextField;
|
15
|
-
|
28
|
+
import javax.swing.border.TitledBorder;
|
16
29
|
|
17
30
|
|
18
|
-
|
31
|
+
public class LogPanel2 extends JPanel implements ActionListener,Runnable{
|
19
32
|
|
33
|
+
private JTextArea ta = new JTextArea(); //editer
|
20
|
-
|
34
|
+
private JTextField tf = null; //title
|
35
|
+
private JButton btn = null; //ボタン
|
21
36
|
|
22
|
-
|
37
|
+
private static int selected;
|
23
|
-
tf.setText(strFileName);
|
24
38
|
|
39
|
+
private static File selectedFile;
|
25
40
|
|
26
|
-
try {
|
27
|
-
if(checkBeforeReadfile(selectedFile)) {
|
28
41
|
|
29
|
-
|
42
|
+
public LogPanel2() { //logAdd classでcheckBeforeReadfileを使用
|
30
43
|
|
31
|
-
|
44
|
+
}
|
32
45
|
|
33
|
-
|
46
|
+
public LogPanel2(String title) {
|
47
|
+
super();
|
48
|
+
this.setLayout(new BorderLayout());
|
34
49
|
|
50
|
+
tf=new JTextField();
|
35
|
-
|
51
|
+
btn=new JButton("Open");
|
36
52
|
|
37
|
-
|
53
|
+
//btn.addActionListener(this);
|
38
54
|
|
55
|
+
JPanel pnl=new JPanel();
|
56
|
+
pnl.setLayout(new BorderLayout());
|
57
|
+
pnl.add(tf,BorderLayout.CENTER);
|
58
|
+
pnl.add(btn,BorderLayout.EAST);
|
39
59
|
|
60
|
+
//ta = new JTextArea();
|
40
|
-
|
61
|
+
JScrollPane sp = new JScrollPane(ta);
|
62
|
+
sp.setAutoscrolls(true);
|
41
63
|
|
42
|
-
sb.append(str);
|
43
|
-
|
64
|
+
this.add(pnl, BorderLayout.NORTH);
|
65
|
+
this.add(sp, BorderLayout.CENTER);
|
44
66
|
|
67
|
+
this.setBorder(new TitledBorder(title));
|
68
|
+
this.setPreferredSize(new Dimension(400,200));
|
45
69
|
|
46
|
-
|
70
|
+
btn.addActionListener(this);
|
47
71
|
|
48
|
-
|
72
|
+
}
|
49
|
-
System.out.println("null");
|
50
73
|
|
51
|
-
ta.setText(new String(sb));
|
52
74
|
|
75
|
+
/* ファイルを開く
|
53
|
-
|
76
|
+
*/
|
54
77
|
|
55
|
-
}
|
56
|
-
}
|
57
|
-
|
58
|
-
|
59
|
-
} catch(IOException err) {
|
60
|
-
|
61
|
-
JOptionPane.showMessageDialog(this,"IOException: ファイルを開くのに失敗しました。");
|
62
|
-
}
|
63
|
-
|
64
|
-
|
65
|
-
}
|
66
|
-
```
|
67
|
-
|
68
|
-
#修正点
|
69
|
-
別スレッドで実装しました。しかし、TextAreaにテキストが表示されませんでした。
|
70
|
-
ta.setTextやta.appendを使用してもTextAreaに文字列が表示されません。
|
71
|
-
|
72
|
-
```Java
|
73
|
-
private JTextArea ta = new JTextArea(); //editer
|
74
|
-
private JTextField tf = null; //title
|
75
|
-
private JButton btn = null; //ボタン
|
76
|
-
|
77
|
-
private static int selected;
|
78
|
-
|
79
|
-
private static File selectedFile;
|
80
|
-
//↑グローバル変数
|
81
|
-
|
82
|
-
|
78
|
+
public void actionPerformed(ActionEvent e) {
|
83
79
|
JFileChooser filechooser = new JFileChooser();
|
84
80
|
|
85
81
|
selected = filechooser.showOpenDialog(this);
|
@@ -154,4 +150,8 @@
|
|
154
150
|
|
155
151
|
|
156
152
|
}
|
153
|
+
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
157
|
```
|
6
修正点の変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -66,11 +66,11 @@
|
|
66
66
|
```
|
67
67
|
|
68
68
|
#修正点
|
69
|
-
別スレッドで実装しました。しかし、TextAreaにテキストが表示されませんでした。
|
69
|
+
別スレッドで実装しました。しかし、TextAreaにテキストが表示されませんでした。
|
70
|
-
|
70
|
+
ta.setTextやta.appendを使用してもTextAreaに文字列が表示されません。
|
71
71
|
|
72
72
|
```Java
|
73
|
-
private JTextArea ta =
|
73
|
+
private JTextArea ta = new JTextArea(); //editer
|
74
74
|
private JTextField tf = null; //title
|
75
75
|
private JButton btn = null; //ボタン
|
76
76
|
|
@@ -79,7 +79,7 @@
|
|
79
79
|
private static File selectedFile;
|
80
80
|
//↑グローバル変数
|
81
81
|
|
82
|
-
|
82
|
+
public void actionPerformed(ActionEvent e) {
|
83
83
|
JFileChooser filechooser = new JFileChooser();
|
84
84
|
|
85
85
|
selected = filechooser.showOpenDialog(this);
|
@@ -123,23 +123,22 @@
|
|
123
123
|
|
124
124
|
BufferedReader br = new BufferedReader(new FileReader(selectedFile));
|
125
125
|
|
126
|
-
StringBuilder sb = new StringBuilder();
|
126
|
+
//StringBuilder sb = new StringBuilder();
|
127
127
|
|
128
|
-
ta.setText("");
|
128
|
+
ta.setText("AA");
|
129
129
|
|
130
|
-
|
131
|
-
|
132
130
|
while((str = br.readLine()) != null) {
|
133
131
|
|
134
|
-
|
132
|
+
ta.append(str);
|
135
|
-
|
133
|
+
ta.append("\n");
|
136
134
|
|
137
135
|
|
138
136
|
System.out.println(str);
|
139
137
|
|
140
138
|
}
|
139
|
+
System.out.println("null");
|
141
140
|
|
142
|
-
ta.setText(new String(sb));
|
141
|
+
//ta.setText(new String(sb));
|
143
142
|
|
144
143
|
br.close();
|
145
144
|
|
@@ -155,9 +154,4 @@
|
|
155
154
|
|
156
155
|
|
157
156
|
}
|
158
|
-
```
|
159
|
-
```エラーメッセージ
|
160
|
-
Exception in thread "Thread-4" java.lang.NullPointerException
|
161
|
-
at logViewer.LogPanel2.run(LogPanel2.java:115)
|
162
|
-
at java.lang.Thread.run(Thread.java:748)
|
163
157
|
```
|
5
修正点追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -67,6 +67,7 @@
|
|
67
67
|
|
68
68
|
#修正点
|
69
69
|
別スレッドで実装しました。しかし、TextAreaにテキストが表示されませんでした。また、ファイルを開くと以下のようなエラーメッセージが出ます。
|
70
|
+
エラーメッセージを読むと、ta.setText("");の部分でエラーが起きています。
|
70
71
|
|
71
72
|
```Java
|
72
73
|
private JTextArea ta = null; //editer
|
4
エラーメッセージの記載
title
CHANGED
File without changes
|
body
CHANGED
@@ -66,7 +66,7 @@
|
|
66
66
|
```
|
67
67
|
|
68
68
|
#修正点
|
69
|
-
別スレッドで実装しました。しかし、TextAreaにテキストが表示されませんでした。
|
69
|
+
別スレッドで実装しました。しかし、TextAreaにテキストが表示されませんでした。また、ファイルを開くと以下のようなエラーメッセージが出ます。
|
70
70
|
|
71
71
|
```Java
|
72
72
|
private JTextArea ta = null; //editer
|
@@ -154,4 +154,9 @@
|
|
154
154
|
|
155
155
|
|
156
156
|
}
|
157
|
+
```
|
158
|
+
```エラーメッセージ
|
159
|
+
Exception in thread "Thread-4" java.lang.NullPointerException
|
160
|
+
at logViewer.LogPanel2.run(LogPanel2.java:115)
|
161
|
+
at java.lang.Thread.run(Thread.java:748)
|
157
162
|
```
|
3
コード修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -76,8 +76,8 @@
|
|
76
76
|
private static int selected;
|
77
77
|
|
78
78
|
private static File selectedFile;
|
79
|
+
//↑グローバル変数
|
79
80
|
|
80
|
-
|
81
81
|
public void actionPerformed(ActionEvent e) {
|
82
82
|
JFileChooser filechooser = new JFileChooser();
|
83
83
|
|
2
コードの修正点を追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -63,4 +63,95 @@
|
|
63
63
|
|
64
64
|
|
65
65
|
}
|
66
|
+
```
|
67
|
+
|
68
|
+
#修正点
|
69
|
+
別スレッドで実装しました。しかし、TextAreaにテキストが表示されませんでした。
|
70
|
+
|
71
|
+
```Java
|
72
|
+
private JTextArea ta = null; //editer
|
73
|
+
private JTextField tf = null; //title
|
74
|
+
private JButton btn = null; //ボタン
|
75
|
+
|
76
|
+
private static int selected;
|
77
|
+
|
78
|
+
private static File selectedFile;
|
79
|
+
|
80
|
+
|
81
|
+
public void actionPerformed(ActionEvent e) {
|
82
|
+
JFileChooser filechooser = new JFileChooser();
|
83
|
+
|
84
|
+
selected = filechooser.showOpenDialog(this);
|
85
|
+
|
86
|
+
if (JFileChooser.APPROVE_OPTION == selected) {
|
87
|
+
|
88
|
+
|
89
|
+
selectedFile = filechooser.getSelectedFile();
|
90
|
+
|
91
|
+
tf.setText("");
|
92
|
+
|
93
|
+
String strFileName = selectedFile.getName();
|
94
|
+
tf.setText(strFileName);
|
95
|
+
|
96
|
+
Thread thread = new Thread(new LogPanel2());
|
97
|
+
thread.start();
|
98
|
+
}
|
99
|
+
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
|
104
|
+
public static boolean checkBeforeReadfile(File file) {
|
105
|
+
if(file.exists()) {
|
106
|
+
if(file.isFile() && file.canRead()) {
|
107
|
+
return true;
|
108
|
+
}
|
109
|
+
}
|
110
|
+
|
111
|
+
return false;
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
public void run() {
|
116
|
+
|
117
|
+
|
118
|
+
try {
|
119
|
+
if(checkBeforeReadfile(selectedFile)) {
|
120
|
+
|
121
|
+
String str;
|
122
|
+
|
123
|
+
BufferedReader br = new BufferedReader(new FileReader(selectedFile));
|
124
|
+
|
125
|
+
StringBuilder sb = new StringBuilder();
|
126
|
+
|
127
|
+
ta.setText("");
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
while((str = br.readLine()) != null) {
|
132
|
+
|
133
|
+
sb.append(str);
|
134
|
+
sb.append("\n");
|
135
|
+
|
136
|
+
|
137
|
+
System.out.println(str);
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
ta.setText(new String(sb));
|
142
|
+
|
143
|
+
br.close();
|
144
|
+
|
145
|
+
}else {
|
146
|
+
System.out.println("null");
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
} catch(IOException err) {
|
151
|
+
|
152
|
+
JOptionPane.showMessageDialog(this,"IOException: ファイルを開くのに失敗しました。");
|
153
|
+
}
|
154
|
+
|
155
|
+
|
156
|
+
}
|
66
157
|
```
|
1
誤字修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
JavaでTextAreaに表示したテキストを文字列が追加されるたびに自動で更新したい。
|
3
3
|
|
4
4
|
#現在の課題
|
5
|
-
以下に
|
5
|
+
以下にTextAreaにテキストファイルの文章を表示する部分のコードを示します。
|
6
6
|
JFrameのTextAreaに無限ループを使用して文字列が追加されるたびに文字列を追記で表示したいのですが、現段階の課題として、無限ループを使用するとTextAreaに文字が表示されません。
|
7
7
|
|
8
8
|
有識者の方々ご協力お願いします
|