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

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.

Related

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;
}

How to Change java Cardlayout from another separate class

Please I have been trying to switch CardLayout from another class (JPanel) which is one of the card on the CardLayout, I have search and made research about this for a very long time but found nothing helpful.
I have a CardLayout and two separate JPanels that I added to the CardLayout, now I want to be able to switch the cards after performing activities on the separate JPanel or separate class, so how do I switch the CardLayout from another class? my code below.
package myApp;
import java.awt.CardLayout;
public class TestmyCard extends javax.swing.JFrame {
/**
* Creates new form TestmyCard
*/
public TestmyCard() {
initComponents();
jPanel1.add(new FirstCard(),"card3");
jPanel1.add(new SecondCard(),"card4");
}
public void chgCard(String nwCard){
CardLayout cl = (CardLayout)(jPanel1.getLayout());
cl.show(jPanel1,nwCard);
}
/**
* 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();
jPanel2 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jPanel3 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setName("Cards");
jPanel1.setLayout(new java.awt.CardLayout());
jPanel2.setName("card2");
jLabel1.setText("second panel");
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(119, 119, 119)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(203, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(140, 140, 140)
.addComponent(jLabel1)
.addContainerGap(92, Short.MAX_VALUE))
);
jPanel1.add(jPanel2, "card2");
jPanel3.setBackground(new java.awt.Color(153, 255, 153));
jButton1.setLabel("First Btn");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setLabel("Second Btn");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jButton2)
.addContainerGap(181, Short.MAX_VALUE))
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
.addContainerGap(27, Short.MAX_VALUE)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addGap(20, 20, 20))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 439, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(37, 37, 37)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
CardLayout cl = (CardLayout)(jPanel1.getLayout());
cl.show(jPanel1,"card3");
//cl.next(jPanel1) ;
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
CardLayout cl = (CardLayout)(jPanel1.getLayout());
cl.show(jPanel1,"card4");
}
/**
* #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(TestmyCard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TestmyCard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TestmyCard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TestmyCard.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 TestmyCard().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
// End of variables declaration
}
FirstCard (separate jpanel)
package myApp;
public class FirstCard extends javax.swing.JPanel {
/**
* Creates new form FirstCard
*/
public FirstCard() {
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() {
jLabel1 = new javax.swing.JLabel();
guName = new javax.swing.JTextField();
guAddrs = new javax.swing.JTextField();
jLabel14 = new javax.swing.JLabel();
jLabel15 = new javax.swing.JLabel();
guOccu = new javax.swing.JTextField();
jLabel16 = new javax.swing.JLabel();
guPhone = new javax.swing.JTextField();
jLabel1.setText("Guarantee Name :");
jLabel14.setText("Address :");
jLabel15.setText("Occupation :");
jLabel16.setText("Phone :");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel14)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(guAddrs, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel16)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(guPhone, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel15)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(guOccu, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(guName, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(228, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(43, 43, 43)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(guName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel15)
.addComponent(guOccu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(30, 30, 30)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel14)
.addComponent(guAddrs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel16)
.addComponent(guPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(135, Short.MAX_VALUE))
);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JTextField guAddrs;
private javax.swing.JTextField guName;
private javax.swing.JTextField guOccu;
private javax.swing.JTextField guPhone;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel14;
private javax.swing.JLabel jLabel15;
private javax.swing.JLabel jLabel16;
// End of variables declaration
}
SecondCard (separate jpanel)
package myApp;
public class SecondCard extends javax.swing.JPanel {
/**
* Creates new form SecondCard
*/
public SecondCard() {
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() {
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jLabel1.setText("this is the second card");
jButton1.setText("SwitchCard");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(120, 120, 120)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
.addComponent(jLabel1))
.addContainerGap(173, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(62, 62, 62)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jButton1)
.addContainerGap(183, Short.MAX_VALUE))
);
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
TestmyCard nc = new TestmyCard();
nc.chgCard("Card2");
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
In your provided code, you never added FirstCard and SecondCard, to the JPanel having layout set to CardLayout. Since what you writing is this :
jPanel1.add(jPanel2, "card2");
here jPanel2 is an instance of JPanel, as you have initialized this in your TestmyClass Class, as :
jPanel2 = new javax.swing.JPanel();
instead I guess what you should be writing is :
jPanel2 = new SecondCard(passPanelWithCardLayoutAsArgument); // So that you can manoeuvre around b/w other JPanels
Here is a small working example for your help :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutExample
{
private JPanel contentPane;
private MyPanel panel1;
private MyPanel panel2;
private MyPanel panel3;
private void displayGUI()
{
JFrame frame = new JFrame("Card Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new CardLayout());
panel1 = new MyPanel(contentPane
, Color.RED.darker().darker());
panel2 = new MyPanel(contentPane
, Color.GREEN.darker().darker());
panel3 = new MyPanel(contentPane
, Color.DARK_GRAY);
contentPane.add(panel1, "Panel 1");
contentPane.add(panel2, "Panel 2");
contentPane.add(panel3, "Panel 3");
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new CardLayoutExample().displayGUI();
}
});
}
}
class MyPanel extends JPanel
{
private JButton jcomp1;
private JPanel contentPane;
private Color backgroundColour;
public MyPanel(JPanel panel, Color c)
{
contentPane = panel;
backgroundColour = c;
setOpaque(true);
setBackground(backgroundColour);
//construct components
jcomp1 = new JButton ("Show New Panel");
jcomp1.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.next(contentPane);
}
});
add(jcomp1);
}
#Override
public Dimension getPreferredSize()
{
return (new Dimension(500, 500));
}
}
LATEST EDIT :
*Using your components and trying to put that into CardLayout, with this code : *
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutExample
{
private JPanel contentPane;
private FirstCard panel1;
private SecondCard panel2;
private void displayGUI()
{
JFrame frame = new JFrame("Card Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new CardLayout());
panel1 = new FirstCard(contentPane);
panel2 = new SecondCard(contentPane);
contentPane.add(panel1, "Panel 1");
contentPane.add(panel2, "Panel 2");
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new CardLayoutExample().displayGUI();
}
});
}
}
class FirstCard extends javax.swing.JPanel
{
private javax.swing.JTextField addField;
private javax.swing.JTextField nameField;
private javax.swing.JTextField occField;
private javax.swing.JTextField phoneField;
private javax.swing.JLabel nameLabel;
private javax.swing.JLabel addLabel;
private javax.swing.JLabel occLabel;
private javax.swing.JLabel phoneLabel;
private JPanel centerPanel;
private JPanel contentPane;
private JButton nextButton;
public FirstCard(JPanel cp)
{
this.contentPane = cp;
initComponents();
}
private void initComponents()
{
setOpaque(true);
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
setBackground(Color.RED);
setLayout(new BorderLayout(5, 5));
nameLabel = new javax.swing.JLabel("Guarantee Name : ");
nameField = new javax.swing.JTextField();
addLabel = new javax.swing.JLabel("Address : ");
addField = new javax.swing.JTextField();
occLabel = new javax.swing.JLabel("Occupation : ");
occField = new javax.swing.JTextField();
phoneLabel = new javax.swing.JLabel("Phone : ");
phoneField = new javax.swing.JTextField();
centerPanel = new JPanel();
nextButton = new JButton("Next");
nextButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent ae)
{
nextButtonAction(ae);
}
});
centerPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
centerPanel.setOpaque(true);
centerPanel.setBackground(Color.WHITE);
centerPanel.setLayout(new GridLayout(0, 2, 5, 5));
centerPanel.add(nameLabel);
centerPanel.add(nameField);
centerPanel.add(addLabel);
centerPanel.add(addField);
centerPanel.add(occLabel);
centerPanel.add(occField);
centerPanel.add(phoneLabel);
centerPanel.add(phoneField);
add(centerPanel, BorderLayout.CENTER);
add(nextButton, BorderLayout.PAGE_END);
}
private void nextButtonAction(ActionEvent ae)
{
CardLayout layout = (CardLayout)contentPane.getLayout();
layout.next(contentPane);
}
}
class SecondCard extends javax.swing.JPanel
{
private javax.swing.JButton nextButton;
private javax.swing.JLabel textLabel;
private JPanel contentPane;
public SecondCard(JPanel cp)
{
contentPane = cp;
initComponents();
}
private void initComponents()
{
setOpaque(true);
setBackground(Color.GREEN.darker().darker());
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
textLabel = new JLabel("this is the second card", JLabel.CENTER);
textLabel.setForeground(Color.WHITE);
nextButton = new javax.swing.JButton();
nextButton.setText("SwitchCard");
nextButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
jButton1ActionPerformed(evt);
}
});
add(textLabel);
add(nextButton);
}
private void jButton1ActionPerformed(ActionEvent evt)
{
CardLayout layout = (CardLayout) contentPane.getLayout();
layout.show(contentPane, "Panel 1");
}
}
Method to clear fields
private void clearFields()
{
Component components[] = centerPanel.getComponents();
for (Component comp : components)
{
if (comp instanceof JTextField)
{
JTextField tfield = (JTextField) comp;
tfield.setText("");
}
else if (comp instanceof JComboBox)
{
JComboBox cbox = (JComboBox) comp;
cbox.setSelectedIndex(0);
}
else if (comp instanceof JRadioButton)
{
JRadioButton rbut = (JRadioButton) comp;
rbut.setSelected(false);
}
}
}
And you will call this inside the actionPerformed() method of the Button, which will take you to the next Card.

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..

Why Java jFrame setResizable() is failling to act?

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

Categories

Resources