Visual Editor のプロジェクト環境を再現する為に、ワークスペースで
保存しています。エラーが出ますが、「プロジェクト」->「クリーン」
と同期の取れていないコードを F5 で同期させる事によって再現できます
社員データの新規登録
処理の中心は、ボタンをクリック後の処理です、
チェック処理と更新処理に限定して別パッケージにしています
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(467, 326);
this.setContentPane(getJContentPane());
this.setTitle("社員マスタメンテ");
this.appCheck = new Check();
this.appUpdate = new Update();
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(306, 23, 116, 31));
jButton.setText("確認");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()");
// 社員コードの入力チェック
if ( appCheck.targetExist(lboxText.getText()) ) {
JOptionPane.showMessageDialog(
Main.this,
"指定されたデータは既に存在します");
return;
}
lboxText.setEditable(false);
jButton.setEnabled(false);
jButton1.setEnabled(true);
int cnt = jContentPane.getComponentCount();
for( int i = 0; i< cnt; i++) {
java.awt.Component child = jContentPane.getComponent(i);
try {
if ( ((LboxText)child).getGroup() == 2 ) {
child.setEnabled(true);
}
}
catch( Exception ex ) {
}
}
}
});
}
return jButton;
}
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new Rectangle(30, 238, 392, 31));
jButton1.setText("更新");
jButton1.setEnabled(false);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()");
// 社員データの更新処理
Object param[] = {lboxText,lboxText1,lboxText2,lboxText3};
if ( !appUpdate.insertData(param) ) {
JOptionPane.showMessageDialog(
Main.this,
"書き込みエラーが発生しました",
"エラー",
JOptionPane.ERROR_MESSAGE);
return;
}
JOptionPane.showMessageDialog(
Main.this,
"更新が終了しました");
lboxText.setEditable(true);
int cnt = jContentPane.getComponentCount();
for( int i = 0; i< cnt; i++) {
java.awt.Component child = jContentPane.getComponent(i);
try {
if ( ((LboxText)child).getGroup() == 2 ) {
child.setEnabled(false);
((LboxText)child).setText("");
}
}
catch( Exception ex ) {
}
}
jButton.setEnabled(true);
jButton1.setEnabled(false);
}
});
}
return jButton1;
}
Check.java
入力した社員コードをファイル名として存在チェックを行っています
package action;
import java.io.File;
//*****************************************************
//入力チェッククラス
//*****************************************************
public class Check {
// 社員コード存在チェック
public boolean targetExist(String code) {
boolean ret = true;
File targetData = new File(code);
if ( !targetData.exists() ) {
ret = false;
}
return ret;
}
}
Update.java
画面データを Object の配列で引き渡してテキストファイルに
書き込んでいます
package action;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import myswing.LboxText;
//*****************************************************
//更新クラス
//*****************************************************
public class Update {
public boolean insertData(Object param[]) {
// デフォルトは正常終了
boolean ret = true;
// 書き込み用
BufferedWriter outBuffer = null;
FileWriter outFile = null;
// 主キーデータ
LboxText code = (LboxText)param[0];
try {
outFile = new FileWriter(code.getText());
outBuffer = new BufferedWriter(outFile);
outBuffer.write(((LboxText)param[1]).getText()+"\n");
outBuffer.write(((LboxText)param[2]).getText()+"\n");
outBuffer.write(((LboxText)param[3]).getText()+"\n");
outBuffer.flush();
outBuffer.close();
}
catch (IOException e) {
System.out.println(e.getMessage());
ret = false;
}
return ret;
}
}
|