Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    [java] JTable - evidenziare la riga al passaggio del mouse

    Ciao,


    ho una jTable riempita con numerosi dati estrapolati da un database..

    vorrei per comodità, evidenziare la riga (per ogni riga) dove passa il cursore del mouse, e farla tornare normale quando il cursore del mouse si allontana...


    ma nn so da che parte iniziare.

  2. #2
    codice:
        private void jTable1MouseEntered(java.awt.event.MouseEvent evt) {
    
            //jTable1.setSelectionBackground(Color.MAGENTA);  // COLORA SOLO LA RIGA MA DEVE CLICCARE
            //jTable1.setBackground(Color.ORANGE); // COLORA TUTTA LA TABELLA
            
            
        }

    facendo così.. ma ho quel problema scritto appena sopra, sul codice.

    dovrebbe fare una combo tra le due... uff

  3. #3
    e con questo trovo

    jTable1.rowAtPoint(evt.getPoint());

    la riga



    mi aiutate a fondere tutto insieme per colorarla?? :berto:

  4. #4
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    Combinando un po' di codice dal tutorial su swing della sun e dal forum...

    codice:
    import javax.swing.*;
    import javax.swing.table.*;
    import java.awt.*;
    import java.awt.event.*;
    
    /**
     *
     * @author Andrea
     */
    public class ColorRows extends JFrame {
        
        private class MyJTable extends JTable {
            
            private final Color rowhlcolor;
            
            private int highLightedRow = -1;
            private Rectangle dirtyRegion = null;
                   
            protected void processMouseMotionEvent(MouseEvent e) {
                int row = rowAtPoint(e.getPoint());
                boolean highlighted = false;
                
                Graphics g = getGraphics();
                
                // row changed
                if(highLightedRow != row) {
                    if(null != dirtyRegion) {
                        paintImmediately( dirtyRegion );
                    }
                    for(int j = 0; j<getRowCount(); j++) {
                        if(row == j) {
                            //highlight
                            Rectangle firstRowRect = getCellRect(row, 0, false);
                            Rectangle lastRowRect = getCellRect(row, getColumnCount() - 1, false);
                            dirtyRegion = firstRowRect.union(lastRowRect);
                            g.setColor(new Color(rowhlcolor.getRed(), rowhlcolor.getGreen(), rowhlcolor.getBlue(), 30));
                            g.fillRect( (int)dirtyRegion.getX(), (int)dirtyRegion.getY(),
                                    (int)dirtyRegion.getWidth(), (int)dirtyRegion.getHeight() );
                            highLightedRow = row;
                        }
                    }
                }
            }
            
            public MyJTable(Object[][] data, Object[] columnNames, Color rowhlcolor  ) {
                super(data, columnNames);            
                this.rowhlcolor = rowhlcolor;            
            }
            
        }
        
        public ColorRows() {
            super("Another test");
            Object[][] data = {
                {"Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false)},
                {"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)},
                {"Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false)},
                {"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)},
                {"Philip", "Milne", "Pool", new Integer(10), new Boolean(false)}
            };
            String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"};
            MyJTable myTable = new MyJTable(data, columnNames, Color.red);
            this.getContentPane().add(new JScrollPane(myTable), BorderLayout.CENTER);
            this.setSize(400,400);
            this.setVisible(true);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
        }
        
        public static void main (String[] args) {
            new ColorRows();
        }
    
    }
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  5. #5
    grazie, lo proverò lunedi

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.