Example usage for javax.swing.table TableColumn setPreferredWidth

List of usage examples for javax.swing.table TableColumn setPreferredWidth

Introduction

In this page you can find the example usage for javax.swing.table TableColumn setPreferredWidth.

Prototype

@BeanProperty(description = "The preferred width of the column.")
public void setPreferredWidth(int preferredWidth) 

Source Link

Document

Sets this column's preferred width to preferredWidth.

Usage

From source file:it.cnr.icar.eric.client.ui.swing.RegistryObjectsTable.java

/**
 * Creates default columns for the table from
 * the data model using the <code>getColumnCount</code> method
 * defined in the <code>TableModel</code> interface.
 *
 * Clears any existing columns before creating the
 * new columns based on information from the model.
 *
 * Overrides base class behaviour by setting the column width as a % of the
 * viewport width./* w  w  w  .  j  ava 2  s . c  om*/
 */
public void createDefaultColumnsFromModel() {
    TableModel m = getModel();
    if (m != null) {
        // Remove any current columns
        TableColumnModel cm = getColumnModel();
        while (cm.getColumnCount() > 0) {
            cm.removeColumn(cm.getColumn(0));
        }

        // get parent width
        int parentWidth = 0;
        Component parent = getParent();
        if (parent != null) {
            parentWidth = parent.getWidth();
        }

        // Create new columns from the data model info
        int columnCount = m.getColumnCount();
        for (int i = 0; i < m.getColumnCount(); i++) {
            int width = tableModel.getColumnWidth(i);
            if (width == 0) {
                width = parentWidth / columnCount;
            } else {
                //Width is a % of the viewport width
                width = (width * parentWidth) / 100;
            }
            TableColumn newColumn = new TableColumn(i);
            newColumn.setPreferredWidth(width);
            addColumn(newColumn);
        }
    }
}

From source file:it.iit.genomics.cru.igb.bundles.mi.view.MITable.java

