JPanel on CardLayout Swing Java - java

I am new to Java and I am making a navigation through CardLayout in Swing. Basically I have two buttons on a JFrame and when I click on one button it should go to card 1 where I have kept two buttons on JPanel, card1 and when I click on another button it should go to card 2 where I have kept a JTextField on panel card2. But it is not happening.
Can anyone fix it?
My code is as below.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CardLayoutTest extends JFrame implements ActionListener {
JFrame f;
JButton b;
JButton c;
JPanel panel;
JPanel cards;
JPanel card1;
JPanel card2;
CardLayout card;
Container pane;
final String card1Text = "One";
final String card2Text = "Two";
CardLayoutTest() {
}
public void passBtn() {
f=new JFrame("Card Layout Test");
card1 = new JPanel();
card1.add(new JButton("Button 1 - Card 1"));
card1.add(new JButton("Button 2 - Card 1"));
card1.setBackground(new Color(255,0,0));
card2 = new JPanel();
card2.add(new JTextField("TextField on Card 2", 20));
card2.setBackground(new Color(0,255,0));
//Create the panel that contains the "cards".
cards = new JPanel(new CardLayout());
cards.add(card1, card1Text);
cards.add(card2, card2Text);
b = new JButton("Page 1");
b.setBounds(50,50,70,30);
b.setBackground(Color.red);
c = new JButton("Page 2");
c.setBounds(50,80,70,30);
c.setBackground(Color.blue);
pane = f.getContentPane();
pane.add(cards, BorderLayout.CENTER);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
card.show(card1, card1Text);
}
});
c.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
card.show(card2, card2Text);
}
});
f.add(b); f.add(c); f.add(panel); f.add(cards);
}
public static void main(String[] args) {
new CardLayoutTest();
}
}

Try:
card = new CardLayout();
cards = new JPanel(card);
instead of cards = new JPanel(new CardLayout());
UPDATE
public class CardLayoutTest extends JFrame {
JButton b;
JButton c;
JPanel cards;
JPanel card1;
JPanel card2;
CardLayout card;
final String card1Text = "One";
final String card2Text = "Two";
CardLayoutTest() {
super();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
passBtn();
}
public void passBtn() {
card1 = new JPanel();
card1.setBackground(new Color(255,0,0));
card2 = new JPanel();
card2.add(new JTextField("TextField on Card 2", 20));
card2.setBackground(new Color(0, 255, 0));
//Create the panel that contains the "cards".
card = new CardLayout();
cards = new JPanel(card);
cards.add(card1, card1Text);
cards.add(card2, card2Text);
b = new JButton("Page 1");
b.setBounds(50, 50, 70, 30);
b.setBackground(Color.red);
card1.add(b);
c = new JButton("Page 2");
c.setBounds(50, 80, 70, 30);
c.setBackground(Color.blue);
card1.add(c);
add(cards);
card.show(cards, card1Text);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
card.show(cards, card1Text);
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
c.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
card.show(cards, card2Text);
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
}
public static void main(String[] args) {
CardLayoutTest cardLayoutTest = new CardLayoutTest();
cardLayoutTest.setVisible(true);
}
}

Related

Moving method after JButton selection

