Why Java jFrame setResizable() is failling to act? - java

This code is automatically generated by Netbean 6.8 GUI builder. The default application was not contained in a jFrame! It was in jPanel only which I didn't know how to make it none re-sizable. so I added a jFrame to contain all of them and although I set its setResizable(); to False...it still re-sizable! Here is the code:
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class BOSSConverterView extends FrameView {
public BOSSConverterView(SingleFrameApplication app) {
super(app);
JDialog dlg = new JDialog();
dlg.setUndecorated(true);
dlg.setModal(true);
dlg.setLayout(new BorderLayout());
ImageIcon img = new ImageIcon(getClass().getResource("splash.png"));
dlg.add(img, BorderLayout.CENTER);
dlg.setLocationRelativeTo(null);
dlg.setVisible(true);
initComponents();
ResourceMap resourceMap = getResourceMap();
int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
messageTimer = new Timer(messageTimeout, new ActionListener() {
public void actionPerformed(ActionEvent e) {
statusMessageLabel.setText("");
}
});
messageTimer.setRepeats(false);
int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
for (int i = 0; i < busyIcons.length; i++) {
busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
}
busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
public void actionPerformed(ActionEvent e) {
busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
}
});
idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
statusAnimationLabel.setIcon(idleIcon);
progressBar.setVisible(false);
// connecting action tasks to status bar via TaskMonitor
TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
String propertyName = evt.getPropertyName();
if ("started".equals(propertyName)) {
if (!busyIconTimer.isRunning()) {
statusAnimationLabel.setIcon(busyIcons[0]);
busyIconIndex = 0;
busyIconTimer.start();
}
progressBar.setVisible(true);
progressBar.setIndeterminate(true);
} else if ("done".equals(propertyName)) {
busyIconTimer.stop();
statusAnimationLabel.setIcon(idleIcon);
progressBar.setVisible(false);
progressBar.setValue(0);
} else if ("message".equals(propertyName)) {
String text = (String)(evt.getNewValue());
statusMessageLabel.setText((text == null) ? "" : text);
messageTimer.restart();
} else if ("progress".equals(propertyName)) {
int value = (Integer)(evt.getNewValue());
progressBar.setVisible(true);
progressBar.setIndeterminate(false);
progressBar.setValue(value);
}
}
});
}
#Action
public void showAboutBox() {
if (aboutBox == null) {
JFrame mainFrame = BOSSConverterApp.getApplication().getMainFrame();
aboutBox = new BOSSConverterAboutBox(mainFrame);
aboutBox.setLocationRelativeTo(mainFrame);
}
BOSSConverterApp.getApplication().show(aboutBox);
}
/** 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() {
mainFrame = new javax.swing.JFrame();
mainPanel = new javax.swing.JPanel();
jTabbedPane1 = new javax.swing.JTabbedPane();
jDesktopPane1 = new javax.swing.JDesktopPane();
jFormattedTextField1 = new javax.swing.JFormattedTextField();
jFormattedTextField2 = new javax.swing.JFormattedTextField();
jDesktopPane2 = new javax.swing.JDesktopPane();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea2 = new javax.swing.JTextArea();
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
statusPanel = new javax.swing.JPanel();
javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
statusMessageLabel = new javax.swing.JLabel();
statusAnimationLabel = new javax.swing.JLabel();
progressBar = new javax.swing.JProgressBar();
menuBar = new javax.swing.JMenuBar();
javax.swing.JMenu fileMenu = new javax.swing.JMenu();
openFileMenuItem = new javax.swing.JMenuItem();
javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
javax.swing.JMenu helpMenu = new javax.swing.JMenu();
javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
mainFrame.setName("mainFrame"); // NOI18N
mainFrame.setResizable(false);
mainPanel.setMaximumSize(new java.awt.Dimension(400, 400));
mainPanel.setName("mainPanel"); // NOI18N
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(bossconverter.BOSSConverterApp.class).getContext().getResourceMap(BOSSConverterView.class);
jTabbedPane1.setBackground(resourceMap.getColor("jTabbedPane1.background")); // NOI18N
jTabbedPane1.setName("jTabbedPane1"); // NOI18N
jDesktopPane1.setBackground(resourceMap.getColor("jDesktopPane1.background")); // NOI18N
jDesktopPane1.setName("jDesktopPane1"); // NOI18N
jFormattedTextField1.setText(resourceMap.getString("jFormattedTextField1.text")); // NOI18N
jFormattedTextField1.setName("jFormattedTextField1"); // NOI18N
jFormattedTextField1.setBounds(10, 120, 560, -1);
jDesktopPane1.add(jFormattedTextField1, javax.swing.JLayeredPane.DEFAULT_LAYER);
jFormattedTextField2.setText(resourceMap.getString("jFormattedTextField2.text")); // NOI18N
jFormattedTextField2.setName("jFormattedTextField2"); // NOI18N
jFormattedTextField2.setBounds(10, 40, 560, -1);
jDesktopPane1.add(jFormattedTextField2, javax.swing.JLayeredPane.DEFAULT_LAYER);
jTabbedPane1.addTab(resourceMap.getString("jDesktopPane1.TabConstraints.tabTitle"), jDesktopPane1); // NOI18N
jDesktopPane2.setBackground(resourceMap.getColor("jDesktopPane2.background")); // NOI18N
jDesktopPane2.setName("jDesktopPane2"); // NOI18N
jScrollPane1.setName("jScrollPane1"); // NOI18N
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jTextArea1.setName("jTextArea1"); // NOI18N
jScrollPane1.setViewportView(jTextArea1);
jScrollPane1.setBounds(10, 30, 760, 98);
jDesktopPane2.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);
jScrollPane2.setName("jScrollPane2"); // NOI18N
jTextArea2.setColumns(20);
jTextArea2.setRows(5);
jTextArea2.setName("jTextArea2"); // NOI18N
jScrollPane2.setViewportView(jTextArea2);
jScrollPane2.setBounds(10, 160, 760, 97);
jDesktopPane2.add(jScrollPane2, javax.swing.JLayeredPane.DEFAULT_LAYER);
jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
jButton1.setName("jButton1"); // NOI18N
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
tester(evt);
}
});
jButton1.setBounds(610, 270, 93, 29);
jDesktopPane2.add(jButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
jLabel1.setName("jLabel1"); // NOI18N
jLabel1.setBounds(30, 140, 330, 16);
jDesktopPane2.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
jLabel2.setName("jLabel2"); // NOI18N
jLabel2.setBounds(20, 10, 330, 16);
jDesktopPane2.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);
jTabbedPane1.addTab(resourceMap.getString("jDesktopPane2.TabConstraints.tabTitle"), jDesktopPane2); // NOI18N
org.jdesktop.layout.GroupLayout mainPanelLayout = new org.jdesktop.layout.GroupLayout(mainPanel);
mainPanel.setLayout(mainPanelLayout);
mainPanelLayout.setHorizontalGroup(
mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jTabbedPane1)
);
mainPanelLayout.setVerticalGroup(
mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, mainPanelLayout.createSequentialGroup()
.add(jTabbedPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 375, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(36, Short.MAX_VALUE))
);
statusPanel.setName("statusPanel"); // NOI18N
statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N
statusMessageLabel.setName("statusMessageLabel"); // NOI18N
statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N
progressBar.setName("progressBar"); // NOI18N
org.jdesktop.layout.GroupLayout statusPanelLayout = new org.jdesktop.layout.GroupLayout(statusPanel);
statusPanel.setLayout(statusPanelLayout);
statusPanelLayout.setHorizontalGroup(
statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(statusPanelSeparator, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 798, Short.MAX_VALUE)
.add(statusPanelLayout.createSequentialGroup()
.addContainerGap()
.add(statusMessageLabel)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 602, Short.MAX_VALUE)
.add(progressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(statusAnimationLabel)
.addContainerGap())
);
statusPanelLayout.setVerticalGroup(
statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(statusPanelLayout.createSequentialGroup()
.add(statusPanelSeparator, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(statusMessageLabel)
.add(statusAnimationLabel)
.add(progressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(3, 3, 3))
);
menuBar.setName("menuBar"); // NOI18N
fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
fileMenu.setName("fileMenu"); // NOI18N
openFileMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.META_MASK));
openFileMenuItem.setText(resourceMap.getString("openFileMenuItem.text")); // NOI18N
openFileMenuItem.setName("openFileMenuItem"); // NOI18N
openFileMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
openFileMenuItemMouseClicked(evt);
}
});
fileMenu.add(openFileMenuItem);
javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(bossconverter.BOSSConverterApp.class).getContext().getActionMap(BOSSConverterView.class, this);
exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
exitMenuItem.setName("exitMenuItem"); // NOI18N
fileMenu.add(exitMenuItem);
menuBar.add(fileMenu);
helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
helpMenu.setName("helpMenu"); // NOI18N
aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
aboutMenuItem.setName("aboutMenuItem"); // NOI18N
helpMenu.add(aboutMenuItem);
menuBar.add(helpMenu);
mainFrame.setJMenuBar(menuBar);
org.jdesktop.layout.GroupLayout mainFrameLayout = new org.jdesktop.layout.GroupLayout(mainFrame.getContentPane());
mainFrame.getContentPane().setLayout(mainFrameLayout);
mainFrameLayout.setHorizontalGroup(
mainFrameLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 838, Short.MAX_VALUE)
.add(mainFrameLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(mainFrameLayout.createSequentialGroup()
.addContainerGap()
.add(mainPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap()))
.add(mainFrameLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(mainFrameLayout.createSequentialGroup()
.addContainerGap()
.add(statusPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap()))
);
mainFrameLayout.setVerticalGroup(
mainFrameLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 531, Short.MAX_VALUE)
.add(mainFrameLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(mainFrameLayout.createSequentialGroup()
.addContainerGap()
.add(mainPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(100, Short.MAX_VALUE)))
.add(mainFrameLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(mainFrameLayout.createSequentialGroup()
.add(474, 474, 474)
.add(statusPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
);
setComponent(mainPanel);
setMenuBar(menuBar);
setStatusBar(statusPanel);
}// </editor-fold>
private void tester(java.awt.event.MouseEvent evt) {
}
private void openFileMenuItemMouseClicked(java.awt.event.MouseEvent evt) {
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JDesktopPane jDesktopPane1;
private javax.swing.JDesktopPane jDesktopPane2;
private javax.swing.JFormattedTextField jFormattedTextField1;
private javax.swing.JFormattedTextField jFormattedTextField2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTabbedPane jTabbedPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextArea jTextArea2;
private javax.swing.JFrame mainFrame;
private javax.swing.JPanel mainPanel;
private javax.swing.JMenuBar menuBar;
private javax.swing.JMenuItem openFileMenuItem;
private javax.swing.JProgressBar progressBar;
private javax.swing.JLabel statusAnimationLabel;
private javax.swing.JLabel statusMessageLabel;
private javax.swing.JPanel statusPanel;
// End of variables declaration
private final Timer messageTimer;
private final Timer busyIconTimer;
private final Icon idleIcon;
private final Icon[] busyIcons = new Icon[15];
private int busyIconIndex = 0;
private JDialog aboutBox;
}
I apologize for the long code, I wasn't sure which part might be important to this case and which one is not.
If you know what is the problem here, please let me know

You have a mistake in your code; my simple self-contained test program shows that setResizable(false) works just fine:
package com.businesslink.core.tst.ld.dct;
import java.awt.*;
import javax.swing.*;
public class SwgTestFrame1b
extends JFrame
{
public SwgTestFrame1b() {
super("SwgTestFrame1b");
setBackground(Color.white);
setResizable(false);
pack();
}
static public void main(String[] args) {
new SwgTestFrame1b().show();
}
}
A quick search of your code reveals nowhere that show or setVisible is invoked on mainFrame; it looks like you are only showing dialogs related to mainFrame. Now setResizable is only done on mainFrame, that will not impact any dialogs that are children of mainFrame. Nor will it affect the frame used by your FrameView.
PS, and just FTR, I absolutely abhor windows that are not resizable they are the mark of either an inadequate GUI toolkit or a lazy programmer.

The default application was not contained in a jFrame!
It was in jPanel only which I didn't know how to make it none re-sizable. so I added a jFrame
you shouldn't be creating an extra JFrame, your panel was already in a frame
You should have used something like this instead of creating a new frame:
getFrame().setResizeable(false);

fill code ((java.awt.Frame)root).setResizable(false); in application
class, exactly in method configureWindow.
(Application class is that class which instantiate View class. )
It should help

Related

how hide a row from jtable if that row is boooked

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.

Java Netbeans For loop error: cannot find symbol class, <identifier> expected?

I'm making a hangman game and this is my current code:
//Declaring the array
String [] WordList = {"anonymous", "tolerate", "appreciation", "commissioner", "harm", "flexibility", "instructional", "scramble", "casino", "tumor"};
// Getting a random word for the game
int num = ((int)Math.ceil(Math.random()*(WordList.length)));
String word = WordList[num];
// The puzzle to be displayed for the user to guess
int letters = word.length();
String hiddenWord = "";
for (int i=0; i<letters; i++){
hiddenWord+="-";
}
At the for loop, I get an error saying 'illegal start of type, cannot find symbol: class i'. 'i' is supposed to be an integer, which I've already declared, so I'm not sure why it's asking for a class.
There's also another error saying, 'identifier expected' and that there's a missing >. As my < is meant to be used as comparison (and not as a declaration or identifier), I don't understand this error.
For clarification, I should add that this is a desktop application in netbeans. The code is located after the line:
public class HangmanGameView extends FrameView {
Most of the rest of my code will be located within the actionPerformed code for my buttons. I need to be able to access the word and hiddenWord variables from within the code for the buttons, which is why I've put them here. I'm not positive if that's the problem as of yet. I'm also not sure where to put the code otherwise; there is no main method as far as I can see.
Edit: I hadn't posted the complete code because this is all I've done for now; the rest is all auto-generated by NetBeans, and I thought it wouldn't be relevant. But here it is:
/*
* HangmanGameView.java
*/
package hangmangame;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
/**
* The application's main frame.
*/
public class HangmanGameView extends FrameView {
//Declaring the array
String [] WordList = {"anonymous", "tolerate", "appreciation", "commissioner", "harm", "flexibility", "instructional", "scramble", "casino", "tumor"};
// Getting a random word for the game
int num = ((int)Math.ceil(Math.random()*(WordList.length)));
String word = WordList[num];
// The puzzle to be displayed for the user to guess
int letters = word.length();
String hiddenWord = "";
for (int i=0; i<letters; i++){
hiddenWord+="-";
}
public HangmanGameView(SingleFrameApplication app) {
super(app);
initComponents();
// status bar initialization - message timeout, idle icon and busy animation, etc
ResourceMap resourceMap = getResourceMap();
int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
messageTimer = new Timer(messageTimeout, new ActionListener() {
public void actionPerformed(ActionEvent e) {
statusMessageLabel.setText("");
}
});
messageTimer.setRepeats(false);
int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
for (int i = 0; i < busyIcons.length; i++) {
busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
}
busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
public void actionPerformed(ActionEvent e) {
busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
}
});
idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
statusAnimationLabel.setIcon(idleIcon);
progressBar.setVisible(false);
// connecting action tasks to status bar via TaskMonitor
TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
String propertyName = evt.getPropertyName();
if ("started".equals(propertyName)) {
if (!busyIconTimer.isRunning()) {
statusAnimationLabel.setIcon(busyIcons[0]);
busyIconIndex = 0;
busyIconTimer.start();
}
progressBar.setVisible(true);
progressBar.setIndeterminate(true);
} else if ("done".equals(propertyName)) {
busyIconTimer.stop();
statusAnimationLabel.setIcon(idleIcon);
progressBar.setVisible(false);
progressBar.setValue(0);
} else if ("message".equals(propertyName)) {
String text = (String)(evt.getNewValue());
statusMessageLabel.setText((text == null) ? "" : text);
messageTimer.restart();
} else if ("progress".equals(propertyName)) {
int value = (Integer)(evt.getNewValue());
progressBar.setVisible(true);
progressBar.setIndeterminate(false);
progressBar.setValue(value);
}
}
});
}
#Action
public void showAboutBox() {
if (aboutBox == null) {
JFrame mainFrame = HangmanGameApp.getApplication().getMainFrame();
aboutBox = new HangmanGameAboutBox(mainFrame);
aboutBox.setLocationRelativeTo(mainFrame);
}
HangmanGameApp.getApplication().show(aboutBox);
}
/** 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() {
mainPanel = new javax.swing.JPanel();
titleLabel = new javax.swing.JLabel();
startButton = new javax.swing.JButton();
guesswordOutput = new javax.swing.JLabel();
guessLabel = new javax.swing.JLabel();
guessInput = new javax.swing.JTextField();
guessButton = new javax.swing.JButton();
picLabel = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
gameOutput = new javax.swing.JTextArea();
menuBar = new javax.swing.JMenuBar();
javax.swing.JMenu fileMenu = new javax.swing.JMenu();
javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
javax.swing.JMenu helpMenu = new javax.swing.JMenu();
javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
statusPanel = new javax.swing.JPanel();
javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
statusMessageLabel = new javax.swing.JLabel();
statusAnimationLabel = new javax.swing.JLabel();
progressBar = new javax.swing.JProgressBar();
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(hangmangame.HangmanGameApp.class).getContext().getResourceMap(HangmanGameView.class);
mainPanel.setBackground(resourceMap.getColor("mainPanel.background")); // NOI18N
mainPanel.setName("mainPanel"); // NOI18N
titleLabel.setFont(titleLabel.getFont().deriveFont(titleLabel.getFont().getSize()+19f));
titleLabel.setForeground(resourceMap.getColor("titleLabel.foreground")); // NOI18N
titleLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
titleLabel.setText(resourceMap.getString("titleLabel.text")); // NOI18N
titleLabel.setName("titleLabel"); // NOI18N
startButton.setFont(startButton.getFont().deriveFont(startButton.getFont().getStyle() & ~java.awt.Font.BOLD, startButton.getFont().getSize()+5));
startButton.setForeground(resourceMap.getColor("startButton.foreground")); // NOI18N
startButton.setText(resourceMap.getString("startButton.text")); // NOI18N
startButton.setToolTipText(resourceMap.getString("startButton.toolTipText")); // NOI18N
startButton.setName("startButton"); // NOI18N
startButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
startButtonActionPerformed(evt);
}
});
guesswordOutput.setText(resourceMap.getString("guesswordOutput.text")); // NOI18N
guesswordOutput.setName("guesswordOutput"); // NOI18N
guessLabel.setFont(resourceMap.getFont("guessLabel.font")); // NOI18N
guessLabel.setText(resourceMap.getString("guessLabel.text")); // NOI18N
guessLabel.setName("guessLabel"); // NOI18N
guessInput.setBackground(resourceMap.getColor("guessInput.background")); // NOI18N
guessInput.setFont(resourceMap.getFont("guessInput.font")); // NOI18N
guessInput.setText(resourceMap.getString("guessInput.text")); // NOI18N
guessInput.setToolTipText(resourceMap.getString("guessInput.toolTipText")); // NOI18N
guessInput.setName("guessInput"); // NOI18N
guessButton.setFont(resourceMap.getFont("guessButton.font")); // NOI18N
guessButton.setForeground(resourceMap.getColor("guessButton.foreground")); // NOI18N
guessButton.setText(resourceMap.getString("guessButton.text")); // NOI18N
guessButton.setName("guessButton"); // NOI18N
guessButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
guessButtonActionPerformed(evt);
}
});
picLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
picLabel.setIcon(resourceMap.getIcon("picLabel.icon")); // NOI18N
picLabel.setText(resourceMap.getString("picLabel.text")); // NOI18N
picLabel.setName("picLabel"); // NOI18N
jScrollPane1.setName("jScrollPane1"); // NOI18N
gameOutput.setBackground(resourceMap.getColor("gameOutput.background")); // NOI18N
gameOutput.setColumns(20);
gameOutput.setRows(5);
gameOutput.setName("gameOutput"); // NOI18N
jScrollPane1.setViewportView(gameOutput);
javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
mainPanel.setLayout(mainPanelLayout);
mainPanelLayout.setHorizontalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(titleLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 485, Short.MAX_VALUE)
.addGroup(mainPanelLayout.createSequentialGroup()
.addGap(42, 42, 42)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(mainPanelLayout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(picLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(mainPanelLayout.createSequentialGroup()
.addComponent(guessLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(guessInput, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(46, 46, 46)
.addComponent(guessButton)))
.addContainerGap(55, Short.MAX_VALUE))
.addGroup(mainPanelLayout.createSequentialGroup()
.addGap(181, 181, 181)
.addComponent(startButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(189, Short.MAX_VALUE))
.addComponent(guesswordOutput, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 485, Short.MAX_VALUE)
);
mainPanelLayout.setVerticalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(titleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(startButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(26, 26, 26)
.addComponent(guesswordOutput, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(38, 38, 38)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(guessLabel)
.addComponent(guessButton)
.addComponent(guessInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane1)
.addComponent(picLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE))
.addGap(39, 39, 39))
);
menuBar.setName("menuBar"); // NOI18N
fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
fileMenu.setName("fileMenu"); // NOI18N
javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(hangmangame.HangmanGameApp.class).getContext().getActionMap(HangmanGameView.class, this);
exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
exitMenuItem.setName("exitMenuItem"); // NOI18N
fileMenu.add(exitMenuItem);
menuBar.add(fileMenu);
helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
helpMenu.setName("helpMenu"); // NOI18N
aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
aboutMenuItem.setName("aboutMenuItem"); // NOI18N
helpMenu.add(aboutMenuItem);
menuBar.add(helpMenu);
statusPanel.setName("statusPanel"); // NOI18N
statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N
statusMessageLabel.setName("statusMessageLabel"); // NOI18N
statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N
progressBar.setName("progressBar"); // NOI18N
javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
statusPanel.setLayout(statusPanelLayout);
statusPanelLayout.setHorizontalGroup(
statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 485, Short.MAX_VALUE)
.addGroup(statusPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(statusMessageLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 315, Short.MAX_VALUE)
.addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(statusAnimationLabel)
.addContainerGap())
);
statusPanelLayout.setVerticalGroup(
statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(statusPanelLayout.createSequentialGroup()
.addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(statusMessageLabel)
.addComponent(statusAnimationLabel)
.addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(3, 3, 3))
);
setComponent(mainPanel);
setMenuBar(menuBar);
setStatusBar(statusPanel);
}// </editor-fold>
private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {
// Initializes the game and displays the empty word for the player
}
private void guessButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
// Variables declaration - do not modify
private javax.swing.JTextArea gameOutput;
private javax.swing.JButton guessButton;
private javax.swing.JTextField guessInput;
private javax.swing.JLabel guessLabel;
private javax.swing.JLabel guesswordOutput;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JPanel mainPanel;
private javax.swing.JMenuBar menuBar;
private javax.swing.JLabel picLabel;
private javax.swing.JProgressBar progressBar;
private javax.swing.JButton startButton;
private javax.swing.JLabel statusAnimationLabel;
private javax.swing.JLabel statusMessageLabel;
private javax.swing.JPanel statusPanel;
private javax.swing.JLabel titleLabel;
// End of variables declaration
private final Timer messageTimer;
private final Timer busyIconTimer;
private final Icon idleIcon;
private final Icon[] busyIcons = new Icon[15];
private int busyIconIndex = 0;
private JDialog aboutBox;
}
Final Edit:
I moved my for loop to inside the start button code - leaving the initial declaration of the hiddenWord string at the beginning - and it's working fine now. Thanks for the help!

create JPanel after click the button /Java

I have a problem with my project. I would like to create new Panel in my Frame after click the button. Here's a part of a code:
private class ComponentListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(newGameItem))
{
System.out.println("dasda");
gamePanel = new GamePanel();
cont.add(gamePanel);
}
if (e.getSource().equals(stopItem)) {
gamePanel.stopGame();
}
}
}
GamePanel gamePanel;
Container cont = this.getContentPane();
When I click the button I see the text in console, also when I'm starting project already with this Panel everything is okay.
I guess this problem is simple but I can't figure out what's going on :( .
I see "dasda" so it means that method actionPerformed works correctly.
miniwolf where should I invoke repaint? In actionPerformed?
Okay, I found a solution. I should have added gamePanel.revalidate() in actionPerformed. Thanks for your help
I have to refresh this topic, cause I have a problem with change JPanel in JFrame form.
When I click the button I want to change a panel but I can't see it, but when I create JFrame (java class) and write code by myself everything's okay.
This is a code from JFrame (java class) made by me.
public class main extends JFrame
{
public main()
{
frameSettings();
this.getContentPane().add(panel1);
panel1.add(button);
panel1.setBackground(Color.red);
button.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e)
{
gamePanel = new GamePanel();
container.add(gamePanel);
gamePanel.startGame();
gamePanel.revalidate();
gamePanel.requestFocus();
}
});
}
private void frameSettings() {
int szer = Toolkit.getDefaultToolkit().getScreenSize().width;
int wys = Toolkit.getDefaultToolkit().getScreenSize().height;
this.setTitle("Pong");
this.setSize(700, 600);
this.setResizable(false);
int szerRamki = this.getSize().width;
int wysRamki = this.getSize().height;
this.setDefaultCloseOperation(3);
this.setLocation((szer - szerRamki) / 2, (wys - wysRamki) / 2);
}
JPanel panel1 = new JPanel();
JButton button = new JButton();
GamePanel gamePanel;
Container container = this.getContentPane();
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
#Override
public void run()
{
new main().setVisible(true);
}
});
}
}
And this is a code from JFrame form
public class MainFrame extends javax.swing.JFrame
{
/**
* Creates new form MainFrame
*/
public MainFrame()
{
initComponents();
frameSettings();
}
private void frameSettings()
{
int szer = Toolkit.getDefaultToolkit().getScreenSize().width;
int wys = Toolkit.getDefaultToolkit().getScreenSize().height;
this.setTitle("Pong");
this.setSize(700, 600);
this.setResizable(false);
int szerRamki = this.getSize().width;
int wysRamki = this.getSize().height;
this.setDefaultCloseOperation(3);
this.setLocation((szer - szerRamki) / 2, (wys - wysRamki) / 2);
}
/**
* 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();
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();
jButton1 = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem3 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(0, 0, 0));
jLabel1.setFont(new java.awt.Font("Monospaced", 3, 160)); // NOI18N
jLabel1.setForeground(new java.awt.Color(51, 255, 255));
jLabel1.setText("Pong");
jLabel2.setFont(new java.awt.Font("Monospaced", 3, 48)); // NOI18N
jLabel2.setForeground(new java.awt.Color(102, 255, 0));
jLabel2.setText("Nowa Gra");
jLabel3.setFont(new java.awt.Font("Monospaced", 3, 48)); // NOI18N
jLabel3.setForeground(new java.awt.Color(102, 255, 0));
jLabel3.setText("Ranking");
jLabel4.setFont(new java.awt.Font("Monospaced", 3, 48)); // NOI18N
jLabel4.setForeground(new java.awt.Color(102, 255, 0));
jLabel4.setText("Wyjście");
jLabel5.setFont(new java.awt.Font("Monospaced", 3, 18)); // NOI18N
jLabel5.setForeground(new java.awt.Color(153, 255, 0));
jLabel5.setText("Najlepszy gracz:");
jButton1.setText("jButton1");
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()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(237, 237, 237)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(jLabel3)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(227, 227, 227)
.addComponent(jLabel2)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(148, 148, 148)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 131, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(102, 102, 102))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(57, 57, 57)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1)
.addComponent(jButton1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel5))
);
jMenu1.setText("File");
jMenuItem1.setText("Nowa Gra");
jMenu1.add(jMenuItem1);
jMenuItem2.setText("jMenuItem2");
jMenu1.add(jMenuItem2);
jMenuItem3.setText("jMenuItem3");
jMenu1.add(jMenuItem3);
jMenuBar1.add(jMenu1);
jMenu2.setText("Edit");
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.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
System.out.println("askdalk");
gamePanel = new GamePanel();
container.add(gamePanel);
gamePanel.startGame();
gamePanel.revalidate();
gamePanel.requestFocus();
}
/**
* #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(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex)
{
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex)
{
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex)
{
java.util.logging.Logger.getLogger(MainFrame.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 MainFrame().setVisible(true);
}
});
}
private Container container = this.getContentPane();
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
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.JPanel jPanel1;
// End of variables declaration
private GamePanel gamePanel;
}

Java/Swing+NetBeans 6.1. Keyboard event is not working

I need to move a Label-control on the form.
I have created a "Java Desktop Application" in NetBeans 6.1.
I have added the following code:
But the label is not moving.
Why?
/*
* DesktopApplication1View.java
*/
package desktopapplication1;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
/**
* The application's main frame.
*/
public class DesktopApplication1View extends FrameView {
public DesktopApplication1View(SingleFrameApplication app)
{
super(app);
initComponents();
// status bar initialization - message timeout, idle icon and busy animation, etc
ResourceMap resourceMap = getResourceMap();
int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
messageTimer = new Timer(messageTimeout, new ActionListener() {
public void actionPerformed(ActionEvent e) {
statusMessageLabel.setText("");
}
});
messageTimer.setRepeats(false);
int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
for (int i = 0; i < busyIcons.length; i++) {
busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
}
busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
public void actionPerformed(ActionEvent e) {
busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
}
});
idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
statusAnimationLabel.setIcon(idleIcon);
progressBar.setVisible(false);
// connecting action tasks to status bar via TaskMonitor
TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
String propertyName = evt.getPropertyName();
if ("started".equals(propertyName)) {
if (!busyIconTimer.isRunning()) {
statusAnimationLabel.setIcon(busyIcons[0]);
busyIconIndex = 0;
busyIconTimer.start();
}
progressBar.setVisible(true);
progressBar.setIndeterminate(true);
} else if ("done".equals(propertyName)) {
busyIconTimer.stop();
statusAnimationLabel.setIcon(idleIcon);
progressBar.setVisible(false);
progressBar.setValue(0);
} else if ("message".equals(propertyName)) {
String text = (String)(evt.getNewValue());
statusMessageLabel.setText((text == null) ? "" : text);
messageTimer.restart();
} else if ("progress".equals(propertyName)) {
int value = (Integer)(evt.getNewValue());
progressBar.setVisible(true);
progressBar.setIndeterminate(false);
progressBar.setValue(value);
}
}
});
jLabel1.isOptimizedDrawingEnabled();
jLabel1.requestFocusInWindow();
}
#Action
public void showAboutBox() {
if (aboutBox == null) {
JFrame mainFrame = DesktopApplication1.getApplication().getMainFrame();
aboutBox = new DesktopApplication1AboutBox(mainFrame);
aboutBox.setLocationRelativeTo(mainFrame);
}
DesktopApplication1.getApplication().show(aboutBox);
}
/** 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() {
mainPanel = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
menuBar = new javax.swing.JMenuBar();
javax.swing.JMenu fileMenu = new javax.swing.JMenu();
javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
javax.swing.JMenu helpMenu = new javax.swing.JMenu();
javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
statusPanel = new javax.swing.JPanel();
javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
statusMessageLabel = new javax.swing.JLabel();
statusAnimationLabel = new javax.swing.JLabel();
progressBar = new javax.swing.JProgressBar();
mainPanel.setName("mainPanel"); // NOI18N
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(desktopapplication1.DesktopApplication1.class).getContext().getResourceMap(DesktopApplication1View.class);
jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
jLabel1.setName("jLabel1"); // NOI18N
jLabel1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
jLabel1KeyPressed(evt);
}
});
javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
mainPanel.setLayout(mainPanelLayout);
mainPanelLayout.setHorizontalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addGap(151, 151, 151)
.addComponent(jLabel1)
.addContainerGap(215, Short.MAX_VALUE))
);
mainPanelLayout.setVerticalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addGap(83, 83, 83)
.addComponent(jLabel1)
.addContainerGap(155, Short.MAX_VALUE))
);
menuBar.setName("menuBar"); // NOI18N
fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
fileMenu.setName("fileMenu"); // NOI18N
javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(desktopapplication1.DesktopApplication1.class).getContext().getActionMap(DesktopApplication1View.class, this);
exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
exitMenuItem.setName("exitMenuItem"); // NOI18N
fileMenu.add(exitMenuItem);
menuBar.add(fileMenu);
helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
helpMenu.setName("helpMenu"); // NOI18N
aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
aboutMenuItem.setName("aboutMenuItem"); // NOI18N
helpMenu.add(aboutMenuItem);
menuBar.add(helpMenu);
statusPanel.setName("statusPanel"); // NOI18N
statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N
statusMessageLabel.setName("statusMessageLabel"); // NOI18N
statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N
progressBar.setName("progressBar"); // NOI18N
javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
statusPanel.setLayout(statusPanelLayout);
statusPanelLayout.setHorizontalGroup(
statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
.addGroup(statusPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(statusMessageLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 226, Short.MAX_VALUE)
.addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(statusAnimationLabel)
.addContainerGap())
);
statusPanelLayout.setVerticalGroup(
statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(statusPanelLayout.createSequentialGroup()
.addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(statusMessageLabel)
.addComponent(statusAnimationLabel)
.addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(3, 3, 3))
);
setComponent(mainPanel);
setMenuBar(menuBar);
setStatusBar(statusPanel);
}// </editor-fold>
private void jLabel1KeyPressed(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
int keyCode = evt.getKeyCode();
int xPos = jLabel1.getX();
int yPos = jLabel1.getY();
switch(keyCode)
{
case KeyEvent.VK_UP:
jLabel1.setLocation(xPos, --yPos);
break;
case KeyEvent.VK_DOWN:
jLabel1.setLocation(xPos, ++yPos);
break;
case KeyEvent.VK_LEFT:
jLabel1.setLocation(--xPos, yPos);
break;
case KeyEvent.VK_RIGHT:
jLabel1.setLocation(++xPos, yPos);
break;
}
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel mainPanel;
private javax.swing.JMenuBar menuBar;
private javax.swing.JProgressBar progressBar;
private javax.swing.JLabel statusAnimationLabel;
private javax.swing.JLabel statusMessageLabel;
private javax.swing.JPanel statusPanel;
// End of variables declaration
private final Timer messageTimer;
private final Timer busyIconTimer;
private final Icon idleIcon;
private final Icon[] busyIcons = new Icon[15];
private int busyIconIndex = 0;
private JDialog aboutBox;
}
Take a look at the Swing tutorial.
1) Only components that have focus can receive KeyEvents. By default a JLabel is not focusable so it will never receive a KeyEvent. A better way to do this is to use Key Bindings. See the section from the Swing tutorial on Key Bindings.
2) If you fix 1, then the label might move, but it won't stay in its new position if the frame is resized, because the layout manager will be invoked and will reposition the label based on its rules.
Read the section from the Swing tutorial on Using Layout Managers. If you really need the label to be in a random position then you will need to use Absolute Positioning which is also discussed in the tutorial.

Getting Exception connecting Yahoo server

package session;
import java.io.FileWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.swing.JOptionPane;
import org.openymsg.network.FireEvent;
import org.openymsg.network.Session;
import org.openymsg.network.SessionState;
import org.openymsg.network.event.SessionListener;
public class BotGUI extends javax.swing.JFrame implements SessionListener{
/** Creates new form BotGUI */
FileWriter fw;
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
public BotGUI() {
initComponents();
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jPanel3 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jPanel4 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
uNameTextField = new javax.swing.JTextField();
uPassPasswordField = new javax.swing.JPasswordField();
jButton1 = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem3 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jPanel3.setBackground(new java.awt.Color(51, 51, 51));
jLabel1.setBackground(new java.awt.Color(0, 0, 255));
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.setText("Yahoo Login Panel");
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addGap(38, 38, 38)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(532, Short.MAX_VALUE))
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
);
jPanel2.add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 710, 30));
jPanel4.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jLabel2.setText("Username");
jPanel4.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 20, 60, 20));
jLabel3.setText("Password");
jPanel4.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 20, 60, 20));
jPanel4.add(uNameTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 20, 140, 20));
jPanel4.add(uPassPasswordField, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 20, 140, -1));
jButton1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jButton1.setText("Login");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jPanel4.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 15, 90, -1));
jPanel2.add(jPanel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 30, 710, 60));
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(293, Short.MAX_VALUE))
);
jMenu1.setText("Option");
jMenuItem1.setText("Logout");
jMenu1.add(jMenuItem1);
jMenuItem2.setText("Load CSV");
jMenu1.add(jMenuItem2);
jMenuItem3.setText("Exit");
jMenu1.add(jMenuItem3);
jMenuBar1.add(jMenu1);
jMenu2.setText("Help");
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.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
public void handleConnectionClosed() {
connectionClosed = true;
loggedIn = false;
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if(!uNameTextField.getText().equals("") && !uPassPasswordField.getText().equals("")){
Yahoo_login(uNameTextField.getText(),uPassPasswordField.getText());
}else{
JOptionPane.showMessageDialog(null, "Plese Enter User Id and Password");
}
}
Session yahooMessengerSession;
MySessionListener mySessionListener;
boolean loggedIn = false;
boolean connectionClosed = false;
public void Yahoo_login(String uName, String pass) {
connectionClosed = false;
if (loggedIn == false) {
yahooMessengerSession = new Session();
mySessionListener = new MySessionListener(this);
yahooMessengerSession.addSessionListener(mySessionListener);
try {
if ((uName.equals("")) || (pass.equals("")))
{
System.out.println("User name/password is blank");
}
else{
//initialized a file writer for log file
System.out.println("Login start........");
yahooMessengerSession.login(uName, pass, true);
//checks whether user was succesful in login in
if (yahooMessengerSession!=null && yahooMessengerSession.getSessionStatus()== SessionState.LOGGED_ON) {
//this loop is reached when the user has been successfully logined
System.out.println("Login Success");
fw.write("User (" + uName + ") logged in at : " + dateFormat.format("09.05.10") + " \n");
fw.close();
} else {
yahooMessengerSession.reset();
}
}
} catch(Exception e){ }
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new BotGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
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.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JTextField uNameTextField;
private javax.swing.JPasswordField uPassPasswordField;
// End of variables declaration
public void dispatch(FireEvent fe) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
===========================================================================================
i have to find the error
SEVERE: error during the dispatch of event: FireEvent [org.openymsg.network.event.SessionListEvent to:null from:null message:null timestamp:0 status:0 list type:Friends size:2 LIST]
java.lang.UnsupportedOperationException: Not supported yet.
at yahoomessangerbot.MySessionListener.dispatch(MySessionListener.java:131)
at org.openymsg.network.EventDispatcher.runEventNOW(EventDispatcher.java:133)
at org.openymsg.network.EventDispatcher.run(EventDispatcher.java:114)
http://jymsg9.sourceforge.net/docs/ymsg/network/Session.html
This will help u..

Categories

Resources