Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6656073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:35:54+00:00 2026-05-26T01:35:54+00:00

I’m creating a dialog as part of Java application. I’m using groovy’s swingBuilder and

  • 0

I’m creating a dialog as part of Java application. I’m using groovy’s swingBuilder and I’m experiencing some difficulty when trying to pack a JDialog. See the code below:

import groovy.swing.SwingBuilder
import javax.swing.*

swing = new SwingBuilder()

swing.edt{
    frame(id: 'mainFrame', title: 'Test', size: [200,200], visible: true, locationRelativeTo: null, resizable: false){
    panel{
        button('Click me', actionPerformed: {createAssessmentCategoryDialog()})
        }
    }
}

public JPanel createAssessmentCategoryPanel(){
    swing.panel(id: 'assessmentCategoryPanel'){
        panel{
            panel(layout: gridLayout(rows:2, cols:2, hgap:4, vgap:4)){
                label("Category Name", horizontalAlignment: JLabel.RIGHT)
                textField(id: 'categoryName', columns: 12)
                button('Save & add another', actionPerformed:{saveAssessmentCategoryInfo()}).toolTipText = 'Saves and clears field'
                button('Close', actionPerformed:{assessmentCategoryDialog.visible = false}).toolTipText = 'Closes dialog without saving'
            }
        }
    }
    return swing.assessmentCategoryPanel
}

public void createAssessmentCategoryDialog(){
    assessmentCategoryDialog = new JDialog(swing.mainFrame, "Add Assessment Category", true)
    assessmentCategoryDialog.resizable = false
    assessmentCategoryDialog.defaultCloseOperation = JDialog.DISPOSE_ON_CLOSE
    assessmentCategoryDialog.contentPane.add(createAssessmentCategoryPanel())
    assessmentCategoryDialog.pack()
    assessmentCategoryDialog.locationRelativeTo = swing.mainFrame
    assessmentCategoryDialog.visible = true
}

If you paste this code in a groovy console it should execute fine. After clicking the ‘click me’ button note the extra space below the dialog.

So its a grid layout that has a label, textField and two buttons. It displays fine but my trouble comes when I try to pack the dialog. It packs well horizontally but I have an extra space (almost like an extra row from the GridLayout) at the bottom. It makes the dialog look odd. I’ve narrowed this problem down to the GridLayout, because if I add just one row of components it packs fine. If I remove the grid completely and add some other random components it packs fine. I know I can try an alternate layout manager but GridLayout works well for me without too much code, like GridBag. So unless there is no other solution I’d really rather stick to GridLayout.

If I use the setSize() method on the JDialog the dialog does get resized, however, across different themes and platforms it does not perform so well. I’d much rather use pack() to adjust the size for any platform. Thanks in advance for any assistance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:35:55+00:00Added an answer on May 26, 2026 at 1:35 am

    Stop using GridLayout, and switch to another layout. GridLayout has equal sizes for each cell so it’s about as useful a football bat. So, unless you want to create a new sport you’re not going to find it useful. You can pick up GridBagLayout, but it’s frustratingly verbose. I’d suggest getting TableLayout.

    http://java.sun.com/products/jfc/tsc/articles/tablelayout/

    It’s a vastly more productive LayoutManager. Way more useful than any of the Sun LayoutManagers. I don’t know how it works with Groovy, but it looks like Groovy can use these classes without needing any special integration developed so hopefully you can grab it, drop it in, and have it work.

    I think your pack() statement is working as designed, but it’s just bad choice of LayoutManager. If you correct this I bet you’ll find setSize() works as well. All pack() really does is calculate the preferred size of your components and sets the size of the dialog to that. However, if you set the size of the JDialog your components will simply layout according to how your LayoutManager distributes space to components. Sort of the different between bottom up where the individual components dictate the size of the dialog vs. top down where dialog says here is the size and the bottom components figure out how big they’ll be.

    For your enjoyment: http://madbean.com/anim/totallygridbag/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.