How can I prevent JTextPane from resetting font settings if the initial text ends with a linefeed? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Java JTextPane Change Font of Selected TextHow does one print total number of pages in a JTextPane footer?JTextPane: How to set the font sizeChanging the font of a text in a JTextPane dynamicallyPrevent HTML JTextPane from formatting text inputHow to append Text in JTextPane?Significant performance differences in JTextPane when text contains linefeedclickable text from jTextPanePasting the styled text in to JTextPane is always pasting the text at the end of JTextPaneJTextPane how to set text in superscript?
Why complex landing gears are used instead of simple, reliable and light weight muscle wire or shape memory alloys?
Is there a verb for listening stealthily?
How to make triangles with rounded sides and corners? (squircle with 3 sides)
Adapting the Chinese Remainder Theorem (CRT) for integers to polynomials
How could a hydrazine and N2O4 cloud (or it's reactants) show up in weather radar?
The test team as an enemy of development? And how can this be avoided?
Why are current probes so expensive?
One-one communication
How do I say "this must not happen"?
Can gravitational waves pass through a black hole?
malloc in main() or malloc in another function: allocating memory for a struct and its members
Is this Half-dragon Quaggoth boss monster balanced?
What is a more techy Technical Writer job title that isn't cutesy or confusing?
Why is there so little support for joining EFTA in the British parliament?
Where did Ptolemy compare the Earth to the distance of fixed stars?
Why did Bronn offer to be Tyrion Lannister's champion in trial by combat?
As a dual citizen, my US passport will expire one day after traveling to the US. Will this work?
Why not use the yoke to control yaw, as well as pitch and roll?
How can I list files in reverse time order by a command and pass them as arguments to another command?
Fit odd number of triplets in a measure?
Problem with display of presentation
My mentor says to set image to Fine instead of RAW — how is this different from JPG?
What does 丫 mean? 丫是什么意思?
How do Java 8 default methods hеlp with lambdas?
How can I prevent JTextPane from resetting font settings if the initial text ends with a linefeed?
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Java JTextPane Change Font of Selected TextHow does one print total number of pages in a JTextPane footer?JTextPane: How to set the font sizeChanging the font of a text in a JTextPane dynamicallyPrevent HTML JTextPane from formatting text inputHow to append Text in JTextPane?Significant performance differences in JTextPane when text contains linefeedclickable text from jTextPanePasting the styled text in to JTextPane is always pasting the text at the end of JTextPaneJTextPane how to set text in superscript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I've run into a problem with JTextPane. The code I am working with sets a number of font attributes, such as BOLD, ITALIC, etc. But if the initial text ends with a single linefeed, and the user clicks on the last line, or is sent to the last line, the default font settings appear for any additional text the user types.
Specifically, this text works as I would expect: jTextPane.setText(String.format("Test This"));.
This text does not:
jTextPane.setText(String.format("Test This%n%n%n"));
I think that JTextPane may consider this to be a new paragraph.
If so, I would like to either
a.) Know how to set a universal font that applies across the entire JTextPane instance's paragraphs.
or
b.) tell the JTextPane instance to consider all of its editable area to be one paragraph.
Here is a toy program to show you what I mean. If you run this, and start typing at the end of the text the font will be whatever the default is for your Swing implementation.
I have also tried setting the document model of the JTextPane and using a Font instance in the JTextPane constructor. The results are the same.
Another alternative is to use a JTextArea instance instead, but this is very complex code and I hesitate to make a change that may break some other area of the application than the one I am working in.
import javax.swing.*;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import java.awt.*;
public class JTextPaneExampleOne
public static void main(String args[])
JFrame frame = new JFrame("JTextPane Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = frame.getContentPane();
JTextPane pane = new JTextPane();
String welcomeString = String.format("Welcome%n%n%nStranger!%n%n%n");
pane.setText( welcomeString );
pane.invalidate();
SimpleAttributeSet attributeSet = new SimpleAttributeSet();
StyleConstants.setBold(attributeSet, true);
StyleConstants.setItalic(attributeSet, true);
StyleConstants.setForeground(attributeSet, Color.red);
pane.setSelectionStart( 0 );
pane.setSelectionEnd( pane.getText().length() );
pane.setParagraphAttributes( attributeSet, true );
pane.setSelectionStart( pane.getText().length() );
pane.validate();
JScrollPane scrollPane = new JScrollPane(pane);
cp.add(scrollPane, BorderLayout.CENTER);
frame.setSize(400, 300);
frame.setVisible(true);
jtextpane sprint
add a comment |
I've run into a problem with JTextPane. The code I am working with sets a number of font attributes, such as BOLD, ITALIC, etc. But if the initial text ends with a single linefeed, and the user clicks on the last line, or is sent to the last line, the default font settings appear for any additional text the user types.
Specifically, this text works as I would expect: jTextPane.setText(String.format("Test This"));.
This text does not:
jTextPane.setText(String.format("Test This%n%n%n"));
I think that JTextPane may consider this to be a new paragraph.
If so, I would like to either
a.) Know how to set a universal font that applies across the entire JTextPane instance's paragraphs.
or
b.) tell the JTextPane instance to consider all of its editable area to be one paragraph.
Here is a toy program to show you what I mean. If you run this, and start typing at the end of the text the font will be whatever the default is for your Swing implementation.
I have also tried setting the document model of the JTextPane and using a Font instance in the JTextPane constructor. The results are the same.
Another alternative is to use a JTextArea instance instead, but this is very complex code and I hesitate to make a change that may break some other area of the application than the one I am working in.
import javax.swing.*;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import java.awt.*;
public class JTextPaneExampleOne
public static void main(String args[])
JFrame frame = new JFrame("JTextPane Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = frame.getContentPane();
JTextPane pane = new JTextPane();
String welcomeString = String.format("Welcome%n%n%nStranger!%n%n%n");
pane.setText( welcomeString );
pane.invalidate();
SimpleAttributeSet attributeSet = new SimpleAttributeSet();
StyleConstants.setBold(attributeSet, true);
StyleConstants.setItalic(attributeSet, true);
StyleConstants.setForeground(attributeSet, Color.red);
pane.setSelectionStart( 0 );
pane.setSelectionEnd( pane.getText().length() );
pane.setParagraphAttributes( attributeSet, true );
pane.setSelectionStart( pane.getText().length() );
pane.validate();
JScrollPane scrollPane = new JScrollPane(pane);
cp.add(scrollPane, BorderLayout.CENTER);
frame.setSize(400, 300);
frame.setVisible(true);
jtextpane sprint
add a comment |
I've run into a problem with JTextPane. The code I am working with sets a number of font attributes, such as BOLD, ITALIC, etc. But if the initial text ends with a single linefeed, and the user clicks on the last line, or is sent to the last line, the default font settings appear for any additional text the user types.
Specifically, this text works as I would expect: jTextPane.setText(String.format("Test This"));.
This text does not:
jTextPane.setText(String.format("Test This%n%n%n"));
I think that JTextPane may consider this to be a new paragraph.
If so, I would like to either
a.) Know how to set a universal font that applies across the entire JTextPane instance's paragraphs.
or
b.) tell the JTextPane instance to consider all of its editable area to be one paragraph.
Here is a toy program to show you what I mean. If you run this, and start typing at the end of the text the font will be whatever the default is for your Swing implementation.
I have also tried setting the document model of the JTextPane and using a Font instance in the JTextPane constructor. The results are the same.
Another alternative is to use a JTextArea instance instead, but this is very complex code and I hesitate to make a change that may break some other area of the application than the one I am working in.
import javax.swing.*;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import java.awt.*;
public class JTextPaneExampleOne
public static void main(String args[])
JFrame frame = new JFrame("JTextPane Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = frame.getContentPane();
JTextPane pane = new JTextPane();
String welcomeString = String.format("Welcome%n%n%nStranger!%n%n%n");
pane.setText( welcomeString );
pane.invalidate();
SimpleAttributeSet attributeSet = new SimpleAttributeSet();
StyleConstants.setBold(attributeSet, true);
StyleConstants.setItalic(attributeSet, true);
StyleConstants.setForeground(attributeSet, Color.red);
pane.setSelectionStart( 0 );
pane.setSelectionEnd( pane.getText().length() );
pane.setParagraphAttributes( attributeSet, true );
pane.setSelectionStart( pane.getText().length() );
pane.validate();
JScrollPane scrollPane = new JScrollPane(pane);
cp.add(scrollPane, BorderLayout.CENTER);
frame.setSize(400, 300);
frame.setVisible(true);
jtextpane sprint
I've run into a problem with JTextPane. The code I am working with sets a number of font attributes, such as BOLD, ITALIC, etc. But if the initial text ends with a single linefeed, and the user clicks on the last line, or is sent to the last line, the default font settings appear for any additional text the user types.
Specifically, this text works as I would expect: jTextPane.setText(String.format("Test This"));.
This text does not:
jTextPane.setText(String.format("Test This%n%n%n"));
I think that JTextPane may consider this to be a new paragraph.
If so, I would like to either
a.) Know how to set a universal font that applies across the entire JTextPane instance's paragraphs.
or
b.) tell the JTextPane instance to consider all of its editable area to be one paragraph.
Here is a toy program to show you what I mean. If you run this, and start typing at the end of the text the font will be whatever the default is for your Swing implementation.
I have also tried setting the document model of the JTextPane and using a Font instance in the JTextPane constructor. The results are the same.
Another alternative is to use a JTextArea instance instead, but this is very complex code and I hesitate to make a change that may break some other area of the application than the one I am working in.
import javax.swing.*;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import java.awt.*;
public class JTextPaneExampleOne
public static void main(String args[])
JFrame frame = new JFrame("JTextPane Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = frame.getContentPane();
JTextPane pane = new JTextPane();
String welcomeString = String.format("Welcome%n%n%nStranger!%n%n%n");
pane.setText( welcomeString );
pane.invalidate();
SimpleAttributeSet attributeSet = new SimpleAttributeSet();
StyleConstants.setBold(attributeSet, true);
StyleConstants.setItalic(attributeSet, true);
StyleConstants.setForeground(attributeSet, Color.red);
pane.setSelectionStart( 0 );
pane.setSelectionEnd( pane.getText().length() );
pane.setParagraphAttributes( attributeSet, true );
pane.setSelectionStart( pane.getText().length() );
pane.validate();
JScrollPane scrollPane = new JScrollPane(pane);
cp.add(scrollPane, BorderLayout.CENTER);
frame.setSize(400, 300);
frame.setVisible(true);
jtextpane sprint
jtextpane sprint
asked Mar 9 at 0:45
Warren WeisWarren Weis
63
63
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072889%2fhow-can-i-prevent-jtextpane-from-resetting-font-settings-if-the-initial-text-end%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072889%2fhow-can-i-prevent-jtextpane-from-resetting-font-settings-if-the-initial-text-end%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown