How do I get the GidBagLayout location from my buttons?

How could I get the location of my JButtons (or JLabels) in my GridBoxLayout?
I've tried all that I could think of..
here some code sample
JButton button;
    GridBagLayout layout = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
        c.weightx = 0.5;
        c.weighty = 0.5;
        c.insets = new Insets(20,20,20,20);
        button = new JButton("Button 1");
        c.gridx = 0;
        c.gridy = 0;
          button.addActionListener(this);
        centerPanel.add(button, c);
        button = new JButton("Button 2");
        c.gridx = 1;
        c.gridy = 0;
          button.addActionListener(this);
        centerPanel.add(button, c);
          System.out.println(((JButton)e.getSource()).getText());
          System.out.println(layout.getLayoutOrigin());
          //System.out.println(((JButton)e.getSource()).);
          //System.out.println(((JButton)e.getSource()).location());
          System.out.println(e.getSource().toString());
          System.out.println(layout.getLayoutDimensions());
//          System.out.println(layout.lookupConstraints(((JButton)e.getSource())));
          System.out.println(layout.getLayoutOrigin());I think that's all you experts would need..
what I'm trying to do is when I click on one of them buttons then fetch where that buttons is (in the GridBagLayout, not absolute) and be able to add a new button to the side or under the clicked button..
the System.out.println(layout.getLayoutOrigin()); does seem to give me x=0 and y=0 but that's the same with all buttons so it isn't good enough

I think that's all you experts would need..You would think wrong. We need a SSCCE to see exactly
what you are doing.
see http://homepage1.nifty.com/algafield/sscce.html,
In general component don't have a size or location
until the GUI is visible. That is the layout manager
isn't invoked until the component is displayed.
If you want to add a component after the component
you click on then you would probably need to use the
add(...) method that takes a position variable as a
parameter.yes I think I need to use the add(..) and a position variable
the problem is that I need to get the position variable from the other component so I could put the new component at the right position
as for the SSCCE I do think that I do that..
but for some extra information
I'm writing an applet.. and I use a borderlayout for some component.. in the center panel I've placed a GridBagLayout and some buttons.. I've given the buttons an ActionListener and now I want to get the location in the GridBag of the Button I've pressed
so when I click one of them buttons I want to know it's location.. as in X=1, Y=2 or something
so I could place the new button on X=1, Y=3

