defining Style colors for jtextpane dynamically how to?
Hallo,
nehmen wir an ich habe einen JColorChooser mit 16,7 Mio Farben zur Auswahl. Nur 1 Farbe davon mchte ich fr den Text einer JTextPane. Es gibt jetzt 16,7 Mio Mglichkeiten welche Farbe der Benutzer auswhlt. Soll ich jetzt 16,7 Mio styles definieren im Stil von:
lets assume i have a jcolorchooser with 16,7 billion colors to choose from. But only 1 color of it I wanna have for the text in a jtextpane. Well there are now 16,7 billion possibilities which color the user can choose. Should I now define 16,7 billion styles of colors like below?:
JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();
Style blue = doc.addStyle("blue", null); // 1. Style
StyleConstants.setForeground(blue, Color.BLUE);
Style red = doc.addStyle("red", null); // 2.STyle
StyleConstants.setForeground(red, Color.RED);
-
-
. // n-Style up to 16,7 billion styles
.
Style greenish = doc.addStyle("greenish", null);
StyleConstants.setForeground(greenish, Color.RED);
lets assume i have a jcombobox offering me 3 colors to keep it simple and i get as a return object a Color object when i selected the jcombobox. Now i dont know the color I have choosed how could i set the Style for the text in the jtextpane? you understand the static problem?
[1404 byte] By [
pel] at [2007-11-20 10:55:26]

# 2 Re: defining Style colors for jtextpane dynamically how to?
Well if you can think up 16.7 billion different names and are prepared to spend the next 10 years typing them all in then go ahead :) seems you understand the problem ;-)
Personally I'd ask the user to select a colour from the JColorChooser and then ask them for a style name and use that to register the style. You will need to check that the name is unique and will also probably want to keep a record of all style names entered (eg using an ArrayList).[/QUOTE]
The user itself should enter a name for the choosen color ? Come on thats very userunfriendly. Commonly a user is used picking a color and then format the text not giving names for colors.
look at that example i made (the 2 classes at the bottom of this page). The user can choose 3 colors. blue, green,red.
Although he user has the choice of 3 colors in the code its always written "red" thats pretty stupid...
I could retrieve the selected item color in the jcombobox:
String mycolor = mycombobox.getSelectedItem().toString();
I could also retrieve it as Object:
Object mycolor = mycombobox.getSelectedItem();
Whatever i do i cant make any connection from the color of the selected Item to the Styles i have predefined. The code quite below is the original code. This following code or the method i have just changed:
The solution is not working. Above all this is very limited. I want to stick to the 16,7 billion color example. There are 1000 applications out there where this case is working and the user need no to enter a name for a color sorry but that is ridiculous ;-)
public void colorizeText(Color mycolor)
{
int offset = textPane.getSelectionStart();
int length = textPane.getSelectionEnd() - offset;
if(mycolor == Color.RED)
{
Style red = doc.addStyle("red", null);
StyleConstants.setForeground(red, mycolor);
doc.setCharacterAttributes(offset,length, textPane.getStyle("red"), true);
}
if(mycolor == Color.blue)
{
Style blue = doc.addStyle("blue", null);
StyleConstants.setForeground(blue, mycolor);
doc.setCharacterAttributes(offset,length, textPane.getStyle("blue"), true);
}
if(mycolor == Color.GREEN)
{
Style green = doc.addStyle("green", null);
StyleConstants.setForeground(green, mycolor);
doc.setCharacterAttributes(offset,length, textPane.getStyle("green"), true);
}
}
public class MainWindow extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
private String text = "Hello I am the text which can be selected and after colorized with a simple click on the button!";
private JTextPane textPane = new JTextPane();
private JScrollPane pane = new JScrollPane(textPane);
private StyledDocument doc = textPane.getStyledDocument();
private DefaultComboBoxModel modelCB = new DefaultComboBoxModel();
private JComboBox mycombobox = new JComboBox(modelCB);
private JButton colorBT = new JButton("colorize Text");
public MainWindow()
{
super("test for coloring selected Text");
setLayout(null);
add(pane);
add(colorBT);
add(mycombobox);
mycombobox.setBounds(400, 0, 130, 50);
colorBT.setBounds(400,51,130,150);
pane.setBounds(0,0,400,200);
Color green = new Color(0,155,0);
Color red = new Color(255,0,0);
Color blue = new Color(30,144,255);
mycombobox.addItem(green);
mycombobox.addItem(red);
mycombobox.addItem(blue);
mycombobox.setRenderer(new ColorComboBoxRenderer());
textPane.setText(text);
colorBT.addActionListener(this);
}
public void colorizeText(Color mycolor)
{
Style red = doc.addStyle("red", null);
StyleConstants.setForeground(red, mycolor);
int offset = textPane.getSelectionStart();
int length = textPane.getSelectionEnd() - offset;
doc.setCharacterAttributes(offset,length, textPane.getStyle("red"), true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(colorBT))
{
colorizeText(setSelectedColor());
}
}
public Color setSelectedColor()
{
int index = mycombobox.getSelectedIndex();
Color tempColor = (Color)mycombobox.getModel().getElementAt(index);
return tempColor;
}
public static void main(String args[])
{
MainWindow myWindow = new MainWindow();
myWindow.setSize(540,228);
myWindow.setLocationRelativeTo(null);
myWindow.setVisible(true);
}
}
class ColorComboBoxRenderer extends JPanel implements ListCellRenderer
{
protected Color mycolor = Color.black;
protected Color focusColor = (Color) UIManager.get("List.selectionBackground");
protected Color nonFocusColor = Color.white;
public Component getListCellRendererComponent(JList list, Object obj, int row, boolean sel, boolean hasFocus)
{
if (hasFocus || sel) setBorder(new CompoundBorder(new MatteBorder(8, 10, 8, 10, focusColor),new LineBorder(Color.black)));
else setBorder(new CompoundBorder(new MatteBorder(8, 10, 8, 10, nonFocusColor),new LineBorder(Color.black)));
if (obj instanceof Color) mycolor = (Color) obj;
return this;
}
public void paintComponent(Graphics g)
{
setBackground(mycolor);
super.paintComponent(g);
}
}
pel at 2007-11-10 2:15:26 >
