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]
# 1 Re: defining Style colors for jtextpane dynamically how to?
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?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 :)
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).
keang at 2007-11-10 2:14:15 >
# 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 >
# 3 Re: defining Style colors for jtextpane dynamically how to?
Is this a cultural change I've missed - ask for help, then dismiss or ridicule any suggestions offered?

Seems to be a lot of it about recently. I blame the parents and the schools.

One day Chao-Chou fell down in the snow, and called out: "Help me! Help Me!" A monk came and lay down beside him. Chao-Chou got up and went away...
Zen koan.
dlorde at 2007-11-10 2:16:22 >
# 4 Re: defining Style colors for jtextpane dynamically how to?
Is this a cultural change I've missed - ask for help, then dismiss or ridicule any suggestions offered?

Seems to be a lot of it about recently. I blame the parents and the schools.

One day Chao-Chou fell down in the snow, and called out: "Help me! Help Me!" A monk came and lay down beside him. Chao-Chou got up and went away...
Zen koan.

sorry dude i appreciate suggestions if they work. And they must work for an average pc n00b. So suggesting to enter a name for a color is a joke. Has nothing to do with cultural change etc... If you would code real life applications you would know that ;-)
pel at 2007-11-10 2:17:19 >
# 5 Re: defining Style colors for jtextpane dynamically how to?
sorry dude i appreciate suggestions if they work. Where I come from, if you ask someone for help and they make an honest suggestion, it's considered polite to show appreciation that they were kind enough to try and help - or at very least not throw it back in their face - whether it works for you or not. You should expect answers that may not suit your requirements if you don't specify them clearly enough.
So suggesting to enter a name for a color is a joke.If a user wants to select a few unnamed colors out of billions to use later, it's not unreasonable to let them name them. If that's not what you had in mind, fine - find another solution, but if you want to end up on some ignore lists, you're going the right way.
Has nothing to do with cultural change etc...There I was thinking a smiley would be too obvious... :rolleyes:
If you would code real life applications you would know that ;-)What do you think I've been doing for the last 25 years?

Those are my principles. If you don't like them I have others...
Groucho Marx
dlorde at 2007-11-10 2:18:29 >
# 6 Re: defining Style colors for jtextpane dynamically how to?
Pel,

I took your rude comments about dlorde's help in your previous post to be down to English not being your first language. However, it now appears that it's actually down to to the way you are. Congratulations on becoming the first peron on my ignore list.
keang at 2007-11-10 2:19:23 >