Original Post
I finally got styles to work today for my project, but then I look at changing the text color and it becomes rather erm... :S
Anyone care to look at it quick and give ideas how to get the foreground color to change?
Thanks for any help.
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class F extends JFrame implements KeyListener, ActionListener {
static boolean colorTeller = false;
static JTextPane textPane;
JTextField user;
Container meowmix = getContentPane();
JButton bold;
JButton italic;
JButton underline;
JButton color;
JPanel stylePanel;
Document doc;
int pickStyle = 0;
String[] styles = {"regular", "italic", "bold", "underline"};
public F() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bold = new JButton("B");
bold.setPreferredSize(new Dimension(43, 30));
italic = new JButton("I");
italic.setPreferredSize(new Dimension(37, 30));
underline = new JButton("U");
underline.setPreferredSize(new Dimension(43, 30));
color = new JButton("C");
color.setPreferredSize(new Dimension(43, 30));
stylePanel = new JPanel();
stylePanel.add(new JPanel());
stylePanel.add(bold);
stylePanel.add(italic);
stylePanel.add(underline);
stylePanel.add(color);
user = new JTextField(13);
textPane = createTextPane();
textPane.setEditable(false);
JScrollPane jsp = new JScrollPane(textPane);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jsp.setPreferredSize(new Dimension(300, 200));
bold.addActionListener(this);
italic.addActionListener(this);
underline.addActionListener(this);
color.addActionListener(this);
meowmix.add(jsp, BorderLayout.NORTH);
meowmix.add(stylePanel, BorderLayout.CENTER);
meowmix.add(user, BorderLayout.SOUTH);
user.addKeyListener(this);
pack();
show();
}
public static void colorSetter(Color c) {
Style s;
Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
Style regular = textPane.addStyle("regular", def);
//StyleConstants.setForeground(s, c);
System.out.println(c);
}
public void actionPerformed(ActionEvent e) {
JButton jb = (JButton) e.getSource();
Style s;
Style def = StyleContext.getDefaultStyleContext().
getStyle(StyleContext.DEFAULT_STYLE);
Style regular = textPane.addStyle("regular", def);
if(jb == bold) {
if(pickStyle != 2) {
pickStyle = 2;
s = textPane.addStyle("bold", regular);
StyleConstants.setBold(s, true);
underline.setBorderPainted(true);
italic.setBorderPainted(true);
bold.setBorderPainted(false);
}
else {
s = textPane.addStyle("bold", regular);
StyleConstants.setBold(s, false);
bold.setBorderPainted(true);
}
}
if(jb == italic) {
if(pickStyle != 1) {
pickStyle = 1;
s = textPane.addStyle("italic", regular);
StyleConstants.setItalic(s, true);
underline.setBorderPainted(true);
italic.setBorderPainted(false);
bold.setBorderPainted(true);
}
else {
s = textPane.addStyle("italic", regular);
StyleConstants.setItalic(s, false);
italic.setBorderPainted(true);
}
}
if(jb == underline) {
if(pickStyle != 3) {
pickStyle = 3;
s = textPane.addStyle("underline", regular);
StyleConstants.setUnderline(s, true);
underline.setBorderPainted(false);
italic.setBorderPainted(true);
bold.setBorderPainted(true);
}
else {
s = textPane.addStyle("underline", regular);
StyleConstants.setUnderline(s, false);
underline.setBorderPainted(true);
}
}
else if(jb == color) {
//if(colorTeller == false) {
colorTeller = true;
new ColorPicker();
//}
}
}
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if(code == KeyEvent.VK_ENTER) {
try {
doc.insertString(doc.getLength(), ">>> " + user.getText() + "\n", textPane.getStyle(styles[pickStyle]));
user.setText("");
}
catch(Exception y) {
System.out.println("error" + y);
}
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
private JTextPane createTextPane() {
JTextPane textPane = new JTextPane();
initStylesForTextPane(textPane);
doc = textPane.getDocument();
return textPane;
}
protected void initStylesForTextPane(JTextPane textPane) {
Style def = StyleContext.getDefaultStyleContext().
getStyle(StyleContext.DEFAULT_STYLE);
Style regular = textPane.addStyle("regular", def);
StyleConstants.setFontFamily(def, "SansSerif");
Style s = textPane.addStyle("italic", regular);
StyleConstants.setItalic(s, true);
s = textPane.addStyle("bold", regular);
StyleConstants.setBold(s, true);
s = textPane.addStyle("underline", regular);
StyleConstants.setUnderline(s, true);
JLabel label = new JLabel("\n");
StyleConstants.setComponent(s, label);
}
public static void main(String [] args) {
new F();
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ColorPicker extends JFrame implements ActionListener {
JButton red;
JButton yellow;
JButton green;
JButton black;
JButton blue;
Container meowmix = getContentPane();
public ColorPicker() {
red = new JButton();
yellow = new JButton();
green = new JButton();
black = new JButton();
blue = new JButton();
red.setOpaque(true);
yellow.setOpaque(true);
green.setOpaque(true);
black.setOpaque(true);
blue.setOpaque(true);
red.setBackground(Color.red);
yellow.setBackground(Color.yellow);
green.setBackground(Color.green);
black.setBackground(Color.black);
blue.setBackground(Color.blue);
red.setPreferredSize(new Dimension(20, 20));
yellow.setPreferredSize(new Dimension(20, 20));
green.setPreferredSize(new Dimension(20, 20));
black.setPreferredSize(new Dimension(20, 20));
blue.setPreferredSize(new Dimension(20, 20));
red.addActionListener(this);
yellow.addActionListener(this);
green.addActionListener(this);
black.addActionListener(this);
blue.addActionListener(this);
meowmix.add(red, BorderLayout.NORTH);
meowmix.add(yellow, BorderLayout.SOUTH);
meowmix.add(black, BorderLayout.CENTER);
meowmix.add(blue, BorderLayout.EAST);
meowmix.add(green, BorderLayout.WEST);
pack();
show();
}
public void actionPerformed(ActionEvent e) {
JButton jf = (JButton) e.getSource();
if(jf == red) {
F.colorSetter(Color.red);
}
else if(jf == yellow) {
F.colorSetter(Color.yellow);
}
else if(jf == blue) {
F.colorSetter(Color.blue);
}
else if(jf == green) {
F.colorSetter(Color.green);
}
else if(jf == black) {
F.colorSetter(Color.black);
}
dispose();
}
public static void main(String [] args) {
new ColorPicker();
}
}