Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

extending AbstractColorChooserPanel

 
Randall Twede
Ranch Hand
Posts: 4467
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am curious about your opinions. there are 2 ways to make a subclass of AbstractColorChooserPanel. you can implement the 5 abstract methods, including buildChooser() which will contain the code that is usually in a constructor. or you can write a constructor AND implements the five abstract methods, but have buildChooser() do nothing. i think i prefer the latter. constructors is where we expect to find this code.
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I've checked the Java 7 code of the class, and noticed that it's called from installChooserPanel just after the internal JColorChooser is set. This JColorChooser is used in methods getColorSelectionModel and (indirectly) getColorFromModel. If you need those to build your panel then you must use the buildPanel method.
 
Randall Twede
Ranch Hand
Posts: 4467
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am struggling trying to figure this out, but am trying to do it without asking for help. i have seen several examples and they are all "different". i just noticed that one did it one way and another used a constructor instead. it made me curious. since i already started the topic i will post my code and ask for help. this is what i have so far.

no time to post questions since the library is closing
any help appreciated.
the JColorChooser itself works fine, just need help with the custom panel
 
Randall Twede
Ranch Hand
Posts: 4467
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i figured out i will have to use JColorChooser.createDialog() instead of JColorChooser.showDialog(). that shouldn't be hard. i am having trouble with how to get the Color from the JComboBox to th JColorChooser. it seems to involve getColorSelectionModel().getSelectedColor() and getColorSelectionModel().setSelectedColor(). i am going to post the example that is most like what i am trying to do. i have a few questions about it.

in the code, he has getColorSelectionModel().setSelectedColor(grays[scale.getValue()]); in his methods stateChanged() and actionPerformed(). what happens if the user chooses the custom tab, wants the default setting, so just clicks OK? it looks to me like it does nothing. shouldn't there be a similar line in his constructor for if the user wants the default setting? the other question is more opinion. he has the following code

while i am in favor of not creating objects unless they are needed, i think i would just create one regardless, eliminating the boolean variable and the if statement. i will see what i can do. i would have given AbstractColorChooserPanel a method that returned the selected color rather than this hard to understand ColorSelectionModel stuff
 
Randall Twede
Ranch Hand
Posts: 4467
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have made some progress. i think the main gui class is finished. it took only minor changes. i am still having problems with the custom tab though. i added an actionPerformed() but color doesnt change. also i do not understand public void updateChooser(). oops! i forgot to add the listener to the component. it works now but i still dont understand updateChooser().
 
Randall Twede
Ranch Hand
Posts: 4467
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
apparently there is no way to do what i want to do. the default selection of the JComboBox in my custom tab is black. i would like it so when you choose my tab the preview changes to black. i tried this
public void updateChooser()
{
//getColorSelectionModel().setSelectedColor(colors[selector.getSelectedIndex()]);
}
and this
public void buildChooser()
{
selector.setEditable(false);
selector.setSelectedIndex(0); // Default selection is black
selector.addActionListener(this);
add(selector);
getColorSelectionModel().setSelectedColor(colors[selector.getSelectedIndex()]);
}
the first one disabled all the other tabs. the second didnt do much(not what i wanted anyway). i noticed the other tabs dont change the preview to their defaults(if they have them) either. i guess that is just the way it is. if the user wants to choose Black from my custom tab they will have to drop down the list and choose it even though it is the default
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic