I dont know whts happening.THe code inside if block is running even though the condition is false.I have designed my interface in netbeans ide 7.4 and just copy pasted the design in eclipse.Here is my code
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class AddSymbol extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
Connection c;
public AddSymbol() {
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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
String x = "hello";
if(x.equals("hii"))
{
System.out.println("Inside hi");
}
jinstrument = new javax.swing.JComboBox();
jsymbol = new javax.swing.JComboBox();
jexpiry = new javax.swing.JComboBox();
joption = new javax.swing.JComboBox();
jstrikeprice = new javax.swing.JComboBox();
instrument = new javax.swing.JLabel();
symbol = new javax.swing.JLabel();
expiry = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
instrumentLabel = new javax.swing.JLabel();
symbolLabel = new javax.swing.JLabel();
expiryLabel = new javax.swing.JLabel();
optionLabel = new javax.swing.JLabel();
strikeLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
jinstrument.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Equity", "OPTIDX", "OPTSTK", "FUTIDX", "FUTSTK", " " }));
getContentPane().add(jinstrument);
jinstrument.setBounds(10, 38, 83, 20);
jsymbol.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
getContentPane().add(jsymbol);
jsymbol.setBounds(120, 38, 210, 20);
jexpiry.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
getContentPane().add(jexpiry);
jexpiry.setBounds(380, 38, 180, 20);
joption.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "PE", "CE" }));
getContentPane().add(joption);
joption.setBounds(10, 85, 66, 20);
jstrikeprice.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
getContentPane().add(jstrikeprice);
jstrikeprice.setBounds(120, 85, 56, 20);
instrument.setText("Instrument");
getContentPane().add(instrument);
instrument.setBounds(10, 13, 83, 14);
symbol.setText("Symbol");
getContentPane().add(symbol);
symbol.setBounds(120, 13, 218, 14);
expiry.setText("Expiry");
getContentPane().add(expiry);
expiry.setBounds(380, 13, 180, 14);
jLabel4.setText("Option Type");
getContentPane().add(jLabel4);
jLabel4.setBounds(10, 69, 83, 14);
jLabel5.setText("Strike Price");
getContentPane().add(jLabel5);
jLabel5.setBounds(120, 69, 56, 14);
jPanel2.setLayout(null);
jinstrument.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent arg0) {
// TODO Auto-generated method stub
jsymbol.removeAllItems();
int enterloop=0;
if(jinstrument.getSelectedItem().equals("FUTSTK"))
{
enterloop =1;
}
if(enterloop == 1);
{ System.out.println("inside if");
}
}
});
getContentPane().add(jPanel2);
jPanel2.setBounds(10, 135, 577, 338);
}// </editor-fold>
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new AddSymbol().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel expiry;
private javax.swing.JLabel expiryLabel;
private javax.swing.JLabel instrument;
private javax.swing.JLabel instrumentLabel;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel2;
private javax.swing.JComboBox jexpiry;
private javax.swing.JComboBox jinstrument;
private javax.swing.JComboBox joption;
private javax.swing.JComboBox jstrikeprice;
private javax.swing.JComboBox jsymbol;
private javax.swing.JLabel optionLabel;
private javax.swing.JLabel strikeLabel;
private javax.swing.JLabel symbol;
private javax.swing.JLabel symbolLabel;
// End of variables declaration
}
whatever i select inside the jinstrument jcombobox i get "inside if " as the output.I have checked through print statements but enterloop is having value 0 and it still enters the if statement block.Please help me with this problem
there is a ; in this line if(enterloop == 1);
; means end of statement.So even the if statement returns false so the next line will be executed
Remove the semicolon [;] in line if(enterloop == 1);
try these updated codes::
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class AddSymbol extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
Connection c;
public AddSymbol() {
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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
String x = "hello";
if(x.equals("hii"))
{
System.out.println("Inside hi");
}
jinstrument = new javax.swing.JComboBox();
jsymbol = new javax.swing.JComboBox();
jexpiry = new javax.swing.JComboBox();
joption = new javax.swing.JComboBox();
jstrikeprice = new javax.swing.JComboBox();
instrument = new javax.swing.JLabel();
symbol = new javax.swing.JLabel();
expiry = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
instrumentLabel = new javax.swing.JLabel();
symbolLabel = new javax.swing.JLabel();
expiryLabel = new javax.swing.JLabel();
optionLabel = new javax.swing.JLabel();
strikeLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
jinstrument.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Equity", "OPTIDX", "OPTSTK", "FUTIDX", "FUTSTK", " " }));
getContentPane().add(jinstrument);
jinstrument.setBounds(10, 38, 83, 20);
jsymbol.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
getContentPane().add(jsymbol);
jsymbol.setBounds(120, 38, 210, 20);
jexpiry.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
getContentPane().add(jexpiry);
jexpiry.setBounds(380, 38, 180, 20);
joption.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "PE", "CE" }));
getContentPane().add(joption);
joption.setBounds(10, 85, 66, 20);
jstrikeprice.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
getContentPane().add(jstrikeprice);
jstrikeprice.setBounds(120, 85, 56, 20);
instrument.setText("Instrument");
getContentPane().add(instrument);
instrument.setBounds(10, 13, 83, 14);
symbol.setText("Symbol");
getContentPane().add(symbol);
symbol.setBounds(120, 13, 218, 14);
expiry.setText("Expiry");
getContentPane().add(expiry);
expiry.setBounds(380, 13, 180, 14);
jLabel4.setText("Option Type");
getContentPane().add(jLabel4);
jLabel4.setBounds(10, 69, 83, 14);
jLabel5.setText("Strike Price");
getContentPane().add(jLabel5);
jLabel5.setBounds(120, 69, 56, 14);
jPanel2.setLayout(null);
jinstrument.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent arg0) {
// TODO Auto-generated method stub
jsymbol.removeAllItems();
int enterloop=0;
if(jinstrument.getSelectedItem().equals("FUTSTK"))
{
enterloop =1;
}
if(enterloop == 1);
{ System.out.println("inside if")
}
}
});
instrumentLabel.setText("Instrument");
jPanel2.add(instrumentLabel);
instrumentLabel.setBounds(21, 11, 102, 14);
symbolLabel.setText("Symbol");
jPanel2.add(symbolLabel);
symbolLabel.setBounds(129, 11, 126, 14);
expiryLabel.setText("Expiry");
jPanel2.add(expiryLabel);
expiryLabel.setBounds(261, 11, 64, 14);
optionLabel.setText("OptionType");
jPanel2.add(optionLabel);
optionLabel.setBounds(377, 11, 56, 14);
strikeLabel.setText("Strike Price");
jPanel2.add(strikeLabel);
strikeLabel.setBounds(462, 11, 53, 14);
getContentPane().add(jPanel2);
jPanel2.setBounds(10, 135, 577, 338);
}// </editor-fold>
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new AddSymbol().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel expiry;
private javax.swing.JLabel expiryLabel;
private javax.swing.JLabel instrument;
private javax.swing.JLabel instrumentLabel;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel2;
private javax.swing.JComboBox jexpiry;
private javax.swing.JComboBox jinstrument;
private javax.swing.JComboBox joption;
private javax.swing.JComboBox jstrikeprice;
private javax.swing.JComboBox jsymbol;
private javax.swing.JLabel optionLabel;
private javax.swing.JLabel strikeLabel;
private javax.swing.JLabel symbol;
private javax.swing.JLabel symbolLabel;
// End of variables declaration
}
your code is giving error because you have a ";" as
if(enterloop == 1);
{ System.out.println("inside if")
}
there should not be a termination after the if condition.
If there is a semicolon(termination) after your condition then whatever codes you write inside{ //the curly braces} will be never executed.
Your code has a fatal defect, namely one
if(enterloop == 1) // ; bad semi-colon. Which, made this a single line if.
{ // And, as a result this was "just" an anonymous code block.
System.out.println("inside if");
}
Related
if i am developing hotel reservation project in netbeans...
if i am having list of rooms in jtable...
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
`enter code here`import javax.swing.table.*;
import net.proteanit.sql.DbUtils;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author ssn
*/
public class welcome extends javax.swing.JFrame {enter code here
/**
* Creates new form welcome
*/
public welcome() {
initComponents();
//fetch();
}
Reservation reservation = new Reservation();
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/loginform?";
String UN = "root";
String PWD = "root";
public void fetch() {
String sql = "select RoomNO,RoomType,BedType,RentPerDay from room";
try {
conn = DriverManager.getConnection("url", "UN", "PWD");
pst = conn.prepareCall(sql);
rs = pst.executeQuery();
jTable1.setModel(DbUtils.resultSetToTableModel(rs));
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
/**
* 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();
jPanel3 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
From = new com.toedter.calendar.JDateChooser();
jLabel3 = new javax.swing.JLabel();
To = new com.toedter.calendar.JDateChooser();
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
BookDays = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
jLabel1.setText("FROM");
jPanel3.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 20, 70, 30));
From.setDateFormatString("yyyy-MM-dd");
jPanel3.add(From, new org.netbeans.lib.awtextra.AbsoluteConstraints(171, 20, 150, 30));
jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
jLabel3.setText("TO");
jPanel3.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 20, 50, 30));
To.setDateFormatString("yyyy-MM-dd");
jPanel3.add(To, new org.netbeans.lib.awtextra.AbsoluteConstraints(401, 20, 140, 30));
jButton1.setText("Bookingdays");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jPanel3.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(560, 20, -1, 30));
jTable1.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 [] {
"RoomNo", "RoomType", "BedType", "RentPerDay"
}
));
jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jTable1MouseClicked(evt);
}
});
jScrollPane1.setViewportView(jTable1);
jPanel3.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 97, -1, 370));
jButton2.setText("Check");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jPanel3.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(590, 260, 70, 30));
jButton3.setText("LogOut");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jPanel3.add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(590, 313, -1, 30));
jPanel3.add(BookDays, new org.netbeans.lib.awtextra.AbsoluteConstraints(690, 20, 70, 30));
jLabel2.setIcon(new javax.swing.ImageIcon("C:\\Users\\ssn\\Desktop\\New folder\\diwan3.jpg")); // NOI18N
jPanel3.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 820, 470));
jPanel1.add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 820, 470));
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.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
UserLogin UL = new UserLogin();
UL.setVisible(true);
dispose();// TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
fetch(); // TODO add your handling code here:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(format.format(From.getDate()));
d2 = format.parse(format.format(To.getDate()));
} catch (ParseException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
long diff = d2.getTime() - d1.getTime();
long hours = diff / (60 * 60 * 1000);
long days = hours / 24;
BookDays.setText(Long.toString(days)+" days");
// TODO add your handling code here:
}
private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {
int index = jTable1.getSelectedRow();
TableModel model = jTable1.getModel();
String RoomNo = model.getValueAt(index,0).toString();
String RoomType = model.getValueAt(index, 1).toString();
String BedType = model.getValueAt(index, 2).toString();
String RentPerDay =model.getValueAt(index,3).toString();
reservation.setVisible(true);
reservation.pack();
reservation.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
reservation.setLocationRelativeTo(null);
reservation.RoomNo.setText(RoomNo);
reservation.RoomType.setText(RoomType);
reservation.BedType.setText(BedType);
reservation.RentPerDay.setText(RentPerDay);
// TODO add your handling code here:
// TODO add your handling code here:
}
/**
* #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(welcome.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(welcome.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(welcome.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(welcome.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 welcome().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField BookDays;
private com.toedter.calendar.JDateChooser From;
private com.toedter.calendar.JDateChooser To;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}
if i clicked one row it goes to reservation ...if i booked that room for particular days then it again it does not show that room in jtable of welcome page.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the e*/
import javax.swing.*;
/**
*
* #author ssn
*/
public class Reservation extends javax.swing.JFrame {
/**
* Creates new form Reservation
*/
public Reservation() {
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() {
jPanel2 = new javax.swing.JPanel();
jPanel1 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
RentPerDay = new javax.swing.JTextField();
BedType = new javax.swing.JTextField();
RoomNo = new javax.swing.JTextField();
RoomType = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
jLabel10 = new javax.swing.JLabel();
jLabel11 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jLabel12 = new javax.swing.JLabel();
jLabel13 = new javax.swing.JLabel();
jLabel14 = new javax.swing.JLabel();
jLabel15 = new javax.swing.JLabel();
jLabel16 = new javax.swing.JLabel();
jLabel17 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jTextField7 = new javax.swing.JTextField();
jTextField8 = new javax.swing.JTextField();
jTextField9 = new javax.swing.JTextField();
NoOfdays = new javax.swing.JTextField();
jTextField11 = new javax.swing.JTextField();
jComboBox2 = new javax.swing.JComboBox<>();
jComboBox3 = new javax.swing.JComboBox<>();
jTextField4 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jLabel2.setText("RESERVATION OF ROOM");
jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 800, 20));
jLabel3.setText("RoomType");
jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 50, 60, 20));
jLabel4.setText("RoomNo");
jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 50, 50, 20));
jPanel1.add(RentPerDay, new org.netbeans.lib.awtextra.AbsoluteConstraints(610, 40, 90, 30));
jPanel1.add(BedType, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 40, 100, 30));
jPanel1.add(RoomNo, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 40, 80, 30));
jPanel1.add(RoomType, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 40, 100, 30));
jLabel5.setText("RentPerDay");
jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(540, 50, -1, 20));
jLabel6.setText("BedType");
jPanel1.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 50, -1, 20));
jLabel7.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
jLabel7.setText("CustomerName");
jPanel1.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 110, 100, 20));
jLabel8.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
jLabel8.setText("CustomerID");
jPanel1.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 160, 90, 20));
jLabel9.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
jLabel9.setText("IDProof");
jPanel1.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 100, 80, 30));
jLabel10.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
jLabel10.setText("Proof_No");
jPanel1.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 160, 70, 20));
jLabel11.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
jLabel11.setText("Address");
jPanel1.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 200, 50, 30));
jButton1.setText("Logout");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 20, -1, -1));
jLabel12.setFont(new java.awt.Font("Times New Roman", 1, 11)); // NOI18N
jLabel12.setText("City");
jPanel1.add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 250, 50, 30));
jLabel13.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
jLabel13.setText("State");
jPanel1.add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 310, 50, 20));
jLabel14.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
jLabel14.setText("NoOfPersons");
jPanel1.add(jLabel14, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 210, 90, 20));
jLabel15.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
jLabel15.setText("Purpose");
jPanel1.add(jLabel15, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 340, 80, 20));
jLabel16.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
jLabel16.setText("MartialStatus");
jPanel1.add(jLabel16, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 360, 110, 20));
jLabel17.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
jLabel17.setText("NoOfDays");
jPanel1.add(jLabel17, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 280, 80, 20));
jButton2.setText("Book");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jPanel1.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 383, 70, 30));
jPanel1.add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 100, 130, 30));
jTextField2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField2ActionPerformed(evt);
}
});
jPanel1.add(jTextField2, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 150, 130, 30));
jPanel1.add(jTextField3, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 200, 130, 30));
jPanel1.add(jTextField7, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 90, 120, 30));
jPanel1.add(jTextField8, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 150, 120, 30));
jTextField9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField9ActionPerformed(evt);
}
});
jPanel1.add(jTextField9, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 200, 120, 30));
NoOfdays.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
NoOfdaysActionPerformed(evt);
}
});
jPanel1.add(NoOfdays, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 270, 120, 30));
jPanel1.add(jTextField11, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 330, 120, 30));
jComboBox2.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Select", "Married", "Single", " " }));
jPanel1.add(jComboBox2, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 350, 130, 30));
jComboBox3.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Select", "Andhra Pradesh ", "Arunachal Pradesh ", "Assam ", "Bihar", "Chhattisgarh ", "Goa ", "Gujarat ", "Haryana ", "Himachal Pradesh ", "Jammu & Kashmir ", "Jharkhand ", "Karnataka ", "Kerala ", "Madhya Pradesh ", "Maharashtra ", "Manipur ", "Meghalaya ", "Mizoram ", "Nagaland", "Odisha ", "Punjab ", "Rajasthan ", "Sikkim ", "Tamil Nadu ", "Telangana", "Tripura ", "Uttarakhand ", "Uttar Pradesh ", "West Bengal ", " " }));
jPanel1.add(jComboBox3, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 300, 130, 30));
jPanel1.add(jTextField4, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 250, 130, 30));
jLabel1.setBackground(new java.awt.Color(0, 0, 0));
jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
jLabel1.setForeground(new java.awt.Color(255, 51, 204));
jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\ssn\\Desktop\\New folder\\living_room_800.jpg")); // NOI18N
jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 800, 430));
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(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jPanel2, 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))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
welcome wl = new welcome();
wl.setVisible(true);
dispose();// TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// JOptionPane.showInternalMessageDialog(null,"Rooom Booked Sucessfully"); // TODO add your handling code here:
}
private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jTextField9ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void NoOfdaysActionPerformed(java.awt.event.ActionEvent evt) {
// String NOofdays = wel.g // TODO add your handling code here:
}
/**
* #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(Reservation.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Reservation.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Reservation.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Reservation.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 Reservation().setVisible(true);
}
});
}
// Variables declaration - do not modify
public javax.swing.JTextField BedType;
private javax.swing.JTextField NoOfdays;
public javax.swing.JTextField RentPerDay;
public javax.swing.JTextField RoomNo;
public javax.swing.JTextField RoomType;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JComboBox<String> jComboBox2;
private javax.swing.JComboBox<String> jComboBox3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel12;
private javax.swing.JLabel jLabel13;
private javax.swing.JLabel jLabel14;
private javax.swing.JLabel jLabel15;
private javax.swing.JLabel jLabel16;
private javax.swing.JLabel jLabel17;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField11;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
private javax.swing.JTextField jTextField7;
private javax.swing.JTextField jTextField8;
private javax.swing.JTextField jTextField9;
// End of variables declaration
}
enter code here
The best way would be to add a Booking table or add a booking Boolean to your room database. When the room is booked skip over that row and do not add it to your table.
(Edited)
I'm creating a small project with dynamically changing panels.
I've been able to write code that can call a JPanel using an action event from a button within the the JFrame (ManagerScreen). For example:
package route66.JFrames;
import route66.pannels.BuyReserve;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import route66.pannels.Buy;
import route66.pannels.BuyReport;
import route66.pannels.CustomerDetails;
import route66.pannels.NewUser;
import route66.pannels.QuotPanel;
import route66.pannels.ReportPanel;
import route66.pannels.Salaries_Com;
import route66.pannels.SaleSucces;
/**
*
* #author Vavu
*/
public class ManagerScreen extends javax.swing.JFrame {
GridBagLayout layout = new GridBagLayout();
Buy BuyPanel = new Buy();
Buy b = new Buy();
BuyReserve buyR = new BuyReserve();
BuyReport brep = new BuyReport();
CustomerDetails cusD = new CustomerDetails();
QuotPanel QP = new QuotPanel();
ReportPanel RP = new ReportPanel();
NewUser NW = new NewUser();
Salaries_Com SalCom = new Salaries_Com();
SaleSucces SalS = new SaleSucces();
boolean visible;
/**
* Creates new form ManagerScreen
* #param pabel
*/
public ManagerScreen() {
initComponents();
DynamicPanel.setLayout(layout);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
DynamicPanel.add(b,c);
DynamicPanel.add(buyR,c);
DynamicPanel.add(brep,c);
DynamicPanel.add(cusD,c);
DynamicPanel.add(QP,c);
DynamicPanel.add(RP,c);
DynamicPanel.add(NW,c);
DynamicPanel.add(SalCom,c);
DynamicPanel.add(SalS,c);
b.setVisible(false);
buyR.setVisible(false);
brep.setVisible(false);
cusD.setVisible(false);
QP.setVisible(false);
RP.setVisible(false);
NW.setVisible(false);
SalCom.setVisible(false);
SalS.setVisible(false);
}
/**
* 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() {
DynamicPanel = new javax.swing.JPanel();
MSbuy = new javax.swing.JButton();
MSQuotation = new javax.swing.JButton();
MSReserve = new javax.swing.JButton();
MSSalCom = new javax.swing.JButton();
MSReport = new javax.swing.JButton();
MSCreateNew = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setMinimumSize(new java.awt.Dimension(980, 740));
getContentPane().setLayout(null);
DynamicPanel.setBackground(new java.awt.Color(255, 0, 0));
DynamicPanel.setMaximumSize(new java.awt.Dimension(500, 500));
DynamicPanel.setMinimumSize(new java.awt.Dimension(500, 500));
DynamicPanel.setPreferredSize(new java.awt.Dimension(700, 500));
javax.swing.GroupLayout DynamicPanelLayout = new javax.swing.GroupLayout(DynamicPanel);
DynamicPanel.setLayout(DynamicPanelLayout);
DynamicPanelLayout.setHorizontalGroup(
DynamicPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 700, Short.MAX_VALUE)
);
DynamicPanelLayout.setVerticalGroup(
DynamicPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 500, Short.MAX_VALUE)
);
getContentPane().add(DynamicPanel);
DynamicPanel.setBounds(330, 260, 700, 500);
MSbuy.setText("Buy");
MSbuy.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MSbuyActionPerformed(evt);
}
});
getContentPane().add(MSbuy);
MSbuy.setBounds(70, 480, 188, 29);
MSQuotation.setText("Quotation");
MSQuotation.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MSQuotationActionPerformed(evt);
}
});
getContentPane().add(MSQuotation);
MSQuotation.setBounds(70, 520, 188, 29);
MSReserve.setText("Reserve");
MSReserve.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MSReserveActionPerformed(evt);
}
});
getContentPane().add(MSReserve);
MSReserve.setBounds(70, 440, 188, 29);
MSSalCom.setText("Salaries & Commission");
MSSalCom.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MSSalComActionPerformed(evt);
}
});
getContentPane().add(MSSalCom);
MSSalCom.setBounds(70, 400, 188, 29);
MSReport.setText("Report");
MSReport.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MSReportActionPerformed(evt);
}
});
getContentPane().add(MSReport);
MSReport.setBounds(70, 560, 187, 29);
MSCreateNew.setText("Create New User");
MSCreateNew.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MSCreateNewActionPerformed(evt);
}
});
getContentPane().add(MSCreateNew);
MSCreateNew.setBounds(70, 600, 187, 29);
jLabel1.setFont(new java.awt.Font("Lucida Grande", 0, 48)); // NOI18N
jLabel1.setForeground(new java.awt.Color(0, 0, 204));
jLabel1.setText("ROUTE66 CAR DEALERSHIP");
getContentPane().add(jLabel1);
jLabel1.setBounds(330, 0, 650, 130);
jLabel3.setText(".");
getContentPane().add(jLabel3);
jLabel3.setBounds(0, 0, 330, 260);
jLabel2.setText(".");
getContentPane().add(jLabel2);
jLabel2.setBounds(330, 90, 650, 210);
pack();
}// </editor-fold>
private void MSbuyActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
b.setVisible(false);
buyR.setVisible(true);
brep.setVisible(false);
cusD.setVisible(false);
QP.setVisible(false);
RP.setVisible(false);
NW.setVisible(false);
SalCom.setVisible(false);
SalS.setVisible(false);
}
private void MSSalComActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
b.setVisible(false);
buyR.setVisible(false);
brep.setVisible(false);
cusD.setVisible(false);
QP.setVisible(false);
RP.setVisible(false);
NW.setVisible(false);
SalCom.setVisible(true);
SalS.setVisible(false);
}
private void MSReserveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
b.setVisible(false);
buyR.setVisible(false);
brep.setVisible(false);
cusD.setVisible(false);
QP.setVisible(false);
RP.setVisible(true);
NW.setVisible(false);
SalCom.setVisible(false);
SalS.setVisible(false);
}
private void MSQuotationActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void MSReportActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void MSCreateNewActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* #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(ManagerScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ManagerScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ManagerScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ManagerScreen.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 ManagerScreen().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel DynamicPanel;
private javax.swing.JButton MSCreateNew;
private javax.swing.JButton MSQuotation;
private javax.swing.JButton MSReport;
private javax.swing.JButton MSReserve;
private javax.swing.JButton MSSalCom;
private javax.swing.JButton MSbuy;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
// End of variables declaration
}
My problem is that the BR panel (buyReserve()), containing two buttons - within the JFrame, is supposed to be able to call either the buy panel or brep panel (BuyReport()) from the within the DynamicPanel() or at least thats what I think, if that makes sense.
so basically I've tried somethings including the following:
package route66.pannels;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import route66.pannels.BuyReport;
import route66.pannels.CustomerDetails;
/**
*
* #author Vavu
*/
public class BuyReserve extends javax.swing.JPanel {
GridBagLayout layout = new GridBagLayout();
Buy BuyPanel = new Buy();
Buy b = new Buy();
BuyReserve buyR = new BuyReserve();
BuyReport brep = new BuyReport();
CustomerDetails cusD = new CustomerDetails();
QuotPanel QP = new QuotPanel();
ReportPanel RP = new ReportPanel();
NewUser NW = new NewUser();
Salaries_Com SalCom = new Salaries_Com();
SaleSucces SalS = new SaleSucces();
/**
* Creates new form BuyReserve
*/
public BuyReserve() {
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() {
btnBuy = new javax.swing.JButton();
btnReserve = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
btnBuy.setText("New Client");
btnBuy.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnBuyActionPerformed(evt);
}
});
btnReserve.setText("Reserve Client");
jLabel1.setText("Select Option");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(144, 144, 144)
.addComponent(btnBuy)
.addGap(35, 35, 35)
.addComponent(btnReserve))
.addGroup(layout.createSequentialGroup()
.addGap(230, 230, 230)
.addComponent(jLabel1)))
.addContainerGap(162, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(62, 62, 62)
.addComponent(jLabel1)
.addGap(72, 72, 72)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnBuy)
.addComponent(btnReserve))
.addContainerGap(201, Short.MAX_VALUE))
);
}// </editor-fold>
private void btnBuyActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
b.setVisible(true);
}
// Variables declaration - do not modify
private javax.swing.JButton btnBuy;
private javax.swing.JButton btnReserve;
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
If I run the ManagerScreen with the inclusion of the second code snippet I get an error. I've also tried other types of code that end up saying things like the jpanel doesn't have a main class n so on.
So, hopefully by this stage I've been able to get my point across to be to able ask (again); how do I call a panel, within a panel that is inside a JFrame?
You state that you want to "call" JPanels, but this doesn't really have any meaning. If your goal is to swap JPanel views, then the simplest and easiest way is to use a CardLayout. For more on this useful tool, please see the CardLayout Tutorial.
For example:
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class ManagerPanel extends JPanel {
private CardLayout cardLayout = new CardLayout();
private JPanel cardLayoutPanel = new JPanel(cardLayout);
private BuyPanel buyPanel = new BuyPanel(this);
private BuyReportPanel buyReportPanel = new BuyReportPanel(this);
private DefaultComboBoxModel<String> comboModel = new DefaultComboBoxModel<>();
private JComboBox<String> panelComboBox = new JComboBox<>(comboModel);
public ManagerPanel() {
comboModel.addElement(BuyPanel.BUY);
comboModel.addElement(BuyReportPanel.BUY_REPORT);
panelComboBox.addActionListener(evt -> {
String key = panelComboBox.getSelectedItem().toString();
cardLayout.show(cardLayoutPanel, key);
});
JPanel comboPanel = new JPanel();
comboPanel.add(panelComboBox);
cardLayoutPanel.add(buyPanel, BuyPanel.BUY);
cardLayoutPanel.add(buyReportPanel, BuyReportPanel.BUY_REPORT);
setLayout(new BorderLayout());
add(cardLayoutPanel, BorderLayout.CENTER);
add(comboPanel, BorderLayout.PAGE_END);
}
private static void createAndShowGui() {
ManagerPanel mainPanel = new ManagerPanel();
JFrame frame = new JFrame("ManagerPanel");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
public void nextPanel() {
cardLayout.next(cardLayoutPanel);
}
}
class BuyReportPanel extends JPanel {
public final static String BUY_REPORT = "Buy Report";
private ManagerPanel managerPanel;
public BuyReportPanel(ManagerPanel managerPanel) {
this.managerPanel = managerPanel;
add(new JLabel(BUY_REPORT), SwingConstants.CENTER);
add(new JButton(new NextPanelAction(managerPanel)));
}
}
class BuyPanel extends JPanel {
public final static String BUY = "Buy";
private ManagerPanel managerPanel;
public BuyPanel(ManagerPanel managerPanel) {
setPreferredSize(new Dimension(400, 300));
this.managerPanel = managerPanel;
add(new JLabel(BUY), SwingConstants.CENTER);
add(new JButton(new NextPanelAction(managerPanel)));
}
}
class NextPanelAction extends AbstractAction {
private ManagerPanel managerPanel;
public NextPanelAction(ManagerPanel managerPanel) {
super("Next Panel");
this.managerPanel = managerPanel;
}
#Override
public void actionPerformed(ActionEvent e) {
managerPanel.nextPanel();
}
}
Note that I've passed my main class, here ManagerPanel, into other classes, and so they can call any of its public methods including nextPanel(), as needed.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
when I try to INSERT RECORD(like username and password) in database on clicking register button it shows error through JOptionPane "java.lang.NullPointerException"...please tell me the solution how to INSERT RECORD**
import java.sql.*;
import javax.swing.*;
public class Newuser extends javax.swing.JInternalFrame {
Connection cn;
PreparedStatement st;
ResultSet rs;
String url,sql;
public Newuser() {
initComponents();
}
public void MyConnection()
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
url="jdbc:sqlserver://localhost;instance=LALLYDB;databaseName=Menu;user=kd;password=123";
cn=DriverManager.getConnection(url); //above instance ,i.e,LALLYDB is DatabaseServer Name
}
catch(Exception ee)
{
JOptionPane.showMessageDialog(this, ee);
}
}
public void Insert_Record(String username,String passwd)
{
try
{
MyConnection(); //calling MyConnection function
st=cn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=st.executeQuery();
sql="Insert into Project values(?,?)";
st=cn.prepareStatement(sql);
st.setString(1,username);
st.setString(2,passwd);
st.executeUpdate();
JOptionPane.showMessageDialog(this,"Record successfully inserted");
cn.close();
}
catch(Exception ee)
{
JOptionPane.showMessageDialog(this, ee);
}
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
nlbl1 = new javax.swing.JLabel();
nlbl2 = new javax.swing.JLabel();
ntxt1 = new javax.swing.JTextField();
ntxt2 = new javax.swing.JTextField();
nlbl3 = new javax.swing.JLabel();
nlbl4 = new javax.swing.JLabel();
nlbl5 = new javax.swing.JLabel();
ntxt3 = new javax.swing.JTextField();
ntxt4 = new javax.swing.JTextField();
regitser = new javax.swing.JButton();
jcombo1 = new javax.swing.JComboBox<>();
setClosable(true);
setIconifiable(true);
setMaximizable(true);
setResizable(true);
setTitle("New User Registration");
setPreferredSize(new java.awt.Dimension(488, 425));
setVisible(true);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
nlbl1.setText("Username");
getContentPane().add(nlbl1, new org.netbeans.lib.awtextra.AbsoluteConstraints(84, 44, -1, -1));
nlbl2.setText("Password");
getContentPane().add(nlbl2, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 80, -1, -1));
getContentPane().add(ntxt1, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 40, 140, -1));
getContentPane().add(ntxt2, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 80, 140, -1));
nlbl3.setText("Email-id");
getContentPane().add(nlbl3, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 120, -1, -1));
nlbl4.setText("Hint Question");
getContentPane().add(nlbl4, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 160, -1, -1));
nlbl5.setText("Hint Answer");
getContentPane().add(nlbl5, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 200, -1, -1));
getContentPane().add(ntxt3, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 120, 140, -1));
getContentPane().add(ntxt4, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 200, 140, -1));
regitser.setText("Register");
regitser.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
regitserActionPerformed(evt);
}
});
getContentPane().add(regitser, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 200, -1, -1));
jcombo1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Select", "What is your pet name ?", "What is your school name ?", "What is your favourite sport ?" }));
getContentPane().add(jcombo1, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 160, -1, -1));
pack();
}// </editor-fold>
private void regitserActionPerformed(java.awt.event.ActionEvent evt) {
Insert_Record(ntxt1.getText(),ntxt2.getText());
}
// Variables declaration - do not modify
private javax.swing.JComboBox<String> jcombo1;
private javax.swing.JLabel nlbl1;
private javax.swing.JLabel nlbl2;
private javax.swing.JLabel nlbl3;
private javax.swing.JLabel nlbl4;
private javax.swing.JLabel nlbl5;
private javax.swing.JTextField ntxt1;
private javax.swing.JTextField ntxt2;
private javax.swing.JTextField ntxt3;
private javax.swing.JTextField ntxt4;
private javax.swing.JButton regitser;
// End of variables declaration
}
The error you get through JOptionPane contains a linenumber, where the Exception occurs (you are catching it and showing it). Either look that linenumber in the MessageDialog up and see what line is the reason, or type ee.printStackTrace() inside the catch-blocks and you can then see the statement throwing the Exception. Also at st=cn.prepareStatement(sql,...); the variable sql has not been set, is this intentional ?
NullPointerException in general means, that you are trying to access a variable that hasn't been initialized before.
I'm currently working on a invoice generation/bill printing system for a industry.
I need a code which print the text as well as the image logo of company.
Here I'm giving a code of JFrame I have created and I need a print code which print both the text and images by fetching data from SQL 2008 server as backend.
here its is a code of netbeans frame
here i need action implementation on Print Button below:
plz help me soon providing a appropriate way to print text and image.
package invoice2;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.HeadlessException;
import java.awt.image.BufferedImage;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import java.awt.print.*;
import java.awt.print.PageFormat;
import javax.swing.JTextPane;
import java.awt.print.PrinterException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.Sides;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
*
* #author Admin Param
*/
public class generate extends javax.swing.JInternalFrame {
/**
* Creates new form generate
*/
public generate() {
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();
jSeparator1 = new javax.swing.JSeparator();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
t7 = new javax.swing.JTextField();
t1 = new javax.swing.JTextField();
t2 = new javax.swing.JTextField();
t3 = new javax.swing.JTextField();
t5 = new javax.swing.JTextField();
t6 = new javax.swing.JTextField();
t4 = new javax.swing.JScrollPane();
t9 = new javax.swing.JTextArea();
jButton1 = new javax.swing.JButton();
d1 = new com.toedter.calendar.JDateChooser();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
logo = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
print = new javax.swing.JButton();
jSeparator2 = new javax.swing.JSeparator();
jLabel10 = new javax.swing.JLabel();
setClosable(true);
setIconifiable(true);
setMaximizable(true);
setResizable(true);
setTitle("Generate Bill/Print Invoice");
setToolTipText("");
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jPanel1.add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, -18, 490, 20));
jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
jLabel1.setText("No. Of Item :");
jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(35, 20, 90, -1));
jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
jLabel2.setText("Price of Item :");
jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(35, 60, 90, -1));
jLabel3.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
jLabel3.setText(" Tex :");
jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 280, -1, 30));
jLabel4.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
jLabel4.setText("Invoice No :");
jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(41, 90, 80, 21));
jLabel5.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
jLabel5.setText(" Billing Address :");
jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 130, -1, 21));
jLabel6.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
jLabel6.setText(" Sub Total :");
jPanel1.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 240, 80, 30));
jLabel8.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
jLabel8.setText(" Total :");
jPanel1.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 330, -1, -1));
jLabel9.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
jLabel9.setText(" Date :");
jPanel1.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 210, -1, -1));
t7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
t7ActionPerformed(evt);
}
});
jPanel1.add(t7, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 320, 140, 30));
t1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
t1ActionPerformed(evt);
}
});
jPanel1.add(t1, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 10, 140, 30));
t2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
t2ActionPerformed(evt);
}
});
jPanel1.add(t2, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 50, 140, 30));
t3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
t3ActionPerformed(evt);
}
});
jPanel1.add(t3, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 90, 140, 30));
t5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
t5ActionPerformed(evt);
}
});
jPanel1.add(t5, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 240, 140, 30));
t6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
t6ActionPerformed(evt);
}
});
jPanel1.add(t6, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 280, 140, 30));
t9.setColumns(20);
t9.setRows(5);
t4.setViewportView(t9);
jPanel1.add(t4, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 130, 180, 60));
jButton1.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
jButton1.setText("Generate Total");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 310, 150, 40));
jPanel1.add(d1, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 200, 160, 30));
jButton4.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
jButton4.setText("Generate SubTotal");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jPanel1.add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 230, 150, 40));
jButton5.setText("image ");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
jPanel1.add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 10, 100, 40));
logo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/invoice2/big.jpg"))); // NOI18N
jPanel1.add(logo, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 70, 130, 130));
getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, 510, 360));
jLabel7.setText(" Tex :");
getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 180, -1, 21));
jButton2.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
jButton2.setText("Exit");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 410, 120, 40));
jButton3.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
jButton3.setText("Save ");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
getContentPane().add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 410, 120, 40));
print.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
print.setText("Print ");
print.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
printActionPerformed(evt);
}
});
getContentPane().add(print, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 410, 130, 40));
getContentPane().add(jSeparator2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 18, 480, 10));
jLabel10.setFont(new java.awt.Font("Times New Roman", 0, 18)); // NOI18N
jLabel10.setText(" Welcome To Invoice Print/Bill Generator Software By ArtSheoran");
getContentPane().add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 0, 510, 20));
pack();
}// </editor-fold>
private void t7ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void t1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void t2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void t3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void t5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void t6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
dispose(); // TODO add your handling code here:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
#SuppressWarnings("empty-statement")
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=invoice;integratedSecurity=true";
Connection con = DriverManager.getConnection(connectionUrl);
PreparedStatement pstmt=con.prepareStatement("insert into record values(?,?,?,?,?,?,?,?)");
int f1=Integer.parseInt(t1.getText());
pstmt.setInt(1,f1);
int f2=Integer.parseInt(t2.getText());
pstmt.setInt(2,f2);
int f3=Integer.parseInt(t3.getText());
pstmt.setInt(3,f3);
pstmt.setString(4,t9.getText());
java.util.Date date=d1.getDate();
String d=String.format("%1$tm-%1$td-%1$ty",date);
pstmt.setString(5,d);
int f4=Integer.parseInt(t5.getText());
pstmt.setInt(6,f4);
int f5=Integer.parseInt(t6.getText());
pstmt.setInt(7,f5);
int f6=Integer.parseInt(t7.getText());
pstmt.setInt(8,f6);
pstmt.executeUpdate();
JOptionPane.showMessageDialog(null,"record saved successfully");
}
catch(ClassNotFoundException | SQLException | NumberFormatException | HeadlessException ob)
{
JOptionPane.showMessageDialog(null, ob.toString());
} // TODO add your handling code here:
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
int nitem=Integer.parseInt(t1.getText());
int price=Integer.parseInt(t2.getText());
int s=nitem*price;
t5.setText(Integer.toString(s));// TODO add your handling code here:
}
private void printActionPerformed(java.awt.event.ActionEvent evt) {
// here need a action under print button for text and image printing.
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=invoice;integratedSecurity=true";
Connection con = DriverManager.getConnection(connectionUrl);
PreparedStatement pstmt=con.prepareStatement("insert into saveimage values(?,?,?,?)");
pstmt.setString(1,"mahendra");
pstmt.setString(2,"Delhi");
pstmt.setString(4,"123456");
JFileChooser chooser = new JFileChooser();
String str="";
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
File ob=chooser.getSelectedFile();
str=ob.getPath();
Icon img = new ImageIcon(str);
logo.setIcon(img);
pstmt.setString(3, str);
//executeUpdate() method execute specified sql query. Here this query
//insert data and image from specified address.
int s = pstmt.executeUpdate();
if(s>0) {
JOptionPane.showMessageDialog(null, "File Uploaded Sucessfully");;
}
else {
JOptionPane.showMessageDialog(null, "File Not uploaded a error has been occured");
}
}
}
catch (ClassNotFoundException | SQLException | HeadlessException ex) {
System.out.println("Found some error : "+ex);
} // TODO add your handling code here:
}
// Variables declaration - do not modify
private com.toedter.calendar.JDateChooser d1;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JLabel logo;
private javax.swing.JButton print;
private javax.swing.JTextField t1;
private javax.swing.JTextField t2;
private javax.swing.JTextField t3;
private javax.swing.JScrollPane t4;
private javax.swing.JTextField t5;
private javax.swing.JTextField t6;
private javax.swing.JTextField t7;
private javax.swing.JTextArea t9;
// End of variables declaration
private static class OutputPrinter implements Printable {
public OutputPrinter(String printData) {
}
#Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
throw new UnsupportedOperationException("Not supported yet.");
}
}
}
Plz help me soon and write appropriate guide.to implement action of printing text and image from a JInternal Frame and using SQL Server 2008.
a good answer is awaited.
You can define HTML and set it to JTextPane. Then print the JTextPane's content.
If you need multi page printing see http://java-sl.com/JEditorPanePrinter.html
this is the class who i use to load the images, i use the constructor to call the metod load who load the images in the array percorso.
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
class Track extends Component {
Image[] percorso;
String path;
int n;
int x;
int y;
public Track(String path, int n) {
int i;
this.path = path;
this.n = n;
this.percorso = new Image[n];
for (i = 0; i < n; i++) {
this.percorso[i] = load(path, n, i);
repaint();
}
}
public final Image load(String path, int n, int i) {
Toolkit tool = Toolkit.getDefaultToolkit();
String dot = ".jpg";
path = this.path;
path = path + Integer.toString(i) + dot;
Image img = tool.getImage(path);
try {
MediaTracker track = new MediaTracker(this);
track.addImage(img, 0);
track.waitForID(0);
} catch (InterruptedException e) {
System.out.println("Image not loaded");
}
return (img);
}
#Override
public void paint(Graphics g) {
int i;
g.drawImage(percorso[1], x, y, this);
}
}
This is the class where i create the gui, and i try to add the images to the jlayeredpane, but it's didn't work.
public class Gui extends javax.swing.JFrame {
/** Creates new form GUI */
public Gui() {
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() {
background = new javax.swing.JLayeredPane();
testata = new javax.swing.JLabel();
nuova = new javax.swing.JLabel();
selectrack = new javax.swing.JLabel();
track1 = new javax.swing.JLabel();
track2 = new javax.swing.JLabel();
track3 = new javax.swing.JLabel();
track4 = new javax.swing.JLabel();
track5 = new javax.swing.JLabel();
track5.setVisible(false);
track6 = new javax.swing.JLabel();
track6.setVisible(false);
track7 = new javax.swing.JLabel();
track7.setVisible(false);
track8 = new javax.swing.JLabel();
track8.setVisible(false);
track9 = new javax.swing.JLabel();
track9.setVisible(false);
track10 = new javax.swing.JLabel();
track10.setVisible(false);
behind = new javax.swing.JLabel();
behind.setVisible(false);
traccy = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
testata.setFont(new java.awt.Font("Tahoma", 0, 36));
testata.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
testata.setText("Thosa Racing");
testata.setBounds(90, 0, 220, 44);
background.add(testata, javax.swing.JLayeredPane.DEFAULT_LAYER);
nuova.setFont(new java.awt.Font("Tahoma", 0, 18));
nuova.setText("Nuova");
nuova.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
nuovaMouseClicked(evt);
}
});
nuova.setBounds(160, 140, 50, 22);
background.add(nuova, javax.swing.JLayeredPane.DEFAULT_LAYER);
selectrack.setVisible(false);
selectrack.setFont(new java.awt.Font("Tahoma", 0, 24));
selectrack.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
selectrack.setText("Scegli il tracciato");
selectrack.setBounds(110, 10, 177, 29);
background.add(selectrack, javax.swing.JLayeredPane.DEFAULT_LAYER);
track1.setVisible(false);
track1.setFont(new java.awt.Font("Tahoma", 0, 24));
track1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
track1.setText("1");
track1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
track1MouseClicked(evt);
}
});
track1.setBounds(50, 50, 13, 29);
background.add(track1, javax.swing.JLayeredPane.DEFAULT_LAYER);
track2.setVisible(false);
track2.setFont(new java.awt.Font("Tahoma", 0, 24));
track2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
track2.setText("2");
track2.setBounds(120, 50, 13, 29);
background.add(track2, javax.swing.JLayeredPane.DEFAULT_LAYER);
track3.setVisible(false);
track3.setFont(new java.awt.Font("Tahoma", 0, 24));
track3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
track3.setText("3");
track3.setBounds(190, 50, 13, 29);
background.add(track3, javax.swing.JLayeredPane.DEFAULT_LAYER);
track4.setVisible(false);
track4.setFont(new java.awt.Font("Tahoma", 0, 24));
track4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
track4.setText("4");
track4.setBounds(250, 50, 13, 29);
background.add(track4, javax.swing.JLayeredPane.DEFAULT_LAYER);
track5.setFont(new java.awt.Font("Tahoma", 0, 24));
track5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
track5.setText("5");
track5.setBounds(320, 50, 13, 29);
background.add(track5, javax.swing.JLayeredPane.DEFAULT_LAYER);
track6.setFont(new java.awt.Font("Tahoma", 0, 24));
track6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
track6.setText("6");
track6.setBounds(50, 110, 13, 29);
background.add(track6, javax.swing.JLayeredPane.DEFAULT_LAYER);
track7.setFont(new java.awt.Font("Tahoma", 0, 24));
track7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
track7.setText("7");
track7.setBounds(120, 110, 13, 29);
background.add(track7, javax.swing.JLayeredPane.DEFAULT_LAYER);
track8.setFont(new java.awt.Font("Tahoma", 0, 24));
track8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
track8.setText("8");
track8.setBounds(190, 110, 13, 29);
background.add(track8, javax.swing.JLayeredPane.DEFAULT_LAYER);
track9.setFont(new java.awt.Font("Tahoma", 0, 24));
track9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
track9.setText("9");
track9.setBounds(250, 110, 13, 29);
background.add(track9, javax.swing.JLayeredPane.DEFAULT_LAYER);
track10.setFont(new java.awt.Font("Tahoma", 0, 24));
track10.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
track10.setText("10");
track10.setBounds(320, 110, 26, 29);
background.add(track10, javax.swing.JLayeredPane.DEFAULT_LAYER);
behind.setFont(new java.awt.Font("Tahoma", 0, 18));
behind.setText("Behind");
behind.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
behindMouseClicked(evt);
}
});
behind.setBounds(10, 260, 54, 22);
background.add(behind, javax.swing.JLayeredPane.DEFAULT_LAYER);
traccy.setBounds(0, 0, 400, 300);
background.add(traccy, javax.swing.JLayeredPane.DEFAULT_LAYER);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addComponent(background,
javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE));
layout.setVerticalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addComponent(background,
javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE));
pack();
}// </editor-fold>
private void nuovaMouseClicked(java.awt.event.MouseEvent evt) {
// Disabilita menu principale
byte d = 1;
disable(d);
}
private void behindMouseClicked(java.awt.event.MouseEvent evt) {
// Torna al menu principale
byte d = 2;
disable(d);
}
private void track1MouseClicked(java.awt.event.MouseEvent evt) {
trackMDisable(false);
Track tracciato1 = new Track("src/track/m", 5);
background.add(tracciato1);
}
// Metodo dis/able menu
public void disable(byte i) {
boolean d = false;
switch (i) {
case 1: {
// Dal menu principale alla lista tracciati
d = true;
principalMDisable(d);
trackMDisable(d);
}
case 2: {
// Dalla lista tracciati al menu principale
trackMDisable(d);
principalMDisable(d);
}
}
}
public void principalMDisable(Boolean b) {
if (b == true) {
// se vero disable il menu principale
testata.setVisible(false);
nuova.setVisible(false);
} else {
// se falso able il menu principale
testata.setVisible(true);
nuova.setVisible(true);
}
}
public void trackMDisable(Boolean b) {
if (b == true) {
// se vero able la lista dei tracciati
selectrack.setVisible(true);
track1.setVisible(true);
track2.setVisible(true);
track3.setVisible(true);
track4.setVisible(true);
track5.setVisible(true);
track6.setVisible(true);
track7.setVisible(true);
track8.setVisible(true);
track9.setVisible(true);
track10.setVisible(true);
behind.setVisible(true);
} else {
// se falso disable la lista dei tracciati
selectrack.setVisible(false);
track1.setVisible(false);
track2.setVisible(false);
track3.setVisible(false);
track4.setVisible(false);
track5.setVisible(false);
track6.setVisible(false);
track7.setVisible(false);
track8.setVisible(false);
track9.setVisible(false);
track10.setVisible(false);
behind.setVisible(false);
}
}
// Variables declaration - do not modify
private javax.swing.JLayeredPane background;
private javax.swing.JLabel behind;
private javax.swing.JLabel nuova;
private javax.swing.JLabel selectrack;
private javax.swing.JLabel testata;
private javax.swing.JLabel traccy;
private javax.swing.JLabel track1;
private javax.swing.JLabel track10;
private javax.swing.JLabel track2;
private javax.swing.JLabel track3;
private javax.swing.JLabel track4;
private javax.swing.JLabel track5;
private javax.swing.JLabel track6;
private javax.swing.JLabel track7;
private javax.swing.JLabel track8;
private javax.swing.JLabel track9;
// End of variables declaration}
}
i don't receive any compiler error, i think the error is in the Gui class where i add the images, in the metod track1MouseClicked, i use the JFrom plugin of NetBeans 7.01 for create the frame and company.
I haven't gone through all of your code, but this will cause problems:
public final Image load(String path, int n, int i) {
Toolkit tool = Toolkit.getDefaultToolkit();
String dot = ".jpg";
path = this.path; // *****
Since you don't really set the path field here. You'll want to reverse that line.
More importantly, you'll want to try to solve your problem in a much smaller simpler program first, a test program that does nothing but read in a bunch of images and displays them, but does nothing else. Then once you have that solved use the code in your larger program. The other benefit is that if your smaller program doesn't work, you can then post it here as a more reasonably sized program that more folks will be willing to read, analyze, test and correct.
Also, don't mix AWT components such as Component with Swing components unless absolutely necessary -- which isn't the case with your program.