When I select the first option and hit select the JFrame stays as is and I want it to close and move into the next method being called and open a different JFrame. Can anyone see the problem? I can't figure out where I am going wrong
public class GUI extends JFrame
{
public static void main(String args [])
{
final JFrame frame = new JFrame("Choose an option");
frame.setSize(350, 180);
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(0, 1, 0, 0));
final JRadioButton sb = new JRadioButton("Student");
final JRadioButton lb = new JRadioButton("Lecturer");
final JRadioButton cdb = new JRadioButton("Course Director");
final JRadioButton ab = new JRadioButton("Admin");
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(sb);
buttonGroup.add(lb);
buttonGroup.add(cdb);
buttonGroup.add(ab);
JPanel panel = new JPanel();
panel.add(sb);
panel.add(lb);
panel.add(cdb);
panel.add(ab);
frame.getContentPane().add(panel);
panel.setLayout(new GridLayout(0, 1, 0, 0));
JButton select = new JButton("Select");
JButton cancel = new JButton("Cancel");
select.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(sb.isSelected())
{
frame.dispose();
StudentGUI();
}
else if(lb.isSelected())
System.out.println("Lecturer");
else if(cdb.isSelected())
System.out.println("Course Director");
else if(ab.isSelected())
System.out.println("Admin");
}
});
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
JPanel panel2 = new JPanel();
panel2.add(select);
panel2.add(cancel);
panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 0));
frame.getContentPane().add(panel2);
frame.setVisible(true);
}
public static void StudentGUI()
{
JFrame frame1 = new JFrame("Input Username");
frame1.setSize(350, 180);
frame1.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
JTextField tf = new JTextField("Input username here");
JButton submit = new JButton("Submit");
JPanel panel1 = new JPanel();
panel1.add(tf);
panel1.add(submit);
frame1.getContentPane().add(panel1);
}
You forgot to set frame1.setVisible(true); in your StudentGUI method, and you never close the window in the first method (use yourframe.dispose()).
So try:
public void actionPerformed(ActionEvent e)
{
if(sb.isSelected())
StudentGUI();
else if(lb.isSelected())
System.out.println("Lecturer");
else if(cdb.isSelected())
System.out.println("Course Director");
else if(ab.isSelected())
System.out.println("Admin");
yourframe.dispose();//don't know your frame variable
}
public static void StudentGUI()
{
JFrame frame1 = new JFrame("Input Username");
frame1.setSize(350, 180);
frame1.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
frame1.setVisible(true);
//code omitted
}

Trying to use cards layout, but cant make show work

im trying to to use cards layout, and i have 2 buttons at the the top that supoose to change the card but for some reason it wont work, the next method works but the show or first\last doesnt, ofcourse i cant use next, cause i want a specific card for every button, here is my code:
cards = new CardLayout();
cardPanel = new JPanel();
cardPanel.setLayout(cards);
cards.show(cardPanel, "gapas");
JPanel firstCard = new JPanel();
firstCard.setBackground(Color.WHITE);;
JPanel secondCard = new JPanel();
secondCard.setBackground(Color.blue);
cardPanel.add(firstCard, "kalam");
cardPanel.add(secondCard, "gapan");
guiFrame.add(tabsPanel,BorderLayout.NORTH);
guiFrame.add(cardPanel,BorderLayout.CENTER);
guiFrame.setVisible(true);
}
ActionListener action = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().matches("kalam")){
cards.show(cardPanel,"kalam");
System.out.println("kalam");
}
else{
cards.show(cardPanel, "gapas");
System.out.println("gapas");
}
}
};
I think you want something like this.
public class TestCard extends JFrame implements ActionListener {
CardLayout cards;
JPanel cardPanel, tabsPanel;
JButton b1, b2;
public TestCard() {
b1= new JButton("kalam");
b2= new JButton("gapas");
tabsPanel = new JPanel();
cards = new CardLayout();
cardPanel = new JPanel();
cardPanel.setLayout(cards);
JPanel firstCard = new JPanel();
firstCard.setBackground(Color.WHITE);
JPanel secondCard = new JPanel();
secondCard.setBackground(Color.blue);
cardPanel.add(firstCard, "kalam");
cardPanel.add(secondCard, "gapas");
tabsPanel.add(b1);
tabsPanel.add(b2);
add(tabsPanel, BorderLayout.NORTH);
add(cardPanel, BorderLayout.CENTER);
b1.addActionListener(this);
b2.addActionListener(this);
setSize(800, 600);
cards.show(cardPanel, "gapas");
setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().matches("kalam")) {
cards.show(cardPanel, "kalam");
System.out.println("kalam");
} else {
cards.show(cardPanel, "gapas");
System.out.println("gapas");
}
}
public static void main(String[] args) {
new TestCard();
}
}
Documentation states:
show (Container parent, String name) Flips to the component that was added to this layout with the specified name, using the addLayoutComponent method.
You add two items:
kalam
gapan
but you try to show: gapas.
Additionally I would add first and then try 2 show.

How to position JButton in a JFrame window?