public MITable(MITableModel model, IgbService service, final MIQuery query) {
    super(model);

    this.query = query;
    igbLogger = IGBLogger.getInstance(query.getLabel());
    this.igbService = service;

    this.SymSelectionListener = new MouseListener() {

        @SuppressWarnings("unchecked")
        @Override/*ww w. ja  v a  2s  .c  o m*/
        public void mouseClicked(MouseEvent e) {

            if (e.getComponent().isEnabled() && e.getButton() == MouseEvent.BUTTON1) {

                if (getSelectedRow() >= 0) {
                    int modelRow = convertRowIndexToModel(getSelectedRow());

                    MIResult interaction = ((MITableModel) getModel()).getResult(modelRow);

                    structuresPanel.setCurrentInteraction(interaction);
                }

                MITable table = (MITable) e.getComponent();
                int modelRow = convertRowIndexToModel(table.getSelectedRow());
                int column = table.getSelectedColumn();

                if (e.getClickCount() == 1) {
                    if (column == MITableModel.TRACK_COLUMN) {
                        Object value = table.getValueAt(table.getSelectedRow(), column);

                        if (value instanceof JButton) {
                            MIResult result = ((MITableModel) table.getModel()).getResult(modelRow);

                            TypeContainerAnnot interactorTrack = result.createTrack();

                            igbService.addTrack(interactorTrack, interactorTrack.getID());

                            igbService.getSeqMapView().updatePanel();

                            for (TierGlyph t : igbService.getAllTierGlyphs()) {

                                if (TierGlyph.TierType.ANNOTATION.equals(t.getTierType())
                                        && (t.getAnnotStyle().getTrackName().equals(interactorTrack.getID()))) {

                                    SimpleTrackStyle style = new SimpleTrackStyle(interactorTrack.getID(),
                                            false) {

                                        @Override
                                        public boolean drawCollapseControl() {
                                            return false;
                                        }
                                    };

                                    t.getAnnotStyle().copyPropertiesFrom(style);
                                    t.getAnnotStyle().setColorProvider(new RGB());
                                    interactorTrack.setProperty(TrackLineParser.ITEM_RGB, "on");
                                }
                            }

                            igbService.getSeqMapView().updatePanel();

                            ((JButton) value).setText(interactorTrack.getID());
                            ((JButton) value).setEnabled(false);

                            updateUI();
                        }
                    }
                } else {
                    // symmetry: zoom-in
                    if (column == MITableModel.SYMS1_COLUMN) {
                        MoleculeEntry entry = ((MITableModel) table.getModel()).getResult(modelRow)
                                .getInteractor1();
                        if (query.getTaxid().equals(entry.getTaxid())) {
                            Collection<SeqSymmetry> syms = ((MITableModel) table.getModel()).getResult(modelRow)
                                    .getSymmetries1();
                            zoomToSym(syms);
                        }
                    } else if (column == MITableModel.SYMS2_COLUMN) {
                        MoleculeEntry entry = ((MITableModel) table.getModel()).getResult(modelRow)
                                .getInteractor2();
                        if (query.getTaxid().equals(entry.getTaxid())) {
                            Collection<SeqSymmetry> syms = ((MITableModel) table.getModel()).getResult(modelRow)
                                    .getSymmetries2();
                            zoomToSym(syms);
                        }
                    }

                    // Protein: link to uniprot
                    if (column == MITableModel.INTERACTOR1_COLUMN
                            || column == MITableModel.INTERACTOR2_COLUMN) {

                        MIResult miResult = ((MITableModel) table.getModel()).getResult(modelRow);

                        String id;
                        String taxid;

                        MoleculeEntry interactor;
                        if (column == MITableModel.INTERACTOR1_COLUMN) {
                            interactor = miResult.getInteractor1();
                        } else {
                            interactor = miResult.getInteractor2();
                        }

                        taxid = interactor.getTaxid();

                        String query;
                        String anchor = "";
                        switch (taxid) {
                        case MoleculeEntry.TAXID_DNA:
                        case MoleculeEntry.TAXID_RNA:
                            if (miResult.getInteractionStructures().isEmpty()) {
                                return;
                            }
                            query = "http://www.pdb.org/pdb/explore/explore.do?structureId="
                                    + miResult.getInteractionStructures().iterator().next().getStructureID();
                            break;
                        case MoleculeEntry.TAXID_LIGAND:
                            if (miResult.getInteractionStructures().isEmpty()) {
                                return;
                            }
                            query = DrugBankMapper.getInstance().isDrug(interactor.getGeneName())
                                    ? DrugBankMapper.getInstance().getDrugBankLink(interactor.getGeneName())
                                    : "http://www.ebi.ac.uk/pdbe-srv/pdbechem/chemicalCompound/show/"
                                            + interactor.getGeneName();
                            break;
                        case MoleculeEntry.TAXID_MODIFICATION:
                            query = "http://www.uniprot.org/uniprot/"
                                    + miResult.getInteractor1().getUniprotAc();
                            anchor = "#ptm_processing";
                            break;
                        default:
                            id = interactor.getUniprotAc();
                            query = "http://www.uniprot.org/uniprot/" + id;
                            break;
                        }
                        try {
                            URI uri = new URI(URIUtil.encodeQuery(query) + anchor);

                            Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
                            if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
                                desktop.browse(uri);
                            }
                        } catch (IOException ioe) {
                            JOptionPane.showMessageDialog(null, "Cannot reach Uniprot website.");
                            return;
                        } catch (URISyntaxException ue) {
                            JOptionPane.showMessageDialog(null, "Cannot reach Uniprot website: " + query);
                        }
                    }

                    // interaction type: link to psicquic
                    if (column == MITableModel.INTERACTION_TYPE_COLUMN) {

                        MIResult miResult = ((MITableModel) table.getModel()).getResult(modelRow);

                        String queryURL;
                        String idA = miResult.getInteractor1().getUniprotAc();
                        String idB = miResult.getInteractor2().getUniprotAc();

                        if (null == miResult.getPsicquicUrl()) {
                            // from the structure database
                            if (query.searchDSysMap()) {
                                queryURL = "http://http://dsysmap.irbbarcelona.org/results.php?type=proteins&neigh=2&value="
                                        + idA + "," + idB;
                            } else if (query.searchInteractome3D()) {
                                try {
                                    queryURL = "http://interactome3d.irbbarcelona.org/interaction.php?ids="
                                            + idA + ";" + idB + "&dataset="
                                            + it.iit.genomics.cru.bridges.interactome3d.ws.Utils
                                                    .getDataset(query.getTaxid());
                                } catch (Interactome3DException e3d) {
                                    // it will never happend: if the taxid was not known by 
                                    // I3D, we wouldn't have an interaction
                                    return;
                                }

                            } else {
                                return;
                            }
                        } else {

                            if (false == idA.equals(idB)) {
                                queryURL = miResult.getPsicquicUrl() + "query/id:"
                                        + miResult.getInteractor1().getUniprotAc() + "* AND id:"
                                        + miResult.getInteractor2().getUniprotAc() + "*";
                            } else {
                                queryURL = miResult.getPsicquicUrl() + "query/idA:"
                                        + miResult.getInteractor1().getUniprotAc() + "* AND idB:"
                                        + miResult.getInteractor2().getUniprotAc() + "*";
                            }
                        }
                        try {
                            URI uri = new URI(URIUtil.encodeQuery(queryURL));

                            Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
                            if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
                                desktop.browse(uri);
                            }
                        } catch (IOException ioe) {
                            JOptionPane.showMessageDialog(null, "Cannot reach psicquic server.");
                        } catch (URISyntaxException ue) {
                            JOptionPane.showMessageDialog(null, "Cannot reach psicquic server: " + queryURL);
                        }

                    }
                }
            }

        }

        @Override
        public void mousePressed(MouseEvent me) {
        }

        @Override
        public void mouseReleased(MouseEvent me) {
        }

        @Override
        public void mouseEntered(MouseEvent me) {
        }

        @Override
        public void mouseExited(MouseEvent me) {
        }
    };

    TableRowSorter<MITableModel> sorter = new MITableRowSorter(model);
    setRowSorter(sorter);

    sorter.setRowFilter(evidenceRowFilter());
    model.fireTableDataChanged();
    this.getTableHeader().setReorderingAllowed(false);

    TableCellRenderer rend = getTableHeader().getDefaultRenderer();
    TableColumnModel tcm = getColumnModel();
    for (int j = 0; j < tcm.getColumnCount(); j += 1) {
        TableColumn tc = tcm.getColumn(j);
        TableCellRenderer rendCol = tc.getHeaderRenderer(); // likely null
        if (rendCol == null) {
            rendCol = rend;
        }
        Component c = rendCol.getTableCellRendererComponent(this, tc.getHeaderValue(), false, false, 0, j);
        tc.setPreferredWidth(c.getPreferredSize().width);
    }

    TableCellRenderer buttonRenderer = new JTableButtonRenderer();

    getColumn(model.getColumnName(MITableModel.TRACK_COLUMN)).setCellRenderer(buttonRenderer);

    getColumn(model.getColumnName(MITableModel.SYMS1_COLUMN)).setCellRenderer(new GeneRenderer());
    getColumn(model.getColumnName(MITableModel.SYMS2_COLUMN)).setCellRenderer(new GeneRenderer());

    getColumn(model.getColumnName(MITableModel.INTERACTOR1_COLUMN)).setCellRenderer(new MoleculeRenderer());
    getColumn(model.getColumnName(MITableModel.INTERACTOR2_COLUMN)).setCellRenderer(new MoleculeRenderer());

    getColumn(model.getColumnName(MITableModel.INTERACTION_TYPE_COLUMN))
            .setCellRenderer(new EvidenceRenderer());

    getColumn(model.getColumnName(MITableModel.STRUCTURES_COLUMN)).setCellRenderer(new StructuresRenderer());

    setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);

    int smallWidth = 75;
    int mediumWidth = 120;
    int largeWidth = 200;
    getColumnModel().getColumn(MITableModel.TRACK_COLUMN).setMinWidth(smallWidth);

    getColumnModel().getColumn(MITableModel.STRUCTURES_COLUMN).setMinWidth(smallWidth);
    getColumnModel().getColumn(MITableModel.STRUCTURES_COLUMN).setMaxWidth(smallWidth);
    getColumnModel().getColumn(MITableModel.STRUCTURES_COLUMN).setPreferredWidth(smallWidth);

    addMouseListener(SymSelectionListener);

    getSelectionModel().addListSelectionListener(new RowSelectionListener());

}

