Thema: KChannelList

  1. #1

    Registriert seit
    19.12.2011
    Beiträge
    356
    Thanked 164 Times in 101 Posts

    Standard KChannelList

    PHP-Code:
    package KCClient.UI.Components;

    import KCClient.UI.ImageLoader;
    import java.awt.*;
    import java.awt.event.MouseAdapter;
    import java.awt.font.TextAttribute;
    import java.util.HashMap;
    import java.util.Map;
    import javax.swing.*;

    /**
     *
     * @author DeKaDeNz
     */
    public class KChannelList extends JList{
        
        public 
    KChannelList()
        {
            
    super();
            
    this.addMouseMotionListener(new KChannelList_MouseEvents());
        }
        
         public 
    KChannelList(ListModel model)
        {
            
    super(model);
            
    this.addMouseMotionListener(new KChannelList_MouseEvents());
        }

        
    int hoverIndex = -1;
        
        public class 
    KChannelList_MouseEvents extends MouseAdapter
        
    {
            @
    Override
            
    public void mouseMoved(java.awt.event.MouseEvent evt) {
                
    KChannelList list = (KChannelList)evt.getComponent();
                
    int pos evt.getY();
                
    int itemI pos/list.getFixedCellHeight();
                if(
    itemI!=list.hoverIndex)
                {
                    list.
    hoverIndex=itemI;
                    list.
    repaint();

                }

            }
            @
    Override
            
    public void mouseExited(java.awt.event.MouseEvent evt)
            {
                    
    KChannelList list = (KChannelList)evt.getComponent();
                    list.
    hoverIndex=-1;
                    list.
    repaint();
            }
        }
        
        @
    Override
        
    public void paintComponent(Graphics g)
        {
            
    drawBorder(g);
            
    drawBackground(g);
            
    drawItems(g);
        }

        
    Color borderColor Color.BLACK;
        public 
    Color getBorderColor()
        {
            return 
    borderColor;
        }
        public 
    void setBorderColor(Color c)
        {
            
    borderColor c;
        }
        private 
    void drawBorder(Graphics g) {
            
    g.setColor(getBorderColor());
            
    g.drawRect(00this.WIDTH-1this.HEIGHT-1);

        }

        private 
    void drawBackground(Graphics g) {
            
    g.setColor(this.getBackground());
            
    g.fillRect(00this.getWidth(), this.getHeight());
            
        }

       
        
        
        private 
    void drawItemAsString(Graphics gString sint iint y)
        {
            
    FontMetrics met g.getFontMetrics();
            
    int h 25;
            
    int y2=y+(h-met.getHeight())/2+met.getAscent();
                
                
                
                
    //drawfieldback
                
    if(this.getSelectedIndex()==i)
                {
                    
    g.setColor(Color.BLUE.darker());
                    
    g.fillRect(0,y,this.getWidth(),y+h);
                    
    g.setColor(Color.WHITE);
                  
                }
                else
                {
                    
    g.setColor(this.getBackground());
                    
    g.fillRect(0,y,this.getWidth(),y+h);
                    
    g.setColor(Color.BLACK);
                }
                
                
                
                
    String item s;
                
                
                
    int w = (int)met.getStringBounds(sg).getWidth();
                if(
    w<maxx)
                   
    maxx w;
                
    g.drawString(item,3,y2);
        }
        
        
    Map<TextAttributeIntegerfontAttributes = new HashMap<TextAttributeInteger>();
        
    int maxx 0;
        private 
    void drawItems(Graphics g) {
            
    this.setFixedCellHeight(19);
            
            

            
    ListModel model this.getModel();

            
            
    int y 0;
            for(
    int i 0i<model.getSize();i++)
            {
                
    Object c model.getElementAt(i);
                try {
                    
    String s = (String)c;
                    
    drawItemAsString(g,s,i,y);
                    
    y+=this.getFixedCellHeight();
                    continue;
                }
                catch(
    Exception s)
                {
                    
                }
                
    KChannelList_Item item = (KChannelList_Item)c;
                
    Font f g.getFont();
                
    int fstyle=0;
                if(
    item.bold)
                    
    fstyle fstyle Font.BOLD;
                if(
    item.italic)
                    
    fstyle fstyle Font.ITALIC;
                
               
                if(
    hoverIndex==i)
                        
    fontAttributes.put(TextAttribute.UNDERLINETextAttribute.UNDERLINE_ON);
                else
                        
    fontAttributes.remove(TextAttribute.UNDERLINE);

                
               
    Font newf = new Font(f.getFontName(), f.getStyle() | fstylef.getSize()).deriveFont(fontAttributes);
               
    g.setFont(newf);
                
                
                
                
    FontMetrics met g.getFontMetrics();
                
    int h this.getFixedCellHeight();
                
    int y2=y+(h-met.getHeight())/2+met.getAscent();
                

               
                
    //drawfieldback
                
    if(this.getSelectedIndex()==i)
                {
                    
    g.setColor(Color.BLUE.darker());
                    
    g.fillRect(0,y,this.getWidth(),y+h);
                    
    g.setColor(Color.WHITE);
                  
                }
                else
                {
                    
    g.setColor(this.getBackground());
                    
    g.fillRect(0,y,this.getWidth(),y+h);
                    
    g.setColor(item.color);
                }
                
                
                
                
    String s item.toString();
                
    g.drawString(s,3,y2);
                
                
                
                
                
    int w = (int)met.getStringBounds(sg).getWidth();
                if(
    w<maxx)
                   
    maxx w;

                
    //imgdrawing
                
    if(!item.imgUrl.isEmpty())
                {
                    
    Image img ImageLoader.getImage(item.imgUrl);
                    if(
    img!=null)
                    {
                        
    int ih img.getHeight(this);
                        
    int iw img.getWidth(this);
                        
    int iy=y+(h-iw)/2;
                        
                        
                        
    g.drawImage(imgw+5iythis);
                        
    maxx+=5+iw;
                    }
                    
                   
                }
                
                
                
                
                
                
                
                
                
    y+=h;
                
    g.setFont(f);
                
                
                
               
                
                
            }
            
    this.setFixedCellWidth(maxx);
        }




    PHP-Code:
    package KCClient.UI.Components;

    import java.awt.Color;

    /**
     *
     * @author DeKaDeNz
     */
     
    public class KChannelList_Item {
            public 
    int user 1;
            public 
    String value;
            public 
    boolean bold=false;
            public 
    boolean italic=false;
            public 
    String imgUrl="";
            public 
    Color color Color.BLACK;
            
            public 
    KChannelList_Item(String value)
            {
                
    this.value value;
            }
            
            public 
    KChannelList_Item(String value,int user,boolean boldboolean italicColor c)
            {
                
    this.value value;
                
    this.bold bold;
                
    this.italic italic;
                
    this.color c;
                
    this.user user;
            }
            public 
    KChannelList_Item(String value,int user,  boolean boldboolean italic)
            {
                
    this.value value;
                
    this.bold bold;
                
    this.italic italic;
                
    this.user 1;
            }
            public 
    KChannelList_Item(String value,int user,  boolean boldboolean italicColor cString imgUrl)
            {
                
    this.value value;
                
    this.bold bold;
                
    this.italic italic;
                
    this.color c;
                
    this.imgUrl imgUrl;
                
    this.user user;
            }
            
            
            @
    Override
            
    public String toString() {
                return 
    value " ("+user+")";
            }
            
        } 


    Nicht gerade das schönste vom schönsten, abes es geht


    Example:



    //EDIT:
    Update, Anzahl der User im Channel mit eingefügt im Source
    Ausserdem neuer Konstruktor, danke Golem
    Geändert von DeKaDeNz (08.02.2012 um 13:48 Uhr)

  2. The Following 4 Users Say Thank You to DeKaDeNz For This Useful Post:

    DnL (08.02.2012), getsha (08.02.2012), Mardcore (11.02.2012), uncopyable (08.02.2012)

  3. #2

    Registriert seit
    06.02.2012
    Beiträge
    5
    Thanked 1 Time in 1 Post

    Standard AW: KChannelList

    Kannst du auch ein Sample erstellen, wie man die JList einfügt?
    Habs in einem JFrame gemacht, des funzt aber nicht.

    Hab die KChannelList zwar hinzugefügt, kann aber kein Item eintragen.

    Hat sich erledigt ^^
    Hier die Lösung:

    PHP-Code:
    package test;

    import javax.swing.DefaultListModel;
    import javax.swing.JFrame;

    public class 
    Test {
        public static 
    void main(String[] args) {
            
    JFrame test = new JFrame();
            
    DefaultListModel model = new DefaultListModel();
            
    KChannelList list = new KChannelList();
            
    model.add(0, new KChannelList_Item("test"));
            list.
    setModel(model);
            
    test.add(list);
            
            
    test.setSize(200500);
            
    test.setVisible(true);
        }

    Du solltest den Construktor von KChannelList nochmals erweitern. Denn bei einer JList kann im konstruktor ein ListModel angegeben werden:

    PHP-Code:
     public Component_List(ListModel model) {
            
    super(model);
        } 
    Geändert von Golem (08.02.2012 um 13:41 Uhr)

  4. The Following User Says Thank You to Golem For This Useful Post:

    DeKaDeNz (08.02.2012)

  5. #3

    Registriert seit
    19.12.2011
    Beiträge
    356
    Thanked 164 Times in 101 Posts

    Standard AW: KChannelList

    Funktioniert genauso wie bei der normalen JList.
    Achtung gibt grad ein Update des Sources zwecks Underline der gehoverten Fläche.


    PHP-Code:
    DefaultListModel listModel = new DefaultListModel();
            
    listModel.addElement(new KChannelList_Item("Lobby",new Random().nextInt(50), true,false,Color.RED,"pics/icon_FullChannel.gif"));
            
    listModel.addElement(new KChannelList_Item("Support",new Random().nextInt(50), false,true,Color.BLACK));
              
    listModel.addElement(new KChannelList_Item("Lobby",new Random().nextInt(50), true,false,Color.RED,"pics/icon_FullChannel.gif"));
            
    listModel.addElement(new KChannelList_Item("Support",new Random().nextInt(50), false,true,Color.BLACK));
              
    listModel.addElement(new KChannelList_Item("Lobby",new Random().nextInt(50), true,false,Color.RED,"pics/icon_FullChannel.gif"));
            
    listModel.addElement(new KChannelList_Item("Support",new Random().nextInt(50), false,true,Color.BLACK));
              
    listModel.addElement(new KChannelList_Item("Lobby",new Random().nextInt(50), true,false,Color.RED,"pics/icon_FullChannel.gif"));
            
    listModel.addElement(new KChannelList_Item("Support",new Random().nextInt(50), false,true,Color.BLACK));
              
    listModel.addElement(new KChannelList_Item("Lobby",new Random().nextInt(50), true,false,Color.RED,"pics/icon_FullChannel.gif"));
            
    listModel.addElement(new KChannelList_Item("Support",new Random().nextInt(50), false,true,Color.BLACK));
              
    listModel.addElement(new KChannelList_Item("Lobby",new Random().nextInt(50), true,false,Color.RED,"pics/icon_FullChannel.gif"));
            
    listModel.addElement(new KChannelList_Item("Support",new Random().nextInt(50), false,true,Color.BLACK));
              
    listModel.addElement(new KChannelList_Item("Lobby",new Random().nextInt(50), true,false,Color.RED,"pics/icon_FullChannel.gif"));
            
    listModel.addElement(new KChannelList_Item("Support",new Random().nextInt(50), false,true,Color.BLACK));
              
    channelList.setModel(listModel); 
    Achja addet keine einzelnen Strings. Diese Methode ist veraltet und wird nicht mehr supported

Diese Seite nutzt Cookies, um das Nutzererlebnis zu verbessern. Klicken Sie hier, um das Cookie-Tracking zu deaktivieren.