JTextfield Fonts and Attributes problem
So I am creating a program that changes the JTextField depending on what the user Chooses. So its pretty much like a Word Document with fonts(from a JComboBox), sizes, and attributes(Bold...etc). Obviously mine is very small and only works with a single line(A JTextField). The Problem i'm getting is that After I've written some things down into the field with specific attributes and I want to add more words down with different attributes, it changes the whole text field rather than just the new part I added. I know the problem with it is
Writer.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
if((e.getKeyChar() >= e.VK_A && e.getKeyChar()<= e.VK_Z) || (e.getKeyChar() >= 'a' && e.getKeyChar()<='z')|| e.getKeyChar() == '\b' ) // Checks to make sure No Numbers
{
Writer.setEditable(true);
}
else
{
Writer.setEditable(false);
}
if(font.equals("Arial"))
{
if(size.equals("8"))
{
setSize = 8;
}
else if(size.equals("10"))
{
setSize = 10;
}
else if(size.equals("12"))
{
setSize = 12;
}
if(color.equals("Black"))
{
setColor = Color.BLACK;
}
else if(color.equals("Blue"))
{
setColor = Color.BLUE;
}
else if(color.equals("Red"))
{
setColor = Color.red;
}
开发者_运维技巧 Font font = new Font("Arial", setAttribute, setSize);
Writer.setFont(font); // I Know that this sets the font everytime, so i'm pretty sure this is where my problem is.
Writer.setForeground(setColor);
}
Any ideas on how I can make the Change so it newly entered characters can have different fonts than the previous characters.
JComponents for styled text - How to Use Editor Panes and Text Panes, examples here, here, some of examples on this forum
JTextFields allow the use of HTML. It might take a bit of work to parse and insert new html code, but you might be able to do it that way.
There's a list of WYSIWYG text editors for Java here. I especially like metaphase editor, based on Charles Bell's HTMLDocumentEditor
.
精彩评论