Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By jimboat

Thread: Append to JTextArea after setting dialog visible

  1. #1
    jimboat is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Append to JTextArea after setting dialog visible

    I am trying to show a dialog and then append text to a JTextArea within that dialog to show the status of an export. I am unable to get the appended text to appear if I set the dialog visible first. I would appreciate any help.

    See the last three lines in the constructor; I only see "Hello" in my TextArea but expect to see "Hello World"

    Thank you.

    My dialog class is below:

    public class StatusDialog extends JDialog implements ActionListener {

    private JScrollPane jScrollPane1;
    private JTextArea statusTextArea;

    public StatusDialog(JFrame parent, String title) {
    super(parent, title, true);
    if (parent != null) {
    Dimension parentSize = parent.getSize();
    Point p = parent.getLocation();
    setLocation(p.x + parentSize.width / 12, p.y + parentSize.height / 12);
    }
    JPanel messagePane = new JPanel();
    jScrollPane1 = new javax.swing.JScrollPane();
    statusTextArea = new JTextArea();
    statusTextArea.setFont(new java.awt.Font("SansSerif", 0, 14)); // NOI18N
    statusTextArea.setColumns(30);
    statusTextArea.setRows(20);
    statusTextArea.setEditable(false);
    jScrollPane1.setViewportView(statusTextArea);

    JButton closeStatusButton = new JButton("OK");
    closeStatusButton.addActionListener(this);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.CENTER)
    .addComponent(closeStatusButton, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 599, Short.MAX_VALUE))
    .addContainerGap())
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
    .addGap(18, 18, 18)
    .addComponent(closeStatusButton)
    .addContainerGap())
    );

    setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    pack();

    statusTextArea.append("Hello");
    setVisible(true);
    statusTextArea.append("World");
    }

    public void actionPerformed(ActionEvent e) {
    setVisible(false);
    dispose();
    }

    public JTextArea getStatusTextArea() {
    return statusTextArea;
    }

    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Append to JTextArea after setting dialog visible

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

  3. #3
    jimboat is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Re: Append to JTextArea after setting dialog visible

    Quote Originally Posted by DarrylBurke View Post
    Sorry, I should have read the new member information first. I have re-posted my simplified code with formatting intact.

    I'll restate my question as well. Why am I unable to append to a TextArea after my dialog has been made visible? I expect the constructor below to produce "Hello World", but I only get "Hello".

    Thanks

    Java Code:
    package com.mitreboxav.DeviceProtocol;
    
    /**
     *
     * @author Jim
     */
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    
    public class TestDialogTextArea extends JDialog implements ActionListener {
    
        private JScrollPane jScrollPane1;
        private JTextArea statusTextArea;
    
        public TestDialogTextArea(JFrame parent, String title) {
            super(parent, title, true);
            if (parent != null) {
                Dimension parentSize = parent.getSize();
                Point p = parent.getLocation();
                setLocation(p.x + parentSize.width / 12, p.y + parentSize.height / 12);
            }
    
            statusTextArea = new JTextArea();
            statusTextArea.setFont(new java.awt.Font("SansSerif", 0, 14)); // NOI18N
            statusTextArea.setColumns(30);
            statusTextArea.setRows(20);
            statusTextArea.setEditable(false);
    
            JButton closeStatusButton = new JButton("OK");
            closeStatusButton.addActionListener(this);
            
            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            
            getContentPane().add(statusTextArea, BorderLayout.NORTH);
            getContentPane().add(closeStatusButton, BorderLayout.SOUTH);
            pack();
            statusTextArea.append("Hello ");
            setVisible(true);
            statusTextArea.append("World");
        }
        
        public void actionPerformed(ActionEvent e) {
            setVisible(false);
            dispose();
        }
    
        public JTextArea getStatusTextArea() {
            return statusTextArea;
        }
        
        public static void main(String[] a) {
            TestDialogTextArea dlg = new TestDialogTextArea(new JFrame(), "title");
        }
    }
    DarrylBurke likes this.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Append to JTextArea after setting dialog visible

    A modal dialog halts the Thread of execution until the dialog is disposed. Add a couple of System.out.println(...) statements before and after setVisible(true) to familiarize yourself with the concept.

    More in this tutorial: How to Use Modality in Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

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

  5. #5
    jimboat is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Re: Append to JTextArea after setting dialog visible

    Quote Originally Posted by DarrylBurke View Post
    A modal dialog halts the Thread of execution until the dialog is disposed. Add a couple of System.out.println(...) statements before and after setVisible(true) to familiarize yourself with the concept.

    More in this tutorial: How to Use Modality in Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

    db
    Thank you!

Similar Threads

  1. Append to JTextArea instantly Problem
    By jrdncchr in forum AWT / Swing
    Replies: 2
    Last Post: 12-17-2011, 07:50 PM
  2. JTextArea on PopUp -JTextArea isn't editable
    By Richy76 in forum AWT / Swing
    Replies: 3
    Last Post: 02-01-2010, 08:51 PM
  3. Replies: 13
    Last Post: 12-11-2009, 09:43 AM
  4. Problems with setting the menubar and a JPanel visible
    By leitbug06 in forum AWT / Swing
    Replies: 1
    Last Post: 03-07-2009, 12:46 AM
  5. JTextArea setting
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 01-29-2008, 03:57 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
  •