本文整理了Java中javax.swing.text.Element
类的一些代码示例,展示了Element
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Element
类的具体详情如下:
包路径:javax.swing.text.Element
类名称:Element
暂无
代码示例来源:origin: SonarSource/sonarqube
protected String getTextLineNumber(int rowStartOffset) {
Element root = component.getDocument().getDefaultRootElement();
int index = root.getElementIndex(rowStartOffset);
Element line = root.getElement(index);
if (line.getStartOffset() == rowStartOffset)
return String.valueOf(index + 1);
else
return "";
}
代码示例来源:origin: groovy/groovy-core
private int calcEndParse(int offset) {
MultiLineRun mlr = getMultiLineRun(offset);
if (mlr != null) {
// means we're in middle of mlr, so end is at end of mlr
offset = mlr.end();
}
else {
// otherwise, latest position in line not part of mlr
offset = styledDocument.getParagraphElement(offset).getEndOffset();
mlr = getMultiLineRun(offset);
offset = mlr == null ? offset : mlr.end();
}
return offset;
}
代码示例来源:origin: SonarSource/sonarqube
Rectangle r = component.modelToView(rowStartOffset);
int lineHeight = fontMetrics.getHeight();
int y = r.y + r.height;
fonts = new HashMap<>();
Element root = component.getDocument().getDefaultRootElement();
int index = root.getElementIndex(rowStartOffset);
Element line = root.getElement(index);
for (int i = 0; i < line.getElementCount(); i++) {
Element child = line.getElement(i);
AttributeSet as = child.getAttributes();
String fontFamily = (String) as.getAttribute(StyleConstants.FontFamily);
Integer fontSize = (Integer) as.getAttribute(StyleConstants.FontSize);
String key = fontFamily + fontSize;
fm = component.getFontMetrics(font);
fonts.put(key, fm);
代码示例来源:origin: skylot/jadx
fonts = new HashMap<>();
Element root = codeArea.getDocument().getDefaultRootElement();
int index = root.getElementIndex(rowStartOffset);
Element line = root.getElement(index);
for (int i = 0; i < line.getElementCount(); i++) {
Element child = line.getElement(i);
AttributeSet as = child.getAttributes();
String fontFamily = (String) as.getAttribute(StyleConstants.FontFamily);
Integer fontSize = (Integer) as.getAttribute(StyleConstants.FontSize);
String key = fontFamily + fontSize;
FontMetrics fm = fonts.computeIfAbsent(key, k -> {
代码示例来源:origin: groovy/groovy-core
public int findTabLocation(int offset) {
// find first {
boolean cont = true;
while (offset > -1 && cont) {
Element el = doc.getCharacterElement(offset);
Object color =
el.getAttributes().getAttribute(StyleConstants.Foreground);
if (!COMMENT_COLOR.equals(color)) {
cont = segment.array[offset] != '{' &&
segment.array[offset] != '}';
}
offset -= cont ? 1 : 0;
}
if (offset > -1 && segment.array[offset] == '{') {
while (offset > -1 &&
!Character.isWhitespace(segment.array[offset--])) {
}
}
int index = offset < 0 || segment.array[offset] == '}' ? -4 : 0;
if (offset > -1) {
Element top = doc.getDefaultRootElement();
offset = top.getElement(top.getElementIndex(offset)).getStartOffset();
while (Character.isWhitespace(segment.array[offset++])) {
index++;
}
}
return index;
}
}
代码示例来源:origin: stackoverflow.com
Element rootElement = doc.getDefaultRootElement();
int line = rootElement.getElementIndex( offset );
int i = rootElement.getElement(line).getStartOffset();
StringBuilder whiteSpace = new StringBuilder(text);
String temp = doc.getText(i, 1);
AbstractDocument doc = (AbstractDocument)textArea.getDocument();
doc.setDocumentFilter( new EndOfLineFilter() );
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new JScrollPane( textArea ) );
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
代码示例来源:origin: stackoverflow.com
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestTextAreaPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
setLayout(new BorderLayout());
textArea = new JTextArea(20, 100);
textArea.setWrapStyleWord(true);
textArea.read( br, null );
br.close();
int startIndex = textArea.viewToModel(p);
int endIndex = textArea.viewToModel(p);
Element root = textArea.getDocument().getDefaultRootElement();
int startLine = root.getElementIndex( startIndex );
int endLine = root.getElementIndex( endIndex );
int lines = endLine - startLine + 1;
System.out.println(lines);
代码示例来源:origin: stackoverflow.com
textArea.setText("This is just a line of text, "
+ "This is just a line of text, This is just a line of text, "
+ "This is just a line of text,\nThis is just a line of text,"
public void actionPerformed(ActionEvent e) {
textArea.setFont(textArea.getFont().deriveFont(20f));
frame.pack();
scrollPane.setViewportView(textArea);
frame.add(scrollPane);
frame.add(button, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(100, 100);
frame.pack();
frame.setVisible(true);
Element paragraph = textArea.getDocument().getDefaultRootElement();
int contentCount = paragraph.getElementCount();
for (int i = 0; i < contentCount; i++) {
Element e = paragraph.getElement(i);
int rangeStart = e.getStartOffset();
int rangeEnd = e.getEndOffset();
String line;
try {
line = textArea.getText(rangeStart, rangeEnd - rangeStart);
System.out.println(line);
} catch (BadLocationException ex) {
代码示例来源:origin: stackoverflow.com
public JComponent makeUI() {
JTextArea textArea = new JTextArea(8,0);
textArea.setText("> aaa\n> ");
((AbstractDocument)textArea.getDocument()).setDocumentFilter(
new NonEditableLineDocumentFilter());
JPanel p = new JPanel(new BorderLayout());
p.add(new JScrollPane(textArea), BorderLayout.NORTH);
return p;
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new OnlyEditCurrentLineTest().makeUI());
f.setSize(320,240);
f.setLocationRelativeTo(null);
f.setVisible(true);
String text, AttributeSet attrs) throws BadLocationException {
Document doc = fb.getDocument();
Element root = doc.getDefaultRootElement();
int count = root.getElementCount();
int index = root.getElementIndex(offset);
Element cur = root.getElement(index);
int promptPosition = cur.getStartOffset()+PROMPT.length();
String cmd = doc.getText(promptPosition, offset-promptPosition);
if(cmd.isEmpty()) {
text = "\n"+PROMPT;
代码示例来源:origin: stackoverflow.com
textArea.setEditable(false);
JPanel p = new JPanel(new BorderLayout());
p.add(new JScrollPane(textArea));
p.add(new JButton(new AbstractAction("add") {
@Override
public void actionPerformed(ActionEvent e) {
try {
Document doc = textArea.getDocument();
Element root = doc.getDefaultRootElement();
Element element = root.getElement(2);
int start = element.getStartOffset();
int end = element.getEndOffset();
System.out.println(doc.getText(start, end - start));
} catch (BadLocationException ex) {
ex.printStackTrace();
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new ElementEndOffsetTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
代码示例来源:origin: stackoverflow.com
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
getContentPane().add(jScrollPane1);
myFormattedOuput.setContentType("text/html");
myFormattedOuput.setEditable(false);
jScrollPane1.setViewportView(myFormattedOuput);
myFormattedOuput.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
Document doc = myFormattedOuput.getDocument();
Element html = doc.getRootElements()[0];
Element body = html.getElement(1);
Element table = body.getElement(1);
try {
Element tr2 = table.getElement(1);
Element tr2td1 = tr2.getElement(0);
doc.insertString(tr2td1.getStartOffset(), "1: 123,456",
SimpleAttributeSet.EMPTY);
Element tr3 = table.getElement(2);
Element tr3td1 = tr3.getElement(0);
doc.insertString(tr3td1.getStartOffset(), "2: 765.123",
SimpleAttributeSet.EMPTY);
} catch (BadLocationException e) {
代码示例来源:origin: stackoverflow.com
String kind = elem.getName();
if (kind != null) {
if (kind.equals(AbstractDocument.ContentElementName)) {
JFrame frame=new JFrame("Custom default Tab Size in EditorKit example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JEditorPane edit=new JEditorPane();
edit.setEditorKit(new TabSizeEditorKit());
try {
edit.getDocument().insertString(0,"1\t2\t3\t4\t5", new SimpleAttributeSet());
} catch (BadLocationException e) {
e.printStackTrace();
frame.getContentPane().add(new JScrollPane(edit));
frame.setSize(300,100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
代码示例来源:origin: stackoverflow.com
textArea.setEditable(false);
JPanel p = new JPanel(new BorderLayout());
p.add(new JScrollPane(textArea));
p.add(new JButton(new AbstractAction("add") {
@Override public void actionPerformed(ActionEvent e) {
Document doc = textArea.getDocument();
Element root = doc.getDefaultRootElement();
Optional.ofNullable(root.getElement(2)).ifPresent(el -> {
try {
doc.insertString(el.getEndOffset() - 1, " my text here", null);
} catch (BadLocationException ex) {
ex.printStackTrace();
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new ElementEndOffsetTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
代码示例来源:origin: stackoverflow.com
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200,200);
getContentPane().setLayout(new BorderLayout());
JTextPane jtp = new JTextPane();
jtp.setEditorKit(new WrapEditorKit());
JScrollPane jsp = new JScrollPane(jtp);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
getContentPane().add(jsp, BorderLayout.CENTER);
jtp.setText("ExampleOfTheWrapLongWordWithoutSpaces");
setVisible(true);
String kind = elem.getName();
if (kind != null) {
if (kind.equals(AbstractDocument.ContentElementName)) {
代码示例来源:origin: stackoverflow.com
JComboBox cBox = createComboBox(textArea);
JFrame frame = new JFrame();
frame.add(new JScrollPane(textArea));
frame.add(cBox, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
public void actionPerformed(ActionEvent e) {
int index = (Integer)cBox.getSelectedItem();
textArea.setCaretPosition(
textArea.getDocument().getDefaultRootElement().getElement(index).getStartOffset());
textArea.requestFocusInWindow();
textArea.setMargin(new Insets(15, 15, 15, 15));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
代码示例来源:origin: stackoverflow.com
final JFrame frm = new JFrame("Editor pane test");
pane.setContentType("text/html");
pane.setText("<html>Here is the text with a <a href=\"http://google.com\">link</a></html>");
frm.add(new JScrollPane(pane));
final JButton btn = new JButton(new AbstractAction("Find link") {
final HTMLDocument doc = (HTMLDocument) pane.getDocument();
final Collection<String> links = new LinkedHashSet<String>();
final AttributeSet a = el.getAttributes();
final AttributeSet anchor = (AttributeSet)a.getAttribute(HTML.Tag.A);
if (anchor != null) {
links.add((String)anchor.getAttribute(HTML.Attribute.HREF));
frm.add(btn, BorderLayout.EAST);
frm.pack();
frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frm.setVisible(true);
代码示例来源:origin: stackoverflow.com
JFrame frame = new JFrame();
JEditorPane edPane = new JEditorPane();
edPane.setContentType("text/html");
System.out.println(edPane.getText());
edPane.setEditorKit(hek);
HTMLDocument doc = (HTMLDocument) edPane.getDocument();
for( int i = 0; i < roots[0].getElementCount(); i++ ) {
Element element = roots[0].getElement( i );
if( element.getAttributes().getAttribute( StyleConstants.NameAttribute ) == HTML.Tag.BODY ) {
body = element;
break;
frame.add(edPane);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
代码示例来源:origin: stackoverflow.com
public Smiley() throws Exception {
p.setEditorKit(new StyledEditorKit());
getContentPane().add(p, BorderLayout.CENTER);
SimpleAttributeSet attrs = new SimpleAttributeSet();
StyleConstants.setIcon(attrs, getImage());
p.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
StyledDocument doc = (StyledDocument) p.getDocument();
String text = doc.getText(0, p.getDocument().getLength());
int index = text.indexOf(":)");
int start = 0;
while (index > -1) {
Element el = doc.getCharacterElement(index);
if (StyleConstants.getIcon(el.getAttributes()) == null) {
doc.remove(index, 2);
SimpleAttributeSet attrs = new SimpleAttributeSet();
StyleConstants.setIcon(attrs, getImage());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400, 400);
test11.show();
代码示例来源:origin: bobbylight/RSyntaxTextArea
g.setFont(font!=null ? font : textComponent.getFont());
Document doc = textComponent.getDocument();
rootElement = doc.getDefaultRootElement();
numDocLines = rootElement.getElementCount(); // The number of lines in our document.
currentDocLineNumber = 0; // The line number of the document we're currently on.
int startingOffset = 0; // Used when a line is so long it has to be wrapped.
Element currentLine = rootElement.getElement(currentDocLineNumber);
int currentLineStart = currentLine.getStartOffset();
int currentLineEnd = currentLine.getEndOffset();
doc.getText(currentLineStart+startingOffset, currentLineEnd-(currentLineStart+startingOffset),
currentLineSeg);
} catch (BadLocationException ble) {
currentPos++;
try {
doc.getText(currentLineStart+startingOffset, currentPos, currentLineSeg);
} catch (BadLocationException ble) {
System.err.println(ble);
代码示例来源:origin: stackoverflow.com
public class Sample extends JFrame {
public Sample(){
JEditorPane pane = new JEditorPane();
pane.setContentType("text/html");
pane.setText("<ol id='foo'><li>One</li><li>Two</li></ol>");
HTMLDocument doc = (HTMLDocument) pane.getDocument();
add(pane);
//Get the ref of foo element
Element ele=doc.getElement("foo");
ListView view=new ListView(ele);
System.out.println(ele.getElementCount());
try{
doc.insertBeforeEnd(ele.getElement(0), "<ul><li>Test");
}catch(Exception ex){}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,300);
setVisible(true);
}
}
内容来源于网络,如有侵权,请联系作者删除!