From source file:logdruid.ui.DateEditor.java

private void initColumnSizes(JTable theTable) {
    MyTableModel2 model = (MyTableModel2) theTable.getModel();
    TableColumn column = null;
    Component comp = null;/*  w w  w .j  ava 2  s. c o  m*/
    int headerWidth = 0;
    int cellWidth = 0;
    // Object[] longValues = model.longValues;
    TableCellRenderer headerRenderer = theTable.getTableHeader().getDefaultRenderer();

    for (int i = 0; i < 3; i++) {
        column = theTable.getColumnModel().getColumn(i);

        comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0);
        headerWidth = comp.getPreferredSize().width;

        /*
         * comp = table.getDefaultRenderer(model.getColumnClass(i)).
         * getTableCellRendererComponent( table, longValues[i], false,
         * false, 0, i);
         */
        cellWidth = comp.getPreferredSize().width;

        if (DEBUG) {
            logger.info("Initializing width of column " + i + ". " + "headerWidth = " + headerWidth
                    + "; cellWidth = " + cellWidth);
        }

        column.setPreferredWidth(Math.max(headerWidth, cellWidth));
    }
}

From source file:com.emental.mindraider.ui.outline.OutlineJPanel.java

/**
 * Creates notebook outline./* w  w w.ja  va2 s. c om*/
 */
