Results 1 to 9 of 9
- 11-16-2010, 05:34 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
how to link an object to a JButton
Hi,
I'm making an application that controls a wireless network. When I refresh the network the app creates a JButton for each node detected. Those buttons can be moved arround the screen because there is a image of a map underneath them.
Then thing is that I need that when I push a node button I need to open a new frame with information about that node like its address and some other info.
The problem is that I don't know how I can relate each JButton to each node.
I tried getting the hashCode from the button and saving it in its corresponding node object so when I click the button the app gets its hashCode and looks for a node that have saved the same hashCode but it always returns the same node object which is the one I gave the hashCode of the first Jbutton created. I also tried using the toolTip text and giving each button the address of the Node it corresponds to but it doesn't work. It does the same thing as the hashCode.
this is the code where I create each JButton
Any Ideas?Java Code:public int createNodeButton(Node node) { bnNode = new JButton("", new ImageIcon("xbee.jpg")); bnNode.setBounds(node.getxCord(),node.getyCord(), 57, 50); bnNode.setToolTipText(node.getAddress64().toString()); bnNode.addMouseMotionListener(new DragHandler()); bnNode.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { frNode.setVisible(true); tfNodeMac.setText(connection.getNode(bnNode.getToolTipText()).getAddress64().toString()); // TODO Add actionPerformed when node clicked } }); nodeBtns.add(node.getNodeId(), bnNode); layered.add(bnNode, new Integer(2)); return bnNode.hashCode(); }
Thanks!
-
It's not the JButton that you want to link to your node, it's the button's actions. You can create a class that implements ActionListener or that extends AbstractAction that has a variable that holds this information, and then add the action or actionlistener to the button. Also, you'll more than likely want to display a JDialog not a JFrame when the button is pushed.
- 11-16-2010, 05:59 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
Hi Fubarable,
I understand what you mean but the thing is that every button has the same name and then I add them all to a vector. They have all the same name because they are all created in runtime when the program detects a new node. When they have the same name I don't know how to differentiate which one was clicked.
Since each node have different information I need to know which JButton was clicked so I display the correct data.
-
- 11-16-2010, 06:14 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
Then how do I know which button was clicked? I don't really know what you mean. Could you give me a web or something like that where I could read about it?
I'm my GUI class I have implement the actionlistener like this
But doing that won't help me because of the name thing or how do I do that.Java Code:public void actionPerformed(ActionEvent event) { if (event.getSource() == miSalir) { System.exit(0); } if (event.getSource() == bnRefrescar) { try { taMain.setText(""); connection.getData(cbNodes.getSelectedIndex()); } catch (XBeeException e) { System.out.println(e); // Debug } }
I'm sorry but I'm new to this.
-
You're making a common newbie GUI mistake, having your GUI implement the ActionListener, and I strongly urge you not to do this but instead to keep them separate. Again, create your ActionListener or AbstractAction objects on the fly and let them know which button they're attached to. If I had time I'd show you how in a small program, but I'm busy at work and can't do this.
Last edited by Fubarable; 11-16-2010 at 08:16 PM.
- 11-16-2010, 08:22 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
Well I kind of get what you are telling me but not 100% because I'm not sure how to do it but don't worry I'll read to see if I can find a solution.
I don't expect you to spend more time helping me.
Thank you for everything!
-
Here's a small app
sorry for lack of comments!Java Code:import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class ButtonsOnTheFly extends JPanel { private static final Dimension VIEW_SIZE = new Dimension(200, 400); private int buttonCount = 0; private JButton makeButton = new JButton("Make Button"); private JPanel buttonPanel = new JPanel(); public ButtonsOnTheFly() { makeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { makeButtonActionPerformed(); } }); buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.PAGE_AXIS)); JScrollPane scrollPane = new JScrollPane(buttonPanel); scrollPane.getViewport().setPreferredSize(VIEW_SIZE); add(makeButton); add(scrollPane); } private void makeButtonActionPerformed() { buttonCount++; JButton button = new JButton("Button #" + buttonCount); button.addActionListener(new MyActionListener(buttonCount)); JPanel panel = new JPanel(); panel.add(button); buttonPanel.add(panel); buttonPanel.revalidate(); } private class MyActionListener implements ActionListener { private int buttonNumber; // give this class a field that allows it to know // which button it is. It can be an int, or anything public MyActionListener(int buttonNumber) { this.buttonNumber = buttonNumber; } public void actionPerformed(ActionEvent arg0) { System.out.println("Button number " + buttonNumber + " has been pressed"); } } private static void createGui() { JFrame frame = new JFrame("App"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new ButtonsOnTheFly()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createGui(); } }); } }
- 11-16-2010, 08:42 PM #9
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Operator < cannot be applied to java.lang.Object, Object
By Albert in forum Advanced JavaReplies: 2Last Post: 11-26-2010, 02:12 AM -
Create object of unknown class, based on existing object
By Zack in forum New To JavaReplies: 2Last Post: 06-22-2010, 04:29 AM -
[Paremeters] in a method(Object expA, Object expB){}
By Unsub in forum New To JavaReplies: 2Last Post: 01-29-2010, 02:01 AM -
Link between two JFrame using JButton
By Ravi Ranjan in forum NetBeansReplies: 2Last Post: 06-09-2009, 10:30 PM -
how to pass an arraylist from an object back to the parent object that it was created
By george_a in forum New To JavaReplies: 1Last Post: 03-04-2009, 06:14 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks