JRadioButton toggle unexpectedly in CardLayout panels - java

So the problem is: I'm trying to make a wizard-like CardLayout. In each card panel, I put back & next JButton and 3 JRadioButton to switch between 3 pages.
Now, when I select the radio buttons the 1st time, it works normally. However, the 2nd time I select the radio button, they don't get selected as expected. For example, I want to select page 2, the card panel 2 does show up, but the radio button 2 state does not show that it's being selected, instead either radio button 1 or 3 gets selected. Button 2 only gets selected when I click it again. Same thing happens when I try to select the others.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutWizardDemo extends JFrame{
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
CardLayoutWizardDemo frame= new CardLayoutWizardDemo();
frame.init();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
private static final long serialVersionUID = 1L;
private JPanel cardPanel, panel1, panel2, panel3, btnPanel1, btnPanel2, btnPanel3;
private JLabel label1, label2, label3;
private JRadioButton step1, step2, step3;
private ButtonGroup bg;
private CardLayout cl = new CardLayout();
private void init(){
setTitle("CardLayoutWizardDemo");
cardPanel = new JPanel();
cardPanel.setLayout(cl);
panel1 = new JPanel(new BorderLayout());
panel2 = new JPanel(new BorderLayout());
panel3 = new JPanel(new BorderLayout());
label1 = new JLabel("label 1");
label2 = new JLabel("label 2");
label3 = new JLabel("label 3");
panel1.add(label1, BorderLayout.NORTH);
panel2.add(label2, BorderLayout.NORTH);
panel3.add(label3, BorderLayout.NORTH);
btnPanel1 = new JPanel();
btnPanel2 = new JPanel();
btnPanel3 = new JPanel();
btnPanel1.setName("panel1");
btnPanel2.setName("panel2");
btnPanel3.setName("panel3");
btnPanel1 = initTutBtn(btnPanel1);
btnPanel2 = initTutBtn(btnPanel2);
btnPanel3 = initTutBtn(btnPanel3);
panel1.add(btnPanel1, BorderLayout.SOUTH);
panel2.add(btnPanel2, BorderLayout.SOUTH);
panel3.add(btnPanel3, BorderLayout.SOUTH);
cardPanel.add(panel1, "1");
cardPanel.add(panel2,"2");
cardPanel.add(panel3,"3");
getContentPane().add(cardPanel, BorderLayout.CENTER);
setPreferredSize(new Dimension(350,500));
setMinimumSize(new Dimension(240,320));
pack();
setLocationByPlatform(true);
}
/**create new set of 3 step buttons
*/
private JPanel initTutBtn(JPanel btnPanel){
btnPanel.setLayout(new BoxLayout(btnPanel,BoxLayout.X_AXIS));
step1 = new JRadioButton();
step2 = new JRadioButton();
step3 = new JRadioButton();
step1.setActionCommand("step1");
step2.setActionCommand("step2");
step3.setActionCommand("step3");
bg = new ButtonGroup();
bg.add(step1);
bg.add(step2);
bg.add(step3);
if (btnPanel.getName().equals("panel1")){
step1.setSelected(true);
}else if (btnPanel.getName().equals("panel2")){
step2.setSelected(true);
}else if (btnPanel.getName().equals("panel3")){
step3.setSelected(true);
}
step1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
goToStep(e);
}
});
step2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
goToStep(e);
}
});
step3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
goToStep(e);
}
});
btnPanel.add(step1);
btnPanel.add(step2);
btnPanel.add(step3);
return btnPanel;
}
private void goToStep(ActionEvent evt){
if(evt.getActionCommand().equals("step1")){
cl.show(cardPanel, "1");
}else if(evt.getActionCommand().equals("step2")){
cl.show(cardPanel, "2");
}else if(evt.getActionCommand().equals("step3")){
cl.show(cardPanel, "3");
}
}
}
I think maybe the problems lie where I create new radio buttons within initButton() and goToStep(ActionEvent evt)but I can't figure out what I did wrong

Related

How can i use multiple panels with different layouts for my gui?

