내 응용 프로그램에서는 JTextPane의 확장 인 사용자 지정 구성 요소를 사용합니다. 스타일을 위해 이것을 확장해야합니다. 이 구성 요소는 다중 줄이 아닌 JTextField의 역할을하기 때문에 단어 줄 바꿈을 해제 할 수있었습니다.JTextPane - JScrollPane을 사용하지 않고 표시되는 텍스트 스크롤
그러나 내가 직면 한 문제는 JTextPane이 텍스트가 보이는 영역 바깥에있는 경우 JScrollpane 내부에서 사용되지 않을 때 JTextPane이 비 단어 래핑 된 텍스트를 매우 잘 처리하지 않는 것입니다. 표시 영역 외부의 텍스트는 JTextField를 사용할 때와 같이 이동할 수 없습니다.
JTextComponent를 사용하는 많은 레거시 코드가 있으므로 JScrollpane을 사용하고 싶지 않습니다.
내 질문은 입력 위치가 표시 영역에 맞도록 유지하는 방법으로 입력 위치가 보이는 가장자리에 도달하거나 보이는 영역 바깥에있는 텍스트를 선택하려고 할 때 해당 텍스트가 JTextField는 않습니다.
내 문제의 작동 예제는 아래와 같습니다. JTextPane 위에 JTextField를 표시합니다. 텍스트 필드에 입력하면 보이는 영역이 텍스트와 함께 움직이는 것을 볼 수 있습니다. 텍스트 창에 입력하면 이런 일은 발생하지 않습니다.
또한 자바 7에서 이것을 수행합니다.
감사합니다.
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class NoAutoScroll {
public NoAutoScroll() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField textField = new JTextField();
textField.setText("This text field can show the text outside the visible area.");
textField.setPreferredSize(new Dimension(150, 30));
JTextPane textPane = new JTextPane();
textPane.setEditorKit(new MyEditorKit());
textPane.setText("This text pane cannot show the text outside the visible area.");
textPane.setPreferredSize(new Dimension(150, 30));
textPane.setBorder(textField.getBorder());
JPanel mainPanel = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
mainPanel.add(new JLabel("JTextField"), constraints);
constraints.gridx = 1;
constraints.gridy = 0;
mainPanel.add(textField, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
mainPanel.add(new JLabel("JTextPane"), constraints);
constraints.gridx = 1;
constraints.gridy = 1;
mainPanel.add(textPane, constraints);
frame.add(mainPanel);
frame.pack();
frame.setVisible(true);
}
public class MyViewFactory implements ViewFactory {
private final class NonBreakingLabelView extends LabelView {
private NonBreakingLabelView(Element elem) {
super(elem);
}
@Override
public int getBreakWeight(int axis, float pos, float len){
return BadBreakWeight;
}
}
@Override
public View create(final Element elem) {
String kind = elem.getName();
if (kind != null) {
if (kind.equals(AbstractDocument.ContentElementName)) {
return new NonBreakingLabelView(elem);
} else if (kind.equals(AbstractDocument.ParagraphElementName)) {
return new ParagraphView(elem);
} else if (kind.equals(AbstractDocument.SectionElementName)) {
return new BoxView(elem, View.Y_AXIS);
} else if (kind.equals(StyleConstants.ComponentElementName)) {
return new ComponentView(elem);
} else if (kind.equals(StyleConstants.IconElementName)) {
return new IconView(elem);
}
}
// default to text display
return new LabelView(elem);
}
}
@SuppressWarnings("serial")
public class MyEditorKit extends StyledEditorKit {
@Override
public ViewFactory getViewFactory() {
return new MyViewFactory();
}
}
public static void main(String[] args) {
new NoAutoScroll();
}
}
+1 동의합니다. 가시의 JScrollBars와 비 표시의 JScrollBars의 양쪽 모두를 가지는 JScrollPane입니다. – StanislavL