Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

JScrollPane relayout Help Request

Thread view: 
pitoniakm@msn.com - 23 May 2006 18:48 GMT
Hello,

I am having great diffuclty getting an embedded  JScrollPane in
aJFrame to validate as the user resized this JFrame. I have tried
everything. It appears that the JFrame's ComponentAdapter does not fire
also (i was hoping to valiadte with this as frame resized).

Can someone offer a suggestion? I am hopelessly deadlocked.

many many thanks,

mike

==================================================================

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.ByteArrayOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import javax.swing.text.StyledDocument;

public class Console2 extends JFrame implements MouseListener,
MouseMotionListener{
    private PrintStream m_StdOutPrintStream = null;
    MyJTextPane textPane = new MyJTextPane();

    public Console2() throws IOException {
        this.setLayout(new GridBagLayout());

        m_StdOutPrintStream = new PrintStream(new StdOutFilteredStream(
                new ByteArrayOutputStream()));

        System.setOut(m_StdOutPrintStream);
        // Add a scrolling text area
        //textArea.setEditable(false);
        //textArea.setRows(20);
        //textArea.setColumns(50);
        textPane.setBackground(Color.black);
        textPane.setForeground(Color.white);
        //textArea.setLineWrap(false);

        //textArea.setFont(new Font("Serif", Font.PLAIN, 16));

        GridBagConstraints gridBagConstraints = new
java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        JScrollPane jScrollPane = new JScrollPane(textPane);
        JTabbedPane jTabbedpane = new JTabbedPane();
        jTabbedpane.add(jScrollPane);

        getContentPane().add(jTabbedpane, gridBagConstraints);

        //show the frame
        this.pack();
        //this.setLocationRelativeTo(null);
        this.setSize(new Dimension(800, 600));
        this.setVisible(true);

        WindowListener wndCloser = new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        };
        addWindowListener(wndCloser);

        addComponentListener(new ComponentAdapter() {
           public void ComponentResized(ComponentEvent e) {
                   System.out.println("resized");
           }
   });

        MouseMotionAdapter mouseMotionAdapter = new
java.awt.event.MouseMotionAdapter()
                {
                           public void mouseMoved(java.awt.event.MouseEvent evt) {

                               validate();
                           }
                 };

        this.addMouseMotionListener(mouseMotionAdapter);

        //addMouseMotionListener(this);
        //addMouseListener(this);
        //textPane.setD

        //this.setResizable(false);

    }

    //not to be confused with StreamReaders in SysExecCmdThread
    //which collect stdout, and stderr for processes which
    //do "not" display to jvm console window
    private class StdOutFilteredStream extends FilterOutputStream {

        public StdOutFilteredStream(OutputStream aStream) {
            super(aStream);
        }

        public void write(byte b[]) throws IOException {
            String aString = new String(b);
            if (aString != null && !aString.equals("")) {
                try {
                    StyledDocument styledDocument = (StyledDocument) textPane
                            .getDocument();
                    styledDocument.insertString(styledDocument.getLength(),
                            aString, null);

                    //                        Make sure the last line is always visible
                    textPane.setCaretPosition(styledDocument.getLength());

                    // Keep the text area down to a certain character size
                    int idealSize = 10;
                    int maxExcess = 5;
                    int excess = styledDocument.getLength() - idealSize;
                    if (excess >= maxExcess) {
                        //styledDocument.remove(0, excess);
                    }
                }catch (Exception e) {
                }
            }
        }

        //this method is the one called by out redirected printstreams
        public void write(byte b[], int off, int len) throws IOException {
            //TODO trim removes leading white space.....we need to just trim \n
at end
            String aString = new String(b, off, len);

            if (aString != null && !aString.equals("")) {
                try {
                    StyledDocument styledDocument = (StyledDocument) textPane
                            .getDocument();
                    //TODO fix null
                    styledDocument.insertString(styledDocument.getLength(),
                            aString, null);

                    // Keep the text area down to a certain character size
                    int idealSize = 2000;
                    int maxExcess = 500;
                    int excess = styledDocument.getLength() - idealSize;
                    if (excess >= maxExcess) {
                        styledDocument.remove(0, excess);
                    }
                    //                      Make sure the last line is always visible
                    textPane.setCaretPosition(styledDocument.getLength());
                }catch (Exception e) {
                }
            }
        }
    }

    public void mouseClicked(MouseEvent e) {
        //validate();
        System.err.println("poo");

    }

    public void mousePressed(MouseEvent e) {
        //validate();
        System.err.println("poo");

    }

    public void mouseReleased(MouseEvent e) {
        //validate();
        System.err.println("poo");

    }

    public void mouseEntered(MouseEvent e) {
        //validate();
        System.err.println("poo");
    }

    public void mouseExited(MouseEvent e) {
        //validate();
        System.err.println("poo");

    }

    public void mouseDragged(MouseEvent e) {
        //validate();
        System.err.println("poo");
    }

    public void mouseMoved(MouseEvent e) {
        //validate();
        System.err.println("poo");

    }
    public static void main(String[] args) {
        try {
            Console2 console2 = new Console2();

            //while (true) {
                System.out
                        .println("looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong");
                System.err.println("error");
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                }
            //}
            //int i = 1/0;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class MyJTextPane extends JTextPane {
    /**
    * overridden from JEditorPane to suppress line wraps
    *
    * @see setSize
    */
    public boolean getScrollableTracksViewportWidth() {
        return false;
    }

    /**
    * overridden from JEditorPane to suppress line wraps
    *
    * @see getScrollableTracksViewportWidth
    */
    public void setSize(Dimension d) {
        if (d.width < getParent().getSize().width) {
            d.width = getParent().getSize().width;
        }
        super.setSize(d);
    }

}
Fred Kleinschmidt - 23 May 2006 21:26 GMT
> Hello,
>
[quoted text clipped - 254 lines]
>
> }

Your Console2 class should override setBounds() method. This is the
method that is called when your component gets resized. In that method
you should do whatever needs to be done to reconfigure itself to fit
in the new size.
Signature

Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.