i have problems with nesting 2 different button panels into my main panel. It worked for me with only one 1 panel. Now with my actual nesting i see the last added panel over my main panel and my first button panel. Additionally the "editButton" i added, is not showing up. I got no errors while compiling. It seems to be that i just made a mistake in the arrangement.
Would be very nice if there are any suggestions. Here is my code snippet:
Thanks in advance!
private final String name;
private JLabel catLabel;
private JButton cat1Button;
private JButton cat2Button;
private JButton cat3Button;
private JButton cat4Button;
private JButton editButton;
private JButton deleteButton;
private JButton insertButton;
public JPanel panelMain;
public JPanel panel;
public JPanel panel1;
public ReadwriteQuiz readwrite;
public GUIEdit(String name){
this.name = name;
this.setTitle(name);
this.setLocationRelativeTo(null);
this.setLayout((null));
this.setSize(400,300);
this.setLocation(400,150);
this.setResizable(false);
panelMain = new JPanel();
panelMain.setLayout(new BoxLayout(panelMain, BoxLayout.Y_AXIS));
panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.setBackground(Color.lightGray);
panel1 = new JPanel();
panel1.setLayout(new BoxLayout(panel1, BoxLayout.X_AXIS));
panel1.setBackground(Color.ORANGE);
panelMain.add(panel);
panelMain.add(panel1);
catLabel= new JLabel("Willkommen zum Quizzeln ! \n Wählen Sie Ihre Kategorie");
catLabel.setBounds(90,10,260,40);
panel.add(catLabel);
cat1Button = new JButton("Kategorie 1");
cat1Button.setBounds(52,90,120,40);
panel.add(cat1Button);
cat1Button.addActionListener((e) -> {
try {
readwrite.readFile("kategorie1.txt");
} catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
}
});
cat2Button = new JButton("Kategorie 2");
cat2Button.setBounds(220,90,120,40);
panel.add(cat2Button);
cat3Button = new JButton("Kategorie 3");
cat3Button.setBounds(52,160,120,40);
panel.add(cat3Button);
cat4Button = new JButton("Kategorie 4");
cat4Button.setBounds(220,160,120,40);
panel.add(cat4Button);
editButton = new JButton("Frage editieren");
editButton.setBounds(52,400,120,40);
panel1.add(editButton);
this.add(panelMain);
this.add(panel);
this.add(panel1);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
#Override
public void actionPerformed(ActionEvent e) {
}
You have one main panel and you have already added the other nested panels to the main panel
so you only add that main panel to the parent panel
but you're adding the main panel ( including the other 2 ) and again adding the 1st panel and again adding the 2nd panel
so
this.add(panelMain);
should be enough
Full class
public class app {
private JTextField textField1;
private JPanel panel;
private JLabel label;
private JLabel catLabel;
private JButton cat1Button;
private JButton cat2Button;
private JButton cat3Button;
private JButton cat4Button;
private JButton editButton;
private JButton deleteButton;
private JButton insertButton;
public JPanel panelMain;
public JPanel panel2;
public JPanel panel1;
public app(JFrame frame) {
panelMain = new JPanel();
panelMain.setLayout(new BoxLayout(panelMain, BoxLayout.Y_AXIS));
panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
panel2.setBackground(Color.lightGray);
panel1 = new JPanel();
panel1.setLayout(new BoxLayout(panel1, BoxLayout.X_AXIS));
panel1.setBackground(Color.ORANGE);
panelMain.add(panel2);
panelMain.add(panel1);
catLabel= new JLabel("Willkommen zum Quizzeln ! \n Wählen Sie Ihre Kategorie");
catLabel.setBounds(90,10,260,40);
panel2.add(catLabel);
cat1Button = new JButton("Kategorie 1");
cat1Button.setBounds(52,90,120,40);
panel2.add(cat1Button);
cat2Button = new JButton("Kategorie 2");
cat2Button.setBounds(220,90,120,40);
panel2.add(cat2Button);
cat3Button = new JButton("Kategorie 3");
cat3Button.setBounds(52,160,120,40);
panel2.add(cat3Button);
cat4Button = new JButton("Kategorie 4");
cat4Button.setBounds(220,160,120,40);
panel2.add(cat4Button);
editButton = new JButton("Frage editieren");
editButton.setBounds(52,400,120,40);
panel1.add(editButton);
frame.add(panelMain);
}
public static void main(String[] args) {
JFrame fram = new JFrame("app");
new app(fram);
fram.pack();
fram.setVisible(true);
}
}

Java Button disappears after clicking

