Solved

Using a JDialog component

Posted on 2004-04-02
27
565 Views
Last Modified: 2008-02-01
How do i put text into a JDialog box. I have it declared as the following

helpDialog = new javax.swing.JDialog();
helpDialog.setTitle("About");

I want to add text to the dialog box that describes my application.
Help would be greatly appreciated
0
Comment
Question by:liamgannon
  • 11
  • 9
  • 3
  • +2
27 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10744212
helpDialog.add(new JLabel("This is my help dialog and I'm going to help you"));
0
 

Author Comment

by:liamgannon
ID: 10744528
that works but when i click on the buton to display the help dialog it comes up at it's smallest. I have to drag it out to size to be able to see anything in it. How do i set the size that it's displayed at?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10744596
You can do

helpDialog.setSize(100, 100);
0
 

Author Comment

by:liamgannon
ID: 10744655
the same thing is happening with the actual applicatin. When i run it it's small and i have to maximise it to see anything. How do i set the size of the frame?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10744666
Exactly the same way
0
 
LVL 15

Expert Comment

by:Javatm
ID: 10745448
Hi Friend;

// Call this method which creates a dialog . . .

private void CreateDialog() {

  final JDialog j1 = new JDialog();
  j1.setModal(true);

  // To put the dialog on the center of the screen . . .

  Dimension sd = Toolkit.getDefaultToolkit().getScreenSize();

  j1.setLocation(sd.width / 2 - 270 / 2,
  sd.height / 2 - 150 / 2);
  j1.setResizable(false);
  j1.setSize(270, 150);
  j1.setVisible(true);

}

Hope this helps . . .
Javatm
0
 
LVL 92

Expert Comment

by:objects
ID: 10745456
> How do i set the size of the frame?

frame.pack();

and for dialog:

dialog.pack();
0
 
LVL 92

Expert Comment

by:objects
ID: 10745470
> I want to add text to the dialog box that describes my application.

JOptionPane.showMessageDialog(frame, labeltext, "About", JOptionPane.INFORMATION_MESSAGE);
0
 

Author Comment

by:liamgannon
ID: 10745716
Thanks CEHJ, your way works fine. One last thing, i have a textarea called searchResults that i'm using to display the results of an array as follows

for(int cnt = 0;cnt <tmp.length;cnt++){
    searchResults.append (tmp[cnt]);
    searchResults.append ("\n");
}

This works but the results are only visible when i click out of that frame and click back into it again. How can i get the results to just print out straight away
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10745738
Where is that loop being called - i.e. inside what method?
0
 

Author Comment

by:liamgannon
ID: 10745759
I have a button, an input box and the results box. When the button is pressed the following method is called