private OutlineJPanel() {
    setDoubleBuffered(true);
    setLayout(new BorderLayout());

    /*
     * toolbar
     */

    JToolBar toolbar = createToolbar();

    /*
     * tree
     */

    // tree table itself
    outlineTableTree = OutlineTreeInstance.getInstance();
    outlineTableTreeModel = new NotebookOutlineModel(outlineTableTree.getOutlineRoot());
    treeTable = new JTreeTable(outlineTableTreeModel);
    treeTable.tree.addTreeSelectionListener(new TreeSelectionListenerImplementation());
    // add key listener
    treeTable.addKeyListener(new KeyListenerImplementation());

    // label column
    TableColumn tableColumn = treeTable
            .getColumn(NotebookOutlineModel.columnNames[NotebookOutlineModel.COLUMN_LABEL]);
    tableColumn.setMaxWidth(LABEL_COLUMN_MAX_WIDTH);
    tableColumn.setMinWidth(0);
    tableColumn.setPreferredWidth(LABEL_COLUMN_PREFERRED_WIDTH);
    // date column
    tableColumn = treeTable.getColumn(NotebookOutlineModel.columnNames[NotebookOutlineModel.COLUMN_CREATED]);
    tableColumn.setMaxWidth(DATE_COLUMN_MAX_WIDTH);
    tableColumn.setMinWidth(0);
    tableColumn.setPreferredWidth(DATE_COLUMN_PREFERRED_WIDTH);
    // and the rest will be annotation
    JScrollPane treeTableScrollPane = new JScrollPane(treeTable);

    // outline treetabble + toolbar panel
    JPanel treeAndToolbarPanel = new JPanel(new BorderLayout());
    treeAndToolbarPanel.add(toolbar, BorderLayout.NORTH);
    treeAndToolbarPanel.add(treeTableScrollPane, BorderLayout.CENTER);

    /*
     * outline / list tabbed pane
     */

    outlineAndTreeTabbedPane = new JTabbedPane(JTabbedPane.BOTTOM);
    outlineAndTreeTabbedPane.add(treeAndToolbarPanel, "Outline");
    outlineSorterJPanel = new OutlineSorterJPanel();
    outlineAndTreeTabbedPane.add(outlineSorterJPanel, "Sorter");
    outlineArchiveJPanel = new OutlineArchiveJPanel();
    outlineAndTreeTabbedPane.add(outlineArchiveJPanel, "Archive");

    /*
     * concept sidebar
     */

    conceptJPanel = (ConceptJPanel) MindRaiderSpringContext.getCtx().getBean("conceptPanel");

    /*
     * vertical split of notebook outline and RDF graph
     */

    treeAndSpidersSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    treeAndSpidersSplit.setContinuousLayout(true);
    treeAndSpidersSplit.setOneTouchExpandable(true);
    treeAndSpidersSplit.setDividerLocation(200);
    treeAndSpidersSplit.setLastDividerLocation(150);
    treeAndSpidersSplit.setDividerSize(6);
    treeAndSpidersSplit.add(outlineAndTreeTabbedPane);

    // spiders & tags visual navigation
    spidersAndTagsTabs = new JTabbedPane(JTabbedPane.BOTTOM);
    if (MindRaider.profile.isEnableSpiders()) {
        // notebook mind map
        spidersAndTagsTabs.addTab("Mind Map", MindRaider.spidersGraph.getPanel()); // TODO bundle
    }
    // global tags
    spidersAndTagsTabs.addTab("Tag Cloud", MindRaider.tagCustodian.getPanel()); // TODO bundle
    // global mind map
    //spidersAndTagsTabs.addTab("Global Mind Map",new JPanel()); // TODO bundle

    // lazy spiders rendering
    spidersAndTagsTabs.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent changeEvent) {
            JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.getSource();
            if (sourceTabbedPane.getSelectedIndex() == 0) {
                MindRaider.spidersGraph.renderModel();
            }
        }
    });

    if (!new ConfigurationBean().isDefaultTabMindMap()) {
        spidersAndTagsTabs.setSelectedIndex(1);
    }

    MindRaider.tagCustodian.redraw();

    // add spiders panel
    treeAndSpidersSplit.add(spidersAndTagsTabs);

    /*
     * horizontal split of outline/graph slit and concept sidebar
     */

    rightSiderbarSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treeAndSpidersSplit, conceptJPanel);
    rightSiderbarSplitPane.setOneTouchExpandable(true);
    rightSiderbarSplitPane.setContinuousLayout(true);
    rightSiderbarSplitPane.setDividerLocation(500);
    rightSiderbarSplitPane.setLastDividerLocation(500);
    rightSiderbarSplitPane.setDividerSize(6);

    add(rightSiderbarSplitPane);
}

