Here is my problem, when I start typing and reach the vertical edge of my JEditorPane, the jeditor pane extends in height along with my typing until the end of the window. My textareas below the jeditor eventually disappear. Basically, everything shifts down and I want the jeditor pane to stay at a static height.
Why would the jeditor pane move? How do I prevent it?
I have seen this before but don't remember how to fix it. I think it is related to how I use the Layout managers. I could use a different layout but I am more familiar with GridBag. I tried preferred height and it works initially but like I said...after typing the height changes. Strange.
Here is the code (it is scala, but think of it as pseudo code).
import java.awt.{ Insets, GridBagConstraints, Dimension }
import javax.swing._
import javax.swing.event._
import java.awt._
import java.awt.event._
import scala.swing.Swing._
import scala.swing.{ MainFrame, Panel, SimpleSwingApplication }
import org.slf4j._
import java.io.{ File, FileReader }
import javax.swing.filechooser.FileNameExtensionFilter
import org.berlin.syntax.actions._
import org.berlin.syntax._
import org.berlin.syntax.components._
class MyPanel extends JPanel {
val constraints: GridBagConstraints = this.defaultLayoutContraints()
val outputLogTextArea = new OutputTextArea
val outputStatusTextArea = new StatusTextArea
val inputConsoleTextArea = new InputTextArea
val labelCaretPos = new javax.swing.JLabel("(0)")
val outputTextScrollPane = defaultScroll(new JScrollPane(outputLogTextArea))
val outputStatusScrollPane = defaultScroll(new JScrollPane(outputStatusTextArea))
val inputTextScrollPane = defaultScroll(new JScrollPane(inputConsoleTextArea))
val fileChooser = new JFileChooser
{
// Constructor
this.setLayout(new GridBagLayout)
this.add(outputTextScrollPane, constraints)
this.add(outputStatusScrollPane, shiftDown(constraints))
this.add(inputTextScrollPane, shiftDown(constraints))
this.add(labelCaretPos, shiftDown(constraints))
fileChooser.setCurrentDirectory(targetInitialDir)
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("Current Log Files", "log"))
this.outputTextScrollPane.setViewportView(outputLogTextArea)
documentTypeChanged("text/plain")
new CaretMonitor(outputLogTextArea, labelCaretPos)
} // End constructor
def defaultLayoutContraints(): GridBagConstraints = {
val constraints = new GridBagConstraints
val insets = new Insets(2, 2, 2, 2)
constraints.insets = insets
constraints.anchor = GridBagConstraints.NORTHWEST
constraints.gridy = 3
constraints.gridx = 1
constraints.weightx = 1
constraints.weighty = 1
constraints.fill = GridBagConstraints.BOTH
return constraints
}
def defaultScroll(s: JScrollPane): JScrollPane = {
s.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS)
s.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS)
return s
}
def shiftDown(c: GridBagConstraints): GridBagConstraints = {
constraints.gridy = constraints.gridy + 1;
return constraints;
}
protected class OutputTextArea extends JEditorPane {
???????????
this.setPreferredSize(0, maxHeight - 230);
this.setCaretPosition(0)
this.setEditable(true)
this.setFont(new Font("Courier New", Font.PLAIN, 11))
val bundle = java.util.ResourceBundle.getBundle("jsyntaxpane/Bundle")
this.setContentType(bundle.getString("SyntaxTester.jEdtTest.contentType"))
this.setCaretColor(new java.awt.Color(0, 0, 0))
}
protected class StatusTextArea extends JTextArea {
this.setColumns(maxTextAreaCols)
this.setRows(6)
this.setLineWrap(false)
this.setCaretPosition(0)
this.setEditable(false)
this.setFont(new Font("Courier New", Font.PLAIN, 12))
}
MyFrame extends JFrame {
this.setJMenuBar(coreContentPanel.createMenuBar)
this.setLocation(initXPos, initYPos)
this.setLayout(new FlowLayout(FlowLayout.CENTER))
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
this.preferredSize = (maxWidth, maxHeight)
this.focusable = true
this.add(new MyPanel)
pack
}
} // End of the Class //
Setting the preferred size of the editors won't have any effect once it's wrapped inside a JScrollPane
. You need to set the preferred size of the scroll pane itself (perhaps by getting the wrapped component's preferred size).
If you're using a GridBagLayout
, you might also find that setting the weights on the GridBagContraints
to 0 on elements that have a fixed size (such as labels) can help increase the amount of space available for the components that need room to stretch.