I'm trying to add a button using an JOptionPane in another button, but after I clicked the original button just keeps disappearing.
I've tried adding the JPanel manually instead of using 'handPanelNPC.getHandPanel()' by iterating through the handPanel.buttons but it stil wont work. I've checked the ArrayList size and it is already inserted properly.
LayoutTesting.java
public class LayoutTesting extends JFrame{
HandPanel handPanelNPC = new HandPanel();
public LayoutTesting(HandPanel handPanel, int type){
Container pane = getContentPane();
if (type==1){
handPanelNPC = handPanel;
}
pane.setLayout(new BorderLayout());
pane.add(handPanelNPC.getHandPanel(), BorderLayout.NORTH);
validate();
repaint();
}
public LayoutTesting(){
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
pane.add(handPanelNPC.getHandPanel(), BorderLayout.NORTH);
}
}
HandPanel.java
public class HandPanel implements ActionListener{
JFrame frame = null;
JPanel panel = new JPanel();
ArrayList<JButton> buttons = new ArrayList<JButton>();
public HandPanel(){
addNewButton();
panel.setLayout(new FlowLayout());
panel.add(buttons.get(0));
}
public JComponent getHandPanel(){
panel = new JPanel();
for(int i=0; i<buttons.size(); i++){
JButton button = buttons.get(i);
panel.add(button);
}
return panel;
}
public void addNewButton(){
JButton button = new JButton();
button.setPreferredSize(new Dimension(40,58));
button.addActionListener(this);
buttons.add(button);
}
public void actionPerformed(ActionEvent e) {
String[] options = {"Summon", "Set", "Add Card"};
int messageType = JOptionPane.QUESTION_MESSAGE;
int code = JOptionPane.showOptionDialog(
frame,
"What would you like to do?",
"Card Name",
0, messageType, null,
options, options[1]);
if (code==2){
addNewButton();
LayoutTesting frame = new LayoutTesting(this, 1);
}
}
}
Main.java
public class Main extends JFrame{
public static void main(String[] args){
LayoutTesting frame = new LayoutTesting();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
}
}
You have too many odd things happening in your code to give a simple answer.
Try debugging your own code by looking at this line in HandPanel.java:
LayoutTesting frame = new LayoutTesting(this, 1);
What does it really do? Now remove that line and the button will not disappear. Now try and work out what that line was doing, and why the button disappeared.
Also 'panel.add(buttons.get(0));' does nothing because there is never a button in the array when you make that call (You add the button afterwards in another method).
Here is a rough working demo that lets you add as many new cards as you want to the first frame, and each card has a button that will let you summon a new card.
public class LayoutTesting extends JFrame{
Container pane;
//Add card when button is pressed:
public void addCard(){
Container card = new JPanel();
card.setBackground(Color.red);
JButton newButton = addNewButton();
newButton.setBackground(Color.red);
card.add(newButton);
pane.add(card);
revalidate();
repaint();
}
//Setup window and add button:
public LayoutTesting(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setVisible(true);
setLayout(new FlowLayout());
pane = new JPanel();
pane.setLayout(new GridBagLayout());
JButton firstButton = addNewButton();
firstButton.setBackground(Color.green);
add(pane);
add(firstButton);
}
//Create button and action listener:
public JButton addNewButton(){
JButton button = new JButton();
button.setPreferredSize(new Dimension(40, 58));
button.addActionListener(new java.awt.event.ActionListener(){
#Override
public void actionPerformed(java.awt.event.ActionEvent evt){
buttonAction(evt);
}
});
return button;
}
//Action fer each button:
public void buttonAction(ActionEvent e) {
String[] options = {"Summon", "Set", "Add Card"};
int messageType = JOptionPane.QUESTION_MESSAGE;
int code = JOptionPane.showOptionDialog(
this,
"What would you like to do?",
"Card Name",
0, messageType, null,
options, options[1]);
if (code==2){
addCard();
}
}
}

BorderLayout not working JFrame

For some reason I can't get the BorderLayout to set the way it's supposed to. Just would like to know where I'm going wrong.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ColorFactory extends JFrame
{
final int width = 500;
final int height = 300;
private JPanel buttonPanel;
private JPanel radioButtonPanel;
private JLabel msgChangeColor;
public ColorFactory()
{
setTitle("Color Factory");
setSize(width, height);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
createTopPanel();
add(buttonPanel, BorderLayout.NORTH);
createBottomPanel();
add(radioButtonPanel, BorderLayout.SOUTH);
msgChangeColor = new JLabel("Top buttons change the panel color and bottom radio buttons change the text color.");
add(msgChangeColor, BorderLayout.CENTER);
pack();
}
private void createTopPanel()
{
buttonPanel = new JPanel();
setLayout(new FlowLayout());
JButton redButton = new JButton("Red");
redButton.setBackground(Color.RED);
redButton.addActionListener(new ButtonListener());
redButton.setActionCommand("R");
JButton orangeButton = new JButton("Orange");
orangeButton.setBackground(Color.ORANGE);
orangeButton.addActionListener(new ButtonListener());
orangeButton.setActionCommand("O");
JButton yellowButton = new JButton("Yellow");
yellowButton.setBackground(Color.YELLOW);
yellowButton.addActionListener(new ButtonListener());
yellowButton.setActionCommand("Y");
buttonPanel.add(redButton);
buttonPanel.add(orangeButton);
buttonPanel.add(yellowButton);
}
private void createBottomPanel()
{
radioButtonPanel = new JPanel();
setLayout(new FlowLayout());
JRadioButton greenRadioButton = new JRadioButton("Green");
greenRadioButton.setBackground(Color.GREEN);
greenRadioButton.addActionListener(new RadioButtonListener());
greenRadioButton.setActionCommand("G");
JButton blueRadioButton = new JButton("Blue");
blueRadioButton.setBackground(Color.BLUE);
blueRadioButton.addActionListener(new RadioButtonListener());
blueRadioButton.setActionCommand("B");
JButton cyanRadioButton = new JButton("Cyan");
cyanRadioButton.setBackground(Color.CYAN);
cyanRadioButton.addActionListener(new RadioButtonListener());
cyanRadioButton.setActionCommand("C");
radioButtonPanel.add(greenRadioButton);
radioButtonPanel.add(blueRadioButton);
radioButtonPanel.add(cyanRadioButton);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String actionColor = e.getActionCommand();
if(actionColor.equals("R"))
{
buttonPanel.setBackground(Color.RED);
radioButtonPanel.setBackground(Color.RED);
}
if(actionColor.equals("O"))
{
buttonPanel.setBackground(Color.ORANGE);
radioButtonPanel.setBackground(Color.ORANGE);
}
if(actionColor.equals("Y"))
{
buttonPanel.setBackground(Color.YELLOW);
radioButtonPanel.setBackground(Color.YELLOW);
}
}
}
private class RadioButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String actionTextColor = e.getActionCommand();
if(actionTextColor.equals("G"))
{
msgChangeColor.setForeground(Color.GREEN);
}
if(actionTextColor.equals("B"))
{
msgChangeColor.setForeground(Color.BLUE);
}
if(actionTextColor.equals("C"))
{
msgChangeColor.setForeground(Color.CYAN);
}
}
}
public static void main(String[] args)
{
ColorFactory run = new ColorFactory();
run.setVisible(true);
}
}
The problem is you are changing the layout manager for the frame when you create your top and bottom panels...
private void createTopPanel() {
buttonPanel = new JPanel();
setLayout(new FlowLayout()); // <--- This is call setLayout on the frame
This is why it's dangerous to...
Extend from something like JFrame directly...
Dynamically build components
It's all to easy to lose context and start effecting components you didn't actually want to...
Another problem (besides the one posted by MadProgrammer) is that you add your components to the JFrame itself.
You should add content to the content pane of the frame which you can get by calling JFrame.getContentPane().
Example:
JFrame f = new JFrame("Test");
Container c = f.getContentPane();
c.add(new JButton("In Center"), BorderLayout.CENTER);
c.add(new JButton("At the Bottom"), BorderLayout.SOUTH);
c.add(new JButton("At the Top"), BorderLayout.NORTH);
c.add(new JButton("On the Left"), BorderLayout.WEST);
c.add(new JButton("On the Right"), BorderLayout.EAST);
You can set/change the content panel by calling JFrame.setContentPane(). The default content panel already has BorderLayout so you don't even need to change it nor to set a new panel.

positioning jpanel at the bottom

I want to position the JPanel that contains the send button and the textfield (and right now uses a flowlayout) at the bottom of the JTextArea (The white area).
How can I achieve this?
public GUI()
{
mainWindow = new JFrame("Chat GUI");
lowerPanel = new JPanel(new FlowLayout());
usersPanel = new JPanel(new GridLayout(GRIDLAYOUT_ROWS, GRIDLAYOUT_COLS));
users = new JList(data);
usersPanel.add(users);
sendButton = new JButton("Send");
textField = new JTextField(TEXTFIELD_WIDTH);
textArea = new JTextArea(TEXTAREA_HEIGHT, TEXTAREA_WIDTH);
textArea.setEditable(false);
}
private void addButtonListener(JButton b) {
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
}
public void createGUI() {
addButtonListener(sendButton);
lowerPanel.add(sendButton);
lowerPanel.add(textField);
mainWindow.add(users);
mainWindow.add(textArea);
mainWindow.add(lowerPanel);
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.setVisible(true);
mainWindow.setLayout(new FlowLayout());
mainWindow.pack();
}
You Will have Layout :
this.add(buttonPanel,BorderLayout.SOUTH);
see this answer

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