8 Replies Latest reply on Feb 20, 2004 11:32 AM by 807587

    Is there such a thing as detailed jdialog in swing?

    807587
      Hello All,

      I am looking for an error dialog that can be expanded and contracted to show/"un-show" the details of the error. Is there such a class?
      I havent really been exposed to the swing api until recently. i need some sort of guidance.
      Any help is appreciated. Thanks.

      Best regards,
      Cocoh
        • 1. Re: Is there such a thing as detailed jdialog in swing?
          camickr
          No. You could just create a JDialog that uses a BorderLayout. Add the summary information to the North of the layout.

          Add a show details button. When you click on the show details button, create a details panel and add it to the South of the BorderLayout. Then to a dialog.pack(). Change the show details button to a hide details button. When you click on the hide button, remove the details panel and repack the dialog.

          • 2. Re: Is there such a thing as detailed jdialog in swing?
            807587
            Thanks again, carnickr, for taking time to help me out. :)
            • 3. Re: Is there such a thing as detailed jdialog in swing?
              807587
              Hello carnickr,

              I have implemented the ff class based on your suggestion. When I instantiate this class, a dialog box appears, with the specified components, but when I click the detail button, an exception occurs when the south component is added. Could you please take a look at my test code? What did I do wrong?
              class DetailedDialog extends JDialog implements ActionListener
              {
                  static String sTitle = "Hmmm";
                  
                  protected Frame coParent = null;
                  JPanel oSouthPanel = null;
                  
                  public DetailedDialog( Frame oFrame )
                  {
                      super(oFrame, "Detailed ", true); 
                      
                      //setLayout( new BorderLayout() );
                      
                      String sSummaryInfo = "Something is not right! ";        
                      String sDetailTitle = "The problem is";
                      String sDetail[] = {"Details1", "Details2","Details3", "Details4","Details5", "Details6","Details7", "Details8"};                
                              
                      JButton oButton = new JButton( "DETAIL" );
                      oButton.addActionListener( this );
                      
                      JButton oButtonOK = new JButton("OK");         
                      oButtonOK.addActionListener( new ActionListener() {         
                          public void actionPerformed(ActionEvent e) {             
                              setVisible(false);             
                          }             
                      } );         
                            
                      
                      JPanel oNorthPanel = new JPanel( new BorderLayout() );
                      JLabel oLabel = new JLabel();
                      oLabel.setText( sSummaryInfo );
                              
                      oNorthPanel.add( oLabel, BorderLayout.NORTH );
                      oNorthPanel.add( oButton, BorderLayout.EAST );
                      oNorthPanel.add( oButtonOK, BorderLayout.WEST );
                                              
                      JPanel oSouthPanel = new JPanel( new BorderLayout() );
                      JLabel oDetailLabel = new JLabel();
                      oDetailLabel.setText( sDetailTitle );
                      JList oList = new JList( sDetail );
                      oList.setFixedCellHeight( 5 );
                      oSouthPanel.add( oDetailLabel, BorderLayout.NORTH );
                      oSouthPanel.add( oList, BorderLayout.CENTER );                
                              
                      getContentPane().add( oNorthPanel, BorderLayout.NORTH );
                                          
                      pack();
                      setVisible( true );   
                      setResizable(false); 
                  }
                  
                   public void actionPerformed(ActionEvent e)
                   {
                      JButton oJButton = (JButton)e.getSource();
                      if (oJButton.getActionCommand().equals("DETAIL"))
                      {            
                          getContentPane().add( oSouthPanel, BorderLayout.SOUTH );
                          //SwingUtilities.windowForComponent (this).pack ();
                          pack();    
                          oJButton.setActionCommand( "NO DETAIL" );
                      }
                      else if (oJButton.getActionCommand().equals("NO DETAIL"))
                      {            
                          getContentPane().remove( oSouthPanel );
                          //SwingUtilities.windowForComponent (this).pack ();
                          pack();
                          oJButton.setActionCommand( "DETAIL" );
                      }
                  }                                  
              }
              the erorr is :

              java.lang.NullPointerException
                   at java.awt.Container.addImpl(Container.java:584)
                   at java.awt.Container.add(Container.java:490)
                   
              Please bear with me.

              Thanks very much.
              • 4. Re: Is there such a thing as detailed jdialog in swing?
                807587
                Don't think camickr is in your timezone but I'm sure he won't mind me pointing out that you've declared oSouthPanel as a member variable and then redeclared it as a local variable where you create it. Thus, the oSouthPanel that is referenced in the callback is never set.
                • 5. Re: Is there such a thing as detailed jdialog in swing?
                  807587
                  gosh. I feel so stupid.sorry for wasting ur time. thanks Tom.Sanders for pointing that out. :)
                  • 6. Re: Is there such a thing as detailed jdialog in swing?
                    807587
                    That's quite alright. You are not wasting my time. I choose to read the forums because I can learn from them (as well as helping people out). You are obviously trying to help yourself as well as asking help of others, which not every poster tries to do....
                    • 7. Re: Is there such a thing as detailed jdialog in swing?
                      807587
                      That's quite alright. You are not wasting my time. I
                      choose to read the forums because I can learn from
                      them (as well as helping people out). You are
                      obviously trying to help yourself as well as asking
                      help of others, which not every poster tries to do....
                      Ta-daa!!!
                      (instant help)
                      • 8. Re: Is there such a thing as detailed jdialog in swing?
                        807587
                        I have a code that does something which u r looking for. Maybe this can help you.
                        /*
                         * AdvancedErrorDialog.java
                         *
                         */
                        
                        package com.debopam.utils;
                        
                        import java.awt.BorderLayout;
                        import javax.swing.JPanel;
                        import javax.swing.JScrollPane;
                        import javax.swing.JTextArea;
                        import javax.swing.SwingUtilities;
                        
                        /**
                         *
                         * @author  DebopamG
                         */
                        public class AdvancedErrorDialog extends javax.swing.JDialog
                        {
                            /** Creates new form AdvancedErrorDialog */
                            public AdvancedErrorDialog(java.awt.Frame parent)
                            {
                                super(parent, true);
                                initComponents();
                                txa_ErrorSummary.setBackground(pnl_SummaryPanel.getBackground());
                                initDetailsPanel();
                            }
                            private void initDetailsPanel()
                            {
                                pnl_DetailsPanel.setLayout(new BorderLayout(10, 10));
                                txa_ErrorDetails.setEditable(false);
                                txa_ErrorDetails.setBackground(pnl_DetailsPanel.getBackground());
                                txa_ErrorDetails.setLineWrap(true);
                                txa_ErrorDetails.setWrapStyleWord(true);
                                jsp_DetailsScrollPane.setViewportView(txa_ErrorDetails);
                                jsp_DetailsScrollPane.setPreferredSize(new java.awt.Dimension(350, 150));
                                pnl_DetailsPanel.add(jsp_DetailsScrollPane, BorderLayout.CENTER);
                            }
                            public void displayErrorDialog()
                            {
                                txa_ErrorSummary.setEditable(false);
                                txa_ErrorSummary.setText(str_ErrorSummary);
                                txa_ErrorDetails.setText(str_ErrorDetail);
                                putDialogInCenter(this.getPreferredSize().width,
                                this.getPreferredSize().height);
                            }
                            private void putDialogInCenter(int width, int height)
                            {
                                java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
                                int screenX = screenSize.width;
                                int screenY = screenSize.height;
                                setBounds((screenX-width)/2, (screenY-height)/2, width, height);
                                show();
                            }
                            public void setErrorSummary(String str_ErrorSummary)
                            {
                                this.str_ErrorSummary = str_ErrorSummary;
                            }
                            public void setErrorDetail(String str_ErrorDetail)
                            {
                                this.str_ErrorDetail = str_ErrorDetail;
                            }
                        
                            private void initComponents()
                            {
                                java.awt.GridBagConstraints gridBagConstraints;
                                
                                pnl_SummaryPanel = new javax.swing.JPanel();
                                pnl_ButtonPanel = new javax.swing.JPanel();
                                btn_Details = new javax.swing.JButton();
                                txa_ErrorSummary = new javax.swing.JTextArea();
                                lbl_FillerLeft = new javax.swing.JLabel();
                                lbl_FillerTop = new javax.swing.JLabel();
                                jLabel1 = new javax.swing.JLabel();
                                
                                addWindowListener(new java.awt.event.WindowAdapter()
                                {
                                    public void windowClosing(java.awt.event.WindowEvent evt)
                                    {
                                        closeDialog(evt);
                                    }
                                });
                                
                                pnl_SummaryPanel.setLayout(new java.awt.BorderLayout());
                                
                                pnl_ButtonPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
                                
                                btn_Details.setMnemonic('d');
                                btn_Details.setText("Details >>");
                                btn_Details.addActionListener(new java.awt.event.ActionListener()
                                {
                                    public void actionPerformed(java.awt.event.ActionEvent evt)
                                    {
                                        btn_DetailsActionPerformed(evt);
                                    }
                                });
                                
                                pnl_ButtonPanel.add(btn_Details);
                                
                                pnl_SummaryPanel.add(pnl_ButtonPanel, java.awt.BorderLayout.SOUTH);
                                
                                txa_ErrorSummary.setLineWrap(true);
                                txa_ErrorSummary.setWrapStyleWord(true);
                                txa_ErrorSummary.setBorder(new javax.swing.border.EtchedBorder());
                                txa_ErrorSummary.setPreferredSize(new java.awt.Dimension(350, 150));
                                pnl_SummaryPanel.add(txa_ErrorSummary, java.awt.BorderLayout.CENTER);
                                
                                lbl_FillerLeft.setText("        ");
                                lbl_FillerLeft.setPreferredSize(new java.awt.Dimension(24, 25));
                                pnl_SummaryPanel.add(lbl_FillerLeft, java.awt.BorderLayout.WEST);
                                
                                lbl_FillerTop.setText("          ");
                                lbl_FillerTop.setPreferredSize(new java.awt.Dimension(30, 25));
                                pnl_SummaryPanel.add(lbl_FillerTop, java.awt.BorderLayout.NORTH);
                                
                                jLabel1.setText("        ");
                                pnl_SummaryPanel.add(jLabel1, java.awt.BorderLayout.EAST);
                                
                                getContentPane().add(pnl_SummaryPanel, java.awt.BorderLayout.NORTH);
                                
                                pack();
                            }
                            
                            private void btn_DetailsActionPerformed(java.awt.event.ActionEvent evt)
                            {
                                // Add your handling code here:
                                if(btn_Details.getText().equalsIgnoreCase("Details >>"))
                                {
                                    btn_Details.setText("Hide <<");
                                    SwingUtilities.invokeLater(new Runnable()
                                    {
                                        public void run()
                                        {
                                            addDetailsFrame();
                                        }
                                    });
                                }
                                else
                                {
                                    btn_Details.setText("Details >>");
                                    SwingUtilities.invokeLater(new Runnable()
                                    {
                                        public void run()
                                        {
                                            removeDetailsFrame();
                                        }
                                    });
                                }
                            }
                            /** Closes the dialog */
                            private void closeDialog(java.awt.event.WindowEvent evt)
                            {
                                setVisible(false);
                                dispose();
                                System.exit(0);
                            }
                            private void addDetailsFrame()
                            {
                                getContentPane().add(pnl_DetailsPanel, java.awt.BorderLayout.SOUTH);
                                pack();
                                this.validate();
                            }
                            private void removeDetailsFrame()
                            {
                                this.getContentPane().remove(pnl_DetailsPanel);
                                this.pack();
                                this.validate();
                            }
                            /**
                             * @param args the command line arguments
                             */
                            public static void main(String args[])
                            {
                                AdvancedErrorDialog adv = new AdvancedErrorDialog(new javax.swing.JFrame());
                                adv.setErrorSummary("This is a summary of the error.");
                                adv.setErrorDetail("This is the error in detail.");
                                adv.displayErrorDialog();
                            }
                        
                            private javax.swing.JButton btn_Details;
                            private javax.swing.JLabel jLabel1;
                            private javax.swing.JLabel lbl_FillerLeft;
                            private javax.swing.JLabel lbl_FillerTop;
                            private javax.swing.JPanel pnl_ButtonPanel;
                            private javax.swing.JPanel pnl_SummaryPanel;
                            private javax.swing.JTextArea txa_ErrorSummary;
                            private JPanel pnl_DetailsPanel = new JPanel();
                            private JTextArea txa_ErrorDetails = new JTextArea();
                            private JScrollPane jsp_DetailsScrollPane = new JScrollPane();
                            private String str_ErrorSummary;
                            private String str_ErrorDetail;
                            
                        }
                        Debopam.