Reputation:
I am having trouble putting my Panel class to my main class. I can't seem to instantiate and register the buttons to the actionPerformed method. This is project is suppose to use a grid layout with 9 input buttons (3 other buttons for enter, space, and a clear) Then display the the input on a JTextArea. I believe I have Panel class set up correctly, but having problems putting together the JButton Array, and then registering it to the actionPerformed method. Any pointers would be greatly appreciated. (Side question, how do you copy and paste code with it being all taken in a code block?)
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class TextButtons extends JFrame implements ActionListener {
private JButton[] buttons;
private JTextArea textArea;
private final int ENTER; //Index of Enter button in buttons
private final int SPACE; //Index of Space button in buttons
private final int CLEAR; //Index of Clear button in buttons
public TextButtons(String title) {
super(title);
JFrame frame = new JFrame("Text Button");
//TODO: instantiate all JButtons, add them to the buttons array,
// and register "this" as the ActionListener for each button.
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton();
//this.buttons.addActionListener(e);
}
ENTER = 9;
SPACE = 10;
CLEAR = 11;
buttons[ENTER] = new JButton("\n");
buttons[SPACE] = new JButton(" ");
buttons[CLEAR] = new JButton("clear");
JTextArea textArea = new JTextArea();
textArea.setEditable(false);
TextButtonsPanel mainPanel = new TextButtonsPanel(buttons, textArea);
this.getContentPane().add(mainPanel);
this.pack();
this.setVisible(true);
}
/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
@Override
public void actionPerformed(ActionEvent e) {
// ??
}
public static void main(String[] args) {
final TextButtons f = new TextButtons("Text Buttons");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
PanelClass
public class TextButtonsPanel extends JPanel {
public TextButtonsPanel(JButton[] buttons, JTextArea textArea) {
int ENTER = 11;
JPanel mainPanel = new JPanel(new GridLayout(4, 3));
JButton b1 = new JButton("A");
JButton b2 = new JButton("B");
JButton b3 = new JButton("C");
JButton b4 = new JButton("1");
JButton b5 = new JButton("2");
JButton b6 = new JButton("3");
JButton b7 = new JButton("X");
JButton b8 = new JButton("Y");
JButton b9 = new JButton("Z");
add(b1);
mainPanel.add(b2);
mainPanel.add(b3);
mainPanel.add(b4);
mainPanel.add(b5);
mainPanel.add(b6);
mainPanel.add(b7);
mainPanel.add(b8);
mainPanel.add(b9);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setPreferredSize(new Dimension(80, 120));
mainPanel.add(scrollPane);
}
}
}
Upvotes: 3
Views: 5271
Reputation: 347334
Your TextButtonsPanel
extends JPanel
, but you are adding everything to JPanel
called mainPanel
which is never begin added to anything.
Because the JButton
s are local variables, you will never be able to attach an external ActionListener
to them. Not sure if this a good or bad thing.
I would create a TextButtonsPane
which contained nothing but buttons, had no other components. All it does is generate events.
I would provide a addActionListener
and removeActionListener
which would simply register the listener against all the buttons contained within it.
This way, the button pane doesn't care what it's being used for, it's just producing events that something else to can use.
You may want to take some time to read through Creating a GUI with Swing
Upvotes: 2
Reputation: 48649
I can't seem to instantiate and register the buttons to the actionPerformed method.
You could organize your code like this:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TextButtonPanel extends JPanel implements ActionListener {
public TextButtonPanel(String[] labels) {
setLayout(new GridLayout(0, 3)); //same as self.setLayout(..)
JButton button;
for (String label : labels) {
button = new JButton(label);
button.addActionListener(this);
add(button); //same as self.add(button)
}
}
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
System.out.println(button.getText());
}
}
class MyGui {
public MyGui() {
JFrame frame = new JFrame("Name");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setBounds(200, 100, 500, 300);
Container cpane = frame.getContentPane();
String[] labels = {"A", "B", "C", "D"};
TextButtonPanel panel = new TextButtonPanel(labels);
cpane.add(panel);
frame.setVisible(true);
}
}
public class SwingProg {
private static void createAndShowGUI() {
new MyGui();
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Upvotes: 2