[英]Changing text alignment of specific line in JTextPane
I am creating a program for screenplay writing and I have come up on a problem where I need to change the text alignment inside of a specific line JPanel
based on what's typed inside of the line.我正在创建一个用于编写剧本的程序,我遇到了一个问题,我需要根据行内键入的内容更改特定行
JPanel
内的文本 alignment。 I've been able to pull the actual text from the line and put it into a string, Just haven't been able to align a single line.我已经能够从行中提取实际文本并将其放入字符串中,只是无法对齐一行。
This is what I've tried so far, but it's giving me an illegal state exception.到目前为止,这是我尝试过的方法,但它给了我一个非法的 state 异常。
StyledDocument doc = textPane1.getStyledDocument();
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
textPane1.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {
}
@Override
public void insertUpdate(DocumentEvent e) {
if(ReadText.currentLine(textPane1).contains("INT.")){
doc.setParagraphAttributes(0,1,center,false);
System.out.println("Centered");
}
}
@Override
public void changedUpdate(DocumentEvent arg0) {
}
});
What is the best method of doing this?这样做的最佳方法是什么?
it's giving me an illegal state exception.
它给了我一个非法的 state 异常。
You can't update the Document
in a Document
listener.您不能在
Document
侦听器中更新Document
。
You need to wrap the code in a SwingUtilities.invokeLater()
so the code gets added to the end of the EDT and is executed after all the text has been added to the Document.您需要将代码包装在
SwingUtilities.invokeLater()
中,以便将代码添加到 EDT 的末尾,并在所有文本添加到文档后执行。
doc.setParagraphAttributes(0,1,center,false);
Not sure if that is what you want.不确定这是否是您想要的。 That will always set the attribute of the first line of the Document since you are using an offset of 0.
这将始终设置文档第一行的属性,因为您使用的偏移量为 0。
Here is a basic example that allows you to have different alignment for each paragraph in the Document.这是一个基本示例,它允许您为文档中的每个段落设置不同的 alignment。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class TextPaneAttributes extends JPanel
{
public TextPaneAttributes()
{
setLayout( new BorderLayout() );
JTextPane textPane = new JTextPane();
textPane.setText( "one\ntwo\nthree\nfour\nfive\nsix\nseven\neight" );
// DefaultHighlighter highlighter = (DefaultHighlighter)textPane.getHighlighter();
// highlighter.setDrawsLayeredHighlights(false);
// Define some character and paragraph attributes
SimpleAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setBold(keyWord, true);
SimpleAttributeSet green = new SimpleAttributeSet();
StyleConstants.setForeground(green, Color.GREEN);
System.out.println(green);
System.out.println(textPane.getInputAttributes());
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
SimpleAttributeSet left = new SimpleAttributeSet();
StyleConstants.setAlignment(left, StyleConstants.ALIGN_LEFT);
// Change attributes on some existing text
StyledDocument doc = textPane.getStyledDocument();
textPane.setCaretPosition(12);
doc.setCharacterAttributes(0, 3, keyWord, false);
doc.setCharacterAttributes(8, 5, green, false);
doc.setParagraphAttributes(20, 1 , center, false);
// Add some text with attributes
try
{
doc.insertString(doc.getLength(), "\nNormal text", null);
doc.insertString(doc.getLength(), "\nGreen text centered", green);
// int pos = doc.getLength();
// doc.insertString(pos, "more green text", doc.getCharacterElement(pos-1).getAttributes());
doc.setParagraphAttributes(doc.getLength(), 1 , center, false);
doc.insertString(doc.getLength(), "\nKeyword text", keyWord);
doc.setParagraphAttributes(doc.getLength(), 1 , left, false);
// Newly typed text at the end of the document will inherit the
// "keyword" attributes unless we remove the attributes
textPane.setCaretPosition(doc.getLength());
textPane.getInputAttributes().removeAttributes(keyWord);
}
catch(Exception e) {}
// Add text pane to frame
JScrollPane scrollPane = new JScrollPane( textPane );
scrollPane.setPreferredSize( new Dimension( 200, 250 ) );
add( scrollPane );
// Create a Button panel
JPanel buttons = new JPanel();
add(buttons, BorderLayout.PAGE_END);
// Add a Bold button
JButton bold = new JButton( new StyledEditorKit.BoldAction() );
buttons.add( bold );
// Add Right Alignment button
JButton right = new JButton( new StyledEditorKit.AlignmentAction("Align Right", StyleConstants.ALIGN_RIGHT) );
buttons.add( right );
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TextPaneAttributes());
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater( () -> createAndShowGUI() );
/*
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
*/
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.