Example usage for java.awt Component getPreferredSize

List of usage examples for java.awt Component getPreferredSize

Introduction

In this page you can find the example usage for java.awt Component getPreferredSize.

Prototype

public Dimension getPreferredSize() 

Source Link

Document

Gets the preferred size of this component.

Usage

From source file:net.sf.jabref.gui.fieldeditors.FileListEditor.java

public void adjustColumnWidth() {
    for (int column = 0; column < this.getColumnCount(); column++) {
        int width = 0;
        for (int row = 0; row < this.getRowCount(); row++) {
            TableCellRenderer renderer = this.getCellRenderer(row, column);
            Component comp = this.prepareRenderer(renderer, row, column);
            width = Math.max(comp.getPreferredSize().width, width);
        }/*from   w ww . j a  va2  s  .  c  om*/
        this.columnModel.getColumn(column).setPreferredWidth(width);
    }
}

From source file:logdruid.ui.mainpanel.EventRecordingSelectorPanel.java

private void initColumnSizes(JTable theTable) {
    TableColumn column = null;// ww w.j av a2 s. co  m
    Component comp = null;
    int headerWidth = 0;
    int cellWidth = 0;
    TableCellRenderer headerRenderer = theTable.getTableHeader().getDefaultRenderer();

    for (int i = 0; i < 4; i++) {
        column = theTable.getColumnModel().getColumn(i);
        comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0);
        headerWidth = comp.getPreferredSize().width;
        cellWidth = comp.getPreferredSize().width;
        column.setPreferredWidth(Math.max(headerWidth, cellWidth));
    }
}

From source file:logdruid.ui.table.EventRecordingEditorTable.java

private void initColumnSizes(JTable theTable) {
    MyTableModel model = (MyTableModel) theTable.getModel();
    TableColumn column = null;/*  w ww. ja  v  a2  s .  co m*/
    Component comp = null;
    int headerWidth = 0;
    int cellWidth = 0;
    TableCellRenderer headerRenderer = theTable.getTableHeader().getDefaultRenderer();

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

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

        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:ru.develgame.jflickrorganizer.MainForm.java

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor./*from  w w  w  . j  av a2s  .  c om*/
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {

    jLabelBackupFolder = new javax.swing.JLabel();
    jTextFieldBackupFolder = new javax.swing.JTextField();
    jButtonChooseBackupFolder = new javax.swing.JButton();
    jButtonBackup = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTreeAlbums = new javax.swing.JTree();
    jScrollPane2 = new javax.swing.JScrollPane();
    jTablePhotos = new JTable() {
        @Override
        public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
            Component component = super.prepareRenderer(renderer, row, column);
            int rendererWidth = component.getPreferredSize().width + 5;
            TableColumn tableColumn = getColumnModel().getColumn(column);
            tableColumn.setPreferredWidth(
                    Math.max(rendererWidth + getIntercellSpacing().width, tableColumn.getPreferredWidth()));
            return component;
        }
    };
    jComboBoxVisibility = new javax.swing.JComboBox<>();
    jPanel1 = new javax.swing.JPanel();
    jLabelAll = new javax.swing.JLabel();
    jMenuBar1 = new javax.swing.JMenuBar();
    jMenu1 = new javax.swing.JMenu();
    jMenuItemSetPassword = new javax.swing.JMenuItem();
    jMenuItemExit = new javax.swing.JMenuItem();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("MainForm"); // NOI18N
    setTitle(bundle.getString("MainForm.title")); // NOI18N
    setMinimumSize(new java.awt.Dimension(1024, 717));
    setName("Form"); // NOI18N

    jLabelBackupFolder.setText(bundle.getString("MainForm.jLabelBackupFolder.text")); // NOI18N
    jLabelBackupFolder.setName("jLabelBackupFolder"); // NOI18N

    jTextFieldBackupFolder.setName("jTextFieldBackupFolder"); // NOI18N

    jButtonChooseBackupFolder.setText(bundle.getString("MainForm.jButtonChooseBackupFolder.text")); // NOI18N
    jButtonChooseBackupFolder.setName("jButtonChooseBackupFolder"); // NOI18N
    jButtonChooseBackupFolder.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButtonChooseBackupFolderActionPerformed(evt);
        }
    });

    jButtonBackup.setText(bundle.getString("MainForm.jButtonBackup.text")); // NOI18N
    jButtonBackup.setName("jButtonBackup"); // NOI18N
    jButtonBackup.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButtonBackupActionPerformed(evt);
        }
    });

    jScrollPane1.setName("jScrollPane1"); // NOI18N

    jTreeAlbums.setName("jTreeAlbums"); // NOI18N
    jScrollPane1.setViewportView(jTreeAlbums);

    jScrollPane2.setName("jScrollPane2"); // NOI18N

    jTablePhotos.setAutoCreateRowSorter(true);
    jTablePhotos.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {

    }, new String[] {

    }));
    jTablePhotos.setToolTipText(bundle.getString("MainForm.jTablePhotos.toolTipText")); // NOI18N
    jTablePhotos.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
    jTablePhotos.setName("jTablePhotos"); // NOI18N
    jTablePhotos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    jScrollPane2.setViewportView(jTablePhotos);

    jComboBoxVisibility.setName("jComboBoxVisibility"); // NOI18N

    jPanel1.setName("jPanel1"); // NOI18N

    jLabelAll.setText(bundle.getString("MainForm.jLabelAll.text")); // NOI18N
    jLabelAll.setName("jLabelAll"); // NOI18N

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout
            .setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                            jPanel1Layout.createSequentialGroup()
                                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabelAll).addContainerGap()));
    jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE).addComponent(jLabelAll)));

    jMenuBar1.setName("jMenuBar1"); // NOI18N

    jMenu1.setText(bundle.getString("MainForm.jMenu1.text")); // NOI18N
    jMenu1.setName("jMenu1"); // NOI18N

    jMenuItemSetPassword.setText(bundle.getString("MainForm.jMenuItemSetPassword.text")); // NOI18N
    jMenuItemSetPassword.setName("jMenuItemSetPassword"); // NOI18N
    jMenuItemSetPassword.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jMenuItemSetPasswordActionPerformed(evt);
        }
    });
    jMenu1.add(jMenuItemSetPassword);

    jMenuItemExit.setText(bundle.getString("MainForm.jMenuItemExit.text")); // NOI18N
    jMenuItemExit.setName("jMenuItemExit"); // NOI18N
    jMenuItemExit.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jMenuItemExitActionPerformed(evt);
        }
    });
    jMenu1.add(jMenuItemExit);

    jMenuBar1.add(jMenu1);

    setJMenuBar(jMenuBar1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup().addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup().addComponent(jLabelBackupFolder)
                                    .addGap(68, 68, 68).addComponent(jTextFieldBackupFolder)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jButtonChooseBackupFolder)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jButtonBackup))
                            .addGroup(layout.createSequentialGroup()
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 213,
                                            javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(layout
                                            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                    784, Short.MAX_VALUE)
                                            .addGroup(layout.createSequentialGroup()
                                                    .addComponent(jComboBoxVisibility,
                                                            javax.swing.GroupLayout.PREFERRED_SIZE, 271,
                                                            javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addGap(0, 0, Short.MAX_VALUE)))))
                    .addContainerGap())
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                    Short.MAX_VALUE));
    layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
            javax.swing.GroupLayout.Alignment.TRAILING,
            layout.createSequentialGroup().addContainerGap().addGroup(layout
                    .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldBackupFolder, javax.swing.GroupLayout.PREFERRED_SIZE,
                            javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonBackup).addComponent(jButtonChooseBackupFolder)
                    .addComponent(jLabelBackupFolder))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                    .addComponent(jComboBoxVisibility, javax.swing.GroupLayout.PREFERRED_SIZE,
                                            javax.swing.GroupLayout.DEFAULT_SIZE,
                                            javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 590,
                                            Short.MAX_VALUE))
                            .addComponent(jScrollPane1))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jPanel1,
                            javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                            javax.swing.GroupLayout.PREFERRED_SIZE)));

    pack();
}

