create JPanel after click the button /Java - 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;
}

Related

Frame resets when label or text change

I am currently having some problem in some javaFrame application. It is a simple game where the ball is running around and there is one button which starts and stops the ball. The problem is whenever i change the text of a label or a button while the ball is running the frame resets(the ball is resetting where it was in the first place) and i don't want that to happen. I created the frame and the panel by dragging from the side(i did not write code to create frame and panels). I don't know all the rules in this site and i don't know if it is permissible but i could not find any ways to show this to you so i linked 2 short youtube videos at the end so you can see what it is(i apologize for that).
What i did in frame:
Created 2 panels.
Added a button to panel1(upper one) and added ball image on top of it.
Added 2 buttons on panel2(lower one) which are Start/Stop and changeLabel.
My NewJFrame Class:
package view;
import controller.BallAction;
public class NewJFrame extends javax.swing.JFrame {
private BallAction ba;
private boolean isBallRolling = false;
private int labelAsciiValue = 65;
public NewJFrame() {
initComponents();
ba = new BallAction(ball);
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
ball = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();
jButton2 = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Ball Game");
ball.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/top.png"))); // NOI18N
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(36, 36, 36)
.addComponent(ball, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(515, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(29, 29, 29)
.addComponent(ball, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(291, Short.MAX_VALUE))
);
jButton2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton2.setText("Start");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton1.setText("Change Label");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel1.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
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(35, 35, 35)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(85, 85, 85)
.addComponent(jButton1)
.addGap(107, 107, 107)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(23, 23, 23)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addContainerGap(35, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
//Start-Stop Button
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
if(isBallRolling){
ba.stop();
jButton2.setText("Start");
isBallRolling = false;
}
else{
ba.start();
jButton2.setText("Stop");
isBallRolling = true;
}
}
//Change Label Button
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jLabel1.setText(String.valueOf((char)(labelAsciiValue++)));
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.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 NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton ball;
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;
// End of variables declaration
}
My BallAction Class:
package controller;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.Timer;
public class BallAction {
private Timer time;
private final ClockListener clock = new ClockListener();
private JButton ball;
public BallAction(JButton ball){
this.ball=ball;
time = new Timer(20,clock);
}
public void start(){
time.start();
}
public void stop(){
time.stop();
}
//If ball.x <= panel.x go right else ball.x = 0.
public void update(){
if(ball.getLocation().x<=ball.getParent().getSize().width-100)
ball.setLocation(ball.getLocation().x+5,ball.getLocation().y);
else{
ball.setLocation(0,ball.getLocation().y);
}
}
private class ClockListener implements ActionListener{
#Override
public void actionPerformed(ActionEvent e) {
update();
}
}
}
Frame without changing anything : https://www.youtube.com/watch?v=yC_8tyUmal0
Frame with changing the text : https://www.youtube.com/watch?v=zGvhCnRRTtw
EDIT:
If i create the frame on my own it works fine.
Why is that please help me thank you.
My Own Frame:
package ballgame0;
import controller.BallAction;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class BallGame0 {
private static boolean isBallRunning = false;
private static int labelAsciiValue = 65;
public static void main(String[] args) {
JFrame frame = new JFrame("New Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000,800);
frame.setVisible(true);
JPanel panel= new JPanel(null);
frame.add(panel);
JLabel changeLabel = new JLabel();
changeLabel.setSize(100,50);
changeLabel.setLocation(500,650);
changeLabel.setFont(new Font("Tahoma", 0, 36));
JButton buttonLabel = new JButton("Change Label");
buttonLabel.setSize(150,40);
buttonLabel.setLocation(300,650);
JButton button = new JButton("asd");
button.setSize(70,70);
JButton button1 = new JButton("Start");
button1.setSize(100,60);
button1.setLocation(100,650);
panel.add(changeLabel);
panel.add(buttonLabel);
panel.add(button);
panel.add(button1);
BallAction ba = new BallAction(button);
ActionListener alis = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
changeLabel.setText(String.valueOf((char)labelAsciiValue++));
}
};
buttonLabel.addActionListener(alis);
ActionListener al = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(isBallRunning){
ba.stop();
button1.setText("Start");
isBallRunning = false;
}else{
ba.start();
button1.setText("Stop");
isBallRunning = true;
}
}
};
button1.addActionListener(al);
}
}

I am getting the error when I run applet in HTML page even after I have signed all the jar files

Both the videoplayer and HTML contains external jar files in its library which are jna-3.5.2.jar,platform-3.5.2.jar and vlcj-2.4.1.jar.
Both videoplayer and HTML page are in different projects in Netbeans and I have included the external jar files mentioned above in both the projects.
This is the code of videoplayer class.
package Player;
import com.sun.jna.NativeLibrary;
import java.awt.BorderLayout;
import java.awt.Color;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
public class videoplayer extends javax.swing.JApplet{
public EmbeddedMediaPlayerComponent mediaPlayerComponent;
#Override
public void init() {
try{
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(),"./src");
NativeLibrary.addSearchPath(RuntimeUtil.getPluginsDirectoryName(),"./src");
setBackground(Color.RED);
setSize(500,500);
mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
}
catch(Exception e){
System.out.println(e);
}
/* 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(videoplayer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(videoplayer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(videoplayer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(videoplayer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the applet */
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
// JApplet jp=new JApplet();
//jp.setLocation(100,100);
//jp.setSize(500,500);
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* This method is called from within the init() method 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() {
jFrame1 = new javax.swing.JFrame();
jFrame2 = new javax.swing.JFrame();
jToolBar1 = new javax.swing.JToolBar();
videopanel = new javax.swing.JPanel();
lowerpanel = new javax.swing.JPanel();
Play = new javax.swing.JButton();
Pause = new javax.swing.JButton();
Forward = new javax.swing.JButton();
Backward = new javax.swing.JButton();
Snapshot = new javax.swing.JButton();
Volume = new javax.swing.JSlider();
upperpanel = new javax.swing.JPanel();
exitfullscreen = new javax.swing.JButton();
Open = new javax.swing.JButton();
Stop = new javax.swing.JButton();
fullscreen = new javax.swing.JButton();
javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
jFrame1.getContentPane().setLayout(jFrame1Layout);
jFrame1Layout.setHorizontalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jFrame1Layout.setVerticalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
javax.swing.GroupLayout jFrame2Layout = new javax.swing.GroupLayout(jFrame2.getContentPane());
jFrame2.getContentPane().setLayout(jFrame2Layout);
jFrame2Layout.setHorizontalGroup(
jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jFrame2Layout.setVerticalGroup(
jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
jToolBar1.setRollover(true);
setBackground(new java.awt.Color(51, 51, 51));
addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
formPropertyChange(evt);
}
});
videopanel.setBackground(new java.awt.Color(51, 51, 51));
lowerpanel.setBackground(new java.awt.Color(0, 0, 0));
Play.setBackground(new java.awt.Color(0, 0, 0));
Play.setForeground(new java.awt.Color(255, 0, 0));
Play.setText("Play");
Play.setToolTipText("");
Play.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
PlayActionPerformed(evt);
}
});
Pause.setBackground(new java.awt.Color(0, 0, 0));
Pause.setForeground(new java.awt.Color(255, 0, 0));
Pause.setText("Pause");
Pause.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
PauseActionPerformed(evt);
}
});
Forward.setBackground(new java.awt.Color(0, 0, 0));
Forward.setForeground(new java.awt.Color(255, 0, 0));
Forward.setText("Fwd");
Forward.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ForwardActionPerformed(evt);
}
});
Backward.setBackground(new java.awt.Color(0, 0, 0));
Backward.setForeground(new java.awt.Color(255, 0, 0));
Backward.setText("Bwd");
Backward.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BackwardActionPerformed(evt);
}
});
Snapshot.setBackground(new java.awt.Color(0, 0, 0));
Snapshot.setForeground(new java.awt.Color(255, 0, 0));
Snapshot.setText("Snapshot");
Snapshot.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SnapshotActionPerformed(evt);
}
});
Volume.setBackground(new java.awt.Color(0, 0, 0));
Volume.setForeground(new java.awt.Color(255, 51, 0));
Volume.setMajorTickSpacing(2);
Volume.setMaximum(200);
Volume.setMinorTickSpacing(1);
Volume.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
VolumeStateChanged(evt);
}
});
javax.swing.GroupLayout lowerpanelLayout = new javax.swing.GroupLayout(lowerpanel);
lowerpanel.setLayout(lowerpanelLayout);
lowerpanelLayout.setHorizontalGroup(
lowerpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(lowerpanelLayout.createSequentialGroup()
.addComponent(Play, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Pause)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Forward)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Backward)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Snapshot)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Volume, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(308, Short.MAX_VALUE))
);
lowerpanelLayout.setVerticalGroup(
lowerpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(lowerpanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(lowerpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(Volume, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(lowerpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Play)
.addComponent(Pause)
.addComponent(Forward)
.addComponent(Backward)
.addComponent(Snapshot)))
.addContainerGap(14, Short.MAX_VALUE))
);
upperpanel.setBackground(new java.awt.Color(0, 0, 0));
upperpanel.setForeground(new java.awt.Color(255, 255, 255));
exitfullscreen.setBackground(new java.awt.Color(0, 0, 0));
exitfullscreen.setForeground(new java.awt.Color(255, 0, 0));
exitfullscreen.setText("Exit Full Screen");
exitfullscreen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitfullscreenActionPerformed(evt);
}
});
javax.swing.GroupLayout upperpanelLayout = new javax.swing.GroupLayout(upperpanel);
upperpanel.setLayout(upperpanelLayout);
upperpanelLayout.setHorizontalGroup(
upperpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, upperpanelLayout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(exitfullscreen)
.addContainerGap())
);
upperpanelLayout.setVerticalGroup(
upperpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, upperpanelLayout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(exitfullscreen))
);
Open.setBackground(new java.awt.Color(0, 0, 0));
Open.setForeground(new java.awt.Color(255, 0, 0));
Open.setText("Open");
Open.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
OpenActionPerformed(evt);
}
});
Stop.setBackground(new java.awt.Color(0, 0, 0));
Stop.setForeground(new java.awt.Color(255, 0, 0));
Stop.setText("Stop");
Stop.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
StopActionPerformed(evt);
}
});
fullscreen.setBackground(new java.awt.Color(0, 0, 0));
fullscreen.setForeground(new java.awt.Color(255, 0, 0));
fullscreen.setText("Full Screen");
fullscreen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fullscreenActionPerformed(evt);
}
});
javax.swing.GroupLayout videopanelLayout = new javax.swing.GroupLayout(videopanel);
videopanel.setLayout(videopanelLayout);
videopanelLayout.setHorizontalGroup(
videopanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lowerpanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(videopanelLayout.createSequentialGroup()
.addGap(2, 2, 2)
.addComponent(Open)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Stop, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(14, 14, 14)
.addComponent(fullscreen)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(upperpanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
videopanelLayout.setVerticalGroup(
videopanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, videopanelLayout.createSequentialGroup()
.addGroup(videopanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(upperpanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(videopanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Open)
.addComponent(Stop)
.addComponent(fullscreen)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 439, Short.MAX_VALUE)
.addComponent(lowerpanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
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(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(videopanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(videopanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
}// </editor-fold>
private void OpenActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
BorderLayout blmain=new BorderLayout();
BorderLayout blvideo=new BorderLayout();
setLayout(blmain);
videopanel.setLayout(blvideo);
videopanel.add(mediaPlayerComponent,blmain.CENTER);
videopanel.add(upperpanel,blmain.NORTH);
videopanel.add(lowerpanel,blmain.SOUTH);
videopanel.validate();
mediaPlayerComponent.getMediaPlayer().prepareMedia(".\\src\\abc.mp4");
mediaPlayerComponent.getMediaPlayer().setVolume(50);
}
catch(Exception e){
System.out.println(e);
}
}
private void PlayActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
mediaPlayerComponent.getMediaPlayer().play();
}
private void PauseActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
mediaPlayerComponent.getMediaPlayer().pause();
}
private void ForwardActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
mediaPlayerComponent.getMediaPlayer().skip(10000);
}
private void BackwardActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
mediaPlayerComponent.getMediaPlayer().skip(-10000);
}
private void SnapshotActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
mediaPlayerComponent.getMediaPlayer().setSnapshotDirectory("D:\\");
mediaPlayerComponent.getMediaPlayer().saveSnapshot();
}
private void VolumeStateChanged(javax.swing.event.ChangeEvent evt) {
// TODO add your handling code here:
mediaPlayerComponent.getMediaPlayer().setVolume(Volume.getValue());
}
private void fullscreenActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
videopanel.setSize(1366,650);
setSize(videopanel.getSize());
videopanel.revalidate();
upperpanel.revalidate();
lowerpanel.revalidate();
}
private void StopActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
mediaPlayerComponent.getMediaPlayer().stop();
videopanel.remove(mediaPlayerComponent);
}
private void formPropertyChange(java.beans.PropertyChangeEvent evt) {
// TODO add your handling code here:
}
private void exitfullscreenActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
setSize(500,500);
videopanel.setSize(500,500);
}
/* */
// Variables declaration - do not modify
private javax.swing.JButton Backward;
private javax.swing.JButton Forward;
private javax.swing.JButton Open;
private javax.swing.JButton Pause;
private javax.swing.JButton Play;
private javax.swing.JButton Snapshot;
private javax.swing.JButton Stop;
private javax.swing.JSlider Volume;
private javax.swing.JButton exitfullscreen;
private javax.swing.JButton fullscreen;
private javax.swing.JFrame jFrame1;
private javax.swing.JFrame jFrame2;
private javax.swing.JToolBar jToolBar1;
private javax.swing.JPanel lowerpanel;
private javax.swing.JPanel upperpanel;
private javax.swing.JPanel videopanel;
// End of variables declaration
}
The code of HTML page
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<applet code ="Player.videoplayer" archive="VLCMediaPlayer.jar" height="500" width ="500"></applet>
</body>
</html>
When I run the applet separately it works fine but when I run it on the HTML page and click on any of its button I get the following error :
Exception in thread "AWT-EventQueue-2" java.lang.ExceptionInInitializerError
Caused by: java.security.AccessControlException: access denied ("java.util.PropertyPermission" "jna.tmpdir" "read")

