-
Array of JButtons?
I'm making a dynamic table of JTextFields and JButtons. The list varies in length each time. Can I have an array of JButtons so that I know which of the buttons is pressed?
I tried it like this:
Code:
JButton [] checkOut = new JButton();
for (int i=0; i<allBooks.size(); ++i)
{
currentBook = (Book)allBooks.get(i);
System.out.println(currentBook);
final JTextField bookName = new JTextField(currentBook.toString());
bookName.setEditable(false);
contentPane.add(bookName);
// if its not checked out adds check out button
if (currentBook.checkedOutBy() == null)
{
checkOut[i].setLabel("Check Out");
checkOut[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if (currentCardholder != null)
{
currentBook.checkOut(currentCardholder);
else
Toolkit.getDefaultToolkit().beep(); // signal error
}
});
contentPane.add(checkOut[i]);
setContentPane(contentPane);
pack();
repaint();
}
Sorry if thats hard to read! Or messed up!
NOMAD
-
Does this code actualy compile? The line JButton [] checkOut = new JButton(); should be JButton [] checkOut = new JButton[numofJButtons]; Also wouldnt a NullPointerException be thrown when the line [i]checkOut.setLabel("Check Out"); is executed?
-
To find out which button was pressed you can either use the
String getActionCommand() method of the java.awt.event. ActionEvent class or the Object getSource() which is found in the java.util.EventObject class.
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class X{
static JLabel jl;
public static void main(String[] args){
JFrame jf = new JFrame();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
jl = new JLabel();
JButton[] jbh = new JButton[]{new JButton("1"), new JButton("2")};
Container c = jf.getContentPane();
for(int i = 0; i < jbh.length; ++i){
p1.add(jbh[i]);
}
for(int i = 0; i < jbh.length; ++i){
jbh[i].addActionListener(new ButtonListener());
}
c.add(p1, BorderLayout.NORTH);
p2.add(jl);
c.add(p2, BorderLayout.SOUTH);
jf.setSize(400,300);
jf.setVisible(true);
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
System.exit(0);
}
});
}
static class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent evt){
jl.setText("Button" + evt.getActionCommand() + " was pressed");
}
}
}
-
Im my previous post the second method i talked about could be applied to the ButtonListener class as follows.....
Code:
static class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent evt){
JButton jb = (JButton) evt.getSource();
jl.setText("Button" + jb.getText() + " was pressed");
}
}
-
Dilenger4:
Thanks for all your help, I through that code up there in a desperate attempt to tell you what was going on, I was late for class as it was and didn't check my posts code. I'm sure it doesn't compile. I'll tried the event.getSource and they all had the same number as there source. Probably cause of the invalid declaration of the JButtons. I'll see what I can do, thanks again for your input!
NOMAD
-
Still lost, I understand your example. But all my buttons have the same name (label) but reference different data. How can I know which of the array of buttons is press when I click it and ActionListener is called?
NOMAD
-
I guess you could create custom JButton's and add in an instance member which would be set to a certain value when each instance of each JButton is created then query the button later on to get the value.
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class XButton extends JButton{
private String whichbutton;
public XButton(){
this("rubberbutton");
}
public XButton(String whichbutton){
this.whichbutton = whichbutton;
}
public XButton(String name, String whichbutton){
super(name);
this.whichbutton = whichbutton;
}
public String getButton(){return whichbutton;}
}
public class X{
static JLabel jl;
public static void main(String[] args){
JButton[] jb = new JButton[3];
jb[0] = new XButton();
jb[1] = new XButton("metalbutton");
jb[2] = new XButton("Cork Button","corkbutton");
JFrame jf = new JFrame();
Container c = jf.getContentPane();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
jl = new JLabel();
for(int i = 0; i < jb.length; ++i){
jb[i].addActionListener(new ButtonListener());
p1.add(jb[i]);
}
p2.add(jl);
c.add(p1,BorderLayout.NORTH);
c.add(p2,BorderLayout.SOUTH);
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
System.exit(0);
}
});
jf.setSize(400,300);
jf.setVisible(true);
}
static class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent evt){
XButton xb = (XButton) evt.getSource() ;
jl.setText("Button " + xb.getButton() + " was pressed");
}
}
}
-
Good idea, thats almost exactly what I did. Since I had a vector of my DTs I added an int to a custom button class. Any ways project worked and is turned it.
Thanks to you for helping me so much!
NOMAD
-