Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2007
    Posts
    45
    Rep Power
    0

    Question Border with LestCellRenderer

    How to give border with ListCellRenderer.
    I tried it using following code but I failed.

    Suppose
    Java Code:
    lstFindResults
    is an instanceof JList
    Java Code:
    ((DefaultListCellRenderer)lstFindResults.getCellRenderer())
                .setBorder(BorderFactory.createLineBorder(Color.BLACK));
    PLEASE SUGGEST ME

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    Can u please send me the piece of code for better understanding?
    Ramya:cool:

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    Subclass DefaultListCellRenderer, create the desired Border as a static final field and setBorder in a getListCellRendererComponent override
    Java Code:
    public Component getListCellRendererComponent(JList list, Object value, int index,
          boolean isSelected, boolean cellHasFocus) {
       super.getListCellRendererComponent(list, value, index,
          isSelected, cellHasFocus);
       setBorder(BORDER);
       return this;
    }
    db

  4. #4
    Join Date
    Aug 2007
    Posts
    45
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Subclass DefaultListCellRenderer, create the desired Border as a static final field and setBorder in a getListCellRendererComponent override
    Java Code:
    public Component getListCellRendererComponent(JList list, Object value, int index,
          boolean isSelected, boolean cellHasFocus) {
       super.getListCellRendererComponent(list, value, index,
          isSelected, cellHasFocus);
       setBorder(BORDER);
       return this;
    }
    db
    Thanks Derry,
    Your solution is working fine.
    My Problem (Related to Border of ListCellRenderer) has been solved.

    But still, I want to know that why following code is not working:-
    Java Code:
    ((DefaultListCellRenderer)lstFindResults.getCellRenderer())
                .setBorder(BorderFactory.createLineBorder(Color.BLACK));
    Last edited by Gajesh Tripathi; 06-02-2009 at 08:22 AM.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    If you really want to know, trace the execution of code through JList / BasicListUI / DefaultListCellRenderer and somewhere you'll find that a border is being set that removes the custom border.

    The only way you can use a custom border (and many other properties) with most renderers is to set it AFTER getting the default properties with a call to the super method.

    db

  6. #6
    Join Date
    Aug 2007
    Posts
    45
    Rep Power
    0

    Default Solved

    Quote Originally Posted by Darryl.Burke View Post
    If you really want to know, trace the execution of code through JList / BasicListUI / DefaultListCellRenderer and somewhere you'll find that a border is being set that removes the custom border.

    The only way you can use a custom border (and many other properties) with most renderers is to set it AFTER getting the default properties with a call to the super method.
    Now I understand that what happenings behind my code

    Thanks

Similar Threads

  1. [SOLVED] Changing a JButton's border
    By a_elalaily in forum AWT / Swing
    Replies: 5
    Last Post: 05-11-2009, 01:07 AM
  2. How to remove border in JComboBox
    By tiro in forum AWT / Swing
    Replies: 2
    Last Post: 03-08-2009, 06:45 PM
  3. Textfield border problem
    By jurka in forum AWT / Swing
    Replies: 2
    Last Post: 08-31-2008, 10:15 AM
  4. Displaying a Button with a Border
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-26-2008, 08:40 PM
  5. Border Layout
    By mark-mlt in forum Java Applets
    Replies: 5
    Last Post: 05-12-2008, 10:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •