Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Swing GridBagConstraints

Status
Not open for further replies.

fatcodeguy

Programmer
Feb 25, 2002
281
0
0
CA
I'm trying to create an Error dialog with three components, a small JLabel for an Icon, a textfield for the error message and an OK button. I want the icon on the far left of the first row, and the message to take the rest of the space for the error message. On the bottom row, I want the OK button alone, in the exact center of the row. The problem is that the message is sometimes larger or shorter, so the OK button is never EXACTLY centered. This is what I have been using:


c.weightx=0.5;
c.weighty=0.5;
c.insets = new Insets(10,10,10,10);
c.anchor=GridBagConstraints.CENTER;


c.gridx=0;
c.gridy=0;
gb.setConstraints(icon,c);
icon.setIcon(new ImageIcon("icons/error.gif"));
contentPane.add(icon);

c.gridx=1;
c.gridwidth=3;
error.setText(errorMessage);
gb.setConstraints(error,c);
contentPane.add(error);


c.gridx=4;
c.gridwidth=1;
gb.setConstraints(fake,c);
contentPane.add(fake);

c.gridy=2;
c.gridx=2;
c.gridwidth=1;
gb.setConstraints(btnOK,c);
contentPane.add(btnOK);


Appreciate any help. thanks
 
Hey someguy,

I don't know exactly what the problem might be in your code, but I have a class that models the exact behavior you're looking for...
Code:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;


public class ErrorDialog extends JFrame
{
    
    // -------------------------------------------
    // CONSTRUCTOR
    // -------------------------------------------
    
    public ErrorDialog( Icon icon, 
                        String dialogTitle, 
                        String messageText )
    {
        super( dialogTitle );
        
        // set up window to listen for window close event
        this.addWindowListener(
            new WindowAdapter() 
            {
                public void windowClosing( WindowEvent we ) 
                {
                    hide();
                    System.exit( 0 );
                }
            }
        );
        
        this.init( icon, messageText );
    }
    
    
    // -------------------------------------------
    // PRIVATE METHODS
    // -------------------------------------------
    
    private void init( Icon icon, String messageText )
    {
        Container contentPane = this.getContentPane();
        
        contentPane.setLayout( new GridBagLayout() );
        
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets( 2, 2, 2, 2 );
        
        // add image icon
        
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0.0;
        gbc.weighty = 0.0;
        gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.CENTER;
        contentPane.add( new JLabel( icon ), gbc );
        
        // add error message textarea
        
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.weighty = 0.0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.WEST;
        
        JTextArea messageTA = new JTextArea( messageText );
        messageTA.setEditable( false );
        messageTA.setBackground( this.getBackground() );
        messageTA.setSelectionColor( this.getBackground() );
        messageTA.setMargin( new Insets( 4, 4, 4, 4 ) );
        contentPane.add( messageTA, gbc );
        
        // add OK button
        
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 1.0;
        gbc.weighty = 0.0;
        gbc.gridwidth = 2;
        gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.CENTER;
        
        JButton okButton = new JButton( "OK" );
        okButton.addActionListener(
            new ActionListener()
            {
                public void actionPerformed( 
                    ActionEvent event )
                {
                    hide();
                }
            }
        );
        
        contentPane.add( okButton, gbc );
        
        this.pack();
    }
    
    
} // end class ErrorDialog
Hope this helps.

Best,
Adam Rice
 
Thanks Adam,

your code is for an applet, but the constraints settings are perfect! Just a question, why use JTextArea instead of JLabel?
 
Hi someguy,

The reason I used a JTextArea instead of a JLabel was simply because it allows you to put linebreaks in quite easily (by using the '\n' character in the String you pass in). I imagine that you can do something similar with a JLabel, but it wasn't apparent to me how to do so.

That's really it though... there is no definitive reason. :)

Adam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top