Similar Messages

  • How can i get the all values from the Property file to Hashtable?

    how can i get the all values from the Property file to Hashtable?
    ok,consider my property file name is pro.PROPERTIES
    and it contain
    8326=sun developer
    4306=sun java developer
    3943=java developer
    how can i get the all keys & values from the pro.PROPERTIES to hashtable
    plz help guys..............

    The Properties class is already a subclass of Hashtable. So if you have a Properties object, you already have a Hashtable. So all you need to do is the first part of that:Properties props = new Properties();
    InputStream is = new FileInputStream("tivoli.properties");
    props.load(is);

  • How can I get the "text" field from the actionEvent.getSource() ?

    I have some sample code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.ArrayList;
    public class JFrameTester{
         public static void main( String[] args ) {
              JFrame f = new JFrame("JFrame");
              f.setSize( 500, 500 );
              ArrayList < JButton > buttonsArr = new ArrayList < JButton > ();
              buttonsArr.add( new JButton( "first" ) );
              buttonsArr.add( new JButton( "second" ) );
              buttonsArr.add( new JButton( "third" ) );
              MyListener myListener = new MyListener();
              ( (JButton) buttonsArr.get( 0 ) ).addActionListener( myListener );
              ( (JButton) buttonsArr.get( 1 ) ).addActionListener( myListener );
              ( (JButton) buttonsArr.get( 2 ) ).addActionListener( myListener );
              JPanel panel = new JPanel();
              panel.add( buttonsArr.get( 0 ) );
              panel.add( buttonsArr.get( 1 ) );
              panel.add( buttonsArr.get( 2 ) );
              f.getContentPane().add( BorderLayout.CENTER, panel );
              f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
              f.setVisible( true );
         public static class MyListener  implements ActionListener{
              public MyListener() {}
              public void actionPerformed( ActionEvent e ) {
                   System.out.println( "hi!! " + e.getSource() );
                   // I need to know a title of the button (which was clicked)...
    }The output of the code is something like this:
    hi! javax.swing.JButton[,140,5,60x25,alignmentX=0.0,alignmentY=0.5,
    border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1ebcda2d,
    flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,
    disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,
    right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,
    rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=first,defaultCapable=true]
    I need this: "first" (from this part: "text=first" of the output above).
    Does anyone know how can I get the "text" field from the e.getSource() ?

    System.out.println( "hi!! " + ( (JButton) e.getSource() ).getText() );I think the problem is solved..If your need is to know the text of the button, yes.
    In a real-world application, no.
    In a RW application, a typical need is merely to know the "logical role" of the button (i.e., the button that validates the form, regardless of whether its text is "OK" or "Save", "Go",...). Text tends to vary much more than the structure of the UI over time.
    In this case you can get the source's name (+getName()+), which will be the name that you've set to the button at UI construction time. Or you can compare the source for equality with either button ( +if evt.getSource()==okButton) {...}+ ).
    All in all, I think the best solution is: don't use the same ActionListener for more than one action (+i.e.+ don't add the same ActionListener to all your buttons, which leads to a big if-then-else series in your actionPerformed() ).
    Eventually, if you're listening to a single button's actions, whose text change over time (e.g. "pause"/"resume" in a VCR bar), I still think it's a bad idea to rely on the text of the button - instead, this text corresponds to a logical state (resp. playing/paused), it is more maintainable to base your logic on the state - which is more resilient to the evolutions of the UI (e.g. if you happen to use 2 toggle buttons instead of one single play/pause button).

  • How can i get the max rpm from cpu fan??

    how can i get the max rpm from my cpu cooler regarding that i don't have a speed controller attached to the cpu fan ,,, ??  please make things clear for me
    thanks

    if you have not reduced it by using a speed controller either hardware or software or by the 7v mod its already going as fast as it will at 12 v dc you cannot speed it up only slow it down
    need more air get a different cooler/fan

  • How can I get the edited pictures from the thumbnails to full size?

    I upgraded to iPhoto 11, the thumbnail photos show my previous edits, but when I click on the photo to make it bigger, it reverts back to the unedited, original picture.  How can I get the edited pictures from the thumbnails to full size?

    Verify you are using iPhoto11 ver 9.5
    if not run the >Software update or check your apps folder and make sure the correct iPhoto is launched,  not an older ver.

  • How can I get the XML structure from a flat structure?

    Hi all,
    in my XI SP 12 I use a JMS adapter to read information using the WebSphereMQ transport protocol.
    The structure that I receive have this format:
    <Name_A.KeyFieldValue><Name_A.fieldName_A1_Value>...<Name_A.fieldName_AN_Value>
    <NumberRecordType_B><NumberRecordType_c>
    <Name_B.KeyFieldValue><Name_B.fieldName_B1_Value>...<Name_B.fieldName_BN_Value>
    <Name_B.KeyFieldValue><Name_B.fieldName_B1_Value>...<Name_B.fieldName_BN_Value>
    <Name_C.KeyFieldValue><Name_C.fieldName_C1_Value>...<Name_C.fieldName_CN_Value>
    <Name_C.KeyFieldValue><Name_C.fieldName_C1_Value>...<Name_C.fieldName_CN_Value>
    the problem is that in this structure each line is not separated by a carriage return or a comma, I have all the information in a single line:
    <Name_A.KeyFieldValue><Name_A.fieldName_A1_Value>...<Name_A.fieldName_AN_Value><NumberRecordType_B><NumberRecordType_c><Name_B.KeyFieldValue><Name_B.fieldName_B1_Value>...<Name_B.fieldName_BN_Value>...<Name_B.KeyFieldValue><Name_B.fieldName_B1_Value>...<Name_B.fieldName_BN_Value><Name_C.KeyFieldValue><Name_C.fieldName_C1_Value>...<Name_C.fieldName_CN_Value>...<Name_C.KeyFieldValue><Name_C.fieldName_C1_Value>...<Name_C.fieldName_CN_Value>
    and the customer don't want to insert a line separator.
    Then, the question is:
    How can I get the XML structure from this structure?
    If possible, I don't want to develop new Module and add it in the JMS Module Sequence.
    PS I have already read the article "How to Use the Content Conversion Module with the XI 3 J2EE JMS Adapter.pdf" and it doesn't seem to help me.
    Best Regards,
    Paolo

    To get context parameters from your web.xml file you can simply get the ActionServlet object from an implementing action object class. In the perform (or execute) method make the following call.
    ServletContext context = getServlet().getServletContext();
    String tempContextVar =
    context.getInitParameter("<your context param >");

  • How can i get the source code from java concurrent program in R12

    Hi 2 all,
    How can i get the source code from java concurrent program in R12? like , "AP Turnover Report" is java concurrent program, i need to get its source code to know its logic. how can i get its source code not the XML template?
    Regards,
    Zulqarnain

    user570667 wrote:
    Hi 2 all,
    How can i get the source code from java concurrent program in R12? like , "AP Turnover Report" is java concurrent program, i need to get its source code to know its logic. how can i get its source code not the XML template?
    Regards,
    ZulqarnainDid you see old threads for similar topic/discussion? -- https://forums.oracle.com/forums/search.jspa?threadID=&q=Java+AND+Concurrent+AND+Source+AND+Code&objID=c3&dateRange=all&userID=&numResults=15&rankBy=10001
    Thanks,
    Hussein

  • How can I get the data back from my game

    How can I get the data back from minecraft if I deleted the app and bought with a different Apple ID

    No, they are tied to the ID that purchased them, and cannot be transferred to anyone else.

  • HT201269 My computer crashed and had to be restored to factory settings. My ipad has my ituned library. how do i get the music collection from the ipad to the computer?

    my computer crashed and had to be restored to factory settings losing ituness collection of music. My ipad has my itunes library. how do i get the music collection from the ipad to the computer?

    You should be able to copy iTunes purchases over to your computer's iTunes via File > Devices > Transfer Purchases. For music from other sources (e.g. copied from CDs) you will need a third-party program to copy them back from your iPad - there are some listed half-way down this page which should work : https://discussions.apple.com/docs/DOC-3991

  • How do I get the MAC address from my iPad?Thanksss.

    How do I get the MAC address from my iPad?Thanksss.

    Settings -> General -> About -> Wi-Fi Address
    Regards.

  • How do i get the trial version from text off my movie

    how do i get the trial version from text off my movie

    billwwww
    What version of Premiere Elements is involved and on what computer operating system is it running?
    Do you now have a purchased version of Premiere Elements?
    You can not remove the Adobe watermark from an export from the tryout, be it an export of a file saved to the computer hard drive
    or a burn to disc product.
    But, you can remove the Adobe watermark from a project file (.prel) from the tryout. To do that, you open the project file from the tryout
    in the purchased version. Then there are two possible scenarios
    1. You go to Timeline Menu/Delete Rendered Files - the Adobe watermark is gone
    or
    2. You do nothing after you open the tryout project file in the purchased Premiere Elements. Many of us, including myself, have found
    that opening the tryout project in the purchased program causes the watermark to disappear spontaneously.
    Please let us know which works for you.
    Thank you.
    ATR

  • How do I get the return value from an executable?

    I'm running an external executable from dbms_scheduler but how do I get the exit code from the program run or any screen output?

    Hi,
    In the additional_info column of schedulerjob_run_details there should be something like
    job of type EXECUTABLE failed with exit code: Operation not permitted
    "Operation not permitted" is the error code (as translated by the errors header file for that platform).
    In releases 10.2 and up there may also be a line in the additional_info
    STANDARD_ERROR="Error text here"
    This gives the standard error messages output by the application. There is no way to get the standard output of the application.
    Hope this helps,
    Ravi.

  • Trying to setup wifi, how do I get the mac address from ipod touch 5th gen?

    trying to setup wifi, how do I get the mac address from ipod touch 5th gen?  cannot get to the setting screen as it is trying to connect to itunes.

    If yo are setting up the iPod vai wifi you have to connect to a network that does not use MAC address filtering.
    After it is set up yu can get the MAC from Settings>General>About>WiFi Address

  • I have bought an iPad 2 recently but when i open itunes, i only have 3 buttons. (downloads, itunes U, podcasts) how do i get the music, music videos etc. buttons?

    I have bought an iPad 2 recently but when i open itunes, i only have 3 buttons. (downloads, itunes U, podcasts) how do i get the music, music videos etc. buttons?

    Reinstall iOS via iTunes on your computer.

  • How can I get the example fonts from the font file Programmatically?

    Hi Friends,
             I am doing one mac application for the fonts management. Now I would like to Preview the Font in the NSTextView.  This is my coding for the textView.
        NSTextView *text3=[[NSTextView alloc]initWithFrame:NSMakeRect(250,500,450,30)];
        [text3 setString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZ"];
        [text3 setFont:[NSFont fontWithName:@"GangofThree" size:40]];
        [[[self window] contentView] addSubview:text3];
    Because I have set the string in English for the NSTextView I can't able to view the other languages characters.I got the character set of the font file by using this code.
       NSCharacterSet *characterset = (NSCharacterSet *) CTFontCopyCharacterSet (fontRef);
    This Font is greek font.Now My question is how can I able to get the greek characters from this characterset.

    Your text string would certainly have to be in Greek itself, not Latin.  No normal font just translates Latin letters to Greek, they use other codepoints reserved for Greek letters.

Maybe you are looking for

  • Add Image in ABAP Report

    Hi All, I want add image in ABAP Report. Please let me know how it can be done? Regards, Jagdish More

  • Why passing string from applet to jsp doesn't work?

    Hi,all: I have a application requires applet to get client side info, then pass this "info"--string to the JSP. Applet code: try{      URL url = new URL(getCodeBase(),"test.jsp? java.version=1.2.2&java.vendor=Sun);      URLConnection conn = url.openC

  • Sorting based on independently provided list

    Is there a way that, when provided with a text file (or xml, etc.) containing filename information, that a script could find specific images within a folder and tag, rate or color code those files if they share the same filenamebase? Can Bridge or Ph

  • On my iPhone I have duplicates of photos in moments after upgrading to ios7

    On my iPhone I have duplicates of photos in moments after upgrading to ios7

  • Movement 201  consumption demand

    HI all SAP Expert, I have a question about use movement 201. I create demand for some ROH material using strategy 10 in material master the aim is to planned demand for sales. this is no usal process. When I create demand for those ROH material  the