|
-
Apr 9th, 2003, 03:59 PM
#1
Thread Starter
Addicted Member
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
-
Apr 9th, 2003, 10:29 PM
#2
Dazed Member
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?
-
Apr 9th, 2003, 11:49 PM
#3
Dazed Member
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");
}
}
}
Last edited by Dilenger4; Apr 9th, 2003 at 11:53 PM.
-
Apr 10th, 2003, 12:05 AM
#4
Dazed Member
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");
}
}
-
Apr 10th, 2003, 11:36 AM
#5
Thread Starter
Addicted Member
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
-
Apr 10th, 2003, 12:14 PM
#6
Thread Starter
Addicted Member
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
-
Apr 11th, 2003, 11:58 PM
#7
Dazed Member
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");
}
}
}
Last edited by Dilenger4; Apr 12th, 2003 at 04:10 AM.
-
Apr 12th, 2003, 10:47 AM
#8
Thread Starter
Addicted Member
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
-
Apr 12th, 2003, 02:26 PM
#9
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|