JScrollPane relayout Help Request

P

pitoniakm

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);
}

}
 
F

Fred Kleinschmidt

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);
}

}

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top