Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How to make JOptionPane.MessageDialog appear with a vertical bar when it calls for a constructor from a java class?

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to make JOptionPane.MessageDialog appear with a vertical bar when it calls for a constructor from a java class?

    Hi,

    I have a mini java swing program where by it has 3 separate classes.

    - application.java
    - question.java
    - test.java

    Now, the application.java is the main application that will invoke the result.

    At the test.java, there's a constructor method as follows:
    public String getResult() {
     
            int score = 0;
            String[] choices;
            String score = "";
            for (int i = 0; i < test.length; i++) {
                if (test[i].getCorrect() == test[i].getAns()) {
                    score++;
                } else {
                    score += "Question " + (i + 1) + " is " + test[i].getCorrect() + "\n";
     
            }
            return result;
    Question.java is mainly the getter and setter.

    At the Test.java,

    there's a line which I'd like a vertical bar to appear in this JOptionPane there.

    JOptionPane.showMessageDialog(null, newTest.getResult()).

    Can anybody tell me how to go about doing it?

    I have read up so much but yet is totally confused.
    One of the examples mentioned that you have to create a JTextArea like below.
    p2 = new JPanel();
    	 p2.add(jtxtDetails = new JTextArea("abc").

    But, what I want to achieve is really get the newTest.getResult into the JTextArea.

    Am really bad about Java programming hence hope someone can shows me how to achieve it.

    Many thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to make JOptionPane.MessageDialog appear with a vertical bar when it calls for a constructor from a java class?

    get the newTest.getResult into the JTextArea.
    Use one of the JTextArea class's methods to put the String returned by the getResult() method into the text area.

    I'd like a vertical bar to appear in this JOptionPane
    Can you explain what you mean by a "vertical bar"? Is it this character: "|"?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jul 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to make JOptionPane.MessageDialog appear with a vertical bar when it calls for a constructor from a java class?

    Quote Originally Posted by Norm View Post
    Use one of the JTextArea class's methods to put the String returned by the getResult() method into the text area.
    Pardon my stupidity, after my futile attempt...it's not working...

    I tried to put these lines at my getResult() method at Test.java.



    JTextArea jta = new JTextArea(result);
    JScrollPane scroller = new JScrollPane(jta);
    scroller.setVerticalScrollBarPolicy(ScrollPaneCons tants.VERTICAL_SCROLLBAR_ALWAYS);

    return result;
    }

    And then I add this code into the application.java :

    JOptionPane.showMessageDialog(null, scroller(newTest.getResult())

    But, it's not working.

    Could you show me how to use one of the JTextArea class's methods to put the String returned by the getResult() method into the text area?
    Can you explain what you mean by a "vertical bar"? Is it this character: "|"?
    The "vertical bar" refers to the scrollbar that appears on the ShowMessageDialog "container" when the result gets very big.

    However, now I don't think I can use ShowMessageDialog right since scroller(newTest.getResult()) is the new container.

    I'm overall at a loss how to do the whole things.
    Hope you can really show me how.

    Thanks.
    Last edited by Norm; March 4th, 2013 at 10:52 AM. Reason: added end quote tag

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to make JOptionPane.MessageDialog appear with a vertical bar when it calls for a constructor from a java class?

    how to do the whole things.
    Sorry, I don't understand what your problem is.
    it's not working.
    Can you explain what you are having problems with?

    It be better if you posted the code you are having problems with and add some comments to the code describing what you want the code to do.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to make a seek bar for media player.
    By manguram in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 8th, 2012, 09:02 AM
  2. which class has a default constructor? (Req. guidance on constructor)
    By DragBall in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2012, 04:42 PM
  3. How to make calls to methods
    By ksahakian21 in forum Java Theory & Questions
    Replies: 4
    Last Post: April 6th, 2012, 10:57 PM
  4. 1 vertical scroll bar fro 2 jtextareas
    By fortune2k in forum AWT / Java Swing
    Replies: 8
    Last Post: March 4th, 2011, 02:04 PM
  5. Design of class named Fan to represent a Fan
    By qaromi in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 7th, 2009, 02:28 PM