private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {
        try{
      String name = "//"+ipAddress+"/dsProjectServer";
      dsProject dsProj = (dsProject) Naming.lookup(name);

               String result = dsProj.webSearch(searchText.getText ());
               String tmp[] = result.split("#div#");
               for(int cnt = 0;cnt <tmp.length;cnt++){
                     searchResults.append (tmp[cnt]);
                     searchResults.append ("\n");
               }
               searchPanel.repaint ();
         }catch(Exception e){
      System.err.println("dsProjServer exception: "+ e.getMessage());
               e.printStackTrace();
         }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10745762
Gotta go now liamgannon - have to leave you in objects' capable hands
0
 
LVL 15

Expert Comment

by:Javatm
ID: 10745968
> This works but the results are only visible when i click out of that frame and click back into it again.

Did you set the dialog to modal ? If yes then

Do 1st the Action Performed before you show the dialog like :

private void ShowDialog() {

  final JDialog j1 = new JDialog();
  j1.setModal(true);

  // ActionListener here . . .

  Dimension sd = Toolkit.getDefaultToolkit().getScreenSize();

  j1.setLocation(sd.width / 2 - 270 / 2,
  sd.height / 2 - 150 / 2);
  j1.setResizable(false);
  j1.setSize(270, 150);
  j1.setVisible(true);

}

Hope that helps . . .
Javatm
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:liamgannon
ID: 10746047
Javatm
It's not the dialog that won't display properly. Its a text area called searchResults that i'm using to display the results of an array as follows

for(int cnt = 0;cnt <tmp.length;cnt++){
    searchResults.append (tmp[cnt]);
    searchResults.append ("\n");
}

This works but the results are only visible when i click out of that frame and click back into it again. How can i get the results to just print out properly
0
 
LVL 16

Expert Comment

by:gnoon
ID: 10746669
I guess the line *Naming.lookup()* cause delay time to show up the string. Trying to wait a period of time and no click anywhere, if it's not show the text then maybe I'm wrong.
0
 
LVL 16

Expert Comment

by:gnoon
ID: 10746674
Did you get any exception from the exception handling after clicked the button?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10747185
Try the following:

      private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {
            String name = "//" + ipAddress + "/dsProjectServer";
            Thread searcher = new Thread(new ProjectSearch(name));
            searcher.start();
      }

  // Inner class
      class ProjectSearch implements Runnable {
            String jndiName;

            public ProjectSearch(String jndiName) {
                  this.jndiName = jndiName;
            }


            /**
             *  Do the JNDI look up and get the search results
             */
            public void run() {
                  try {
                        dsProject dsProj = (dsProject) Naming.lookup(jndiName);
                        String result = dsProj.webSearch(searchText.getText());
                        String tmp[] = result.split("#div#");
                        // check for 0 length first in the real version
                        SearchResultDisplayer displayer = new SearchResultDisplayer(tmp);
                        EventQueue.invokeLater(displayer);
                  }
                  catch (Exception e) {
                        System.err.println("dsProjServer exception: " + e.getMessage());
                        e.printStackTrace();
                  }

            }
      }

  // Inner class
      /**
       *  Displays the result of the search
       *
       * @author     CEHJ
       * @created    03 April 2004
       */
      class SearchResultDisplayer implements Runnable {
            private String[] results;


            public SearchResultDisplayer(String[] results) {
                  this.results = results;
            }


            /**
             *  Append result to text area
             */
            public void run() {
                  for (int cnt = 0; cnt < tmp.length; cnt++) {
                        searchResults.append(results[cnt]);
                        searchResults.append("\n");
                  }
            }

      }
0
 

Author Comment

by:liamgannon
ID: 10747536
CEHJ
The code you gave me just gives the same result as my own. The problem is that I have a gui that has two tabbed panes. One does a web search and the other does a spell check both using the google api. When i do a web search the results won't display until i click on the spell check pane and then click back into the web search pane. It's like it won't display the results until the text area is refreshed. Each subsequent search works fine, it's just the first search after the application has started that causes the problem.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10747578
Firstly, i just noticed that you probably only need to do that jndi lookup only once so it shouldn't be in that event handler, even on a separate thread. btw what *are* you doing in that jndi lookup - it looks like it's looking up part of the gui (!)

But i don't think that's maybe the reason for your main problem anyway. Are you doing anything else that is potentially time-consuming from an event thread?
0
 

Author Comment

by:liamgannon
ID: 10747586
the lookup is to an rmi server. the gui is an rmi client, when i click on search on the gui it invokes a method on the server that queries google using soap and the sends the result all the way back to the client
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10747600
OK. That lookup should only be done on startup, again in a separate thread, but only once.
There's no need for it to look up the component more than once, so it shouldn't be in an event handler.

What i'd do is to put this code

          String name = "//" + ipAddress + "/dsProjectServer";
          Thread searcher = new Thread(new ProjectSearch(name));
          searcher.start();

into a method called, say, findServer and then make the following alteration: change

>>
                    dsProject dsProj = (dsProject) Naming.lookup(jndiName);
                    String result = dsProj.webSearch(searchText.getText());
                    String tmp[] = result.split("#div#");
                    // check for 0 length first in the real version
                    SearchResultDisplayer displayer = new SearchResultDisplayer(tmp);
                    EventQueue.invokeLater(displayer);
>>

to


                    dsProject dsProj = (dsProject) Naming.lookup(jndiName);
                    String result = dsProj.webSearch(searchText.getText());
                    searchButton.setEnabled(true);

The search button should have previously been disabled. It shouldn't be usable until the jndo lookup has returned.

btw, that *still* may not be getting to the actual problem but sounds like it need to be done anyway. Let me know if you think i've misunderstood what's happening in your app



0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10747603
Oh and findServer should be called from a method such as createGui()
0
 

Author Comment

by:liamgannon
ID: 10747636
that doesn't sort the problem either. The search is being carried out properly and it prints out fine in the background when i do a System.out.println(); It's something to do with the way i'm displaying them in the gui. I have an array of web addresses that i want to print into the text area in the following format one under the other.

http://www.search.com/
http://www.altavista.com/
http://www.excite.com/
http://www.yahoo.com/
http://www.lycos.com/

I'm using
for(int cnt = 0;cnt <tmp.length;cnt++){
    searchResults.append (tmp[cnt]);
    searchResults.append ("\n");
}
Is there any other way to output them to the text area?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 115 total points
ID: 10747680
>>that doesn't sort the problem either.

No - and as i said, i didn't think it would, but the adjustment i just suggested needs to be made anyway. I'm fairly certain what your problem is but not having seen the relevant code it's difficult to be specific.

Your problem is probably related to performing time-consuming tasks on the event dispatch thread, which is preventing your gui from updating properly. The adjustment just made corrects an example of just that and there are probably more of them in your code.
0
 
LVL 16

Expert Comment

by:gnoon
ID: 10751359
Hi liamgannon,

Just try to change the string in the textarea without looking up for rmi server and see what's happen. I think it'll be better to know which line of code cause program run improperly and solve that point.
0
 

Author Comment

by:liamgannon
ID: 10753889
CEHJ
Thanks for all your help. Got the problem sorted by using a JTextArea instead of a TextArea. Still don't know what was wrong but it works now so I'm happy enough. Thanks for all your help with my initial problems and the tweaks to my code
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10756339
8-) You certainly should avoid mixing Swing and AWT components if that's what you were doing
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now