Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / November 2005

Tip: Looking for answers? Try searching our database.

choosing linestyles with a combobox

Thread view: 
Tomba - 26 Nov 2005 15:16 GMT
hi,

I'm trying to let a user choose a linestyle (that is several predefined
dash patterns) by selecting one out of a combobox. I try to display a
preview line in each item of the combo.
The problem here is that only the selected element is painted and
visible, while the dropdown list displays nothing.

Here's my cell renderer, I hope anyone knows something to solve this
one. I suspect paint() is only done for the selected item but I have no
clue how to solve it :(

Thanks!

private class LineCellRenderer extends JLabel implements ListCellRenderer {
    private float[] value;
    private JTextField width;
   
    public LineCellRenderer(JTextField width) {
     super();
     setOpaque(true);
     this.width = width;
    }
   
    public Component getListCellRendererComponent(JList list, Object value,
     int index, boolean isSelected, boolean cellHasFocus) {
     this.value = (float[])value;
     return this;
    }
       
    public void paint(Graphics g) {
     Graphics2D g2 = (Graphics2D)g;
     super.paint(g);

     BasicStroke bStroke = new
BasicStroke(Integer.valueOf(width.getText()), BasicStroke.CAP_SQUARE,
BasicStroke.JOIN_ROUND, 5, value, 0);
     Line2D line = new Line2D.Double(2, getHeight()/2, getWidth()-4,
getHeight()/2);
     g2.setColor(Color.BLACK);
     g2.setStroke(bStroke);
     g2.draw(line);
    }
}
Tomba - 26 Nov 2005 16:16 GMT
> hi,
>
[quoted text clipped - 40 lines]
>     }
> }

nevermind I've solved it ;)
Just replacing the JLabel by JPanel

anyway... that was trial and error, could anyone explain that?
Roedy Green - 26 Nov 2005 16:29 GMT
>  public void paint(Graphics g) {
>>       Graphics2D g2 = (Graphics2D)g;
>>       super.paint(g);

you are supposed to override paintComponent, not paint.  Perhaps using
the wrong underlying Component in some strange way compensated for the
error.

see http://mindprod.com/jgloss/paint.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.