ベクトルの助けを借りて、テーブルにデータがデータベースに取り込まJCheckbox
れているGUIがあります。JTable
データベースからデータを抽出するための以下のコードを提供しています。
try {
PreparedStatement pre = con.prepareStatement("select * from ProductInformation where CustomerName='"+cust_name+"'");
Vector<Vector<String>> historyVector = new Vector<Vector<String>>();
rs = pre.executeQuery();
while(rs.next())
{
Vector<String> history = new Vector<String>();
serial_no=rs.getString("SerialNumber");
history.add(serial_no);
history.add(rs.getString("Model"));
history.add(rs.getString("InvoiceNumber"));
history.add(rs.getString("Location"));
historyVector.add(history);
}
data=historyVector;
header = new Vector<String>();
header.add("Serial No");
header.add("Model");
header.add("Invoice no");
header.add("Location");
header.add("ASC");
header.add("AMC");
amcAscResult();
mainframe.add(amcascresult);
mainframe.remove(amc);
mainframe.validate();
mainframe.repaint();
mainframe.pack();
}
catch(Exception e){
System.err.println(e);
}
プログラムを実行すると、一部を選択するJCheckbox
と Null ポインタ例外が返されるなどの問題が発生しています。期待どおりに実行される場合もあります。
テーブルにレコードを挿入するための私のコード
amcasc_table.setModel(new javax.swing.table.DefaultTableModel(
data,header
) {
boolean[] canEdit = new boolean [] {
false, false, false, true, true
};
private static final long serialVersionUID = 1L;
/*@Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}*/
@Override
public Class getColumnClass(int column) {
switch (column) {
case 0:
return String.class;
case 1:
return String.class;
case 2:
return String.class;
case 3:
return String.class;
default:
return Boolean.class;
}
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jScrollPane1.setViewportView(amcasc_table);
の値を取得しますJCheckbox
。
for (int i = 0; i <amcasc_table.getRowCount(); i++) {
boolean isChecked = (Boolean) amcasc_table.getValueAt(i, 4);
if (isChecked) {
//get the values of the columns you need.
String value = (String) amcasc_table.getValueAt(i, 0);
System.out.println(value);
}
}
私が犯した過ちは何ですか?これを克服する方法を教えてくれる人はいますか?
SSCCE
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.sql.*;
import java.text.DateFormat;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Vector;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.swing.*;
public class Combined {
Session session;
JFrame mainframe,mailframe;
JPanel firstpane, newsales, confirmnewsales, checkserialno, producthistory, newcomplaint, newcomplaintentry;
JPanel confirmcomplaint,sendmail,generatereport,generatereportresult,amc,amcascresult,amcascconfirm;
JMenuBar menuBar;
JMenu menu;
JMenuItem menuItem;
JMenu submenu;
JRadioButtonMenuItem rbMenuItem;
JCheckBoxMenuItem cbMenuItem;
Connection con,con1;
Statement st,st1;
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Format mmFormat = new SimpleDateFormat("MM");
DateFormat yyFormat = new SimpleDateFormat("yy");
DateFormat ddFormat = new SimpleDateFormat("dd");
String invoice_no;
String serial_no;
String model;
String cust_name;
String cust_location;
String amc_status;
Date date_of_purchase;
Date amc_start_date;
Date amc_end_date;
String amcend_date;
String amcstart_date;
ButtonModel amc_status_model;
String query;
ResultSet rs,rs1;
Date dateofpurchase;
String date_ofpurchase;
String query1;
String eng_name;
String problem_description;
String contact_person;
String contact_no;
String calldate;
String callmonth;
String callyear;
String callno;
int inccallno;
String lastcallno;
String query3, query4, query5;
String lastcalldate;
String sinccallno;
String db;
String toaddress;
String subject;
String messagebody;
String warrantystatus;
Vector<Vector<String>> data;
Vector<String> header;
String callstatus;
int no_of_calls = 0;
public Combined() {
connect();
framegen();
}
private void framegen() {
mainframe = new JFrame("Product Maintenance and Support Software");
//Create the menu bar.
menuBar = new JMenuBar();
//Build the first menu.
menu = new JMenu("A Menu");
menu.setMnemonic(KeyEvent.VK_A);
menu.getAccessibleContext().setAccessibleDescription(
"The only menu in this program that has menu items");
menuBar.add(menu);
//a group of JMenuItems
menuItem = new JMenuItem("A text-only menu item",
KeyEvent.VK_T);
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext().setAccessibleDescription(
"This doesn't really do anything");
menu.add(menuItem);
menuItem = new JMenuItem("Both text and icon",
new ImageIcon("images/middle.gif"));
menuItem.setMnemonic(KeyEvent.VK_B);
menu.add(menuItem);
menuItem = new JMenuItem(new ImageIcon("images/middle.gif"));
menuItem.setMnemonic(KeyEvent.VK_D);
menu.add(menuItem);
//a group of radio button menu items
menu.addSeparator();
ButtonGroup group = new ButtonGroup();
rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
rbMenuItem.setSelected(true);
rbMenuItem.setMnemonic(KeyEvent.VK_R);
group.add(rbMenuItem);
menu.add(rbMenuItem);
rbMenuItem = new JRadioButtonMenuItem("Another one");
rbMenuItem.setMnemonic(KeyEvent.VK_O);
group.add(rbMenuItem);
menu.add(rbMenuItem);
//a group of check box menu items
menu.addSeparator();
cbMenuItem = new JCheckBoxMenuItem("A check box menu item");
cbMenuItem.setMnemonic(KeyEvent.VK_C);
menu.add(cbMenuItem);
cbMenuItem = new JCheckBoxMenuItem("Another one");
cbMenuItem.setMnemonic(KeyEvent.VK_H);
menu.add(cbMenuItem);
//a submenu
menu.addSeparator();
submenu = new JMenu("A submenu");
submenu.setMnemonic(KeyEvent.VK_S);
menuItem = new JMenuItem("An item in the submenu");
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_2, ActionEvent.ALT_MASK));
submenu.add(menuItem);
menuItem = new JMenuItem("Another item");
submenu.add(menuItem);
menu.add(submenu);
//Build second menu in the menu bar.
menu = new JMenu("Another Menu");
menu.setMnemonic(KeyEvent.VK_N);
menu.getAccessibleContext().setAccessibleDescription(
"This menu does nothing");
menuBar.add(menu);
mainframe.setJMenuBar(menuBar);
firstpane();
mainframe.add(firstpane);
mainframe.pack();
mainframe.setVisible(true);
mainframe.setDefaultCloseOperation(3);
mainframe.setResizable(false);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Combined com = new Combined();
}
private void connect() {
try {
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver);
String db1 = "jdbc:odbc:solaris";
con = DriverManager.getConnection(db1);
st = con.createStatement();
} catch (ClassNotFoundException | SQLException ex) {
System.out.println("database connection failed");
}
}
private void firstpane() {
firstpane = new JPanel();
addnewsales_button = new javax.swing.JButton();
newcomplaint_button = new javax.swing.JButton();
calllist_button = new javax.swing.JButton();
generatereport_button = new javax.swing.JButton();
serialnocheck_button = new javax.swing.JButton();
solaris_logo = new javax.swing.JLabel();
Support_label = new javax.swing.JLabel();
putAMC_button = new javax.swing.JButton();
firstpane.setBackground(new java.awt.Color(255, 255, 255));
addnewsales_button.setText("Add New Sales Entry");
addnewsales_button.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
// addnewsales_buttonActionPerformed(evt);
}
});
newcomplaint_button.setText("New Complaint");
newcomplaint_button.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
// newcomplaint_buttonActionPerformed(evt);
}
});
calllist_button.setText("Call list");
calllist_button.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
// calllist_buttonActionPerformed(evt);
}
});
generatereport_button.setText("Generate Report");
generatereport_button.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
//generatereport_buttonActionPerformed(evt);
}
});
serialnocheck_button.setText("Serial Number Check");
serialnocheck_button.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
// serialnocheck_buttonActionPerformed(evt);
}
});
solaris_logo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/combined/image/Solaris_logo.jpg"))); // NOI18N
Support_label.setText("Support Section");
putAMC_button.setText("Put AMC");
putAMC_button.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
putAMC_buttonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(firstpane);
firstpane.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addComponent(calllist_button).addGap(41, 41, 41).addComponent(putAMC_button).addGap(28, 28, 28).addComponent(generatereport_button).addGap(74, 74, 74)).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addComponent(solaris_logo).addGap(132, 132, 132)))).addGroup(layout.createSequentialGroup().addGap(20, 20, 20).addComponent(addnewsales_button).addGap(44, 44, 44).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addComponent(Support_label, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap()).addGroup(layout.createSequentialGroup().addComponent(serialnocheck_button).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE).addComponent(newcomplaint_button).addGap(23, 23, 23)))));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(32, 32, 32).addComponent(solaris_logo, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(Support_label, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 72, Short.MAX_VALUE).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(addnewsales_button).addComponent(serialnocheck_button).addComponent(newcomplaint_button)).addGap(28, 28, 28).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(calllist_button).addComponent(generatereport_button).addComponent(putAMC_button)).addGap(34, 34, 34)));
}
private void putAMC_buttonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
amc();
mainframe.add(amc);
mainframe.remove(firstpane);
mainframe.validate();
mainframe.repaint();
mainframe.pack();
}
private void amc() {
amc=new JPanel();
custname_label = new javax.swing.JLabel();
custname_txtbox = new javax.swing.JTextField();
amc_search_button = new javax.swing.JButton();
amc.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "AMC Sign up", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 0, 0)));
custname_label.setText("Enter the Customer Name");
amc_search_button.setText("Search");
amc_search_button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
amc_search_buttonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(amc);
amc.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(custname_label)
.addGap(18, 18, 18)
.addComponent(custname_txtbox, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 46, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(116, 116, 116)
.addComponent(amc_search_button)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(46, 46, 46)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(custname_label)
.addComponent(custname_txtbox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(51, 51, 51)
.addComponent(amc_search_button)
.addContainerGap(136, Short.MAX_VALUE))
);
}
private void amc_search_buttonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
cust_name=custname_txtbox.getText().trim();
try {
PreparedStatement pre = con.prepareStatement("select * from ProductInformation where CustomerName='"+cust_name+"'");
Vector<Vector<String>> historyVector = new Vector<Vector<String>>();
rs = pre.executeQuery();
while(rs.next())
{
Vector<String> history = new Vector<String>();
serial_no=rs.getString("SerialNumber");
System.out.println("here");//Empid
history.add(serial_no);
history.add(rs.getString("Model"));
history.add(rs.getString("InvoiceNumber"));
history.add(rs.getString("Location"));
historyVector.add(history);
}
data=historyVector;
header = new Vector<String>();
header.add("Serial No");
header.add("Model");
header.add("Invoice no");
header.add("Location");
header.add("ASC");
header.add("AMC");
amcAscResult();
mainframe.add(amcascresult);
mainframe.remove(amc);
mainframe.validate();
mainframe.repaint();
mainframe.pack();
}
catch(Exception e){
System.err.println(e);
}
}
private void amcAscResult() {
amcascresult=new JPanel();
custname_label = new javax.swing.JLabel();
custname_var_label = new javax.swing.JLabel();
location_label = new javax.swing.JLabel();
custlocation_var_label = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
amcasc_table = new javax.swing.JTable();
putamcasc_button = new javax.swing.JButton();
amcascresult.setBorder(javax.swing.BorderFactory.createTitledBorder("Put AMC"));
custname_label.setText("Customer Name :");
custname_var_label.setText(cust_name);
location_label.setText("Location :");
custlocation_var_label.setText("<location>");
amcasc_table.setModel(new javax.swing.table.DefaultTableModel(
data,header
) {
boolean[] canEdit = new boolean [] {
false, false, false, true, true
};
private static final long serialVersionUID = 1L;
/*@Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}*/
@Override
public Class getColumnClass(int column) {
switch (column) {
case 0:
return String.class;
case 1:
return String.class;
case 2:
return String.class;
case 3:
return String.class;
default:
return Boolean.class;
}
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jScrollPane1.setViewportView(amcasc_table);
putamcasc_button.setText("Put AMC/ASC");
putamcasc_button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
putamcasc_buttonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(amcascresult);
amcascresult.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(putamcasc_button)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(custname_label)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(custname_var_label)
.addGap(18, 18, 18)
.addComponent(location_label)
.addGap(18, 18, 18)
.addComponent(custlocation_var_label))
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(custname_label)
.addComponent(custname_var_label)
.addComponent(location_label)
.addComponent(custlocation_var_label))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
.addComponent(putamcasc_button))
);
}
private void putamcasc_buttonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
for (int i = 0; i <amcasc_table.getRowCount(); i++) {
boolean isChecked = (Boolean) amcasc_table.getValueAt(i, 4);
if (isChecked) {
//get the values of the columns you need.
String value = (String) amcasc_table.getValueAt(i, 0);
System.out.println(value);
}
}
amcAscConfirm();
mainframe.add(amcascconfirm);
mainframe.remove(amcascresult);
mainframe.validate();
mainframe.repaint();
mainframe.pack();
}
private void amcAscConfirm() {
amcascconfirm=new JPanel();
confirm_label = new javax.swing.JLabel();
finish_button = new javax.swing.JButton();
amcascconfirm.setBorder(javax.swing.BorderFactory.createTitledBorder("Confirmation of AMC/ASC"));
confirm_label.setText("AMC/ASC has been availed for the machines selected");
finish_button.setText("Finish");
finish_button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
finish_button1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(amcascconfirm);
amcascconfirm.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(73, 73, 73)
.addComponent(confirm_label)
.addGap(0, 95, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(finish_button)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(59, 59, 59)
.addComponent(confirm_label)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 169, Short.MAX_VALUE)
.addComponent(finish_button)
.addContainerGap())
);
}
private void finish_button1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
mainframe.add(firstpane);
mainframe.remove(amcascconfirm);
mainframe.validate();
mainframe.repaint();
mainframe.pack();
}
private javax.swing.JLabel confirm_label;
private javax.swing.JButton finish_button;
private javax.swing.JTable amcasc_table;
private javax.swing.JLabel custlocation_var_label;
private javax.swing.JLabel custname_label;
private javax.swing.JLabel custname_var_label;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel location_label;
private javax.swing.JButton putamcasc_button;
private javax.swing.JButton amc_search_button;
private javax.swing.JTextField custname_txtbox;
private javax.swing.JLabel Support_label;
private javax.swing.JButton addnewsales_button;
private javax.swing.JButton calllist_button;
private javax.swing.JButton generatereport_button;
private javax.swing.JButton newcomplaint_button;
private javax.swing.JButton putAMC_button;
private javax.swing.JButton serialnocheck_button;
private javax.swing.JLabel solaris_logo;
}