From source file:logdruid.ui.table.StatRecordingEditorTable.java

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

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

        comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0);
        headerWidth = comp.getPreferredSize().width;
        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:BeanContainer.java

protected int getDivider(Container parent) {
        if (m_divider > 0)
            return m_divider;

        int divider = 0;
        for (int k = 0; k < parent.getComponentCount(); k += 2) {
            Component comp = parent.getComponent(k);
            Dimension d = comp.getPreferredSize();
            divider = Math.max(divider, d.width);
        }//  w  w  w  .  ja  va 2  s.  co  m
        divider += m_hGap;
        return divider;
    }

From source file:BeanContainer.java

public Dimension preferredLayoutSize(Container parent) {
        int divider = getDivider(parent);

        int w = 0;
        int h = 0;
        for (int k = 1; k < parent.getComponentCount(); k += 2) {
            Component comp = parent.getComponent(k);
            Dimension d = comp.getPreferredSize();
            w = Math.max(w, d.width);
            h += d.height + m_vGap;/*  www  .  j a v  a2 s .  c  o  m*/
        }
        h -= m_vGap;

        Insets insets = parent.getInsets();
        return new Dimension(divider + w + insets.left + insets.right, h + insets.top + insets.bottom);
    }

From source file:BeanContainer.java

public void layoutContainer(Container parent) {
        int divider = getDivider(parent);

        Insets insets = parent.getInsets();
        int w = parent.getWidth() - insets.left - insets.right - divider;
        int x = insets.left;
        int y = insets.top;

        for (int k = 1; k < parent.getComponentCount(); k += 2) {
            Component comp1 = parent.getComponent(k - 1);
            Component comp2 = parent.getComponent(k);
            Dimension d = comp2.getPreferredSize();

            comp1.setBounds(x, y, divider - m_hGap, d.height);
            comp2.setBounds(x + divider, y, w, d.height);
            y += d.height + m_vGap;// w  ww. java 2s  .  com
        }
    }

From source file:net.sf.jabref.gui.maintable.MainTable.java

@Override
public String getToolTipText(MouseEvent e) {

    // Set tooltip text for all columns which are not fully displayed

    String toolTipText = null;//ww w  .  j  a  va2 s .  com
    Point p = e.getPoint();
    int col = columnAtPoint(p);
    int row = rowAtPoint(p);
    Component comp = prepareRenderer(getCellRenderer(row, col), row, col);

    Rectangle bounds = getCellRect(row, col, false);

    Dimension d = comp.getPreferredSize();
    if ((d != null) && (d.width > bounds.width) && (getValueAt(row, col) != null)) {
        toolTipText = getValueAt(row, col).toString();
    }
    return toolTipText;
}

From source file:logdruid.ui.mainpanel.MetadataRecordingSelectorPanel.java

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

    for (int i = 0; i < 4; i++) {
        column = theTable.getColumnModel().getColumn(i);
        comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0);
        headerWidth = comp.getPreferredSize().width;
        cellWidth = comp.getPreferredSize().width;
        column.setPreferredWidth(Math.max(headerWidth, cellWidth));
    }
}