From source file:modnlp.capte.AlignmentInterfaceWS.java

public JTable autoResizeColWidth(JTable table, DefaultTableModel model) {
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.setModel(model);//w  w w.  j  ava2 s .  co  m

    int margin = 5;

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

        // Get width of column header
        TableCellRenderer renderer = col.getHeaderRenderer();

        if (renderer == null) {
            renderer = table.getTableHeader().getDefaultRenderer();
        }

        Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0,
                0);

        width = comp.getPreferredSize().width;

        // Get maximum width of column data
        for (int r = 0; r < table.getRowCount(); r++) {
            renderer = table.getCellRenderer(r, vColIndex);
            comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false,
                    r, vColIndex);
            width = Math.max(width, comp.getPreferredSize().width);
        }

        // Add margin
        width += 2 * margin;

        // Set the width
        col.setPreferredWidth(width);
    }

    ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer())
            .setHorizontalAlignment(SwingConstants.LEFT);

    // table.setAutoCreateRowSorter(true);
    table.getTableHeader().setReorderingAllowed(false);

    return table;
}

From source file:display.containers.FileManager.java

private void setColumnWidth(int column, int width) {
    TableColumn tableColumn = table.getColumnModel().getColumn(column);
    if (width < 0) {
        // use the preferred width of the header..
        JLabel label = new JLabel((String) tableColumn.getHeaderValue());
        Dimension preferred = label.getPreferredSize();
        // altered 10->14 as per camickr comment.
        width = (int) preferred.getWidth() + 14;
    }/*www  .  j a v a  2 s. co  m*/
    tableColumn.setPreferredWidth(width);
    tableColumn.setMaxWidth(width);
    tableColumn.setMinWidth(width);
}

From source file:grupob.TipoProceso.java

private void agregarDatos() {
    DefaultTableModel modelo = (DefaultTableModel) jTableRegiones.getModel();
    modelo.setRowCount(0);//  w  ww .ja va  2  s .  c  o m
    String datos[] = new String[3];
    for (int i = 0; i < listaRegiones.size(); i++) {
        datos[0] = listaRegiones.get(i).getNombre();
        if (listaRegiones.get(i).getCantidadVotantesRegistrados() == 0) {
            datos[1] = "";
        } else {
            datos[1] = Long.toString(listaRegiones.get(i).getCantidadVotantesRegistrados());
        }
        modelo.addRow(datos);
    }
    TableColumn colum1 = null;
    colum1 = jTableRegiones.getColumnModel().getColumn(0);
    colum1.setPreferredWidth(60);
    TableColumn colum2 = null;
    colum2 = jTableRegiones.getColumnModel().getColumn(1);
    colum2.setPreferredWidth(5);
    TableColumn colum3 = null;
    colum3 = jTableRegiones.getColumnModel().getColumn(2);
    colum3.setPreferredWidth(40);
    colum3.setPreferredWidth(10);
}

From source file:grupob.TipoProceso.java

