Showing posts with label jtable. Show all posts
Showing posts with label jtable. Show all posts

Thursday, May 30, 2013

JTable Scrolling to a specified row index java


public static void scrollToVisible(JTable table, int rowIndex, int vColIndex) {
        if (!(table.getParent() instanceof JViewport)) {
            return;
        }
        JViewport viewport = (JViewport)table.getParent();

        // This rectangle is relative to the table where the
        // northwest corner of cell (0,0) is always (0,0).
        Rectangle rect = table.getCellRect(rowIndex, vColIndex, true);

        // The location of the viewport relative to the table
        Point pt = viewport.getViewPosition();

        // Translate the cell location so that it is relative
        // to the view, assuming the northwest corner of the
        // view is (0,0)
        rect.setLocation(rect.x-pt.x, rect.y-pt.y);

        table.scrollRectToVisible(rect);       
}

A Simple JTable Example Java Code Program


import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JComboBox;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import javax.swing.DefaultCellEditor;

public class TableExample {

    //Note: Typically the main method will be in a
    //separate class. As this is a simple one class
    //example it's all in the one class.
    public static void main(String[] args) {

        //Use the event dispatch thread for Swing components
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {

                new TableExample();
            }
        });

    }

    public TableExample() {
        JFrame guiFrame = new JFrame();

        //make sure the program exits when the frame closes
        guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        guiFrame.setTitle("Creating a Table Example");
        guiFrame.setSize(700, 200);

        //This will center the JFrame in the middle of the screen
        guiFrame.setLocationRelativeTo(null);

        //Create the JTable using the ExampleTableModel implementing 
        //the AbstractTableModel abstract class
        JTable table = new JTable(new ExampleTableModel());

        //Set the column sorting functionality on
        table.setAutoCreateRowSorter(true);

        //Uncomment the next line if you want to turn the grid lines off
        //  table.setShowGrid(false);

        //Change the colour of the table - yellow for gridlines 
        //blue for background
        table.setGridColor(Color.YELLOW);
        table.setBackground(Color.CYAN);

        //String array to populate the combobox options
        String[] countries = {"Australia", "Brazil", "Canada", "China"
                , "France", "Japan", "Norway", "Russia", "South Korea"
                , "Tunisia", "USA"};
        JComboBox countryCombo = new JComboBox(countries);

        //Set the default editor for the Country column to be the combobox
        TableColumn countryColumn = table.getColumnModel().getColumn(2);
        countryColumn.setCellEditor(new DefaultCellEditor(countryCombo));

        //set the Event column to be larger than the rest and the Place column 
        //to be smaller
        TableColumn eventColumn = table.getColumnModel().getColumn(3);
        eventColumn.setPreferredWidth(150);

        TableColumn placeColumn = table.getColumnModel().getColumn(4);
        placeColumn.setPreferredWidth(5);

        //Place the JTable object in a JScrollPane for a scrolling table
        JScrollPane tableScrollPane = new JScrollPane(table);

        guiFrame.add(tableScrollPane);
        guiFrame.setVisible(true);
    }

    //implement a table model by extending a class to use
    //the AbstractTableModel
    class ExampleTableModel extends AbstractTableModel {

        //Two arrays used for the table data
        String[] columnNames = {"First Name", "Surname", "Country"
                , "Event", "Place", "Time", "World Record"};

        Object[][] data = {
                {"Pritom Kumar Mondal", "pritom", "Bangladesh", "50m freestyle", 1, "21.30", false},
                {"Pallob", "pallob", "Bangladesh", "50m freestyle", 2, "21.45", false}
        };

        @Override
        public int getRowCount() {
            return data.length;
        }

        @Override
        public int getColumnCount() {
            return columnNames.length;
        }

        @Override
        public Object getValueAt(int row, int column) {
            return data[row][column];
        }

        //Used by the JTable object to set the column names
        @Override
        public String getColumnName(int column) {
            return columnNames[column];
        }

        //Used by the JTable object to render different
        //functionality based on the data type
        @Override
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            if (column == 0 || column == 1) {
                return false;
            } else {
                return true;
            }
        }
    }
}
http://java.about.com/od/Creating-Tables/ss/A-Simple-Table-Example-Java-Code-Program.htm