My program is about a supermarket. I want to position the JButton 'b1' just below JLabel 'l1' and also below JTextField 'jt1'. I want the JButton 'b1' to also be in the centre but below 'l1' and 'jt1'. Below is the delivery() method of my program:
public static void delivery()
{
final JFrame f1 = new JFrame("Name");
f1.setVisible(true);
f1.setSize(600,200);
f1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f1.setLocation(700,450);
JPanel p1 = new JPanel();
final JLabel l1 = new JLabel("Enter your name: ");
final JTextField jt1 = new JTextField(20);
JButton b1 = new JButton("Ok");
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
input1 = jt1.getText();
f1.setVisible(false);
}
});
p1.add(b1);
p1.add(l1);
p1.add(jt1);
f1.add(p1);
final JFrame f2 = new JFrame("Address");
f2.setVisible(true);
f2.setSize(600,200);
f2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f2.setLocation(700,450);
JPanel p2 = new JPanel();
final JLabel l2 = new JLabel("Enter your address: ");
final JTextField jt2 = new JTextField(20);
JButton b2 = new JButton("Ok");
b2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
input2 = jt2.getText();
f2.setVisible(false);
}
});
p2.add(b2);
p2.add(l2);
p2.add(jt2);
f2.add(p2);
}
}
You can use multiple JPanels to get close to what you want:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MyGui {
public static void delivery()
{
JFrame f1 = new JFrame("Name");
f1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f1.setBounds(200, 100, 500, 300);
Container cpane = f1.getContentPane();
JPanel p1 = new JPanel();
p1.setLayout(new BoxLayout(p1, BoxLayout.LINE_AXIS)); //Horizontal
JLabel l1 = new JLabel("Enter your name: ");
JTextField jt1 = new JTextField(20);
jt1.setMaximumSize( jt1.getPreferredSize() );
p1.add(l1);
p1.add(jt1);
JPanel p2 = new JPanel();
p2.setLayout(new BoxLayout(p2, BoxLayout.PAGE_AXIS)); //Vertical
p2.add(p1);
JButton b1 = new JButton("Ok");
p2.add(b1);
cpane.add(p2);
f1.setVisible(true);
}
}
public class SwingProg {
private static void createAndShowGUI() {
MyGui.delivery();
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Use the GridBagLayout Class. It's for custom designing using Constraints.

Can't get CardLayout/button ActionListeners to Work

I know it's something to do with how I've set it up and the actionlistener not being correctly set to the frame or something but I just can't get my hear around it. If someone could point me in the right direction I'd be much obliged. Sorry for noob question.
Here's what I have:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main implements ActionListener {
JPanel cardHolder;
public static final String HOME_CARD = "Home";
public static final String BLUE_PANEL = "Blue Panel";
public static final String RED_PANEL = "Red Panel";
public static final String ORANGE_PANEL = "Orange Panel";
public static JButton home = new JButton("Home");
public static JButton bluePanel = new JButton("Blue Card");
public static JButton redPanel = new JButton("Red Panel");
public static JButton orangePanel = new JButton("Orange Panel");
public static JPanel createCardHolderPanel() {
JPanel cardHolder = new JPanel(new CardLayout());
cardHolder.setBorder(BorderFactory.createTitledBorder("Card Holder Panel"));
cardHolder.add(createHomeCard(), HOME_CARD);
cardHolder.add(createBluePanel(), BLUE_PANEL);
cardHolder.add(createRedPanel(), RED_PANEL);
cardHolder.add(createOrangePanel(), ORANGE_PANEL);
return cardHolder;
}
private static JPanel createOrangePanel() {
JPanel orangePanel = new JPanel();
orangePanel.setBackground(Color.orange);
return orangePanel;
}
private static Component createRedPanel() {
JPanel redPanel = new JPanel();
redPanel.setBackground(Color.red);
return redPanel;
}
private static Component createBluePanel() {
JPanel bluePanel = new JPanel();
bluePanel.setBackground(Color.blue);
return bluePanel;
}
private static Component createHomeCard() {
JPanel homePanel = new JPanel();
homePanel.setBackground(Color.GRAY);
return homePanel;
}
public static JPanel createButtonPanel() {
JPanel buttonPanel = new JPanel(new GridLayout(4, 0, 5, 5));
buttonPanel.setBorder(BorderFactory.createTitledBorder("Button Panel"));
buttonPanel.add(home);
buttonPanel.add(bluePanel);
buttonPanel.add(redPanel);
buttonPanel.add(orangePanel);
return buttonPanel;
}
public static JPanel createContentPane() {
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setBorder(BorderFactory.createTitledBorder("Main Content Pane"));
contentPane.setBackground(Color.WHITE);
contentPane.setPreferredSize(new Dimension(499, 288));
contentPane.add(createButtonPanel(), BorderLayout.WEST);
contentPane.add(createCardHolderPanel(),BorderLayout.CENTER);
return contentPane;
}
public static JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu file = new JMenu("File");
JMenu users = new JMenu("Users");
JMenu options = new JMenu("Options");
JMenu help = new JMenu("Help");
menuBar.add(file);
menuBar.add(users);
menuBar.add(options);
menuBar.add(help);
return menuBar;
}
public static void createAndShowGUI() {
JFrame frame = new JFrame("Simple CardLayout Program");
frame.setContentPane(createContentPane());
frame.setJMenuBar(createMenuBar());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == home) {
CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
cardLayout.show(cardHolder, HOME_CARD);
}
if (e.getSource() == bluePanel) {
CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
cardLayout.show(cardHolder, BLUE_PANEL);
}
if (e.getSource() == redPanel) {
CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
cardLayout.show(cardHolder, RED_PANEL);
}
if (e.getSource() == orangePanel) {
CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
cardLayout.show(cardHolder, ORANGE_PANEL);
}
}
}
Others have suggested listening to the buttons; in addition:
Prefer the lowest accessibility consistent with use, e.g. private rather than public.
Don't make everything static.
Use static for immutable constants used throughout the class.
Use class variables rather than static members for content.
Don't repeat your self, e.g. initialize cardLayout just once in your actionPerformed)().
Use parameters rather than separate methods for each color, e.g.
private JPanel createColorPanel(Color color) {...}
Revised code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main implements ActionListener {
private static final String HOME_CARD = "Home";
private static final String BLUE_PANEL = "Blue Panel";
private static final String RED_PANEL = "Red Panel";
private static final String ORANGE_PANEL = "Orange Panel";
private JPanel cardHolder;
private JButton homeButton = new JButton("Home");
private JButton blueButton = new JButton("Blue Card");
private JButton redButton = new JButton("Red Panel");
private JButton orangeButton = new JButton("Orange Panel");
public JPanel createCardHolderPanel() {
cardHolder = new JPanel(new CardLayout());
cardHolder.setBorder(BorderFactory.createTitledBorder("Card Holder Panel"));
cardHolder.add(createColorPanel(Color.gray), HOME_CARD);
cardHolder.add(createColorPanel(Color.blue), BLUE_PANEL);
cardHolder.add(createColorPanel(Color.red), RED_PANEL);
cardHolder.add(createColorPanel(Color.orange), ORANGE_PANEL);
return cardHolder;
}
private JPanel createColorPanel(Color color) {
JPanel panel = new JPanel();
panel.setBackground(color);
return panel;
}
public JPanel createButtonPanel() {
JPanel buttonPanel = new JPanel(new GridLayout(4, 0, 5, 5));
buttonPanel.setBorder(BorderFactory.createTitledBorder("Button Panel"));
buttonPanel.add(homeButton);
buttonPanel.add(blueButton);
buttonPanel.add(redButton);
buttonPanel.add(orangeButton);
homeButton.addActionListener(this);
blueButton.addActionListener(this);
redButton.addActionListener(this);
orangeButton.addActionListener(this);
return buttonPanel;
}
public JPanel createContentPane() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createTitledBorder("Main Content Pane"));
panel.setBackground(Color.WHITE);
panel.setPreferredSize(new Dimension(499, 288));
panel.add(createButtonPanel(), BorderLayout.WEST);
panel.add(createCardHolderPanel(), BorderLayout.CENTER);
return panel;
}
public JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu file = new JMenu("File");
JMenu users = new JMenu("Users");
JMenu options = new JMenu("Options");
JMenu help = new JMenu("Help");
menuBar.add(file);
menuBar.add(users);
menuBar.add(options);
menuBar.add(help);
return menuBar;
}
#Override
public void actionPerformed(ActionEvent e) {
CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
if (e.getSource() == homeButton) {
cardLayout.show(cardHolder, HOME_CARD);
}
if (e.getSource() == blueButton) {
cardLayout.show(cardHolder, BLUE_PANEL);
}
if (e.getSource() == redButton) {
cardLayout.show(cardHolder, RED_PANEL);
}
if (e.getSource() == orangeButton) {
cardLayout.show(cardHolder, ORANGE_PANEL);
}
}
public static void createAndShowGUI() {
JFrame frame = new JFrame("Simple CardLayout Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Main main = new Main();
frame.setJMenuBar(main.createMenuBar());
frame.add(main.createContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
}
You have not set an action listener for any of the buttons. Implementing the actionPerformed method of the interface does not automatically set an action listener for the buttons. You have to call the addActionListener method which in your case would look like the following since your class implements the ActionListener Interface.
public static JButton home = new JButton("Home").addActionListener(this);
public static JButton bluePanel = new JButton("Blue Card").addActionListener(this);
public static JButton redPanel = new JButton("Red Panel").addActionListener(this);
public static JButton orangePanel = new JButton("Orange Panel").addActionListener(this);

CardLayout issue

I have a card layout, first card is a menu.
I Select the second card, and carry out some action. We'll say add a JTextField by clicking a button. If I return to the menu card, and then go back to the second card, that JTextField I added the first time will still be there.
I want the second card to be as I originally constructed it each time I access it, with the buttons, but without the Textfield.
Make sure the panel you're trying to reset has code that takes it back to its "as it was originally constructed" state. Then, when you process the whatever event that causes you to change cards, call that code to restore the original state before showing the card.
Here is the final sorted out version, to remove the card, after doing changes to it, have a look, use the revalidate() and repaint() thingy as usual :-)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ApplicationBase extends JFrame
{
private JPanel centerPanel;
private int topPanelCount = 0;
private String[] cardNames = {
"Login Window",
"TextField Creation"
};
private TextFieldCreation tfc;
private LoginWindow lw;
private JButton nextButton;
private JButton removeButton;
private ActionListener actionListener = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == nextButton)
{
CardLayout cardLayout = (CardLayout) centerPanel.getLayout();
cardLayout.next(centerPanel);
}
else if (ae.getSource() == removeButton)
{
centerPanel.remove(tfc);
centerPanel.revalidate();
centerPanel.repaint();
tfc = new TextFieldCreation();
tfc.createAndDisplayGUI();
centerPanel.add(tfc, cardNames[1]);
}
}
};
private void createAndDisplayGUI()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationByPlatform(true);
centerPanel = new JPanel();
centerPanel.setLayout(new CardLayout());
lw = new LoginWindow();
lw.createAndDisplayGUI();
centerPanel.add(lw, cardNames[0]);
tfc = new TextFieldCreation();
tfc.createAndDisplayGUI();
centerPanel.add(tfc, cardNames[1]);
JPanel bottomPanel = new JPanel();
removeButton = new JButton("REMOVE");
nextButton = new JButton("NEXT");
removeButton.addActionListener(actionListener);
nextButton.addActionListener(actionListener);
bottomPanel.add(removeButton);
bottomPanel.add(nextButton);
add(centerPanel, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.PAGE_END);
pack();
setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new ApplicationBase().createAndDisplayGUI();
}
});
}
}
class TextFieldCreation extends JPanel
{
private JButton createButton;
private int count = 0;
public void createAndDisplayGUI()
{
final JPanel topPanel = new JPanel();
topPanel.setLayout(new GridLayout(0, 2));
createButton = new JButton("CREATE TEXTFIELD");
createButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
JTextField tfield = new JTextField();
tfield.setActionCommand("JTextField" + count);
topPanel.add(tfield);
topPanel.revalidate();
topPanel.repaint();
}
});
setLayout(new BorderLayout(5, 5));
add(topPanel, BorderLayout.CENTER);
add(createButton, BorderLayout.PAGE_END);
}
}
class LoginWindow extends JPanel
{
private JPanel topPanel;
private JPanel middlePanel;
private JPanel bottomPanel;
public void createAndDisplayGUI()
{
topPanel = new JPanel();
JLabel userLabel = new JLabel("USERNAME : ", JLabel.CENTER);
JTextField userField = new JTextField(20);
topPanel.add(userLabel);
topPanel.add(userField);
middlePanel = new JPanel();
JLabel passLabel = new JLabel("PASSWORD : ", JLabel.CENTER);
JTextField passField = new JTextField(20);
middlePanel.add(passLabel);
middlePanel.add(passField);
bottomPanel = new JPanel();
JButton loginButton = new JButton("LGOIN");
bottomPanel.add(loginButton);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
add(topPanel);
add(middlePanel);
add(bottomPanel);
}
}
If you just wanted to remove the Latest Edit made to the card, try this code :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ApplicationBase extends JFrame
{
private JPanel centerPanel;
private int topPanelCount = 0;
private String[] cardNames = {
"Login Window",
"TextField Creation"
};
private TextFieldCreation tfc;
private LoginWindow lw;
private JButton nextButton;
private JButton removeButton;
private ActionListener actionListener = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == nextButton)
{
CardLayout cardLayout = (CardLayout) centerPanel.getLayout();
cardLayout.next(centerPanel);
}
else if (ae.getSource() == removeButton)
{
TextFieldCreation.topPanel.remove(TextFieldCreation.tfield);
TextFieldCreation.topPanel.revalidate();
TextFieldCreation.topPanel.repaint();
}
}
};
private void createAndDisplayGUI()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationByPlatform(true);
centerPanel = new JPanel();
centerPanel.setLayout(new CardLayout());
lw = new LoginWindow();
lw.createAndDisplayGUI();
centerPanel.add(lw, cardNames[0]);
tfc = new TextFieldCreation();
tfc.createAndDisplayGUI();
centerPanel.add(tfc, cardNames[1]);
JPanel bottomPanel = new JPanel();
removeButton = new JButton("REMOVE");
nextButton = new JButton("NEXT");
removeButton.addActionListener(actionListener);
nextButton.addActionListener(actionListener);
bottomPanel.add(removeButton);
bottomPanel.add(nextButton);
add(centerPanel, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.PAGE_END);
pack();
setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new ApplicationBase().createAndDisplayGUI();
}
});
}
}
class TextFieldCreation extends JPanel
{
private JButton createButton;
private int count = 0;
public static JTextField tfield;
public static JPanel topPanel;
public void createAndDisplayGUI()
{
topPanel = new JPanel();
topPanel.setLayout(new GridLayout(0, 2));
createButton = new JButton("CREATE TEXTFIELD");
createButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
tfield = new JTextField();
tfield.setActionCommand("JTextField" + count);
topPanel.add(tfield);
topPanel.revalidate();
topPanel.repaint();
}
});
setLayout(new BorderLayout(5, 5));
add(topPanel, BorderLayout.CENTER);
add(createButton, BorderLayout.PAGE_END);
}
}
class LoginWindow extends JPanel
{
private JPanel topPanel;
private JPanel middlePanel;
private JPanel bottomPanel;
public void createAndDisplayGUI()
{
topPanel = new JPanel();
JLabel userLabel = new JLabel("USERNAME : ", JLabel.CENTER);
JTextField userField = new JTextField(20);
topPanel.add(userLabel);
topPanel.add(userField);
middlePanel = new JPanel();
JLabel passLabel = new JLabel("PASSWORD : ", JLabel.CENTER);
JTextField passField = new JTextField(20);
middlePanel.add(passLabel);
middlePanel.add(passField);
bottomPanel = new JPanel();
JButton loginButton = new JButton("LGOIN");
bottomPanel.add(loginButton);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
add(topPanel);
add(middlePanel);
add(bottomPanel);
}
}

Categories

Resources