Here is the code from a class that I have used for years. This class extends JDialog in a way that adding components to it is very painless, since it uses GridBagLayout. I hope to soon comment it better and add an example, but for now you will have to look at the code to see what is going on.
package org.markwebb.gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
/**
* This is an abstract class which takes many of the pains out of
* GridBagLayout. The Dialog is set to modal.
*
* @author Mark Webb
*
*/
public abstract class AbstractDialog extends JDialog implements ActionListener {
protected static final String ACCEPT = "Accept";
protected static final String CANCEL = "Cancel";
protected static final String CLOSE = "Close";
protected JPanel mainPanel;
protected GridBagConstraints c;
private int row, col;
public AbstractDialog( JFrame parent, String title, String borderTitle ){
super( parent, title, true );
setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE );
c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.WEST;
c.insets = new Insets(5,5,5,5);
row = 0;
col = 0;
mainPanel = new JPanel();
mainPanel.setBorder( BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black, 1),
borderTitle, TitledBorder.LEFT, TitledBorder.TOP ));
mainPanel.setLayout( new GridBagLayout() );
add( mainPanel, BorderLayout.CENTER );
JPanel buttons = new JPanel();
JButton accept = new JButton( ACCEPT );
accept.addActionListener( this );
buttons.add( accept );
JButton cancel = new JButton( CANCEL );
cancel.addActionListener( this );
buttons.add( cancel );
JButton close = new JButton( CLOSE );
close.addActionListener( this );
buttons.add( close );
add( buttons, BorderLayout.SOUTH );
}
public void addToRow( JComponent comp, int width ){
// add the component
addItem( col, row, width , comp );
// increment the column number
col++;
}
public void addToNewRow( JComponent comp, int width ){
// increment the row number
row++;
// reset the column number
col = 0;
// add the component
addItem( col, row, width , comp );
// increment the column number
col++;
}
private void addItem( int x, int y, int w, JComponent comp )
{
c.gridx = x;
c.gridy = y;
c.gridwidth = w;
mainPanel.add( comp, c );
}
public void actionPerformed( ActionEvent e ){
if( e.getActionCommand().equals( ACCEPT )){
doAccept();
} else if( e.getActionCommand().equals( CANCEL )){
doCancel();
} else if( e.getActionCommand().equals( CLOSE )){
doClose();
}
}
private void doClose(){
setVisible( false );
}
protected abstract void doAccept();
protected abstract void doCancel();
}
No comments:
Post a Comment