Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 574
  • Last Modified:

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
0
liamgannon
Asked:
liamgannon
  • 11
  • 9
  • 3
  • +2
1 Solution
 
CEHJCommented:
helpDialog.add(new JLabel("This is my help dialog and I'm going to help you"));
0
 
liamgannonAuthor Commented:
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
 
CEHJCommented:
You can do

helpDialog.setSize(100, 100);
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
liamgannonAuthor Commented:
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
 
CEHJCommented:
Exactly the same way
0
 
JavatmCommented:
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
 
objectsCommented:
> How do i set the size of the frame?

frame.pack();

and for dialog:

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

JOptionPane.showMessageDialog(frame, labeltext, "About", JOptionPane.INFORMATION_MESSAGE);
0
 
liamgannonAuthor Commented:
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
 
CEHJCommented:
Where is that loop being called - i.e. inside what method?
0
 
liamgannonAuthor Commented:
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
 
CEHJCommented:
Gotta go now liamgannon - have to leave you in objects' capable hands
0
 
JavatmCommented:
> 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
 
liamgannonAuthor Commented:
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
 
gnoonCommented:
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
 
gnoonCommented:
Did you get any exception from the exception handling after clicked the button?
0
 
CEHJCommented:
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
 
liamgannonAuthor Commented:
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
 
CEHJCommented:
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
 
liamgannonAuthor Commented:
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
 
CEHJCommented:
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
 
CEHJCommented:
Oh and findServer should be called from a method such as createGui()
0
 
liamgannonAuthor Commented:
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
 
CEHJCommented:
>>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
 
gnoonCommented:
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
 
liamgannonAuthor Commented:
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
 
CEHJCommented:
8-) You certainly should avoid mixing Swing and AWT components if that's what you were doing
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 11
  • 9
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now