JScrollPane 中的 JPanel 中的 JTextComponent
JTextComponent inside JPanel inside JScrollPane
我最近需要在单个 JScrollPane 的视口视图中放置几个组件,其中有一个 JTextPane。 (如果有兴趣,请参阅详细信息 )。
我将所有组件(两个 JPanel 和 JTextPane)放在另一个 JPanel 中,这个 JPanel 具有 BorderLayout LayoutManager,并将该 JPanel 设置为 ScrollPane 的视口视图。
我立即注意到的是:
JTextPane 不再根据 JScrollPane 的宽度调整其宽度,即使 JScrollPane 设置为 HORIZONTAL_SCROLLBAR_NEVER。相反,它只是最大化其宽度以将整个文本放入一行(除了“\n”换行符)。
这里有两张图片展示了它的外观:
它应该是这样的:
现在,我发现 this question here 回答了问题的第一部分。需要对JPanel进行子类化,子类需要实现Scrollable。
然而,另一个问题随之而来。 JPanel 内的 JTextPane 不再支持 JPanel 的 BorderLayout.CENTER。如果文本未填充 window 中剩余的 space,则 JTextPane 将不再 自行伸展。
我正在寻找的是解决这个问题的方法。
我希望有一种可靠的方法让 JTextPane 再次遵循 JPanel 的 BorderLayout 并相应地拉伸自身。我不正在寻找一些解决方法,例如将背景设为白色,以便 JTextPane 看起来正在填充框架,而实际上并没有。
下面,我发布了一些代码,您可以在其中尝试不同的配置。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridLayout;
import java.awt.Rectangle;
import javax.swing.JLabel;
import javax.swing.JTextPane;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.Scrollable;
public class MinimalExample extends JFrame {
private JPanel contentPane;
private JPanel outerPanel;
private JPanel firstHeadlinePanel;
private JLabel lblSomeJlabel;
private JPanel secondHeadlinePanel;
private JLabel lblAnotherJlabel;
private JPanel headlinesPanel;
private JTextPane textPane;
private JScrollPane scrollPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MinimalExample frame = new MinimalExample();
frame.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MinimalExample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 230, 350);
contentPane = new JPanel();
contentPane.setBorder(null);
setContentPane(contentPane);
contentPane.setLayout(new GridLayout(1, 0, 0, 0));
outerPanel = new ScrollablePanel();
//outerPanel = new JPanel();
outerPanel.setLayout(new BorderLayout(0, 0));
headlinesPanel = new JPanel();
headlinesPanel.setBorder(new EmptyBorder(5, 0, 0, 0));
outerPanel.add(headlinesPanel, BorderLayout.NORTH);
headlinesPanel.setLayout(new BorderLayout(0, 0));
firstHeadlinePanel = new JPanel();
firstHeadlinePanel.setBorder(new EmptyBorder(0, 0, 5, 0));
headlinesPanel.add(firstHeadlinePanel, BorderLayout.NORTH);
firstHeadlinePanel.setLayout(new BorderLayout(0, 0));
lblSomeJlabel = new JLabel("Some JLabel");
firstHeadlinePanel.add(lblSomeJlabel, BorderLayout.CENTER);
secondHeadlinePanel = new JPanel();
headlinesPanel.add(secondHeadlinePanel, BorderLayout.SOUTH);
secondHeadlinePanel.setLayout(new BorderLayout(0, 0));
lblAnotherJlabel = new JLabel("Another JLabel");
lblAnotherJlabel.setBorder(new EmptyBorder(0, 0, 5, 0));
secondHeadlinePanel.add(lblAnotherJlabel, BorderLayout.NORTH);
textPane = new JTextPane();
textPane.setText(addSomeShortText());
//textPane.setText(addSomeLongText());
outerPanel.add(textPane);
scrollPane = new JScrollPane();
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setViewportView(outerPanel);
contentPane.add(scrollPane);
}
private String addSomeShortText() {
return ("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur facilisis "
+ "dictum ullamcorper. Nulla sed diam sit amet ante pellentesque ultricies in non "
+ "est.");
}
private String addSomeLongText() {
return (" Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur facilisis "
+ "dictum ullamcorper. Nulla sed diam sit amet ante pellentesque ultricies in non "
+ "est. Suspendisse sit amet leo purus. Aenean lacinia ornare quam, a porttitor "
+ "tortor vulputate quis. Quisque suscipit porttitor nisl, mollis suscipit ligula "
+ "laoreet vitae. Nulla quis faucibus dui. Donec vestibulum quam sit amet "
+ "facilisis scelerisque. Pellentesque eu lacus eget nibh elementum venenatis in "
+ "congue magna. Donec et vestibulum magna, ut dapibus sapien.\n"
+ "Donec nec enim magna. Curabitur vitae est est. Etiam non tellus nec ipsum "
+ "dapibus ultricies eget ac lacus. Aliquam eu rhoncus leo. Suspendisse sodales "
+ "nibh eros, vel placerat velit bibendum ut. Vivamus feugiat purus at viverra "
+ "volutpat. Donec tincidunt quam quam, ut mattis enim rutrum sit amet. Integer "
+ "volutpat sit amet metus vitae elementum. Aenean ullamcorper iaculis vulputate. "
+ "Etiam venenatis mollis arcu quis interdum. Quisque et pharetra justo, et "
+ "ullamcorper augue. Phasellus id pellentesque massa. Proin suscipit, mauris et "
+ "congue commodo, mi eros fermentum ante, vel ultricies nisl neque a sem. Mauris "
+ "tristique quis erat vel pulvinar. ");
}
/* Original source of this is here:
* */
private static class ScrollablePanel extends JPanel implements Scrollable {
@Override
public Dimension getPreferredScrollableViewportSize() {
return super.getPreferredSize();
}
@Override
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation,
int direction) {
return 16;
}
@Override
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation,
int direction) {
return 16;
}
@Override
public boolean getScrollableTracksViewportWidth() {
return true;
}
@Override
public boolean getScrollableTracksViewportHeight() {
return false;
}
}
}
在此先感谢您能给我的任何帮助!
好的,基本上,您需要根据 JViewport
和组件的高度更改 getScrollableTracksViewportHeight
的工作方式。
您基本上希望组件跟踪视口的高度(调整自身以匹配),同时视口的高度大于组件的首选高度。当组件的首选高度大于视口的高度时,您希望停止跟踪并允许组件 "overfill" 滚动窗格,例如...
@Override
public boolean getScrollableTracksViewportHeight() {
boolean track = true;
Container parent = getParent();
if (parent instanceof JViewport) {
JViewport viewport = (JViewport) parent;
if (viewport.getHeight() < getPreferredSize().height) {
track = false;
}
}
return track;
}
这就是 "basically" JTable
和 JList
的工作原理
我最近需要在单个 JScrollPane 的视口视图中放置几个组件,其中有一个 JTextPane。 (如果有兴趣,请参阅详细信息
我将所有组件(两个 JPanel 和 JTextPane)放在另一个 JPanel 中,这个 JPanel 具有 BorderLayout LayoutManager,并将该 JPanel 设置为 ScrollPane 的视口视图。
我立即注意到的是:
JTextPane 不再根据 JScrollPane 的宽度调整其宽度,即使 JScrollPane 设置为 HORIZONTAL_SCROLLBAR_NEVER。相反,它只是最大化其宽度以将整个文本放入一行(除了“\n”换行符)。
这里有两张图片展示了它的外观:
它应该是这样的:
现在,我发现 this question here 回答了问题的第一部分。需要对JPanel进行子类化,子类需要实现Scrollable。
然而,另一个问题随之而来。 JPanel 内的 JTextPane 不再支持 JPanel 的 BorderLayout.CENTER。如果文本未填充 window 中剩余的 space,则 JTextPane 将不再 自行伸展。
我正在寻找的是解决这个问题的方法。
我希望有一种可靠的方法让 JTextPane 再次遵循 JPanel 的 BorderLayout 并相应地拉伸自身。我不正在寻找一些解决方法,例如将背景设为白色,以便 JTextPane 看起来正在填充框架,而实际上并没有。
下面,我发布了一些代码,您可以在其中尝试不同的配置。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridLayout;
import java.awt.Rectangle;
import javax.swing.JLabel;
import javax.swing.JTextPane;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.Scrollable;
public class MinimalExample extends JFrame {
private JPanel contentPane;
private JPanel outerPanel;
private JPanel firstHeadlinePanel;
private JLabel lblSomeJlabel;
private JPanel secondHeadlinePanel;
private JLabel lblAnotherJlabel;
private JPanel headlinesPanel;
private JTextPane textPane;
private JScrollPane scrollPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MinimalExample frame = new MinimalExample();
frame.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MinimalExample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 230, 350);
contentPane = new JPanel();
contentPane.setBorder(null);
setContentPane(contentPane);
contentPane.setLayout(new GridLayout(1, 0, 0, 0));
outerPanel = new ScrollablePanel();
//outerPanel = new JPanel();
outerPanel.setLayout(new BorderLayout(0, 0));
headlinesPanel = new JPanel();
headlinesPanel.setBorder(new EmptyBorder(5, 0, 0, 0));
outerPanel.add(headlinesPanel, BorderLayout.NORTH);
headlinesPanel.setLayout(new BorderLayout(0, 0));
firstHeadlinePanel = new JPanel();
firstHeadlinePanel.setBorder(new EmptyBorder(0, 0, 5, 0));
headlinesPanel.add(firstHeadlinePanel, BorderLayout.NORTH);
firstHeadlinePanel.setLayout(new BorderLayout(0, 0));
lblSomeJlabel = new JLabel("Some JLabel");
firstHeadlinePanel.add(lblSomeJlabel, BorderLayout.CENTER);
secondHeadlinePanel = new JPanel();
headlinesPanel.add(secondHeadlinePanel, BorderLayout.SOUTH);
secondHeadlinePanel.setLayout(new BorderLayout(0, 0));
lblAnotherJlabel = new JLabel("Another JLabel");
lblAnotherJlabel.setBorder(new EmptyBorder(0, 0, 5, 0));
secondHeadlinePanel.add(lblAnotherJlabel, BorderLayout.NORTH);
textPane = new JTextPane();
textPane.setText(addSomeShortText());
//textPane.setText(addSomeLongText());
outerPanel.add(textPane);
scrollPane = new JScrollPane();
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setViewportView(outerPanel);
contentPane.add(scrollPane);
}
private String addSomeShortText() {
return ("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur facilisis "
+ "dictum ullamcorper. Nulla sed diam sit amet ante pellentesque ultricies in non "
+ "est.");
}
private String addSomeLongText() {
return (" Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur facilisis "
+ "dictum ullamcorper. Nulla sed diam sit amet ante pellentesque ultricies in non "
+ "est. Suspendisse sit amet leo purus. Aenean lacinia ornare quam, a porttitor "
+ "tortor vulputate quis. Quisque suscipit porttitor nisl, mollis suscipit ligula "
+ "laoreet vitae. Nulla quis faucibus dui. Donec vestibulum quam sit amet "
+ "facilisis scelerisque. Pellentesque eu lacus eget nibh elementum venenatis in "
+ "congue magna. Donec et vestibulum magna, ut dapibus sapien.\n"
+ "Donec nec enim magna. Curabitur vitae est est. Etiam non tellus nec ipsum "
+ "dapibus ultricies eget ac lacus. Aliquam eu rhoncus leo. Suspendisse sodales "
+ "nibh eros, vel placerat velit bibendum ut. Vivamus feugiat purus at viverra "
+ "volutpat. Donec tincidunt quam quam, ut mattis enim rutrum sit amet. Integer "
+ "volutpat sit amet metus vitae elementum. Aenean ullamcorper iaculis vulputate. "
+ "Etiam venenatis mollis arcu quis interdum. Quisque et pharetra justo, et "
+ "ullamcorper augue. Phasellus id pellentesque massa. Proin suscipit, mauris et "
+ "congue commodo, mi eros fermentum ante, vel ultricies nisl neque a sem. Mauris "
+ "tristique quis erat vel pulvinar. ");
}
/* Original source of this is here:
* */
private static class ScrollablePanel extends JPanel implements Scrollable {
@Override
public Dimension getPreferredScrollableViewportSize() {
return super.getPreferredSize();
}
@Override
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation,
int direction) {
return 16;
}
@Override
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation,
int direction) {
return 16;
}
@Override
public boolean getScrollableTracksViewportWidth() {
return true;
}
@Override
public boolean getScrollableTracksViewportHeight() {
return false;
}
}
}
在此先感谢您能给我的任何帮助!
好的,基本上,您需要根据 JViewport
和组件的高度更改 getScrollableTracksViewportHeight
的工作方式。
您基本上希望组件跟踪视口的高度(调整自身以匹配),同时视口的高度大于组件的首选高度。当组件的首选高度大于视口的高度时,您希望停止跟踪并允许组件 "overfill" 滚动窗格,例如...
@Override
public boolean getScrollableTracksViewportHeight() {
boolean track = true;
Container parent = getParent();
if (parent instanceof JViewport) {
JViewport viewport = (JViewport) parent;
if (viewport.getHeight() < getPreferredSize().height) {
track = false;
}
}
return track;
}
这就是 "basically" JTable
和 JList
的工作原理