Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / April 2005

Tip: Looking for answers? Try searching our database.

custom JDialog

Thread view: 
Brandon McCombs - 10 Apr 2005 09:58 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
ways you can make JDialogs into one big demo and I can't sort it all
out.  I need a dialog that I can place buttons and text fields in. Is it
possible to do that with a JOptionPane? If so how do i create the
components when I create the OptionPane?

thanks
Brandon
Spiros Gezerlis - 10 Apr 2005 12:26 GMT
* 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
Piet - 10 Apr 2005 20:41 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 - 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.