3 Replies Latest reply: Aug 5, 2012 10:13 PM by 918178 RSS

    How to add mouse listener for a single row alone

    918178
      I have a requirement. In a JTable when I double click a particular row the cells in the row should set to the width which I have provided.

      The problem with my code is when I click fourth row in the table, the first row gets adjusted.

      So how I need help is

      only if I click the first row, the first row cell size should get adjusted not when I click fourth row.

      Similarly if I give some cell width and height for fourth row cells, then when I double click the fourth row, the fourth should alone get adjusted and not the other rows.

      Hope I have explained clearly.

      How can it be achieved?


      Please find below my code. Everything is hardcoded. So it may look messy. Please excuse.

      // Imports
      import java.awt.BorderLayout;
      import java.awt.Color;
      import java.awt.Component;
      import java.awt.event.MouseAdapter;
      import java.awt.event.MouseEvent;

      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import javax.swing.JScrollPane;
      import javax.swing.JTable;
      import javax.swing.JTextArea;
      import javax.swing.table.DefaultTableModel;
      import javax.swing.table.TableCellRenderer;
      import javax.swing.table.TableColumn;

      class SimpleTableExample extends JFrame {
      // Instance attributes used in this example
      private JPanel topPanel;
      private JTable table;
      private JScrollPane scrollPane;
      String data1 = "";
      String data2 = "123456789ABCDEFGHIJKLMNOPQRSTUVQWXYZabcdefghijklmnopqrstuvwxyzaquickbrownfoxjumpedoverthelazydog";
      int size = data2.length();

      // Constructor of main frame
      public SimpleTableExample() {
           // Set the frame characteristics
           setTitle("Simple Table Application");
           setSize(400, 200);
           setBackground(Color.gray);

           // Create a panel to hold all other components
           topPanel = new JPanel();
           topPanel.setLayout(new BorderLayout());
           getContentPane().add(topPanel);

           // Create columns names
           String columnNames[] = { "SEL", "DESIGN DATA", "PART NUMBER" };

           // Create some data

           String dataValues[][] = { { data1, data2, "67", "77" },
                { "", "43", "853" }, { "", "89.2", "109" },
                { "", "9033", "3092" } };
           DefaultTableModel model = new DefaultTableModel(dataValues, columnNames);
           model.addColumn("PART TITLE");
           model.addColumn("SPECIAL INSTRUCTIONS");
           table = new JTable(model) {
           public boolean isCellEditable(int rowIndex, int colIndex) {
                return false;
           }
           };
           // set specific row height
           table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
           int colInd = 0;
           TableColumn col = table.getColumnModel().getColumn(colInd);
           int width = 50;
           col.setPreferredWidth(width);
           int colInd2 = 1;
           TableColumn col2 = table.getColumnModel().getColumn(colInd2);
           int width2 = 100;
           col2.setPreferredWidth(width2);
           int colInd3 = 2;
           TableColumn col3 = table.getColumnModel().getColumn(colInd3);
           int width3 = 10;
           col3.setPreferredWidth(width3);
           int colInd4 = 3;
           TableColumn col4 = table.getColumnModel().getColumn(colInd4);
           int width4 = 10;
           col4.setPreferredWidth(width4);
           int colInd5 = 4;
           TableColumn col5 = table.getColumnModel().getColumn(colInd5);
           int width5 = 10;
           col5.setPreferredWidth(width5);

           table.addMouseListener(new MouseAdapter() {
           public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                JTable target = (JTable) e.getSource();
                int row = target.getSelectedRow();
                int column = target.getSelectedColumn();

                TableColumn col1 = table.getColumnModel().getColumn(0);
                col1.setPreferredWidth(50);
                TableColumn col2 = table.getColumnModel().getColumn(1);
                col2.setPreferredWidth(400);
                table.getColumnModel().getColumn(1).setCellRenderer(
                     new TableCellLongTextRenderer());
                table.setRowHeight(50);
                TableColumn col5 = table.getColumnModel().getColumn(4);
                col5.setPreferredWidth(200);
                }
           }
           });

           // Create a new table instance

           // table = new JTable(dataValues, columnNames);

           // Add the table to a scrolling pane
           scrollPane = new JScrollPane(table);
           topPanel.add(scrollPane, BorderLayout.CENTER);
      }

      // Main entry point for this example
      public static void main(String args[]) {
           // Create an instance of the test application
           SimpleTableExample mainFrame = new SimpleTableExample();
           mainFrame.setVisible(true);
      }
      }

      class TableCellLongTextRenderer extends JTextArea implements TableCellRenderer {

      public Component getTableCellRendererComponent(JTable table, Object value,
           boolean isSelected, boolean hasFocus, int row, int column) {
           this.setText((String) value);
           this.setWrapStyleWord(true);
           this.setLineWrap(true);
           // set the JTextArea to the width of the table column
           setSize(table.getColumnModel().getColumn(column).getWidth(),
                getPreferredSize().height);
           if (table.getRowHeight(row) != getPreferredSize().height) {
           // set the height of the table row to the calculated height of the
           // JTextArea
           table.setRowHeight(row, getPreferredSize().height);
           }
           return this;
      }

      }

      Edited by: 915175 on Aug 3, 2012 4:24 AM
        • 1. Re: How to add mouse listener for a single row alone
          DrClap
          You wouldn't use a mouse listener for this. You would use a selection listener. Here's a link to the tutorial: How to Write a List Selection Listener. If you're going to be using JTable you might want to also have a look at How to Use Tables rather than just guessing at how to use tables.
          • 2. Re: How to add mouse listener for a single row alone
            898886
            Hi

            Try below code. Hope this will help





            import java.awt.BorderLayout;
            import java.awt.Color;
            import java.awt.Component;
            import java.awt.event.MouseAdapter;
            import java.awt.event.MouseEvent;

            import javax.swing.JFrame;
            import javax.swing.JPanel;
            import javax.swing.JScrollPane;
            import javax.swing.JTable;
            import javax.swing.JTextArea;
            import javax.swing.table.DefaultTableModel;
            import javax.swing.table.TableCellRenderer;
            import javax.swing.table.TableColumn;

            public class SimpleTableExample extends JFrame {

            private JPanel topPanel;
            private JTable table;
            private JScrollPane scrollPane;
            String data1 = "";
            String data2 = "123456789ABCDEFGHIJKLMNOPQRSTUVQWXYZabcdefghijklmnopqrstuvwxyzaquickbrownfoxjumpedoverthelazydog";
            int size = data2.length();

            // Constructor of main frame
            public SimpleTableExample() {
            // Set the frame characteristics
            setTitle("Simple Table Application");
            setSize(400, 200);
            setBackground(Color.gray);

            // Create a panel to hold all other components
            topPanel = new JPanel();
            topPanel.setLayout(new BorderLayout());
            getContentPane().add(topPanel);

            // Create columns names
            String columnNames[] = { "SEL", "DESIGN DATA", "PART NUMBER" };

            // Create some data

            String dataValues[][] = { { data1, data2, "67", "77" },
            { "", "43", "853" }, { "", "89.2", "109" },
            { "", "9033", "3092" } };
            DefaultTableModel model = new DefaultTableModel(dataValues, columnNames);
            model.addColumn("PART TITLE");
            model.addColumn("SPECIAL INSTRUCTIONS");
            table = new JTable(model) {
            public boolean isCellEditable(int rowIndex, int colIndex) {
            return false;
            }
            };
            // set specific row height
            table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            int colInd = 0;
            TableColumn col = table.getColumnModel().getColumn(colInd);
            int width = 50;
            col.setPreferredWidth(width);
            int colInd2 = 1;
            TableColumn col2 = table.getColumnModel().getColumn(colInd2);
            int width2 = 100;
            col2.setPreferredWidth(width2);
            int colInd3 = 2;
            TableColumn col3 = table.getColumnModel().getColumn(colInd3);
            int width3 = 10;
            col3.setPreferredWidth(width3);
            int colInd4 = 3;
            TableColumn col4 = table.getColumnModel().getColumn(colInd4);
            int width4 = 10;
            col4.setPreferredWidth(width4);
            int colInd5 = 4;
            TableColumn col5 = table.getColumnModel().getColumn(colInd5);
            int width5 = 10;
            col5.setPreferredWidth(width5);

            // Cell Render should apply on each column -- add by Rupali
            for(int i=0; i< table.getColumnModel().getColumnCount(); i++){
            table.getColumnModel().getColumn(i).setCellRenderer( new TableCellLongTextRenderer());
            }

            table.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
            JTable target = (JTable) e.getSource();
            int row = target.getSelectedRow();
            int column = target.getSelectedColumn();
            setTableCellHeight(table,row,column); //Added by Rupali
            TableColumn col1 = table.getColumnModel().getColumn(0);
            col1.setPreferredWidth(50);
            TableColumn col2 = table.getColumnModel().getColumn(1);
            col2.setPreferredWidth(400);
            TableColumn col5 = table.getColumnModel().getColumn(4);
            col5.setPreferredWidth(200);
            }
            }
            });

            // Create a new table instance

            // table = new JTable(dataValues, columnNames);

            // Add the table to a scrolling pane
            scrollPane = new JScrollPane(table);
            topPanel.add(scrollPane, BorderLayout.CENTER);
            }

            /**
            * Created By Rupali
            *
            * This will set cell's height and column's width
            *
            * @param table
            * @param row
            * @param column
            */
            public void setTableCellHeight(JTable table, int row, int column) {

            // set the JTextArea to the width of the table column
            setSize(table.getColumnModel().getColumn(column).getWidth(),
            getPreferredSize().height);
            if (table.getRowHeight(row) != getPreferredSize().height) {
            // set the height of the table row to the calculated height of the
            // JTextArea
            table.setRowHeight(row, getPreferredSize().height);
            }
            }

            // Main entry point for this example
            public static void main(String args[]) {
            // Create an instance of the test application
            SimpleTableExample mainFrame = new SimpleTableExample();
            mainFrame.setVisible(true);
            }


            }




            class TableCellLongTextRenderer extends JTextArea implements TableCellRenderer {

            public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
            this.setText((String) value);
            this.setWrapStyleWord(true);
            this.setLineWrap(true);
            return this;
            }

            }
            • 3. Re: How to add mouse listener for a single row alone
              918178
              Thank you for the guidance. I works as I want.