private void agregarDatosDistritos() {
    DefaultTableModel modelo = (DefaultTableModel) jTableDistritos.getModel();
    modelo.setRowCount(0);//from  www . ja  va  2s .com
    String datos[] = new String[4];
    for (int i = 0; i < listaDistritos.size(); i++) {
        datos[0] = listaDistritos.get(i).getNombre();
        if (listaDistritos.get(i).getCantidadVotantesRegistrados() == 0) {
            datos[1] = "";
        } else {
            datos[1] = Long.toString(listaDistritos.get(i).getCantidadVotantesRegistrados());
        }
        String n = Manager.queryByIdRegion(listaDistritos.get(i).getIdRegion()).getNombre();
        datos[2] = "" + n;
        modelo.addRow(datos);
    }
    TableColumn colum1 = null;
    colum1 = jTableDistritos.getColumnModel().getColumn(0);
    colum1.setPreferredWidth(60);
    TableColumn colum2 = null;
    colum2 = jTableDistritos.getColumnModel().getColumn(1);
    colum2.setPreferredWidth(5);
    TableColumn colum3 = null;
    colum3 = jTableDistritos.getColumnModel().getColumn(2);
    colum3.setPreferredWidth(5);
    TableColumn colum4 = null;
    colum4 = jTableDistritos.getColumnModel().getColumn(3);
    colum4.setPreferredWidth(40);
    colum4.setPreferredWidth(10);
}

From source file:grupob.TipoProceso.java

private void buscarRegionesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarRegionesActionPerformed
    ArrayList<Region> listaBuscada = new ArrayList<Region>();
    String busc = textRegiones.getText();
    if (busc.compareTo("") == 0) {
        listaBuscada = Manager.queryAllRegion();
    } else {//w  ww  .ja  v  a2 s  .  com
        listaBuscada = Manager.queryByNameRegion(busc);
    }

    DefaultTableModel modelo = (DefaultTableModel) jTableRegiones.getModel();
    modelo.setRowCount(0);
    String datos[] = new String[3];
    for (int i = 0; i < listaBuscada.size(); i++) {
        datos[0] = listaBuscada.get(i).getNombre();
        if (listaBuscada.get(i).getCantidadVotantesRegistrados() == 0) {
            datos[1] = "";
        } else {
            datos[1] = Long.toString(listaBuscada.get(i).getCantidadVotantesRegistrados());
        }
        modelo.addRow(datos);
    }
    TableColumn colum1 = null;
    colum1 = jTableRegiones.getColumnModel().getColumn(0);
    colum1.setPreferredWidth(60);
    TableColumn colum2 = null;
    colum2 = jTableRegiones.getColumnModel().getColumn(1);
    colum2.setPreferredWidth(5);
    TableColumn colum3 = null;
    colum3 = jTableRegiones.getColumnModel().getColumn(2);
    colum3.setPreferredWidth(40);
    colum3.setPreferredWidth(10);
    listaRegiones = listaBuscada;
}

From source file:grupob.TipoProceso.java

private void buscarDistritosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarDistritosActionPerformed

    ArrayList<Distrito> listaBuscada = new ArrayList<Distrito>();
    String busc = textDistrito.getText();
    if (busc.compareTo("") == 0) {
        listaBuscada = Manager.queryAllDistrito();
    } else {/*  ww w  .j  a  v  a  2  s.  c o  m*/
        listaBuscada = Manager.queryByNameDistrito(busc);
    }
    DefaultTableModel modelo2 = (DefaultTableModel) jTableDistritos.getModel();
    modelo2.setRowCount(0);
    String datos[] = new String[3];
    for (int i = 0; i < listaBuscada.size(); i++) {
        datos[0] = listaBuscada.get(i).getNombre();
        if (listaBuscada.get(i).getCantidadVotantesRegistrados() == 0) {
            datos[1] = "";
        } else {
            datos[1] = Long.toString(listaBuscada.get(i).getCantidadVotantesRegistrados());
        }
        Distrito s = listaBuscada.get(i);
        String n = Manager.queryByIdRegion(s.getIdRegion()).getNombre();
        datos[2] = "" + n;
        modelo2.addRow(datos);
    }
    TableColumn colum1 = null;
    colum1 = jTableDistritos.getColumnModel().getColumn(0);
    colum1.setPreferredWidth(60);
    TableColumn colum2 = null;
    colum2 = jTableDistritos.getColumnModel().getColumn(1);
    colum2.setPreferredWidth(5);
    TableColumn colum3 = null;
    colum3 = jTableDistritos.getColumnModel().getColumn(2);
    colum3.setPreferredWidth(5);
    TableColumn colum4 = null;
    colum4 = jTableDistritos.getColumnModel().getColumn(3);
    colum4.setPreferredWidth(40);
    colum4.setPreferredWidth(10);
    listaDistritos = listaBuscada;
}