Foro sobre Java SE > Como fijar el tamaño del JProgressBar
No entiendo la necesidad de estas líneas:
JPB.setBounds(22, 60, 507, 25);
Panel3.updateUI();
Panel3.repaint();
Aparte de que para fijar el tamaño de un componente, no se debe usar setBounds, sino setPreferredSize.
Tampoco entiendo cómo el setMaximun, que se usa para declarar el límite superior del modelo del JProgressBar, afecta el tamaño del componente.
Creo que hay un fallo de diseño en tu código.
Con ese fragmento que has publicado, no se puede entender el problema que tienes.
import java.awt.Dimension;
import javax.swing.JFrame;
public class Test extends JFrame {
private static final long serialVersionUID = 550_084_196_391_435_811L;
public Test() {
initComponents();
jProgressBar1.setMaximum(123508577);
final Dimension preferredSize = jProgressBar1.getPreferredSize();
System.out.println("ancho= " + preferredSize.width);
System.out.println("alto= " + preferredSize.height);
}
/** 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jProgressBar1 = new javax.swing.JProgressBar();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new java.awt.FlowLayout());
jPanel1.add(jProgressBar1);
getContentPane().add(jPanel1);
pack();
}// </editor-fold>
/**
@param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Test().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
private javax.swing.JProgressBar jProgressBar1;
// End of variables declaration
}
Abra una manera de de fijar el tamaño del jprogressbar teniendo en layout null??? ya que estoy utilizando el layout null porque aparte tengo otros componentes... y solo tengo problemas con la barra de progreso...
Si además usas un layout null, sin ver el código completo no es posible saber qué clase de problema tienes
.
Se pueden crear formularios visuales, con decenas de componentes diferentes, sin necesidad de declarar null el layout del contenedor.
El posicionamiento absoluto, layout null, es el más complicado de manejar:
http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html
Although we strongly recommend that you use layout managers, you can perform layout without them. By setting a container's layout property to null, you make the container use no layout manager. With this strategy, called absolute positioning, you must specify the size and position of every component within that container. One drawback of absolute positioning is that it does not adjust well when the top-level container is resized. It also does not adjust well to differences between users and systems, such as different font sizes and locales.
Para las posiciones y las ubicaciones existen métodos específicos para cada componente: setMinimunSize, setMaximunSize, set PreferredSize, setLocation...
Del mismo enlace anterior:
Any real application will need to reset the layout manager. Again, you should use an appropriate tool to do this, rather than coding the manager by hand.
Como puedes ver, se pueden crear formularios complejos, sin problemas de tamaños y posiciones. En este caso con la ayuda de un buen Editor Gráfico, como el Matisse de NetBeans.
public class Test extends javax.swing.JFrame {
public Test() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jRadioButton1 = new javax.swing.JRadioButton();
jCheckBox1 = new javax.swing.JCheckBox();
jComboBox1 = new javax.swing.JComboBox();
jScrollPane1 = new javax.swing.JScrollPane();
jTree1 = new javax.swing.JTree();
jScrollPane2 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jProgressBar1 = new javax.swing.JProgressBar();
jScrollPane3 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jSpinner1 = new javax.swing.JSpinner();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
java.awt.GridBagLayout layout = new java.awt.GridBagLayout();
layout.columnWidths = new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
layout.rowHeights = new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
getContentPane().setLayout(layout);
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
jLabel1.setText("jLabel1");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridwidth = 3;
gridBagConstraints.ipadx = 54;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(15, 10, 0, 0);
getContentPane().add(jLabel1, gridBagConstraints);
jTextField1.setText("jTextField1");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 4;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridheight = 3;
gridBagConstraints.ipadx = 83;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(12, 4, 0, 0);
getContentPane().add(jTextField1, gridBagConstraints);
jButton1.setText("jButton1");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 6;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridheight = 5;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(11, 18, 0, 0);
getContentPane().add(jButton1, gridBagConstraints);
jRadioButton1.setText("jRadioButton1");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 6;
gridBagConstraints.gridy = 6;
gridBagConstraints.gridwidth = 5;
gridBagConstraints.gridheight = 3;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(10, 18, 0, 0);
getContentPane().add(jRadioButton1, gridBagConstraints);
jCheckBox1.setText("jCheckBox1");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 6;
gridBagConstraints.gridy = 10;
gridBagConstraints.gridwidth = 3;
gridBagConstraints.gridheight = 5;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(18, 18, 0, 0);
getContentPane().add(jCheckBox1, gridBagConstraints);
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 4;
gridBagConstraints.gridy = 6;
gridBagConstraints.ipadx = 33;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(11, 4, 0, 0);
getContentPane().add(jComboBox1, gridBagConstraints);
jScrollPane1.setViewportView(jTree1);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 18;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new java.awt.Insets(11, 10, 11, 0);
getContentPane().add(jScrollPane1, gridBagConstraints);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane2.setViewportView(jTable1);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 4;
gridBagConstraints.gridy = 18;
gridBagConstraints.gridwidth = 9;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new java.awt.Insets(11, 1, 11, 10);
getContentPane().add(jScrollPane2, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 12;
gridBagConstraints.gridy = 14;
gridBagConstraints.gridheight = 3;
gridBagConstraints.ipadx = 156;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(6, 6, 0, 10);
getContentPane().add(jProgressBar1, gridBagConstraints);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane3.setViewportView(jTextArea1);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 12;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridheight = 13;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.ipadx = 143;
gridBagConstraints.ipady = 73;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 6, 0, 10);
getContentPane().add(jScrollPane3, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 4;
gridBagConstraints.gridy = 10;
gridBagConstraints.ipadx = 60;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(7, 4, 0, 0);
getContentPane().add(jSpinner1, gridBagConstraints);
pack();
}// </editor-fold>
/**
@param args the command line arguments
*/
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Test().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JCheckBox jCheckBox1;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JProgressBar jProgressBar1;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JSpinner jSpinner1;
private javax.swing.JTable jTable1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTree jTree1;
// End of variables declaration
}
muchas gracias por sus comentarios... creo que hacer con layout null no iba a quedar fijo el tamaño.
implemente el borderlayut para asi utlizar setpreferesize.. asi fijar el tamaño.
Gracias
Con este post creo que he tenido un déjà-vu...
¿Y ese déjà-vu? ;)
Hola tengo un problema al implementar el JProgressBar... el codigo que estoy utilizando es:
JProgressBar JPB = new JProgressBar();
Panel3.add(JPB);
JPB.setBounds(22, 60, 507, 25);
Panel3.updateUI();
Panel3.repaint();
Hasta ahi todo bien... pero cuando seteo el maximo... es donde se me modifica el tamaño dado en JPB.setBounds(22, 60, 507, 25);
es decir yo seteo JPB..setMaximum(123508577);
se me modifica el tamaño dado...
hay alguna manera de como fijar el setMaximum sin que el tamaño dado en setBounds se modfique?