Example usage for javax.swing JTable JTable

List of usage examples for javax.swing JTable JTable

Introduction

In this page you can find the example usage for javax.swing JTable JTable.

Prototype

public JTable(final Object[][] rowData, final Object[] columnNames) 

Source Link

Document

Constructs a JTable to display the values in the two dimensional array, rowData, with column names, columnNames.

Usage

From source file:Snippet154.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    Composite composite = new Composite(shell, SWT.NO_BACKGROUND | SWT.EMBEDDED);

    /*/*  w ww  .  j  a v  a2  s . c o m*/
     * Set a Windows specific AWT property that prevents heavyweight
     * components from erasing their background. Note that this is a global
     * property and cannot be scoped. It might not be suitable for your
     * application.
     */
    try {
        System.setProperty("sun.awt.noerasebackground", "true");
    } catch (NoSuchMethodError error) {
    }

    /* Create and setting up frame */
    Frame frame = SWT_AWT.new_Frame(composite);
    Panel panel = new Panel(new BorderLayout()) {
        public void update(java.awt.Graphics g) {
            /* Do not erase the background */
            paint(g);
        }
    };
    frame.add(panel);
    JRootPane root = new JRootPane();
    panel.add(root);
    java.awt.Container contentPane = root.getContentPane();

    /* Creating components */
    int nrows = 1000, ncolumns = 10;
    Vector rows = new Vector();
    for (int i = 0; i < nrows; i++) {
        Vector row = new Vector();
        for (int j = 0; j < ncolumns; j++) {
            row.addElement("Item " + i + "-" + j);
        }
        rows.addElement(row);
    }
    Vector columns = new Vector();
    for (int i = 0; i < ncolumns; i++) {
        columns.addElement("Column " + i);
    }
    JTable table = new JTable(rows, columns);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.createDefaultColumnsFromModel();
    JScrollPane scrollPane = new JScrollPane(table);
    contentPane.setLayout(new BorderLayout());
    contentPane.add(scrollPane);

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet154.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 154");
    shell.setLayout(new FillLayout());

    Composite composite = new Composite(shell, SWT.NO_BACKGROUND | SWT.EMBEDDED);

    /*/*  w w  w . ja va 2  s .  c o  m*/
    * Set a Windows specific AWT property that prevents heavyweight
    * components from erasing their background. Note that this
    * is a global property and cannot be scoped. It might not be
    * suitable for your application.
    */
    try {
        System.setProperty("sun.awt.noerasebackground", "true");
    } catch (NoSuchMethodError error) {
    }

    /* Create and setting up frame */
    Frame frame = SWT_AWT.new_Frame(composite);
    Panel panel = new Panel(new BorderLayout()) {
        @Override
        public void update(java.awt.Graphics g) {
            /* Do not erase the background */
            paint(g);
        }
    };
    frame.add(panel);
    JRootPane root = new JRootPane();
    panel.add(root);
    java.awt.Container contentPane = root.getContentPane();

    /* Creating components */
    int nrows = 1000, ncolumns = 10;
    Vector<Vector<String>> rows = new Vector<>();
    for (int i = 0; i < nrows; i++) {
        Vector<String> row = new Vector<>();
        for (int j = 0; j < ncolumns; j++) {
            row.addElement("Item " + i + "-" + j);
        }
        rows.addElement(row);
    }
    Vector<String> columns = new Vector<>();
    for (int i = 0; i < ncolumns; i++) {
        columns.addElement("Column " + i);
    }
    JTable table = new JTable(rows, columns);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.createDefaultColumnsFromModel();
    JScrollPane scrollPane = new JScrollPane(table);
    contentPane.setLayout(new BorderLayout());
    contentPane.add(scrollPane);

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:EmbedJTableSWTNoFlicker.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    Composite composite = new Composite(shell, SWT.NO_BACKGROUND | SWT.EMBEDDED);

    /*/*from ww  w .j a v  a2 s.  com*/
     * Set a Windows specific AWT property that prevents heavyweight components
     * from erasing their background. Note that this is a global property and
     * cannot be scoped. It might not be suitable for your application.
     */
    try {
        System.setProperty("sun.awt.noerasebackground", "true");
    } catch (NoSuchMethodError error) {
    }

    /* Create and setting up frame */
    Frame frame = SWT_AWT.new_Frame(composite);
    Panel panel = new Panel(new BorderLayout()) {
        public void update(java.awt.Graphics g) {
            /* Do not erase the background */
            paint(g);
        }
    };
    frame.add(panel);
    JRootPane root = new JRootPane();
    panel.add(root);
    java.awt.Container contentPane = root.getContentPane();

    /* Creating components */
    int nrows = 1000, ncolumns = 10;
    Vector rows = new Vector();
    for (int i = 0; i < nrows; i++) {
        Vector row = new Vector();
        for (int j = 0; j < ncolumns; j++) {
            row.addElement("Item " + i + "-" + j);
        }
        rows.addElement(row);
    }
    Vector columns = new Vector();
    for (int i = 0; i < ncolumns; i++) {
        columns.addElement("Column " + i);
    }
    JTable table = new JTable(rows, columns);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.createDefaultColumnsFromModel();
    JScrollPane scrollPane = new JScrollPane(table);
    contentPane.setLayout(new BorderLayout());
    contentPane.add(scrollPane);

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:TableDemoApplet.java

