* http://www.javaalmanac.com/egs/javax.swing/pkg.html#JFrame,%20JWindow,%
20JDialog
* http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html
* http://www.google.com/search?q=jdialog+tutorial
> Is there any tutorials out there for explaining how to make custom
> JDialogs? Sun's tutorial seems to want to include all the different
[quoted text clipped - 5 lines]
> thanks
> Brandon
Hi Brandon,
One possibility to make a custom JDialog is to create acustom JPanel
with all the bells and whistles that you need and use that as the
Component in one of the static JOptionPane functions. Please see the
following example for a dialog that is similar to an inputDialog but
allows multiple text areas for user input.
HTH, Piet
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class CustomDialog extends JFrame {
CustomDialog(String title){
super(title);
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
}
);
String[] labels = {"Name","Value"};
String[] defaults = {"",""};
MultilineDialogPanel panel = new
MultilineDialogPanel(labels,defaults,20);
int selection = JOptionPane.showConfirmDialog(this,panel,"Multiline
Dialog",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
if (selection==JOptionPane.OK_OPTION){
System.out.println(panel.getResults()[0]);
System.out.println(panel.getResults()[1]);
}
}
public static void main(String[] args){
CustomDialog dlg = new CustomDialog("Multiline dialog");
dlg.show();
}
}
class MultilineDialogPanel extends JPanel{
GridBagConstraints gbc = new GridBagLayout();
GridBagLayout gbl = new GridBagConstraints();
ArrayList options = new ArrayList();
MultilineDialogPanel(String[] labels,String[] defaults,int
fieldLength){
super();
if (fieldLength == -1){fieldLength = 20;};
this.setLayout(gbl);
for (int i =0;i<labels.length;i++){
addComponent(new
JLabel(labels[i]),0,i*2,GridBagConstraints.WEST);
JTextField text = new JTextField(defaults[i],fieldLength);
addComponent(text,0,i*2+1,GridBagConstraints.WEST);
options.add(text);
}
}
public String[] getResults(){
String[] results = new String[options.size()];
for (int i=0;i<options.size();i++){
results[i] = ((javax.swing.text.JTextComponent)(options.get(i))).getText();
} return results;
}
public void addComponent(Component component,int xpos, int ypos){
gbc.gridx = xpos;
gbc.gridy = ypos;
gbc.insets = new Insets(2,2,2,2);
gbl.setConstraints(component,gbc);
this.add(component);
}
public void addComponent(Component component,int xpos,int ypos,int
anchor){
gbc.anchor = anchor;
addComponent(component,xpos,ypos);
}
}
Brandon McCombs - 12 Apr 2005 02:20 GMT
> > Is there any tutorials out there for explaining how to make custom
> > JDialogs? Sun's tutorial seems to want to include all the different
[quoted text clipped - 6 lines]
> > Brandon
> Hi Brandon,
Thanks, that's just what I needed. I already have my own version started. It's a lot more
clear now how to handle the layout.
> One possibility to make a custom JDialog is to create acustom JPanel
> with all the bells and whistles that you need and use that as the
> Component in one of the static JOptionPane functions. Please see the
> following example for a dialog that is similar to an inputDialog but
> allows multiple text areas for user input.
> HTH, Piet