Help:how to use GridBagLayout?

who can help me completing this sample:
I want to add three JButtons into a JFrame(three JButton are in a row), the ratio of their width must keep 2:3:1 whenever the JFrame change to be smaller or bigger
Thanks!!

Thank you for your particular answer!
I completed the code and run it, but the result is not same with I thought:
1.The ratio of their width is not like 2:3:1,the third button is too
short.
2.When I replace "Button b1 = new Button()..." with the following code:
Button b1 = new Button("123456");Button b2 = new Button("12");Button b3 = new
Button("123456789");
the ratio of their width will be changed according to the length of their title,
and the second button will be very short,the third button will be very long.
3.There is some bank room in the right of the third button.
4.The changing smaller of the width of the JFrame will stopped after
arrived at a special width,the width of the JFrame can not changed to zero.
The following is the complete code,can you change the code?Very thank!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GridBagWindow extends JFrame {
    boolean inAnApplet = true;
    public GridBagWindow() {
          Container container=this.getContentPane();
          GridBagLayout gridBag = new GridBagLayout();
          container.setLayout( gridBag );
          //Button b1 = new Button("123456");
          //Button b2 = new Button("12");
          //Button b3 = new Button("123456789");
          Button b1 = new Button();
          Button b2 = new Button();
          Button b3 = new Button();
          GridBagConstraints b1Constraints = new GridBagConstraints();
          b1Constraints.fill = GridBagConstraints.BOTH;
          b1Constraints.insets = new Insets( 0,0,0,0 );
          b1Constraints.anchor = GridBagConstraints.WEST;
          b1Constraints.gridx = 0; b1Constraints.gridy = 0;
          b1Constraints.gridwidth = 1; b1Constraints.gridheight = 1;
          b1Constraints.weightx = .4; b1Constraints.weighty = 0;
          b1Constraints.ipadx = 0; b1Constraints.ipady = 0;
          gridBag.setConstraints( b1, b1Constraints );
          GridBagConstraints b2Constraints = new GridBagConstraints();
          b2Constraints.fill = GridBagConstraints.BOTH;
          b2Constraints.insets = new Insets( 0,0,0,0 );
          b2Constraints.anchor = GridBagConstraints.CENTER;
          b2Constraints.gridx = 1; b2Constraints.gridy = 0;
          b2Constraints.gridwidth = 1; b2Constraints.gridheight = 1;
          b2Constraints.weightx = .5; b2Constraints.weighty = 0;
          b2Constraints.ipadx = 0; b2Constraints.ipady = 0;
          gridBag.setConstraints( b2, b2Constraints );
          GridBagConstraints b3Constraints = new GridBagConstraints();
          b3Constraints.fill = GridBagConstraints.EAST;
          b3Constraints.insets = new Insets( 0,0,0,0 );
          b3Constraints.anchor = GridBagConstraints.WEST;
          b3Constraints.gridx = 2; b3Constraints.gridy = 0;
          b3Constraints.gridwidth = 1; b3Constraints.gridheight = 1;
          b3Constraints.weightx = .1; b3Constraints.weighty = 0;
          b3Constraints.ipadx = 0; b3Constraints.ipady = 0;
          gridBag.setConstraints( b3, b3Constraints );
          container.add( b1 );
          container.add( b2 );
          container.add( b3 );
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                if (inAnApplet) {
                    dispose();
                } else {
                    System.exit(0);
    public static void main(String args[]) {
        GridBagWindow window = new GridBagWindow();
        window.inAnApplet = false;
        window.setTitle("GridBagLayout");
        window.setSize(300,90);
        window.setVisible(true);

Similar Messages

  • Need help how to use itunes card to download music

    Need help how to use itunes card to download music

    If you want to add the iTunes card to your account, then in the iTunes app on the iPad you should be able to scroll to the bottom of the Music tab and there should be a Redeem button - there is more info here : http://support.apple.com/kb/HT1574

  • Help:how to use java.util.jar to zip or unzip a binary file.

    how to use java.util.jar to zip or unzip a binary file or a file contain native code.

    It may help you to know how I add JARs
    1. I open my Project (myProject)
    2. I Mount the JAR to the FileSystem (like mypackages.jar = which includes com.mus.de.myClass.java)
    3. I Mount the File to the FileSystem (like c:\..myfiles..\myProject)
    3.1 I add the File to my Project
    4. I select File | New -> Classes | Main
    4.1 I typed "import com.mus.de.myClass.java" to refer to this package.
    4.2 I called some of the public methods
    thats it
    Andreas

  • How to use GridBagLayout in JlayeredPane.

    I am using JLayered Pane for Dynamically putting the components at Front and at Back, But Now i want to put the components in a specified grid and for that i think i hav to use GridBagLayout... so Plz tell me how can i implement it.

    But Now i want to put the components in a specified grid I would use the [url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]Grid Layout.

  • (Urgent)help: how to use sdk add a gif image into a pdf document

    I had use sdk plus-in add a new button of mine,if I click the button,a gif image will be inserted into the current page of pdf document
    My questions:
    one : When I clicked the button,there was a warnning box said:the image has not enough data.I don't known why?
    two : How can I be sure the position of the image which I inserted? and how to change it's position?
    three:  how to use sdk get the content of the document?

    hi Leonard:
    I do it like you said, but
    Why my image can not display in the pdf.
    code is:
    //====================================================================
    const ASInt32 theImageByteSize = IMG_WIDTH * IMG_HEIGHT;
    char* buff = new char[theImageByteSize];
    PDEImage volatile pdeImage = NULL;
    PDEImageAttrs pdeImageAttrs;
    PDEColorSpace pdeColorSpace;
    ASFixedMatrix imageMatrix;
    memset(&pdeImageAttrs, 0, sizeof(PDEImageAttrs));
    int hdl = _open(ImagePath, _O_RDONLY | _O_BINARY, _S_IWRITE | _S_IREAD);
    if (hdl == -1)
         AVAlertNote("[%s] create fail !!!!") ;
    if (_read(hdl, buff, theImageByteSize) == -1)
         AVAlertNote("read image fail!") ;
    pdeImageAttrs.width = IMG_WIDTH;
    pdeImageAttrs.height = IMG_HEIGHT;
    pdeImageAttrs.intent = ASAtomNull;
    pdeImageAttrs.bitsPerComponent = 8;
    pdeImageAttrs.flags = kPDEImageExternal | kPDEImageIsIndexed;
    pdeImageAttrs.decode[0] = fixedZero;
    pdeImageAttrs.decode[1] = fixedOne;
    pdeImageAttrs.decode[2] = fixedZero;
    pdeImageAttrs.decode[3] = fixedOne;
    pdeImageAttrs.decode[4] = fixedZero;
    pdeImageAttrs.decode[5] = fixedOne;
    ASFixedRect theMediaBox;
    PDPageGetMediaBox( AVPageViewGetPage(pageView), &theMediaBox );
    ASFixed theFixedWidth = (theMediaBox.right - theMediaBox.left);
    ASFixed theFixedHeight = (theMediaBox.top - theMediaBox.bottom);
    imageMatrix.a = ASInt16ToFixed(theFixedWidth);
    imageMatrix.d = ASInt16ToFixed(theFixedHeight);
    imageMatrix.b = imageMatrix.c = fixedZero;
    imageMatrix.h = 0;
    imageMatrix.v = 0;
    const Int32 cPaletteColors = 256;
    PDEIndexedColorData theIndexedData;
    theIndexedData.size = sizeof(theIndexedData);
    theIndexedData.baseCs = PDEColorSpaceCreateFromName(ASAtomFromString( "DeviceRGB"));
    theIndexedData.hival = cPaletteColors - 1;
    unsigned char data[3] = {255, 0, 0};
    theIndexedData.lookup = (char *)data;
    theIndexedData.lookupLen = cPaletteColors * 3;
    PDEColorSpaceStruct theColorData;
    theColorData.indexed = &theIndexedData;
    pdeColorSpace = PDEColorSpaceCreate(ASAtomFromString( "Indexed" ), &theColorData );
    pdeImage = PDEImageCreate(&pdeImageAttrs, sizeof(pdeImageAttrs), &imageMatrix,
                                                0, pdeColorSpace, NULL, NULL, NULL, (unsigned char*)buff, theImageByteSize);
    I want die, I had done this for so many days, My GIF doesn't insert into pdf,
    unsigned char data[3] = {255, 0, 0};
    theIndexedData.lookup = (char *)data;  "
    whether the data set wrong?
    I confused what I do next   

  • Need help: how to use "FileLock"

    Hi,
    I would like to be able to read the file at any time, but when
    write is acquired (assuming the lock is released), no read is allowed
    until the lock for the write is released.
    How to use FileLock to accomplish this scenario?
    Any input is appreciated.
    Thanks,
    Pin

    I would suppose that the first step would be to determine that the OS you are working on actually supports that...from the java docs...
    Platform dependencies
    Whether or not a lock actually prevents another program from accessing the content of the locked region is system-dependent and therefore unspecified.

  • Need help -how to use a GUI to compile a file

    hello peeps,
    I have this problem, let's get directly into it, i have a program that has GUI with a Text Area that im going to copy a text file into it (using JFileChooser FileReader and readLine() ...) so i have radio buttons like (view, compile and execute) and an OK button to get the selected option, what i want to know is how to compile the file that is in the text area, like how to get to the MS-dos console and javac to start. i hope this description is clear, and i'll be very appreciative for any help!

    thanks for your response, but its not working it keeps on throwing IOException, i tried to put a direct string into it like (Runtime.getRuntime().exec("javac MyClass.java"); and same thing happend. Am i missing something? like is there something to add before calling that line of code? i did import Runtime
    ok here is how my code looks like:
       if(compile.isSelected())
                       try
                       //name of the file im pasting into the text area  
                       String filename = file.getName();
                       Runtime.getRuntime().exec("javac" + filename);
                       catch (IOException e)
                         System.out.println("error compiling program");
                   }

  • Pls. help - How to use user defined URL?

    The page users access currently for login is (only over the Intranet)- http://servername/pls/portal30/url/page/pagename
    I need users to be able to point to - http://project.companyname.com/project
    and be taken to the above Login page.
    What needs to be setup (Apache/Portal)??
    I created a Virtual Hosts entry in the httpd.conf file -
    NameVirtualHost 13.xx.xx.xxx
    <VirtualHost 13.xx.xx.xxx>
    ServerName project.companyname.com
    </VirtualHost>
    That did not help. What am I missing?
    Thanks.

    ATP,
    Pls. refer Portal FAQ. Here is how it works
    Use the Apache Redirect directive in the <ORACLE_HOME>/Apache/Apache/conf/httpd.conf file. For example, if your site's full URL is http://mysite.us.oracle.com:80/pls/portal30, you can place the following directive in httpd.conf:
    Redirect /portalhome http://mysite.us.oracle.com:80/pls/portal30
    Then, if you try:
    http://mysite.us.oracle.com/portalhome
    you will be redirected to the original URL. This technique will also work with any valid path that is appended to the URL. For example:
    http://mysite.us.oracle.com/portalhome/url/folder/ONLINE_HELP
    will redirect the user to the Online Help content area.
    null

  • Help: How to use JWindow as Popup

    Hi there,
    I am developing a program & I wanted to design it with a screen like the Mac OS (tiger?) desktop. That is menu (button bar) at the bottom of the screen (the program's screen - Not the OS).
    I thought I should use a JWindow to do it.
    HOW I WOULD LIKE THE POPUP TO BEHAVE:
    - Only appears when a user moves the mouse towards the bottom of the screen.
    - To disappear (hide) when the user clicks somewhere else on the screen [when focus is lost(?)]
    - To change its position relative to the program. That is, if the program is resized etc...
    I NEED YOUR HELP ON
    The design issues I should take into consideration. I mean interfaces I should implement etc...
    Thanks in advance.
    Yours,
    Me

    Okay, sorry for the late reply, I dont go on the net much.
    With the focus issues, it could be very tricky to know when exactly the Window has lost focus cause a component within the window could still have the focus, hence the window would be in focus.
    One way, could be to add a common FocusListener to all the components within the window by using a recursion method, and allow this FocusListener to determine when all the elements within the window have lost focus, so as to call the window hiding function
    public void addFocusListener(Component c, FocusListener fl) {
      c.addFocusListener(fl);
      if(c instanceof Container) {
         Container cn = (Container)c;
         for(int i = 0; i < cn.getComponents().length; i++) {
             addFocusListener(cn.getComponent(i), fl);
    // also the focus listener could be like this
    class FocusChecker implements FocusListener {
       public void focusLost(FocusEvent e) {
          Component source = (Component)e.getSource();
          if(popupWindow.isAncestorOf(source)) {
              if(!popupWindow.isFocussed()) { // check if the window has a focussed component
                 moveWindowIntoHiding();  // call the method to hide to window
    }This is just a general guideline. Think around this to hide the window when it looses focus
    ICE

  • Help: How to use 'CRM_ORDER_MAINTAIN'

    Hi All,
    I want to create a sales order with following requirements.
    1) schedule lines having requesting dates for which vendor cannot deliver.
    2) change the status from open to some other status.
    i this i have to use
    'CRM_ORDER_MAINTAIN". but i want to know will this API takes care of updating ERP system?
    when i change the status will it also update the purchase order in ERP?
    And also give me some code as how to change status and change schedule lines using 'CRM_ORDER_MAINTAIN".
    Thanks in advance,
    Regards,
    Ujwalkumar

    Hi Robin,
    I am facing problem with CRM_ORDER_MAINTAIN.
    I am trying to change the status of service order from New to Shipped.
    Below is my code.Please look into it and let me the issue.
    Thanks.Rushikesh
    REPORT  ZRD1.
    data:IT_STATUS  TYPE  CRMT_STATUS_COMT,
         wa like line of it_status.
    DATA : ls_input_field         TYPE crmt_input_field,
           ls_input_field_names   TYPE crmt_input_field_names,
           lt_input_fields        TYPE crmt_input_field_tab,
           CT_PARTNER_ATTRIBUTES  TYPE CRMT_PARTNER_ATTRIBUTE_COM_TAB,
           l_i_orderadm_h         TYPE CRMT_ORDERADM_H_COMT,
           l_i_orderadm_i         TYPE CRMT_ORDERADM_I_COMT,
           l_i_obj_guids          TYPE CRMT_OBJECT_GUID_TAB,
           ls_i_obj_guids         like line of l_i_obj_guids.
    wa-REF_GUID = '47FDBF975F6100E1E10080000A0630A7'.
    wa-ref_kind = 'B'.
    wa-status = 'E0023'.
    wa-USER_STAT_PROC = 'ZRV_ST01'.
    wa-activate  = 'X'.
    APPEND wa TO it_status.
          ls_input_field-ref_guid = '47FDBF975F6100E1E10080000A0630A7'.
          ls_input_field-ref_kind = 'B'.
          ls_input_field_names-fieldname = 'REF_GUID'.
          INSERT ls_input_field_names INTO TABLE ls_input_field-field_names.
          ls_input_field_names-fieldname = 'REF_KIND'.
          INSERT ls_input_field_names INTO TABLE ls_input_field-field_names.
          ls_input_field_names-fieldname = 'STATUS'.
          INSERT ls_input_field_names INTO TABLE ls_input_field-field_names.
          ls_input_field_names-fieldname = 'USER_STAT_PROC'.
          INSERT ls_input_field_names INTO TABLE ls_input_field-field_names.
          ls_input_field_names-fieldname = 'ACTIVATE'.
          INSERT ls_input_field_names INTO TABLE ls_input_field-field_names.
          INSERT ls_input_field INTO TABLE lt_input_fields.
    *Start of solution1.
    CALL FUNCTION 'CRM_ORDER_MAINTAIN'
    EXPORTING
          IT_STATUS                     = IT_STATUS
    CHANGING
       CT_INPUT_FIELDS                  = lt_INPUT_FIELDS
       ct_orderadm_h                    = l_i_orderadm_h
       ct_orderadm_i                    = l_i_orderadm_i
    EXCEPTIONS
          ERROR_OCCURRED                = 1
          DOCUMENT_LOCKED               = 2
          NO_CHANGE_ALLOWED             = 3
          NO_AUTHORITY                  = 4
          OTHERS                        = 5.
    IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
             WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    ls_i_obj_guids = '47FDBF975F6100E1E10080000A0630A7'.
    append ls_i_obj_guids to l_i_obj_guids.
    CALL FUNCTION 'CRM_ORDER_SAVE'
    EXPORTING
    it_objects_to_save = l_i_obj_guids
    EXCEPTIONS
    document_not_saved = 1.

  • Help : How to use list of values in Variable in ODI

    Dear All,
    I have a multiple values in table, and I would to use these values a a parameter in ODI to retrieve rows based on these values. As I know that we can use single value in variable in ODI.
    Any one can Help me please?

    Can't you use a join between the tables ?
    ODI mostly take advantage of native technologies.
    i.e If my source technology is oracle then i will create a join between the table i.e. MAINTABLE.column = LOVTABLE.COLUMN and also put a filter on LOVTABLE to take required values.
    OR you can write a subquery in the Filter i.e. MAINTABLE.COLUMN in ( Select LOVVALUES from LOVTABLE where LOVTABLE.ATTRIBUTE = 'BUSINESS_UNIT')

  • Please help how to use WITH query in UPDATE

    Hi Experts,
    Please help me.
    Thanks.

    Not sure about your required output. But try using WITH clause query inside SET clause of UPDATE statement.
       UPDATE SALES_REVENUE SR
        SET
                 (SR.TODAY_REV,
                 SR.TODAY_MARGIN,
                 SR.TODAY_UNIT
                 ) =
    (WITH result_sum_temp
    AS(
        SELECT SALE_ID, IN_TEAM, 1 SALE_FLAG, loc, seg,
            SUM(REV * rate) AS sum_rev,
            SUM(MARGIN * rate) AS sum_mar,
            SUM(UNIT) AS SUM_UNIT
        FROM SALES
        WHERE (IGNORED IS NULL OR IGNORED <> 'Y')
        AND SALE_SPOT = 7
        AND SALE_ID = 375
        AND SALE_MODEL = 'D'
        AND IN_TEAM IS NOT NULL AND loc IS NOT NULL
        GROUP BY SALE_ID, IN_TEAM, loc, seg
         UNION ALL
        SELECT SALE_ID, OUT_TEAM, 2 SALE_FLAG, loc, seg,
            SUM(REV * rate) AS sum_rev,
            SUM(MARGIN * rate) AS sum_mar,
            SUM(UNIT) AS SUM_UNIT
        FROM SALES
        WHERE (IGNORED IS NULL OR IGNORED <> 'Y')
        AND SALE_SPOT = 2
        AND SALE_ID = 375
        AND SALE_MODEL = 'D'
        AND OUT_TEAM IS NOT NULL AND loc IS NOT NULL
        GROUP BY SALE_ID, OUT_TEAM, loc, seg)
                    (SELECT   t.sum_rev, t.sum_mar, t.sum_unit
                       FROM   result_sum_temp t
                      WHERE       SR.SALE_ID = t.SALE_ID
                              AND SR.team_id = t.team_id
                              AND SR.SALE_FLAG = t.SALE_FLAG
                              AND SR.SEG = t.seg
                              AND SR.loc = t.loc
                              AND t.SALE_ID = 255)
        WHERE   SR.SALE_SPOT = 890
                AND SR.SALE_ID = 255
                AND (SR.SALE_ID, SR.team_id, SR.sale_flag, SR.SEGMENT, SR.LOB) IN
                          (SELECT   SALE_ID,
                                    team_id,
                                    sale_flag,
                                    seg,
                                    loc
                             FROM   result_sum_temp));

  • Help:how to use addAttachment method in NormalizedMessage

    i have a problem about the NormalizedMessage's addAttachment method ,it is below:
    public void addAttachment(java.lang.String id, javax.activation.DataHandler content) throws MessagingException
    i have a string .i want to transfer it to a certain SE as the attachment. how can I do!?
    i try to do it as below
    addAttachment("someid", new DataHandler(wantedString,"text/plain"))
    it does works in my computer with Window OS. however it DOESNOT WORK in Linux OS.?why?
    who can give me the correct using about DataHandler?
    many thanks .

    Why can't you just assign the method's return value to a variable and then print that? A bit of scriptlet code will do the trick. (As much as we all hate to use scriptlet code.)
    I believe you'll be able to do what you want in the 1.1 standard JSTL, but for now this will suffice. - MOD

  • Plz Help : How to use FrameGrabbingControl to extract frame from video.

    I m trying to extract frame from the video (from .mpg file)using FrameGrabbingControl on the click of the button.There is no problem related to playing video.
    import javax.media.control.*
    public void actionperformed(ActionEvent ae)
    String s = ae.getActionCommand();
    if(s.equals("click") )
    FrameGrabbingControl fgc = (FrameGrabbingControl) player.getcontrol("FrameGrabbingControl"); //it is returning null
    Buffer bf = fgc.grabFrame(); // so here nullpointerException
    }

    hi I am also experiencing the same prob.can u send me the code if u got one..my mail id is [email protected].we are working on a proj to extract jpeg images from any movie file in jmf...can u help us

  • Help - How to use kioskmode=true?

    This is from a previous post - but I'm not sure what it means. - I've never iweb, but thought someone here might know the answer.
    Your friend has disabled QT Pro saving by using the special html tags (kioskmode="true") in the page source.
    His code also determines the "size" of the player window.
    You'll need to dig through the page source code to find the URL to the .mov file and open it directly via QuickTime Player's "Open URL" feature.
    This may allow saving and full screen playback.
    Basically i've saved a QT movie from FCP and have uploaded to an FTP site.
    I'd like to disable to download triangle in the quicktime window, so the viewer can't save it. - but I don't know how to get into the page source.
    What am I missing?

    http://java.sun.com/docs/books/tutorial/uiswing/misc/plaf.html
    http://java.sun.com/products/jfc/tsc/articles/architecture/
    Try these

Maybe you are looking for

  • Cannot delete file SAP0000.TSK.bck

    hi    I am installing ECC5.0 ides on windows 2000.     I have finished intalling central instance but when doing database instance i am getting following error This is a trace of SAP0000.log file DbSl Trace: ORA-1403 when accessing table SAPUSER (DB)

  • Work Phone and personal apple ID

    I am leaving a company that asked me a few years back to use my personal apple ID for purchasing apps on the company purchased phone and iPad.  I wasn't comfortable doing this and told them at the time that I wasn't.  They told me it was company poli

  • Difference between a Search and a Retrieve web service?

    Anyone has any idea what's the difference between a Search and Retrieve web-services generated by the 7.1 WS Generator? It seems like you can use both to retrieve any kind of result definition and search by any field. I don't mind really, just curiou

  • Pdf or video

    What is the better way to export a slidehow in lightroom 4, pdf or video?

  • Select by Sum

    Hi all, I am attempting to query back records based on whether the sum of a collumn is above 600 but get a "ORA-00934 group function is not allowed here" error. I have a sneaky feeling its because of the join Im using but I'm a beginner so not sure?