开发者

How do I pack a JDialog with a JPanel that contains a GridLayout?

开发者 https://www.devze.com 2023-04-11 06:17 出处:网络
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:

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:{saveAssessmentCategor开发者_开发问答yInfo()}).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.


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/


As suggested by chubbard, I quit using GridLayout and used TableLayout. The library is free along with some great documentation at the URL below:

http://www.clearthought.info/sun/products/jfc/tsc/articles/tablelayout/

Took a little while getting used to but achieving almost any layout seems possible with this layout management. See the above URL for jar, documentation and API. Thanks again chubbard.

And groovy can use the classes without any special integration. As long as you're familiar doing UIs with swing its fairly easy to convert any UI component, layout etc to groovy code. So I did that and its fine. Just a note: even if you're unable to convert to groovy, writing regular java in a .groovy file is perfectly fine. Groovy doesn't force you to do things the groovy way :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号