JAVA Swing - tabbed panel setSelectedIndex() on a card panel from another card panel

I have a GUI as shown below. the tabbed pane in the card layout card 1 panel show 1st tab by default. When i navigate to card 2, I would like to know how to make the button there to navigate to card 1 tab 3. I know how to get to card 1 from that button by using getParent(), but I don't know how to show a specific tab I want from there. (Note: card 1 and card 2 are two different JPanel classes, the parent is a JFrame class)
Image: Left side as card 1 with tabbed panel , Right side as card 2
The following is my code:
the main JFrame:
import java.awt.CardLayout;
public class NewJFrame extends javax.swing.JFrame {
public NewJFrame() {
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() {
jbtCard1 = new javax.swing.JButton();
jbtCard2 = new javax.swing.JButton();
cardPane = new javax.swing.JPanel();
jPanelCard11 = new CardLayoutTest.JPanelCard1();
jPanelCard21 = new CardLayoutTest.JPanelCard2();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jbtCard1.setText("Card 1");
jbtCard1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtCard1ActionPerformed(evt);
}
});
jbtCard2.setText("Card 2");
jbtCard2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtCard2ActionPerformed(evt);
}
});
cardPane.setName(""); // NOI18N
cardPane.setLayout(new java.awt.CardLayout());
cardPane.add(jPanelCard11, "card1");
cardPane.add(jPanelCard21, "card2");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(87, 87, 87)
.addComponent(jbtCard1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jbtCard2)
.addGap(80, 80, 80))
.addComponent(cardPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(cardPane, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jbtCard2)
.addComponent(jbtCard1))
.addContainerGap())
);
pack();
}// </editor-fold>
private void jbtCard1ActionPerformed(java.awt.event.ActionEvent evt) {
CardLayout card = (CardLayout) cardPane.getLayout();
card.show(cardPane, "card1");
}
private void jbtCard2ActionPerformed(java.awt.event.ActionEvent evt) {
CardLayout card = (CardLayout) cardPane.getLayout();
card.show(cardPane, "card2");
}
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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.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 NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel cardPane;
private CardLayoutTest.JPanelCard1 jPanelCard11;
private CardLayoutTest.JPanelCard2 jPanelCard21;
private javax.swing.JButton jbtCard1;
private javax.swing.JButton jbtCard2;
// End of variables declaration
}
Card 1 Panel:
public class JPanelCard1 extends javax.swing.JPanel {
public JPanelCard1() {
initComponents();
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTabbedPane1 = new javax.swing.JTabbedPane();
jPanel1 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jPanel3 = new javax.swing.JPanel();
jLabel3 = new javax.swing.JLabel();
jLabel2.setText("card 1 tab 1 ");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(101, 101, 101)
.addComponent(jLabel2)
.addContainerGap(233, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(62, 62, 62)
.addComponent(jLabel2)
.addContainerGap(138, Short.MAX_VALUE))
);
jTabbedPane1.addTab("tab1", jPanel1);
jLabel1.setText("card 1 tab 2");
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(73, 73, 73)
.addComponent(jLabel1)
.addContainerGap(264, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(92, 92, 92)
.addComponent(jLabel1)
.addContainerGap(108, Short.MAX_VALUE))
);
jTabbedPane1.addTab("tab2", jPanel2);
jLabel3.setText("card 1 tab 3");
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(140, 140, 140)
.addComponent(jLabel3)
.addContainerGap(197, Short.MAX_VALUE))
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addGap(71, 71, 71)
.addComponent(jLabel3)
.addContainerGap(129, Short.MAX_VALUE))
);
jTabbedPane1.addTab("tab3", jPanel3);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTabbedPane1)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 58, Short.MAX_VALUE))
);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JTabbedPane jTabbedPane1;
// End of variables declaration
}
Card 2 Panel:
import java.awt.CardLayout;
import javax.swing.JPanel;
public class JPanelCard2 extends javax.swing.JPanel {
public JPanelCard2() {
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() {
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jButton1.setText("go to Card 1 tab 3");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel1.setText("Card 2");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(261, 261, 261)
.addComponent(jButton1))
.addGroup(layout.createSequentialGroup()
.addGap(52, 52, 52)
.addComponent(jLabel1)))
.addContainerGap(18, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(48, 48, 48)
.addComponent(jLabel1)
.addGap(75, 75, 75)
.addComponent(jButton1)
.addContainerGap(140, Short.MAX_VALUE))
);
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//show card 1 from here
CardLayout card = (CardLayout) this.getParent().getLayout();
card.show((JPanel) this.getParent(), "card1");
//card1 shown, but how to show specific tab in the tab pane from here?
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
I think you're looking for JTabbedPane.html.setSelectedIndex(int).
The following isn't the most elegant solution, but it should work without requiring a lot of refactoring.
Add a method in NewJFrame:
public CardLayoutTest.JPanelCard1 getJPanelCard11() {
return jPanelCard11;
}
Add a method in JPanelCard1:
public void setTab(int index) {
jTabbedPane1.setSelectedIndex(index);
}
Call these in JPanelCard2:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//show card 1 from here
CardLayout card = (CardLayout) this.getParent().getLayout();
card.show((JPanel) this.getParent(), "card1");
//card1 shown, show specific tab in the tab pane from here:
NewJFrame parent = (NewJFrame) this.getParent();
((JPanelCard1) parent.getJPanelCard11()).setTab(1);
}

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

Categories

Resources