3

私はJavaSwingを使用してスタンドアロンアプリケーションを構築しており、データを保存するためのバックエンドとしてExcel2007シートを使用しています。GUIからExcelシートにテキスト値と整数値を正常に挿入しました。JCombobox選択した値をExcelシートのに挿入するのに問題があります。誰かがそれを行う方法を教えてもらえますか?

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

public class ExcelTest extends Frame implements ActionListener

{
 Label l1;
 TextField textID, textFirstName, textLastName, textJobTitle;
 Button First, Next, Previous, Last;
 JComboBox comboGrade;


 public ExcelTest(String s) {

  setTitle("Testing connection");
  setLayout(null);
  l1 = new Label("Job Title");
  textJobTitle = new TextField(20);
  l1.setBounds(50, 100, 200, 50);
  textJobTitle.setBounds(300, 110, 135, 30);

  textID = new TextField(10);
  textID.setBounds(300, 160, 135, 30);

  textFirstName = new TextField(20);
  textFirstName.setBounds(300, 210, 135, 30);

  textLastName = new TextField(20);
  textLastName.setBounds(300, 260, 135, 30);


  final String[] grade = {
   "X",
   "Y",
   "Z"
  };
  comboGrade = new JComboBox(grade);
  comboGrade.setEditable(true);
  comboGrade.addActionListener(new ActionListener() {

   public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("comboBoxEdited")) {
     System.out.println("Adding new player!");
     comboGrade.insertItemAt(comboGrade.getSelectedItem(), 0);
    }
   }
  });


  comboGrade.setBounds(300, 300, 135, 30);


  First = new Button("First");
  First.setBounds(200, 600, 80, 40);

  Previous = new Button("PREVIOUS");
  Previous.setBounds(400, 600, 80, 40);

  Next = new Button("NEXT");
  Next.setBounds(600, 600, 80, 40);

  Last = new Button("LAST");
  Last.setBounds(800, 600, 80, 40);

  add(l1);
  add(textJobTitle);

  add(textFirstName);
  add(textLastName);
  add(textID);
  add(comboGrade);

  add(First);
  First.addActionListener(this);
  add(Next);
  Next.addActionListener(this);
  add(Previous);
  Previous.addActionListener(this);
  add(Last);
  Last.addActionListener(this);

  setSize(1000, 1000);
  setVisible(true);
  setResizable(false);

 }

 public void actionPerformed(ActionEvent ae) {


  try {
   //String id="";
   //String first_name="";
   //String last_name="";
   //String job="";


   String value1 = textID.getText();

   String value2 = textFirstName.getText();

   String value3 = textLastName.getText();

   String value4 = textJobTitle.getText();

   String value5 = comboGrade.getText();




   //connect to the database  
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
   Connection con = DriverManager.getConnection("jdbc:odbc:Testing");

   //Execute some sql and load the records into the resultset

   Statement stmt = con.createStatement();
   String q = "insert into [sheet1$](id,first_name,last_name,job_Title,grade) values     ('" + value1 + "','" + value2 + "','" + value3 + "','" + value4 + "','" + value5 + "')";
   int ds = stmt.executeUpdate(q);
   System.out.println("Data is inserted");

   //move the cursor to the first record and get the data




   stmt.close();
   con.close();

  } catch (java.lang.ClassNotFoundException e) {
   System.err.print("ClassNotFoundException: ");
   System.err.println(e.getMessage());
  } catch (SQLException err) {
   JOptionPane.showMessageDialog(ExcelTest.this, err.getMessage());
  }

  //catch(java.lang.InstantiationException i){}
  catch (Exception e) {}
 }


 public static void main(String s[]) throws NullPointerException {

  ExcelTest myFrame = new ExcelTest("MANAGEMENT OF STATEMENT OF WORKS");

  myFrame.addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent we) {
    System.exit(0);
   }
  });
 }

}
4

1 に答える 1

1

この行:

String value5=comboGrade.getText();

次のようにする必要があります。

String value5 = (String) comboGrade.getSelectedItem();
于 2012-08-22T08:15:13.547 に答える