Link to home
Start Free TrialLog in
Avatar of liamgannon
liamgannon

asked on

Using a JDialog component

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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

helpDialog.add(new JLabel("This is my help dialog and I'm going to help you"));
Avatar of liamgannon
liamgannon

ASKER

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?
You can do

helpDialog.setSize(100, 100);
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?
Exactly the same way
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
> How do i set the size of the frame?

frame.pack();

and for dialog:

dialog.pack();
> I want to add text to the dialog box that describes my application.

JOptionPane.showMessageDialog(frame, labeltext, "About", JOptionPane.INFORMATION_MESSAGE);
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
Where is that loop being called - i.e. inside what method?
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();
         }
}
Gotta go now liamgannon - have to leave you in objects' capable hands
> 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
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
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.
Did you get any exception from the exception handling after clicked the button?
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");
                  }
            }

      }
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.
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?
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
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



Oh and findServer should be called from a method such as createGui()
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?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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
8-) You certainly should avoid mixing Swing and AWT components if that's what you were doing