private static void createGUI(Container contentPane) {
    Object[][] rowData = new String[][] { { "98-43", "AraAra! SL" }, { "81-31", "Aragones Transports SA" },
            { "12-72", "Rocca SL" }, { "99-10", "Rodriguez e Hijos SA" }, { "00-65", "Rimbau Motors SL" } };
    JTable table = new JTable(rowData, new String[] { "Part No", "Provider" });

    JComboBox companyComboBox = new JComboBox(new Object[] { "AraAra! SL", "Aragones Transports SA", "Rocca SL",
            "Rodriguez e Hijos SA", "Rimbau Motors SL" });
    companyComboBox.setEditable(true);//from   w ww .  j  a v  a2s . c o  m
    new S15WorkingBackspace(companyComboBox);

    // setup the ComboBoxCellEditor, DefaultCellEditor won't work!
    table.getColumnModel().getColumn(1).setCellEditor(new ComboBoxCellEditor(companyComboBox));

    table.setPreferredScrollableViewportSize(new Dimension(400, 100));
    JScrollPane scrollPane = new JScrollPane(table);

    contentPane.setLayout(new java.awt.FlowLayout());
    contentPane.add(scrollPane);
    contentPane.add(new JTextField("HALLO!"));
}

From source file:Main.java

public Main() {
    final JTable table = new JTable(10, 5) {
        public boolean getScrollableTracksViewportWidth() {
            return getPreferredSize().width < getParent().getWidth();
        }//w  w w  .  j a va  2  s.c om
    };
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    final JScrollPane scrollPane = new JScrollPane(table);
    getContentPane().add(scrollPane);
}

From source file:Main.java

public Main() {
    setSize(600, 300);//from w  ww . j  a  v  a2  s . co m
    String[] columnNames = { "A", "B", "C" };
    Object[][] data = { { "M", "a", 2 }, { "J", "e", 4 }, { "M", "z", 6 } };

    JTable table = new JTable(data, columnNames);
    JScrollPane tableSP = new JScrollPane(table);

    add(tableSP);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

From source file:Main.java

public Main() {
    JTable table = new JTable(3, 3);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    for (int i = 0; i < table.getColumnCount(); i++) {
        DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
        TableColumn col = colModel.getColumn(i);
        int width = 0;

        TableCellRenderer renderer = col.getHeaderRenderer();
        if (renderer == null) {
            renderer = table.getTableHeader().getDefaultRenderer();
        }/*from   w  w  w .j a va  2  s  .  c o  m*/
        Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0,
                0);
        width = comp.getPreferredSize().width;
        col.setPreferredWidth(width + 2);
    }
    JFrame f = new JFrame();
    f.add(new JScrollPane(table));
    f.setSize(300, 300);
    f.setVisible(true);
}

From source file:Main.java

public Main() {
    JTable table = new JTable(3, 3);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    for (int i = 0; i < table.getColumnCount(); i++) {
        DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
        TableColumn col = colModel.getColumn(i);
        int width = 0;

        TableCellRenderer renderer = col.getHeaderRenderer();
        for (int r = 0; r < table.getRowCount(); r++) {
            renderer = table.getCellRenderer(r, i);
            Component comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, i), false, false,
                    r, i);/*w  w  w  .j a  v a2  s  .c  o  m*/
            width = Math.max(width, comp.getPreferredSize().width);
        }
        col.setPreferredWidth(width + 2);
    }
    JFrame f = new JFrame();
    f.add(new JScrollPane(table));
    f.setSize(300, 300);
    f.setVisible(true);
}

From source file:MainClass.java

MainClass() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p = new JPanel();
    p.setPreferredSize(new Dimension(200, 200));

    JTable jt = new JTable(10, 3);
    p.add(new JScrollPane(jt));

    getContentPane().add(p);/*w w w .j a v  a  2s  .c  om*/

    pack();
    setVisible(true);
}

From source file:Main.java

Main() {
    Object[][] data = { { "A", "B", "Snowboarding", new Integer(5) }, { "C", "D", "Pool", new Integer(10) } };
    Object[] columnNames = { "firstname", "lastname", "age" };
    final JTable table = new JTable(data, columnNames) {
        @Override//from   w ww  .  j a  v a  2  s .  co m
        public Dimension getPreferredScrollableViewportSize() {
            Dimension d = getPreferredSize();
            int n = getRowHeight();
            return new Dimension(d.width, (n * ROWS));
        }
    };
    JPanel jPanel = new JPanel();
    jPanel.setLayout(new GridLayout());
    JScrollPane sp = new JScrollPane(table);
    jPanel.add(sp);
    JDialog jdialog = new JDialog();
    jdialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    jdialog.setContentPane(jPanel);

    jdialog.pack();
    jdialog.setVisible(true);
}