Results 1 to 9 of 9
  1. #1
    Twistx77 is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Unhappy 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


    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();
    	}
    Any Ideas?

    Thanks!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    Twistx77 is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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.
    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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Twistx77 View Post
    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.
    Name doesn't matter and in fact doesn't exist at run-time. What matters is that you need to add an actionlistener to them and this listener can be created on the fly and with the information you need.

  5. #5
    Twistx77 is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default

    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
    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
    			}
    
    		}
    But doing that won't help me because of the name thing or how do I do that.

    I'm sorry but I'm new to this.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Twistx77 View Post
    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?

    But doing that won't help me because of the name thing or how do I do that.

    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 09:16 PM.

  7. #7
    Twistx77 is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default

    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!

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Here's a small app
    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();
                }
            });
        }
    }
    sorry for lack of comments!

  9. #9
    Twistx77 is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default

    WoW Thank you so much!!

    That's really helpful! I'll check it carefully so I can do the same in mine.

    Once again, thank you, you've been very helpful.

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2010, 03:12 AM
  2. Replies: 2
    Last Post: 06-22-2010, 05:29 AM
  3. Replies: 2
    Last Post: 01-29-2010, 03:01 AM
  4. Link between two JFrame using JButton
    By Ravi Ranjan in forum NetBeans
    Replies: 2
    Last Post: 06-09-2009, 11:30 PM
  5. Replies: 1
    Last Post: 03-04-2009, 07:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •