Example usage for javax.swing JTable setAutoResizeMode

List of usage examples for javax.swing JTable setAutoResizeMode

Introduction

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

Prototype

@BeanProperty(enumerationValues = { "JTable.AUTO_RESIZE_OFF", "JTable.AUTO_RESIZE_NEXT_COLUMN",
        "JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS", "JTable.AUTO_RESIZE_LAST_COLUMN",
        "JTable.AUTO_RESIZE_ALL_COLUMNS" }, description = "Whether the columns should adjust themselves automatically.")
public void setAutoResizeMode(int mode) 

Source Link

Document

Sets the table's auto resize mode when the table is resized.

Usage

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  2s  . 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);//  ww  w . ja v a 2  s .  co  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

public MainClass() {
    super("Simple JTable Test");
    setSize(300, 200);/*from w w w.  j a  va2  s. c  o  m*/
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    File pwd = new File(".");
    Object[][] stats = getFileStats(pwd);

    JTable jt = new JTable(stats, titles);
    jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    jt.setColumnSelectionAllowed(true);

    JScrollPane jsp = new JScrollPane(jt);
    getContentPane().add(jsp, BorderLayout.CENTER);
}

From source file:TableFeature.java

public TableFeature() {
    super("Simple JTable Test");
    setSize(300, 200);//from  w  w w .  j  av  a 2 s .  c o  m
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    File pwd = new File(".");
    Object[][] stats = getFileStats(pwd);

    JTable jt = new JTable(stats, titles);
    jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    jt.setColumnSelectionAllowed(true);

    JScrollPane jsp = new JScrollPane(jt);
    getContentPane().add(jsp, BorderLayout.CENTER);
}

From source file:MainClass.java

public MainClass() {
    super("Custom TableModel Test");
    setSize(300, 200);//from  w  ww. ja  v  a  2 s.com
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    FileModel fm = new FileModel();
    JTable jt = new JTable(fm);
    jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    jt.setColumnSelectionAllowed(true);

    JScrollPane jsp = new JScrollPane(jt);
    getContentPane().add(jsp, BorderLayout.CENTER);
}

From source file:SampleSortingTableModel.java

public SortableTableModelAbstractTableModel() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JTable tableOrig = new JTable(model);
    tableOrig.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    JTable tableSorted = new JTable(new SampleSortingTableModel(model, 0));
    tableSorted.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    JPanel panel = new JPanel(new GridLayout(1, 2));
    panel.add(new JScrollPane(tableOrig));
    panel.add(new JScrollPane(tableSorted));
    getContentPane().add(panel, BorderLayout.CENTER);
    pack();//  ww  w.  ja v a 2s . co m
}

From source file:FileTable2.java

public FileTable2() {
    super("Custom TableModel Test");
    setSize(300, 200);/*www. j a va2  s  .  c om*/
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    FileModel fm = new FileModel();
    JTable jt = new JTable(fm);
    jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    jt.setColumnSelectionAllowed(true);
    jt.setDefaultRenderer(Number.class, new BigRenderer(1000));

    JScrollPane jsp = new JScrollPane(jt);
    getContentPane().add(jsp, BorderLayout.CENTER);
}

From source file:Main.java

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

From source file:ThreadViewer.java

public ThreadViewer() {

    JTable table = new JTable(tableModel);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);

    TableColumnModel colModel = table.getColumnModel();
    int numColumns = colModel.getColumnCount();

    for (int i = 0; i < numColumns - 1; i++) {
        TableColumn col = colModel.getColumn(i);

        col.sizeWidthToFit();//w  w w  . j  av  a 2s. c  o m
        col.setPreferredWidth(col.getWidth() + 5);
        col.setMaxWidth(col.getWidth() + 5);
    }

    JScrollPane sp = new JScrollPane(table);

    setLayout(new BorderLayout());
    add(sp, BorderLayout.CENTER);
}

From source file:joinery.impl.Display.java

public static <V> void show(final DataFrame<V> df) {
    final List<Object> columns = new ArrayList<>(df.columns());
    final List<Class<?>> types = df.types();
    SwingUtilities.invokeLater(new Runnable() {
        @Override//from   w  w w  .  j  av  a2 s . co  m
        public void run() {
            final JFrame frame = new JFrame(title(df));
            final JTable table = new JTable(new AbstractTableModel() {
                private static final long serialVersionUID = 1L;

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

                @Override
                public int getColumnCount() {
                    return df.size();
                }

                @Override
                public Object getValueAt(final int row, final int col) {
                    return df.get(row, col);
                }

                @Override
                public String getColumnName(final int col) {
                    return String.valueOf(columns.get(col));
                }

                @Override
                public Class<?> getColumnClass(final int col) {
                    return types.get(col);
                }
            });
            table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.add(new JScrollPane(table));
            frame.pack();
            frame.setVisible(true);
        }
    });
}