J. G.
J. G.

Reputation: 23

Updating cards in CardLayout

I need to make GUI which saves the user's input using a linked list and let him/her "browse" through the saved data. I made a simpler version of the code which I included below. My problem is how to "update" the buttons in the browse card after the user entered and saved a text.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class sampleDemo extends JPanel {
    NodeSample newNode = new NodeSample();
    SampleSave s = new SampleSave();
    public static final String CARD_ADD = "Card Add";
    public static final String CARD_BROWSE = "Card Browse";

    public static CardLayout cardLayout = new CardLayout();
    public static JPanel card = new JPanel(cardLayout);
    public static sampleDemo sd = new sampleDemo();

public sampleDemo() {
    WindowAdd wina = new WindowAdd();
    card.add(wina, CARD_ADD);
    WindowBrowse winb = new WindowBrowse();
    card.add(winb, CARD_BROWSE);

    setLayout(new BorderLayout());
    add(card, BorderLayout.PAGE_END);
}

public static void createAndShowGUI() {
    JPanel buttonPanel = new  JPanel();
    JButton addButton = new JButton("Add");
    JButton browseButton = new JButton("Browse");

    addButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            cardLayout.show(card, "Card Add");
        }
    });
    browseButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            cardLayout.show(card, "Card Browse");
        }
    });

    buttonPanel.add(addButton);
    buttonPanel.add(browseButton);
    JFrame frame = new JFrame("Sample Demo");
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.getContentPane().add(buttonPanel, BorderLayout.PAGE_END);
    frame.getContentPane().add(sd);
    frame.setSize(300, 200);
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
}

class WindowAdd extends JPanel {
    public ActionListener action;

    public WindowAdd() {
        init();
    }
    public void init() {
        JTextField textField = new JTextField(10);
        NodeSample newNode = new NodeSample();
        JPanel content = new JPanel(new FlowLayout());
        JButton saveButton = new JButton("Save");
        saveButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                newNode.textContent = textField.getText();
                s.insert(newNode);
            }
        });
        content.add(textField);
        content.add(saveButton);
        textField.setText(null);

        add(content);
    }

}

class WindowBrowse extends JPanel {
    public WindowBrowse() {
        init();
    }
    public void init() {
        setLayout(new GridLayout(1, 3));
        JButton a = new JButton();
        JButton b = new JButton();
        JButton c = new JButton();
        try {
            a = new JButton(s.head.textContent);
            b = new JButton(s.head.next.textContent);
            c = new JButton(s.head.next.next.textContent);
        }catch (NullPointerException e) {
        //
        }
        add(a);
        add(b);
        add(c);
    }
}

class NodeSample {
    String textContent;
    NodeSample next;
}

class SampleSave {
    NodeSample head;
    NodeSample tail;

    public void insert(NodeSample add) {
        if(head == null){
            head = add;
            tail = add;
        }else {
            add.next = head;
            head = add;
        }
    }
}
}

Upvotes: 0

Views: 138

Answers (1)

Jonah
Jonah

Reputation: 1003

In order to change the text on a JButton use something similar to this in your action listener

btn.setText(textfield.getText());

This will change the text of whatever button to the text entered in the textfield

Upvotes: 2

Related Questions