Here is my complete code...
When I run this and click button its showing nullpointer exception at resultset
package com.openbravo.pos.followup;
/**
*
* #author manideep
*/
public class FollowUp extends javax.swing.JPanel implements JPanelView, BeanFactoryApp {
private AppView m_App;
protected DataLogicCustomers dlCustomers;
// CustomerInfo customer;
private AppConfig conf;
// private ListProvider lpr
private DefaultTableModel modelx, model;
protected DataLogicFollowUp dlFollowUp;
/**
* Creates new form FollowUp
*/
public FollowUp() {
initComponents();
String[] args = {};
conf = new AppConfig(args);
conf.load();
jLabel3.setText("");
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
m_jTicketId3 = new javax.swing.JLabel();
m_jTicketId1 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jcmdOK = new javax.swing.JButton();
jcmdCancel = new javax.swing.JButton();
jCheckBox1 = new javax.swing.JCheckBox();
jLabel1.setText("Enter Date");
jTextField1.setPreferredSize(new java.awt.Dimension(90, 19));
jButton1.setText("Submit");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel3.setText("jLabel3");
jButton3.setText("Customer");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setText("Product");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
m_jTicketId3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
m_jTicketId3.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createLineBorder(javax.swing.UIManager.getDefaults().getColor("Button.darkShadow")), javax.swing.BorderFactory.createEmptyBorder(1, 4, 1, 4)));
m_jTicketId3.setOpaque(true);
m_jTicketId3.setPreferredSize(new java.awt.Dimension(160, 25));
m_jTicketId3.setRequestFocusEnabled(false);
m_jTicketId1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
m_jTicketId1.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createLineBorder(javax.swing.UIManager.getDefaults().getColor("Button.darkShadow")), javax.swing.BorderFactory.createEmptyBorder(1, 4, 1, 4)));
m_jTicketId1.setOpaque(true);
m_jTicketId1.setPreferredSize(new java.awt.Dimension(160, 25));
m_jTicketId1.setRequestFocusEnabled(false);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE))
.addGap(154, 154, 154)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(m_jTicketId1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(m_jTicketId3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jLabel3))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 32, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(24, 24, 24)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton3)
.addComponent(m_jTicketId3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton4))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGap(17, 17, 17)
.addComponent(m_jTicketId1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(0, 0, Short.MAX_VALUE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel3)
.addGap(24, 24, 24))
);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Customer", "Phone No.", "Product", "Last sold Qty.", "Consumption", "last sale date", "Delivery Date", "Remark 1", "Remark 2", "Stock(Y/N)", "Invoice No."
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
};
boolean[] canEdit = new boolean [] {
false, false, false, false, false, false, false, true, true, true, true
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jTable1.getTableHeader().setReorderingAllowed(false);
jScrollPane1.setViewportView(jTable1);
jTable1.getColumnModel().getColumn(0).setResizable(false);
jTable1.getColumnModel().getColumn(1).setResizable(false);
jTable1.getColumnModel().getColumn(3).setResizable(false);
jTable1.getColumnModel().getColumn(4).setResizable(false);
jTable1.getColumnModel().getColumn(5).setResizable(false);
jTable1.getColumnModel().getColumn(6).setResizable(false);
jTable1.getColumnModel().getColumn(7).setResizable(false);
jTable1.getColumnModel().getColumn(7).setPreferredWidth(0);
jTable1.getColumnModel().getColumn(8).setResizable(false);
jTable1.getColumnModel().getColumn(9).setResizable(false);
jTable1.getColumnModel().getColumn(10).setResizable(false);
jcmdOK.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/button_ok.png"))); // NOI18N
jcmdOK.setText("OK");
jcmdOK.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jcmdOKActionPerformed(evt);
}
});
jcmdCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/button_cancel.png"))); // NOI18N
jcmdCancel.setText("Cancel");
jcmdCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jcmdCancelActionPerformed(evt);
}
});
jCheckBox1.setText("Select All");
jCheckBox1.setMaximumSize(new java.awt.Dimension(0, 0));
jCheckBox1.setPreferredSize(new java.awt.Dimension(0, 0));
jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jCheckBox1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(353, 353, 353)
.addComponent(jcmdOK)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jcmdCancel)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1408, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jCheckBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(74, 74, 74))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(8, 8, 8)
.addComponent(jCheckBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 406, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jcmdOK)
.addComponent(jcmdCancel))
.addGap(31, 31, 31))
);
}// </editor-fold>//GEN-END:initComponents
private void getListSRLine(Element e){
String customer= getAttrDataFromElement((Element)e.getElementsByTagName("customer").item(0),"identifier");
String phone= getCharacterDataFromElement((Element)e.getElementsByTagName("phone").item(0));
String product=getAttrDataFromElement((Element)e.getElementsByTagName("product").item(0),"identifier");
String lastsoldqty=getCharacterDataFromElement((Element)e.getElementsByTagName("lastsoldqty").item(0));
String consumption=getCharacterDataFromElement((Element)e.getElementsByTagName("consumption").item(0));
String lastsaledate=getCharacterDataFromElement((Element)e.getElementsByTagName("lastsaledate").item(0));
String deliverydate=getCharacterDataFromElement((Element)e.getElementsByTagName("deliverydate").item(0));
String remark1=getCharacterDataFromElement((Element)e.getElementsByTagName("remark1").item(0));
String remark2=getCharacterDataFromElement((Element)e.getElementsByTagName("remark2").item(0));
String stock=getCharacterDataFromElement((Element)e.getElementsByTagName("stock").item(0));
String invoiceno=getCharacterDataFromElement((Element)e.getElementsByTagName("invoiceno").item(0));
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
model.addRow(new Object[]{customer,phone,product,lastsoldqty,consumption,lastsaledate,deliverydate,remark1,remark2,stock,invoiceno});
//sortColumn(model, 1, true);
model.fireTableDataChanged();
jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jTable1.setAutoCreateRowSorter(true);
jTable1.getColumnModel().getColumn(0).setPreferredWidth(150);
jTable1.getColumnModel().getColumn(1).setPreferredWidth(120);
jTable1.getColumnModel().getColumn(2).setPreferredWidth(200);
jTable1.getColumnModel().getColumn(3).setPreferredWidth(50);
jTable1.getColumnModel().getColumn(4).setPreferredWidth(100);
jTable1.getColumnModel().getColumn(5).setPreferredWidth(100);
jTable1.getColumnModel().getColumn(6).setPreferredWidth(100);
jTable1.getColumnModel().getColumn(7).setPreferredWidth(100);
jTable1.getColumnModel().getColumn(8).setPreferredWidth(100);
jTable1.getColumnModel().getColumn(9).setPreferredWidth(100);
jTable1.getColumnModel().getColumn(10).setPreferredWidth(100);
jTable1.getColumnModel().getColumn(11).setPreferredWidth(0);
}
private void executeSearch() {
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your PostgreSQL JDBC Driver? "
+ "Include in your library path!");
e.printStackTrace();
return;
}
System.out.println("PostgreSQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/openbravopos", "postgres",
"postgres");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
try {
System.out.println("You made it, take control your database now!");
int colCount;
String [] headers;
Statement stmt = null;
// rs=(ResultSet) dlFollowUp.getFollowUpList();
// getFollowUplist();
ResultSet rs = null;
rs = stmt.executeQuery("SELECT id,customer,phone,product,lastsoldqty,consumption,lastsaledate,deliverydate,remark1,remark2,stock,invoiceno FROM followup");
java.sql.ResultSetMetaData rsmd = rs.getMetaData();
int colNo = rsmd.getColumnCount();
while(rs.next()){
Object[] objects = new Object[colNo];
for(int i=0;i<colNo;i++){
objects[i]=rs.getObject(i+1);
}
model.addRow(objects);
}
jTable1.setModel(model);
} catch (SQLException ex) {
Logger.getLogger(FollowUp.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
System.out.println("Failed to make connection!");
}
}
public static String getAttrDataFromElement(Element e,String attr){
return e.getAttribute(attr);
}
public static String getCharacterDataFromElement(Element e) {
Node child = e.getFirstChild();
if (child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
}
return "";
}
private void jcmdOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcmdOKActionPerformed
// jButton1ActionPerformed(evt);
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
//dispose();
}//GEN-LAST:event_jcmdOKActionPerformed
private void jcmdCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcmdCancelActionPerformed
dispose();
}//GEN-LAST:event_jcmdCancelActionPerformed
private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jCheckBox1ActionPerformed
String field="";
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
jLabel3.setText("");
executeSearch();
// entity = response.getafilter();
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
JCustomerFinder finder = JCustomerFinder.getCustomerFinder(this, dlCustomers);
finder.setVisible(true);
CustomerInfo custsel=finder.getSelectedCustomer();
m_jTicketId3.setText(custsel.printName());
}//GEN-LAST:event_jButton3ActionPerformed
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
/* // TODO add your handling code here:
JProductFinder finder = JProductFinder.getProductFinder(this, dlSales, m_App.getInventoryLocation(),m_App);
finder.setVisible(true);
ProductAttrInfoExt prod = finder.getSelectedProduct();
m_jTicketId1.setText(prod.getName());*/
}//GEN-LAST:event_jButton4ActionPerformed
private void dispose(){
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
model.setRowCount(0);
jLabel3.setText(null);
model.fireTableDataChanged();
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JCheckBox jCheckBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JTextField jTextField1;
private javax.swing.JButton jcmdCancel;
private javax.swing.JButton jcmdOK;
private javax.swing.JLabel m_jTicketId1;
private javax.swing.JLabel m_jTicketId3;
// End of variables declaration//GEN-END:variables
#Override
public String getTitle() {
return "Follow Up";
}
#Override
public void activate() throws BasicException {
resetFollowUp();
}
public void resetFollowUp(){
jTextField1.setText("");
m_jTicketId3.setText("");
m_jTicketId1.setText("");
}
#Override
public boolean deactivate() {
return true;
}
#Override
public JComponent getComponent() {
return this;
}
#Override
public void init(AppView app) throws BeanFactoryException {
jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jTable1.getColumnModel().getColumn(0).setPreferredWidth(150);
jTable1.getColumnModel().getColumn(1).setPreferredWidth(150);
jTable1.getColumnModel().getColumn(2).setPreferredWidth(250);
jTable1.getColumnModel().getColumn(3).setPreferredWidth(100);
jTable1.getColumnModel().getColumn(4).setPreferredWidth(200);
jTable1.getColumnModel().getColumn(5).setPreferredWidth(250);
jTable1.getColumnModel().getColumn(6).setPreferredWidth(250);
jTable1.getColumnModel().getColumn(7).setPreferredWidth(250);
jTable1.getColumnModel().getColumn(8).setPreferredWidth(200);
jTable1.getColumnModel().getColumn(9).setPreferredWidth(200);
jTable1.getColumnModel().getColumn(10).setPreferredWidth(100);
DefaultTableModel modelx = (DefaultTableModel) jTable1.getModel();
for(int j=0;j< modelx.getRowCount();j++){
}
m_App=app;
// setWarehouse(m_App.getWarehouseSearchKey());
dlCustomers = (DataLogicCustomers) m_App.getBean("com.openbravo.pos.customers.DataLogicCustomers");
}
#Override
public Object getBean() {
return this;
}
}
You never initialise the Statement object....
Statement stmt = null; // Look ma, I'm null
// rs=(ResultSet) dlFollowUp.getFollowUpList();
// getFollowUplist();
ResultSet rs = null;
// Look ma, I'm still null
rs = stmt.executeQuery("SELECT id,customer,phone,product,lastsoldqty,consumption,lastsaledate,deliverydate,remark1,remark2,stock,invoiceno FROM followup");
You need to create the stmt before you can use...
stmt = connection.createStatement();
You may find reading through the JDBC tutorials of benifit
Problem #2
You define model as instance variable at the start of your FollowUp class...
private DefaultTableModel modelx, model;
But you never initialise it to anything....
I would imagine, based on your code, you would need to initialise it in your executeSearch method...
// You will need to fill this out with appropriate values,
// the number of columns in you JTable should match the
// the number columns from your query...
Object[] columns = new Object[]{....};
model = new DefaultTableModel(columns, 0);
while(rs.next()){
Object[] objects = new Object[colNo];
for(int i=0;i<colNo;i++){
objects[i]=rs.getObject(i+1);
}
model.addRow(objects);
}
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
I have a class Hangman and one class GUI. Hangman class for the process. So, I want to passing the value String form Hangman class to jLabel in GUI. When I run the project and I am strungling in java.lang.NullPointerException even I wrote the right code. When user click start. The value from class Hangman will show to the jLabel
Hangman class
package hangman;
import java.util.*;
import java.io.*;
import java.awt.*;
public class Hangman {
String filename = "words.txt";
String[] wordList;
String[] wordBanks = {"DISAVOW","KEYHOLE","QUIZZES",
"WHOMEVER","ZIGZIG","WHEEZY","UNWORTHY",
"WHIZZING","JOCKEY","MICROWAVE"};
String[] answer;
int index;
String[] resultarray;
StringBuilder stringbuilder = new StringBuilder();
String resultstring;
public Hangman()
{
initialComponent();
}
public void initialComponent()
{
index = randomIndex();
answer = wordBanks[index].split("");
resultarray = new String[answer.length];
for(int i = 0; i < answer.length ;i++)
{
resultarray[i] = " _ ";
stringbuilder.append(resultarray[i]);
}
resultstring = stringbuilder.toString();
}
public int randomIndex()
{
int rand = 0;
// define the range
int max = 10;
int min = 0;
int range = max - min;
// generate random numbers within 1 to 10
for (int i = 0; i < 10; i++) {
rand = (int)(Math.random() * range) + min;
// Output is different everytime this code is executed
//System.out.println(rand);
}
return rand;
}
public void printWord()
{
System.out.println(wordBanks[randomIndex()].toString() + "|" + wordBanks[randomIndex()].length());
}
public void initialWord()
{
int random = randomIndex();
//answer = new String[random];
answer = wordBanks[random].split("");
for (int i = 0; i < answer.length;i++)
System.out.println(answer[i]);
}
public static void main(String[] args) {
// TODO code application logic here
Hangman hangman = new Hangman();
GUI gui = new GUI();
gui.setVisible(true);
}
}
GUI class
'''
package hangman;
public class GUI extends javax.swing.JFrame {
Hangman hangman;
/**
* Creates new form GUI
*/
public GUI() {
initComponents();
//defaultResult();
}
public void defaultResult()
{
jLabelGuess.setText(hangman.resultstring);
}
#SuppressWarnings("unchecked")
private void initComponents() {
jFrame1 = new javax.swing.JFrame();
jLabel1 = new javax.swing.JLabel();
jButtonStart = new javax.swing.JButton();
jButtonA = new javax.swing.JButton();
jButtonB = new javax.swing.JButton();
jButtonC = new javax.swing.JButton();
jButtonD = new javax.swing.JButton();
jButtonE = new javax.swing.JButton();
jButtonF = new javax.swing.JButton();
jButtonG = new javax.swing.JButton();
jButtonH = new javax.swing.JButton();
jButtonI = new javax.swing.JButton();
jButtonJ = new javax.swing.JButton();
jButtonO = new javax.swing.JButton();
jButtonP = new javax.swing.JButton();
jButtonQ = new javax.swing.JButton();
jButtonR = new javax.swing.JButton();
jButtonK = new javax.swing.JButton();
jButtonS = new javax.swing.JButton();
jButtonL = new javax.swing.JButton();
jButtonT = new javax.swing.JButton();
jButtonN = new javax.swing.JButton();
jButtonM = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
jButtonO1 = new javax.swing.JButton();
jButtonP1 = new javax.swing.JButton();
jButtonK1 = new javax.swing.JButton();
jButtonL1 = new javax.swing.JButton();
jButtonN1 = new javax.swing.JButton();
jButtonM1 = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
jLabelAtemp = new javax.swing.JLabel();
jLabelGuess = new javax.swing.JLabel();
javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
jFrame1.getContentPane().setLayout(jFrame1Layout);
jFrame1Layout.setHorizontalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jFrame1Layout.setVerticalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Welcome to Hangman");
jButtonStart.setText("Start");
jButtonStart.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonStartActionPerformed(evt);
}
});
jButtonA.setText("A");
jButtonB.setText("B");
jButtonC.setLabel("C");
jButtonD.setText("D");
jButtonD.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonDActionPerformed(evt);
}
});
jButtonE.setText("E");
jButtonF.setText("F");
jButtonG.setText("G");
jButtonH.setText("H");
jButtonH.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonHActionPerformed(evt);
}
});
jButtonI.setText("I");
jButtonJ.setText("J");
jButtonJ.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonJActionPerformed(evt);
}
});
jButtonO.setText("O");
jButtonO.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonOActionPerformed(evt);
}
});
jButtonP.setText("P");
jButtonQ.setText("Q");
jButtonR.setText("R");
jButtonK.setText("K");
jButtonK.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonKActionPerformed(evt);
}
});
jButtonS.setText("S");
jButtonS.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonSActionPerformed(evt);
}
});
jButtonL.setText("L");
jButtonT.setText("T");
jButtonN.setText("N");
jButtonN.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonNActionPerformed(evt);
}
});
jButtonM.setText("M");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 256, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jButtonO1.setText("O");
jButtonO1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonO1ActionPerformed(evt);
}
});
jButtonP1.setText("P");
jButtonK1.setText("K");
jButtonK1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonK1ActionPerformed(evt);
}
});
jButtonL1.setText("L");
jButtonN1.setText("N");
jButtonN1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonN1ActionPerformed(evt);
}
});
jButtonM1.setText("M");
jLabel2.setText("Atemps left:");
jLabelAtemp.setText("jLabel3");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButtonN1, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonO1, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonP1, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(189, 189, 189))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(57, 57, 57)
.addComponent(jButtonK1, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonL1, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonM1, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jButtonA, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonB, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonC, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonD, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jButtonK, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonL, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonM, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButtonN, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonO, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonP, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonQ, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonR, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonS, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonT, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(163, 163, 163)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabelGuess, javax.swing.GroupLayout.PREFERRED_SIZE, 287, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(jButtonE, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonF, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonG, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonH, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonI, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonJ, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(192, 192, 192)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addGap(20, 20, 20)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel2)
.addComponent(jButtonStart))
.addGap(18, 18, 18)
.addComponent(jLabelAtemp)))
.addGap(155, 155, 155)))
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(35, 35, 35)
.addComponent(jLabelGuess, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButtonA)
.addComponent(jButtonB)
.addComponent(jButtonC)
.addComponent(jButtonD)
.addComponent(jButtonE)
.addComponent(jButtonF)
.addComponent(jButtonG)
.addComponent(jButtonH)
.addComponent(jButtonI)
.addComponent(jButtonJ))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButtonK)
.addComponent(jButtonL)
.addComponent(jButtonM)
.addComponent(jButtonO)
.addComponent(jButtonP)
.addComponent(jButtonQ)
.addComponent(jButtonR)
.addComponent(jButtonS)
.addComponent(jButtonT)
.addComponent(jButtonN))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButtonK1)
.addComponent(jButtonL1)
.addComponent(jButtonM1)
.addComponent(jButtonO1)
.addComponent(jButtonP1)
.addComponent(jButtonN1))
.addGap(28, 28, 28)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jLabelAtemp))
.addGap(29, 29, 29)
.addComponent(jButtonStart)))
.addGap(19, 19, 19))
);
pack();
}// </editor-fold>
private void jButtonDActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jButtonHActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jButtonJActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jButtonOActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jButtonSActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButtonNActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButtonKActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jButtonO1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButtonK1ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jButtonN1ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jButtonStartActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jLabelGuess.setText(hangman.resultstring);
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//Hangman hangman = new Hangman();
//GUI gui = new GUI();
//gui.setVisible(true);
/* Create and display the form */
/*java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new GUI().setVisible(true);
}
});*/
}
// Variables declaration - do not modify
private javax.swing.JButton jButtonA;
private javax.swing.JButton jButtonB;
private javax.swing.JButton jButtonC;
private javax.swing.JButton jButtonD;
private javax.swing.JButton jButtonE;
private javax.swing.JButton jButtonF;
private javax.swing.JButton jButtonG;
private javax.swing.JButton jButtonH;
private javax.swing.JButton jButtonI;
private javax.swing.JButton jButtonJ;
private javax.swing.JButton jButtonK;
private javax.swing.JButton jButtonK1;
private javax.swing.JButton jButtonL;
private javax.swing.JButton jButtonL1;
private javax.swing.JButton jButtonM;
private javax.swing.JButton jButtonM1;
private javax.swing.JButton jButtonN;
private javax.swing.JButton jButtonN1;
private javax.swing.JButton jButtonO;
private javax.swing.JButton jButtonO1;
private javax.swing.JButton jButtonP;
private javax.swing.JButton jButtonP1;
private javax.swing.JButton jButtonQ;
private javax.swing.JButton jButtonR;
private javax.swing.JButton jButtonS;
private javax.swing.JButton jButtonStart;
private javax.swing.JButton jButtonT;
private javax.swing.JFrame jFrame1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabelAtemp;
private javax.swing.JLabel jLabelGuess;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
The message error when I run the application.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at hangman.GUI.jButtonStartActionPerformed(GUI.java:400)
at hangman.GUI.access$000(GUI.java:12)
at hangman.GUI$1.actionPerformed(GUI.java:96)
It seems that in the GUI class you declare a member variable, Hangman hangman, but you never initialize it. Therefore, the code jLabelGuess.setText(hangman.resultstring); and similar calls will result in a null pointer exception.
In the GUI class, you just only declare Hangman hangman, which mean the class Hangman has never been called to execute or initialized any value. Therefore, hangman.resultstring is null when you called jLabelGuess.setText(hangman.resultstring), since resultstring value is get from Hangman class; and cause Null Pointer Exception error at jLabelGuess.setText() and the function jButtonStartActionPerformed()
You might fix it if you initialize the Hangman class: Hangman hangman = new Hangman(); Or calling the function that initializes the value of resultstring which is hangman.initialComponent()
I want to add checkboxes to a jpanel, which is on a jFrame. The UI was made on netbeans and i have copied the code to work on its functionality on eclipse.
I cant get my checkboxes come over the Jpanel.
The code
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JCheckBox;
import javax.swing.border.Border;
public class QueryBuilderUI extends javax.swing.JFrame {
QueryBuilderMethods objQBM = new QueryBuilderMethods();
DefaultComboBoxModel<String> repoModel = new DefaultComboBoxModel<String>();
public QueryBuilderUI() {
getRepositoryListing();
initComponents();
}
private void getRepositoryListing()
{
repoModel = new QueryBuilderMethods().getAllRepositoryName();
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jComboBox1 = new javax.swing.JComboBox();
jLabel2 = new javax.swing.JLabel();
jComboBox2 = new javax.swing.JComboBox();
jLabel3 = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
jTextField1 = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Choose Repository");
jComboBox1.setModel(repoModel);
jComboBox1.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
jComboBox1ItemStateChanged(evt);
}
});
jLabel2.setText("Choose Table");
//jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
jComboBox2.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
jComboBox2ItemStateChanged(evt);
}
});
jLabel3.setText("Choose Columns");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
Border border = BorderFactory.createTitledBorder("Select Columns");
jPanel1.setBorder(border);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 459, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 124, Short.MAX_VALUE)
);
jTextField1.setText("jTextField1");
jLabel4.setText("Generated Query");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addGap(57, 57, 57)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 412, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(65, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(44, 44, 44)
.addComponent(jLabel4)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(92, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {
// TODO add your handling code here:
if(evt.getStateChange()== ItemEvent.SELECTED){
Object item = evt.getItem();
DefaultComboBoxModel<String> modelTable = objQBM.getAllTablesForRepo(item.toString());
jComboBox2.setModel(modelTable);
}
}
private void jComboBox2ItemStateChanged(java.awt.event.ItemEvent evt) {
// TODO add your handling code here:
if(evt.getStateChange()== ItemEvent.SELECTED){
Object item = evt.getItem();
String RepoName = jComboBox1.getSelectedItem().toString();
List<String> columnNames = objQBM.getAllColumnsForTable(item.toString());
addCheckBox(columnNames);
}
}
private void addCheckBox(List<String> columnNames)
{
int numberCheckBox = columnNames.size();
JCheckBox[] checkBoxList = new JCheckBox[numberCheckBox];
for(int i = 0; i < numberCheckBox; i++) {
checkBoxList[i] = new JCheckBox("hi"+i);
jPanel1.add(checkBoxList[i]);
}
jPanel1.revalidate();
}
public static void main(String args[]) {
// java.awt.EventQueue.invokeLater(new Runnable() {
// public void run() {
new QueryBuilderUI().setVisible(true);
// }
// });
}
// Variables declaration - do not modify
private javax.swing.JComboBox jComboBox1;
private javax.swing.JComboBox jComboBox2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
the UI Looks like this
The checkboxes needs to be created dynamically according to what is selected on jComboBox2.
Your jPanel1 JPanel is being constrained by its layout, GroupLayout, which does not take kindly to the addition of new components without significant additions to code that are difficult to make, since GroupLayout was not built to be used for hand coding but rather for GUI-builder coding. I suggest that you build your GUI with much more coder-friendly layouts, or better still, combinations of nested JPanels each using a simpler layout, ones that are much more conducive to the addition of new components, and then your problems will be more easily solved.
You could even still use GroupLayout for the main of your GUI if you desire, but just not for jPanel1. I suggest that you consider using GridLayout or GridBagLayout to add a grid of JCheckBoxes. Or even FlowLayout. For example the following code uses the default JPanel's FlowLayout:
import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JCheckBox;
import javax.swing.border.Border;
public class QueryBuilderUI extends javax.swing.JFrame {
protected static final int PREF_W = 400;
protected static final int PREF_H = 200;
QueryBuilderMethods objQBM = new QueryBuilderMethods();
DefaultComboBoxModel<String> repoModel = new DefaultComboBoxModel<String>();
public QueryBuilderUI() {
getRepositoryListing();
initComponents();
}
private void getRepositoryListing() {
repoModel = new QueryBuilderMethods().getAllRepositoryName();
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jComboBox1 = new javax.swing.JComboBox();
jLabel2 = new javax.swing.JLabel();
jComboBox2 = new javax.swing.JComboBox();
jLabel3 = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel() {
#Override
// !! so the JPanel has some size. This is a shameless kludge *****
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
};
jTextField1 = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Choose Repository");
jComboBox1.setModel(repoModel);
jComboBox1.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
jComboBox1ItemStateChanged(evt);
}
});
jLabel2.setText("Choose Table");
// jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
// "Item 1", "Item 2", "Item 3", "Item 4" }));
jComboBox2.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
jComboBox2ItemStateChanged(evt);
}
});
jLabel3.setText("Choose Columns");
// *************** Note Changes Below ****************
// !! javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
// !! jPanel1);
// !! jPanel1.setLayout(jPanel1Layout);
// !! jPanel1 now uses JPanel's default FlowLayout
Border border = BorderFactory.createTitledBorder("Select Columns");
jPanel1.setBorder(border);
jTextField1.setText("jTextField1");
jLabel4.setText("Generated Query");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addContainerGap()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(
jLabel1)
.addComponent(
jLabel2))
.addGap(57, 57, 57)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(
jComboBox1,
javax.swing.GroupLayout.PREFERRED_SIZE,
106,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(
jComboBox2,
javax.swing.GroupLayout.PREFERRED_SIZE,
106,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(
jPanel1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(
jTextField1,
javax.swing.GroupLayout.PREFERRED_SIZE,
412,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(65, Short.MAX_VALUE)));
layout.setVerticalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jComboBox1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jComboBox2,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(jLabel3)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(44, 44, 44)
.addComponent(jLabel4)
.addGap(18, 18, 18)
.addComponent(jTextField1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(92, Short.MAX_VALUE)));
pack();
}// </editor-fold>
private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {
// TODO add your handling code here:
if (evt.getStateChange() == ItemEvent.SELECTED) {
Object item = evt.getItem();
DefaultComboBoxModel<String> modelTable = objQBM
.getAllTablesForRepo(item.toString());
jComboBox2.setModel(modelTable);
}
}
private void jComboBox2ItemStateChanged(java.awt.event.ItemEvent evt) {
// TODO add your handling code here:
if (evt.getStateChange() == ItemEvent.SELECTED) {
Object item = evt.getItem();
String RepoName = jComboBox1.getSelectedItem().toString();
List<String> columnNames = objQBM.getAllColumnsForTable(item
.toString());
addCheckBox(columnNames);
}
}
private void addCheckBox(List<String> columnNames) {
int numberCheckBox = columnNames.size();
JCheckBox[] checkBoxList = new JCheckBox[numberCheckBox];
System.out.println("numberCheckBox = " + numberCheckBox);
for (int i = 0; i < numberCheckBox; i++) {
checkBoxList[i] = new JCheckBox("hi" + i);
jPanel1.add(checkBoxList[i]);
}
jPanel1.revalidate();
jPanel1.repaint();
System.out.println("check boxes added");
}
public static void main(String args[]) {
// java.awt.EventQueue.invokeLater(new Runnable() {
// public void run() {
new QueryBuilderUI().setVisible(true);
// }
// });
}
// Variables declaration - do not modify
private javax.swing.JComboBox jComboBox1;
private javax.swing.JComboBox jComboBox2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
// mock code added so that the above code will compile and run
// because you didn't give us this code
class QueryBuilderMethods {
public DefaultComboBoxModel<String> getAllRepositoryName() {
DefaultComboBoxModel<String> cModel = new DefaultComboBoxModel<>();
for (String item : new String[] { "one", "two", "three", "four", "five" }) {
cModel.addElement(item);
}
return cModel;
}
public List<String> getAllColumnsForTable(String string) {
List<String> columns = new ArrayList<>();
for (String item : new String[] { "col 1", "col 2", "col 3", "col 4" }) {
columns.add(item);
}
return columns;
}
public DefaultComboBoxModel<String> getAllTablesForRepo(String string) {
DefaultComboBoxModel<String> cModel = new DefaultComboBoxModel<>();
for (String item : new String[] { "repo one", "repo two", "repo three",
"repo four", "repo five" }) {
cModel.addElement(item);
}
return cModel;
}
}
Hi i am trying to print in a Jdialog the values of a selected row from the main frame .
i m trying this ,but when i call the method("m.SetRecoger) in the main frame it´s showing this error :
"method SetRecoger in class Modificar cannot be applied to given types;
required: String[]
found: no arguments
reason: actual and formal argument lists differ in length"
the complete code :
the main frame where i have the jtable:
public class DniFrame extends javax.swing.JFrame {
/**
* Creates new form DniFrame
*/
public DniFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jScrollPane2 = new javax.swing.JScrollPane();
jTable2 = new javax.swing.JTable();
jButton1 = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem4 = new javax.swing.JMenuItem();
jMenuItem3 = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});
jTable2.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Nif", "Nom", "Cognoms", "Telefon"
}
));
jScrollPane2.setViewportView(jTable2);
jButton1.setText("Actualizar Datos");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 593, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 379, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jMenu1.setText("Aplicació");
jMenuItem1.setText("Sortir");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem1);
jMenuBar1.add(jMenu1);
jMenu2.setText("Base de Dades");
jMenu2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenu2ActionPerformed(evt);
}
});
jMenuItem2.setText("Afegir");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem2);
jMenuItem4.setText("Modificar");
jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem4ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem4);
jMenuItem3.setText("Eliminar");
jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem3ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem3);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
}
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
new afegir(this, true).setVisible(true);
}
private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {
modify();
Modificar m=new Modificar(this, true);
m.SetRecoger();
new Modificar(this, true).setVisible(true);
}
private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel modelo = (DefaultTableModel) this.jTable2.getModel();
int i = jTable2.getSelectedRow();
if (i == -1) {
JOptionPane.showMessageDialog(null, "Debes selecioonar una fila");
} else {
int ax = JOptionPane.showConfirmDialog(null, "Estás seguro de que quieres eliminar este registro?");
if (ax == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null, "Has seleccionado SI.");
String dni = (String) jTable2.getValueAt(i, 0);
modelo.removeRow(jTable2.getSelectedRow());
try {
Class.forName("com.mysql.jdbc.Driver");
try (Connection conexion = DriverManager.getConnection("jdbc:mysql://192.168.100.128/mibase", "asis", "titanic24")) {
Statement comando = conexion.createStatement();
comando.executeUpdate("delete from dades_pers where nif = '" + dni + "'");
}
JOptionPane.showMessageDialog(null, "Registro borrado");
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(null, "No se ha podido eliminar el registro");
} catch (SQLException ex) {
Logger.getLogger(DniFrame.class.getName()).log(Level.SEVERE, null, ex);
}
} else if (ax == JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog(null, "Has seleccionado NO.");
}
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conexion = DriverManager.getConnection("jdbc:mysql://192.168.100.128/mibase", "asis", "titanic24");
Statement comando = conexion.createStatement();
ResultSet result = comando.executeQuery("select * from dades_pers");
ResultSetMetaData rsMD = result.getMetaData();
int numcolumnas = rsMD.getColumnCount();
DefaultTableModel modelo = new DefaultTableModel();
this.jTable2.setModel(modelo);
for (int x = 1; x <= numcolumnas; x++) {
modelo.addColumn(rsMD.getColumnLabel(x));
}
while (result.next()) {
Object[] fila = new Object[numcolumnas];
for (int i = 0; i < numcolumnas; i++) {
fila[i] = result.getObject(i + 1);
}
modelo.addRow(fila);
}
} catch (ClassNotFoundException e) {
} catch (SQLException sqe) {
}
// TODO add your handling code here:
}
private void jMenu2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void formWindowOpened(java.awt.event.WindowEvent evt) {
añadirTabla();
}
public void añadirTabla() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conexion = DriverManager.getConnection("jdbc:mysql://192.168.100.128/mibase", "asis", "titanic24");
Statement comando = conexion.createStatement();
ResultSet result = comando.executeQuery("select * from dades_pers");
ResultSetMetaData rsMD = result.getMetaData();
int numcolumnas = rsMD.getColumnCount();
DefaultTableModel modelo = new DefaultTableModel();
this.jTable2.setModel(modelo);
for (int x = 1; x <= numcolumnas; x++) {
modelo.addColumn(rsMD.getColumnLabel(x));
}
while (result.next()) {
Object[] fila = new Object[numcolumnas];
for (int i = 0; i < numcolumnas; i++) {
fila[i] = result.getObject(i + 1);
}
modelo.addRow(fila);
}
} catch (ClassNotFoundException e) {
} catch (SQLException sqe) {
}
}
public String [] modify() {
String[] registros = new String[4];
int i = jTable2.getSelectedRow();
if (i == -1) {
JOptionPane.showMessageDialog(null, "Debes selecioonar una fila");
new Modificar(this, true).setVisible(false);
} else {
registros[0] = (String) jTable2.getValueAt(i, 0).toString();
registros[1] = (String) jTable2.getValueAt(i, 1).toString();
registros[2] = (String) jTable2.getValueAt(i, 2).toString();
registros[3] = (String) jTable2.getValueAt(i, 3).toString();
}
return registros;
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(DniFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(DniFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(DniFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(DniFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DniFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTable jTable2;
// End of variables declaration
}
//the jdialog (the second frame);
public class Modificar extends javax.swing.JDialog {
private DniFrame p;
/**
* Creates new form Modificar
*/
public Modificar(java.awt.Frame parent, boolean modal) {
super(parent, modal);
this.p= (DniFrame)parent;
initComponents();
}
public void SetRecoger(String[] v) {
String nif = v[0];
String nom = v[1];
String cognom =v[2];
String tel = v[3];
String []datos ={nif,nom,cognom,tel};
jTextField5.setText(nif);
jTextField6.setText(nom);
jTextField7.setText(cognom);
jTextField8.setText(tel);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel2 = new javax.swing.JPanel();
jLabel5 = new javax.swing.JLabel();
jTextField5 = new javax.swing.JTextField();
jTextField6 = new javax.swing.JTextField();
jTextField7 = new javax.swing.JTextField();
jTextField8 = new javax.swing.JTextField();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jLabel5.setText("Nom");
jTextField5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField5ActionPerformed(evt);
}
});
jTextField6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField6ActionPerformed(evt);
}
});
jTextField7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField7ActionPerformed(evt);
}
});
jTextField8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField8ActionPerformed(evt);
}
});
jLabel6.setText("NIF");
jLabel7.setText("Cognoms");
jLabel8.setText("Telefon");
jButton2.setText("Desar les dades");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("Tancar");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Estat"));
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 68, Short.MAX_VALUE)
.addContainerGap())
);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(31, 31, 31)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(78, 78, 78)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addContainerGap(23, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(17, 17, 17)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(15, 15, 15)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 10, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jTextField5ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jTextField6ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jTextField7ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jTextField8ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(afegir.class.getName()).log(Level.SEVERE, null, ex);
}
try {
Connection conexion = DriverManager.getConnection("jdbc:mysql://192.168.100.128/mibase", "asis", "titanic24");
Statement comando = conexion.createStatement();
String nif = jTextField5.getText();
String nom = jTextField6.getText();
String cognom = jTextField7.getText();
String tel = jTextField8.getText();
comando.executeUpdate("UPDATE dades_pers SET nom= '" + nom + "',cognoms='" + cognom + "',telefon= '" + tel + "' where nif= '" + nif + "'");
conexion.close();
jLabel1.setText("Datos Modificados correctamente");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error al modificar los datos" + e);
}
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
}
//
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Modificar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Modificar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Modificar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Modificar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the dialog */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Modificar dialog = new Modificar(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
#Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify
Thanks!!
You are calling the method SetRecoger in your DniFrame without passing any arguments to the method.
The method as you have defined it in your Modificar dialog requires a String array as an argument.
public void SetRecoger(String[] v) {....}
Since your modify() method returns a String array, Try this:
String[] mod = modify();
Modificar m = new Modificar(this, true);
m.SetRecoger(mod);
new Modificar(this, true).setVisible(true);
Problem 1: So basically I am reading from a text file(name, mark1, mark2) for 8 students, I store this in an ArrayList along with a String that is the path for a photo of the user - I then put all the student names in a ComboBox, and when the user selects a name, the two relevant marks and photo then need to be displayed in labels and a panel respectively. Problem is, I have no idea how to retrieve the information when the user selects a name from the ComboBox. My intial thought was to get the selected item and use that, but no idea where to from there...!
Problem 2: When I try to write new information to the text file I get a Nullpointer Excpetion!
//class for reading and writing to the text file
public class FileConnect {
ArrayList<Student> studs = new ArrayList<Student>();
public FileConnect() {
String s;
String a;
try {
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Student\\Desktop\\Marks.txt"));
while ((s = br.readLine()) != null) {
a = br.readLine();
int i = Integer.parseInt(a);
a = br.readLine();
int k = Integer.parseInt(a);
studs.add(new Student(s, "C:\\Users\\Student\\Desktop\\res\\" + s + ".png", i, k));
}
} catch (FileNotFoundException fnf) {
System.out.println("FileNotFoundException: " + fnf);
} catch (IOException ex) {
System.out.println("IOException: " + ex);
}
}
public void addStud(String name, String compLit, String linux) {
try {
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("C:\\Users\\Student\\Desktop\\Marks.txt")), true);
out.write(name);
out.write(compLit);
out.write(linux);
} catch (IOException ex) {
System.out.println("IOException: " + ex);
}
}
}
//class for student accessor methods
public class Student {
private String name;
private int compLit;
private int linux;
private String photo;
public Student(String name, String photo, int compLit, int linux) {
this.name = name;
this.photo = photo;
this.compLit = compLit;
this.linux = linux;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getCompLit() {
return this.compLit;
}
public void setCompLit(int compLit) {
this.compLit = compLit;
}
public int getLinux() {
return this.linux;
}
public void setLinux(int linux) {
this.linux = linux;
}
public String getPhoto() {
return this.photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
}
//main GUI where it all happens
public class MainGUI extends javax.swing.JFrame {
String name;
int compLit;
int linux;
String photo;
private FileConnect file;
private Student stu;
public MainGUI() {
initComponents();
}
private void menuItemExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void menuItemAboutActionPerformed(java.awt.event.ActionEvent evt) {
Date now = new Date();
JOptionPane.showMessageDialog(this, "Author: Jean van der Walt \n " + now, "About" , JOptionPane.INFORMATION_MESSAGE);
}
**private void cbStudActionPerformed(java.awt.event.ActionEvent evt) {
//user selects item in combo box
//get all the info for the selected item
//display them
//I tried to do something with
//cbStud.getSelected..... but don't think that'll work
}**
**private void btnStudAddActionPerformed(java.awt.event.ActionEvent evt) {
file.addStud(tfStudName.getText(), tfStudCompLit.getText(), tfStudLinux.getText());
//get a Nullpointer exception HERE!!!
}**
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainGUI().setVisible(true);
}
});
}
-----> The text file I'm reading from looks like this:
Anthony Braddock \n
97 \n
87
...... (7 more students)
---------------------------------->
//Entire GUI code
import java.util.Date;
import javax.swing.JOptionPane;
public class MainGUI extends javax.swing.JFrame {
String name;
int compLit;
int linux;
String photo;
private FileConnect file = null;
private Student stu = null;
public MainGUI() {
initComponents();
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
lblHeading = new javax.swing.JLabel();
pnlSelectStud = new javax.swing.JPanel();
cbStud = new javax.swing.JComboBox();
pnlMarks = new javax.swing.JPanel();
lblCompLitMark = new javax.swing.JLabel();
lblLinuxMark = new javax.swing.JLabel();
lblCompLitDisplay = new javax.swing.JLabel();
lblLinuxDisplay = new javax.swing.JLabel();
pnlPhoto = new javax.swing.JPanel();
lblPic = new javax.swing.JLabel();
pnlAddStud = new javax.swing.JPanel();
lblStudName = new javax.swing.JLabel();
lblStudCompLit = new javax.swing.JLabel();
lblStudLinux = new javax.swing.JLabel();
tfStudName = new javax.swing.JTextField();
tfStudCompLit = new javax.swing.JTextField();
tfStudLinux = new javax.swing.JTextField();
btnStudAdd = new javax.swing.JButton();
menuBar = new javax.swing.JMenuBar();
menuFile = new javax.swing.JMenu();
menuItemExit = new javax.swing.JMenuItem();
menuEdit = new javax.swing.JMenu();
menuItemAbout = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
lblHeading.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
lblHeading.setText("Individual Student Marks");
pnlSelectStud.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Select Student", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14))); // NOI18N
cbStud.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Anthony Braddock", "Charl Fourie", "Darren Tiplady", "Jacobus Langenhoven", "Jaun Pierre Reyneke", "Johan van der Merwe", "Johannes Van Eck", "Lodewyk Duminy" }));
cbStud.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cbStudActionPerformed(evt);
}
});
javax.swing.GroupLayout pnlSelectStudLayout = new javax.swing.GroupLayout(pnlSelectStud);
pnlSelectStud.setLayout(pnlSelectStudLayout);
pnlSelectStudLayout.setHorizontalGroup(
pnlSelectStudLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(pnlSelectStudLayout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(cbStud, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(20, Short.MAX_VALUE))
);
pnlSelectStudLayout.setVerticalGroup(
pnlSelectStudLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(pnlSelectStudLayout.createSequentialGroup()
.addGap(61, 61, 61)
.addComponent(cbStud, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(66, Short.MAX_VALUE))
);
pnlMarks.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Marks", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14))); // NOI18N
lblCompLitMark.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
lblCompLitMark.setText("Computer Literacy");
lblLinuxMark.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
lblLinuxMark.setText("Using Linux");
lblCompLitDisplay.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
lblCompLitDisplay.setText("UNKNOWN");
lblLinuxDisplay.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
lblLinuxDisplay.setText("UNKNOWN");
javax.swing.GroupLayout pnlMarksLayout = new javax.swing.GroupLayout(pnlMarks);
pnlMarks.setLayout(pnlMarksLayout);
pnlMarksLayout.setHorizontalGroup(
pnlMarksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(pnlMarksLayout.createSequentialGroup()
.addContainerGap()
.addGroup(pnlMarksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblCompLitMark)
.addComponent(lblLinuxMark))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 164, Short.MAX_VALUE)
.addGroup(pnlMarksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblCompLitDisplay)
.addComponent(lblLinuxDisplay))
.addGap(18, 18, 18))
);
pnlMarksLayout.setVerticalGroup(
pnlMarksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(pnlMarksLayout.createSequentialGroup()
.addGap(24, 24, 24)
.addGroup(pnlMarksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblCompLitMark)
.addComponent(lblCompLitDisplay))
.addGap(37, 37, 37)
.addGroup(pnlMarksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblLinuxMark)
.addComponent(lblLinuxDisplay))
.addContainerGap(64, Short.MAX_VALUE))
);
pnlPhoto.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Photo", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14))); // NOI18N
javax.swing.GroupLayout pnlPhotoLayout = new javax.swing.GroupLayout(pnlPhoto);
pnlPhoto.setLayout(pnlPhotoLayout);
pnlPhotoLayout.setHorizontalGroup(
pnlPhotoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(pnlPhotoLayout.createSequentialGroup()
.addGap(55, 55, 55)
.addComponent(lblPic, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(54, Short.MAX_VALUE))
);
pnlPhotoLayout.setVerticalGroup(
pnlPhotoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(pnlPhotoLayout.createSequentialGroup()
.addGap(33, 33, 33)
.addComponent(lblPic, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(33, Short.MAX_VALUE))
);
pnlAddStud.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Add your name and marks", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14))); // NOI18N
lblStudName.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
lblStudName.setText("Name");
lblStudCompLit.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
lblStudCompLit.setText("Computer Literacy");
lblStudLinux.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
lblStudLinux.setText("Using Linux");
tfStudName.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
tfStudCompLit.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
tfStudLinux.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
btnStudAdd.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
btnStudAdd.setText("Add");
btnStudAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnStudAddActionPerformed(evt);
}
});
javax.swing.GroupLayout pnlAddStudLayout = new javax.swing.GroupLayout(pnlAddStud);
pnlAddStud.setLayout(pnlAddStudLayout);
pnlAddStudLayout.setHorizontalGroup(
pnlAddStudLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(pnlAddStudLayout.createSequentialGroup()
.addContainerGap()
.addGroup(pnlAddStudLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblStudName)
.addComponent(lblStudCompLit)
.addComponent(lblStudLinux))
.addGap(14, 14, 14)
.addGroup(pnlAddStudLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, pnlAddStudLayout.createSequentialGroup()
.addGap(14, 14, 14)
.addComponent(tfStudName, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(pnlAddStudLayout.createSequentialGroup()
.addGap(166, 166, 166)
.addGroup(pnlAddStudLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(tfStudLinux, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(tfStudCompLit, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)))))
.addComponent(btnStudAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 398, javax.swing.GroupLayout.PREFERRED_SIZE)
);
pnlAddStudLayout.setVerticalGroup(
pnlAddStudLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(pnlAddStudLayout.createSequentialGroup()
.addContainerGap()
.addGroup(pnlAddStudLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblStudName)
.addComponent(tfStudName, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(pnlAddStudLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblStudCompLit)
.addComponent(tfStudCompLit, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(9, 9, 9)
.addGroup(pnlAddStudLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblStudLinux)
.addComponent(tfStudLinux, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(9, 9, 9)
.addComponent(btnStudAdd, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE))
);
menuFile.setText("File");
menuItemExit.setText("Exit");
menuItemExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menuItemExitActionPerformed(evt);
}
});
menuFile.add(menuItemExit);
menuBar.add(menuFile);
menuEdit.setText("Edit");
menuItemAbout.setText("About");
menuItemAbout.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menuItemAboutActionPerformed(evt);
}
});
menuEdit.add(menuItemAbout);
menuBar.add(menuEdit);
setJMenuBar(menuBar);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(24, 24, 24)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(pnlPhoto, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(pnlSelectStud, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(pnlMarks, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(pnlAddStud, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(26, 26, 26))
.addGroup(layout.createSequentialGroup()
.addGap(216, 216, 216)
.addComponent(lblHeading)
.addContainerGap(250, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(lblHeading, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(34, 34, 34)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(pnlMarks, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(pnlSelectStud, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(pnlAddStud, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(pnlPhoto, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(19, 19, 19))
);
pack();
}// </editor-fold>
private void menuItemExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void menuItemAboutActionPerformed(java.awt.event.ActionEvent evt) {
Date now = new Date();
JOptionPane.showMessageDialog(this, "Author: Jean van der Walt \n " + now, "About" , JOptionPane.INFORMATION_MESSAGE);
}
private void cbStudActionPerformed(java.awt.event.ActionEvent evt) {
//user selects item in combo box
//get all the info for the selected item
//display them
}
private void btnStudAddActionPerformed(java.awt.event.ActionEvent evt) {
file.addStud(tfStudName.getText(), tfStudCompLit.getText(), tfStudLinux.getText());
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btnStudAdd;
private javax.swing.JComboBox cbStud;
private javax.swing.JLabel lblCompLitDisplay;
private javax.swing.JLabel lblCompLitMark;
private javax.swing.JLabel lblHeading;
private javax.swing.JLabel lblLinuxDisplay;
private javax.swing.JLabel lblLinuxMark;
private javax.swing.JLabel lblPic;
private javax.swing.JLabel lblStudCompLit;
private javax.swing.JLabel lblStudLinux;
private javax.swing.JLabel lblStudName;
private javax.swing.JMenuBar menuBar;
private javax.swing.JMenu menuEdit;
private javax.swing.JMenu menuFile;
private javax.swing.JMenuItem menuItemAbout;
private javax.swing.JMenuItem menuItemExit;
private javax.swing.JPanel pnlAddStud;
private javax.swing.JPanel pnlMarks;
private javax.swing.JPanel pnlPhoto;
private javax.swing.JPanel pnlSelectStud;
private javax.swing.JTextField tfStudCompLit;
private javax.swing.JTextField tfStudLinux;
private javax.swing.JTextField tfStudName;
// End of variables declaration//GEN-END:variables
}
Problem 1. it's kind of unclear what you have since you seem to be missing some of the gui code, but generally, i would store a Map<String, Student> which maps each "Student name" to their "Student instance" and use that to find the Student when chosen by the combo box (assuming you are showing the student names and that the names are unique).
Problem 2. You don't ever seem to be initializing file.
I know I am missing something simple, I think in getting the combobox to access the array.
package my.freelancebillingapp;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import javax.swing.*;
public class billingInfoUI extends javax.swing.JFrame {
public billingInfoUI() {
initComponents();
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
hoursWorked = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
flatRate = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
workType = new javax.swing.JList();
jScrollPane2 = new javax.swing.JScrollPane();
jList2 = new javax.swing.JList();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jComboBox1 = new javax.swing.JComboBox();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Billing Information", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.ABOVE_BOTTOM, new java.awt.Font("Bleeding Cowboys", 0, 48))); // NOI18N
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 457, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
hoursWorked.setHorizontalAlignment(javax.swing.JTextField.CENTER);
hoursWorked.setText("Hours Worked");
hoursWorked.setBorder(null);
jLabel1.setText("Or");
flatRate.setHorizontalAlignment(javax.swing.JTextField.CENTER);
flatRate.setText("Flat Rate");
flatRate.setBorder(null);
workType.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Work Type", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP));
workType.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Web Design", "Graphic Design", "Consulting" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
workType.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
jScrollPane1.setViewportView(workType);
jList2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Any Extras?", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP));
jList2.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Blog", "Forum", "Templating", "Rush Delivery" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jScrollPane2.setViewportView(jList2);
jButton1.setText("Save");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});
jButton2.setText("Invoice");
jButton3.setText("Close");
jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton3MouseClicked(evt);
}
});
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jComboBox1.setModel(customers);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(89, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(hoursWorked, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(48, 48, 48)
.addComponent(jLabel1)
.addGap(1, 1, 1))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(31, 31, 31)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(49, 49, 49)
.addComponent(flatRate, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(79, 79, 79))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(62, 62, 62)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 103, Short.MAX_VALUE)
.addComponent(jButton2)
.addGap(79, 79, 79)
.addComponent(jButton3)
.addGap(52, 52, 52))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(134, Short.MAX_VALUE)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(107, 107, 107))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(30, 30, 30)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(hoursWorked, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(flatRate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(20, 20, 20)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(51, 51, 51)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2)
.addComponent(jButton3)
.addComponent(jButton1))
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {
new FreelanceBillingUI().setVisible(true);
setVisible(false);
}
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
String hoursWorkedtxt = hoursWorked.getText();
String flatRatetxt = flatRate.getText();
workType.getSelectedValue();
workType.toString();
Object workTypetxt = workType.getSelectedValue();
if(hoursWorkedtxt.contains("H")){
hoursWorkedtxt = "0";
}else{
flatRatetxt = "0";
}
}
public class CustomerData {
private static final String JDBC_CONNECTION_URL = "jdbc:mysql://localhost/test";
private static final String DATABASE_USER = "root";
private static final String DATABASE_PASSWORD = "root";
private static final String SQL_FETCH_CUSTOMERS = "SELECT custName FROM customers";
private Connection connection = null;
public CustomerData(){
initConnection();
}
private void initConnection() {
try {
//load the mysql driver
Class.forName("com.mysql.jdbc.Driver");
//create the database connection
connection = DriverManager.getConnection(JDBC_CONNECTION_URL, DATABASE_USER, DATABASE_PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void closeConnection(){
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
connection = null;
}
}
}
public ArrayList fetchCustomerData(){
if (connection != null){
Statement statement = null;
try {
statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(SQL_FETCH_CUSTOMERS);
return convertResultSetToCustomersArray(resultSet);
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (statement != null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}else{
System.out.println("NO VALID DATABASE CONNECTION, CAN'T FETCH CUSTOMER DATA!");
}
return new ArrayList();
}
private ArrayList convertResultSetToCustomersArray(ResultSet results) throws SQLException{
ArrayList customers = new ArrayList();
while (results.next()){
customers.add(results.getString("custName"));
}
return customers;
}
}
private void initData(){
CustomerData customerData = new CustomerData();
ArrayList custArrayList = customerData.fetchCustomerData();
customers = (String[]) custArrayList.toArray(new String[0]);
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new billingInfoUI().setVisible(true);
}
});
}
private String[] customers = null;
private javax.swing.JTextField flatRate;
private javax.swing.JTextField hoursWorked;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JList jList2;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JList workType;
}
There is too much code there to look at 90% of it is NOT related to adding data in a combo box. You have a dozen other components which clutter the code so we can't see what actually happening with the comobo box.
It you really need help then post a SSCCE.
That is, create a simple frame and load hard coded data into the combo box.
Then create a simple program to read data from the database and use System.out.println(...) to display each value.
Once you've mastered the two basic steps, then put the two programs together to load a combobox dynamically from a database.
Finally add the rest of you components to the frame.
Break you problem down into small, manageable steps and you will be able to debug it easier.
A few points:
Wading through your code, I don't see a call to initData being made anywhere. Without that call, you wont call your database code. Without the database call, you wont get the data.
JComboBox does not have a setModel method that takes an array. You can wrap your array in a DefaultComboBoxModel and set that on the model.
It is good to keep in mind that blocking calls like database access should not be made in the AWT painting thread, and neither should calls that update the GUI be made outside of the AWT thread. You should look into creating your GUI and grabbing your data in separate threads, and making use of SwingUtlities.invokeLater or similar tools to update your GUI.