Results 1 to 1 of 1
  1. #1
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default because of the background I cannot Highlight a specific Row

    Java Code:
    package xxTestxx;
    
    import swingGUISamples.MyTableCellEditor;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableCellRenderer;
    import javax.swing.table.TableModel;
    import javax.swing.table.TableRowSorter;
    
    @SuppressWarnings("serial")
    public class Inventory extends JFrame {
    
      private JTable inventoryTable;
      private JPanel inventoryPanel;
      private JScrollPane scroll;
      private ImageIcon tableBackground;
      private TableModel model;
    
      public Inventory() {
    
        initializeInventory();
      }
    
    
      private void initializeInventory() {
    
        inventoryPanel = new JPanel();
    
        inventoryPanel.setLayout(null);
        tableBackground = new ImageIcon("c:/pics/inventorytablebck.jpg");
    
    
        final String[] columnNames = {"Item Code", "Item Quantity", "Item Desciption", "Item Cost",
                                      "Cost Extension", "Retail", "Retail Extension", "Expiration Code"};
    
        final Object[][] data = {{"100000", new Integer(32), "TOBI MIX NUTS 45g", new Double(18.52), new Double(100.50),
                                new Double(21.75), new Double(150.50), new Long(100142)},
                                {"1000013", new Integer(45), "TOBI MELON SEEDs 100g", new Double(30.00), new Double(100.50),
                                new Double(21.75), new Double(150.50), new Long(100142)},
                                {"1000002", new Integer(15), "TOBI MIX NUTS 100g", new Double(39.59), new Double(100.50),
                                new Double(21.75), new Double(150.50), new Long(100142)} ,
                                {"1000015", new Integer(323), "TOBI SQUASH SEEDS 100g", new Double(31.35), new Double(100.50),
                                new Double(21.75), new Double(150.50), new Long(100142)},
                                {"1000018", new Integer(64), "CASINO FEM E ALC70%SOL 250mL", new Double(26.36), new Double(100.50),
                                new Double(21.75), new Double(150.50), new Long(100142)},
                                {"1000005", new Integer(123), "CASINO FEM E ALC70%SOL 500mL", new Double(45.41), new Double(100.50),
                                new Double(21.75), new Double(150.50), new Long(100142)},
                                {"1000021", new Integer(154), "EIGHT O CLOCK MGO 100%43.5g", new Double(8.54), new Double(100.50),
                                new Double(21.75), new Double(150.50), new Long(100142)}};
    
    
    
            inventoryTable = new JTable(data, columnNames) {
    
                public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
    
                    Component c = super.prepareRenderer(renderer, row, column);
    
                    // We want renderer component to be
                    // transparent so background image is visible
                    if(c instanceof JComponent) {
    
                        ((JComponent)c).setOpaque(false);
                    }
    
                    return c;
                }
    
                public void paint( Graphics g ) {
    
                    // tile the background image
                    Dimension d = getSize();
    
                    for( int x = 0; x < d.width; x += tableBackground.getIconWidth()) {
    
                        for( int y = 0; y < d.height; y += tableBackground.getIconHeight()) {
    
                            g.drawImage( tableBackground.getImage(), x, y, null, null );
                        }
    
                        // Now let the paint do its usual work
                        super.paint(g);
                    }
                }
            };
            inventoryTable.setOpaque(false);
            inventoryTable.setRowSelectionAllowed(true);
            inventoryTable.setSelectionForeground(Color.RED);
            inventoryTable.getColumnModel().getColumn(0).setCellEditor(new MyTableCellEditor());
            inventoryTable.getColumnModel().getSelectionModel().addListSelectionListener(new ListSelectionListener() {
    
                public void valueChanged(ListSelectionEvent e) {
    
                    int rowIndex = inventoryTable.getSelectedRow();
    
                    inventoryTable.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    
                    System.out.println("row index: " + rowIndex);
                }
            });
    
    
            scroll = new JScrollPane(inventoryTable);
            scroll.setBounds(0, 300, 1395, 444);
    
            inventoryPanel.add(scroll);
    
            getContentPane().add(inventoryPanel);
    
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            setTitle("Inventory Window");
            setSize(1400, 500);
            setLocationRelativeTo(null);
            setVisible(true);
      }
    
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            new Inventory();
          }
        });
      }
    }
    I need to maintain this background of my JTable but when I click one of the row I cant see the background highlight maybe because of the TableRenderer,
    well I can see the foreground highlight but how can I resolve this problem without disregarding the background image.

    what I want is, when i click a specific row, that row's "OPACITY" or "RENDER" will be disregarded and the background highlight will be visible and when I deselected it the background will come back... Is this possible?

    and on more thing, I can only see the foreground highlight when i click the mouse once I mean when the mouse click is not released, but when i release the mouse the foreground highlight disappears.
    Last edited by bigj; 01-31-2010 at 05:58 AM.

Similar Threads

  1. Highlight rows of a jtable
    By simmi in forum AWT / Swing
    Replies: 5
    Last Post: 08-20-2009, 01:13 PM
  2. How to Highlight a String in JTextArea
    By ramvaidhya in forum AWT / Swing
    Replies: 6
    Last Post: 12-31-2008, 07:23 AM
  3. Replies: 2
    Last Post: 11-08-2008, 10:40 AM
  4. GNU Source-highlight 2.7
    By levent in forum Java Software
    Replies: 0
    Last Post: 06-12-2007, 08:39 AM
  5. How to select/highlight an entire row in JTextArea
    By Valeriano in forum AWT / Swing
    Replies: 2
    Last Post: 05-28-2007, 11:20 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
  •