Results 1 to 4 of 4
  1. #1
    syon is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default ActionListener on a button with several lines

    Heya, to start.. sry that the code i will put aint pasted with the "java code" (showing lines and etc), but after looking around a bit didnt see what to write to make it appear.

    I wanted to make a Button that i could define its size and make the several words appear in different lines, from what i searched, a way to do it would be using labbels.

    my solution was:

    public class MultiLineButton extends JButton {

    public MultiLineButton(String label1, String label2) {
    this.setLayout(new GridLayout(2, 1));
    add(new JLabel(label1));
    add(new JLabel(label2));
    }
    }

    using gridlayout so the lines dont appear at the extremes like it would with borderlayout.north/south.

    My problem starts on how can i add a actionListner on a button wich text is customized?
    the previus situations where i used actionListner was for a JMenuBar where i would type the text shown in the ( e.getActionCommand().equals(xxxx) ) and it activated.

    i have seen that you can use Button.getText() to recieve its text, i tried adding on my MultiLineButton:

    private String label1;
    private String label2;

    public String getText() { return label1 + " " + label2; }

    doing this changes the appearence of my buttons to:

    ex: Add (label1)
    null null
    Category (label2)


    would appreciate if someone could point what im doing wrong/missing or tell me some new stuff i should look into for what i intend to do.

    cumps

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

    Default Re: ActionListener on a button with several lines

    One possible solution: give the JButton its own actionCommand via the setActionCommand(String text) method. Another solution: give the button its own anonymous ActionListener.

  3. #3
    syon is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default Re: ActionListener on a button with several lines

    thank you for the answer, i wanted to something similar to the first solution you mentioned.. but from what i saw on the api, thought it wasnt possible.. will work on it now.

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

    Default Re: ActionListener on a button with several lines

    You're welcome. The key is that the actionCommand String and the JButton's text are distinct. Yes they're set to the same value if you construct the JButton with a String, but you can easily change the actionCommand property to whatever String you like.

Similar Threads

  1. Replies: 3
    Last Post: 10-25-2011, 06:29 PM
  2. ActionListener for JButton after changing Button to JButton
    By ravi.joshi53 in forum Java Applets
    Replies: 2
    Last Post: 10-07-2011, 07:35 AM
  3. Problem w/ ActionListener on a button
    By qwertyuiop23 in forum AWT / Swing
    Replies: 2
    Last Post: 11-02-2009, 06:25 AM
  4. Replies: 3
    Last Post: 03-26-2009, 10:31 PM
  5. Replies: 8
    Last Post: 03-09-2009, 11:32 AM

Posting Permissions

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