Results 1 to 5 of 5
  1. #1
    fredlo2010 is offline Member
    Join Date
    Jan 2014
    Posts
    28
    Rep Power
    0

    Default Adding Object to JOptionPane.showMessageDialog

    Hello,

    I am trying to do this but I am not sure how to do it or if it's even possible. Basically I want to add two scroll-able panes to a JOptionPane.showMessageDialog.

    Something along this lines.

    Java Code:
        public static void showListInfo(String activeWords , String removedWords) {
    
            JTextArea active = new JTextArea(activeWords, 20, 30);
            JTextArea removed = new JTextArea(removedWords,20,30);
                            
            JScrollPane activeScrollPane = new JScrollPane(active);
            JScrollPane removedScrollPane = new JScrollPane(removed);
            
            JLabel activeLabel = new JLabel("Active Words");
            JLabel removedLabel = new JLabel("Removed Words");
            
            Container c= new Container();
            
            c.add(activeLabel);
            c.add(activeScrollPane);
            c.add(removedLabel);
            c.add(removedScrollPane);
    
            JOptionPane.showMessageDialog(null, c,"My Tittle" ,JOptionPane.INFORMATION_MESSAGE  );
    
        }
    Can I do this ?

    Thanks

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Adding Object to JOptionPane.showMessageDialog

    Can I do this ?
    Did you try it?
    What happens when you compile and execute the code?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    fredlo2010 is offline Member
    Join Date
    Jan 2014
    Posts
    28
    Rep Power
    0

    Default Re: Adding Object to JOptionPane.showMessageDialog

    Hi Norm,

    I really love how you make me go back to the drawing board try some other things; I don't know if its intentional but it's great. I am learning so much more.

    After running my code above I did not get any error and it compiled properly. The only problem is that it was showing an empty message.

    Then, I looked a little bit more into the Container class because I was getting the lists if they were been called directly (no container) here Container (Java Platform SE 7 )
    While reading this I realized that this was not a swing element but an awt ( I am not sure of I can used them together or not) but the fact that both were is different packages made me search for a different solution.


    So then I discovered JPane and some of the layout options. This is my final code and it's working as intended.

    Java Code:
    public class JavaApplication2 {
    
        public static void main(String[] args) {       
            String list1 = "Old\n education\n him\n	departure\n any\n arranging\n one\n prevailed\n" +
                           "Old\n education\n him\n departure\n any\n arranging\n one\n prevailed\n" +
                           "Old\n education\n him\n departure\n any\n arranging\n one\n prevailed\n";
            String list2 = "Old\n education\n him\n	departure\n any\n arranging\n one\n prevailed\n";                
            showListInfo(list1, list2);        
        }
            
        public static void showListInfo(String activeWords, String removedWords) {
    
            JTextArea active = new JTextArea(activeWords, 20, 30);
            JTextArea removed = new JTextArea(removedWords, 20, 30);
    
            JScrollPane activeScrollPane = new JScrollPane(active);
            JScrollPane removedScrollPane = new JScrollPane(removed);
    
            JLabel activeLabel = new JLabel("Active Words");
            JLabel removedLabel = new JLabel("Removed Words");
            
            // Create the container.  
            JPanel c = new JPanel();
            c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
            c.setSize(50, 130);
            
            // Align all componenets to the left.
            activeLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
            activeScrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);
            removedLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
            removedScrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);
            
            // Add the elements to the container.
            c.add(activeLabel);        
            c.add(activeScrollPane);
            c.add(Box.createRigidArea(new Dimension(0,30)));
            c.add(removedLabel);
            c.add(removedScrollPane);
    
            JOptionPane.showMessageDialog(null,c , "My Tittle", JOptionPane.INFORMATION_MESSAGE);
        }
    }
    Once again thanks a lot for the help :)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Adding Object to JOptionPane.showMessageDialog

    Glad you got it working.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Adding Object to JOptionPane.showMessageDialog

    Note that Box has a method to create a vertical strut (int), which is what you should be using instead of creating a rigid area (0, 30).

    You could also consider using a JSeparator.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. JOptionPane.showMessageDialog Customization
    By NoviceJavaNoob in forum New To Java
    Replies: 0
    Last Post: 04-12-2014, 11:28 AM
  2. JOptionPane.showMessageDialog use
    By camaro01 in forum New To Java
    Replies: 7
    Last Post: 04-22-2011, 04:19 AM
  3. help with JOptionPane.showMessageDialog()
    By gmn1 in forum New To Java
    Replies: 8
    Last Post: 03-30-2009, 09:31 AM
  4. About JOptionPane.showMessageDialog
    By jhetfield18 in forum AWT / Swing
    Replies: 2
    Last Post: 11-02-2007, 10:45 PM
  5. About JOptionPane.showMessageDialog
    By jhetfield18 in forum Advanced Java
    Replies: 0
    Last Post: 11-02-2007, 10:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •