BoxLayout won't respect JScrollPane/JTextPane sizes?
[nobr]Hi all. I'm in Swing Layout Hell. I'm trying to set up a JPanel that looks and acts like this:
+-++
| label1: | |
| | |
+-++
| label2: | |
| | |
+-++
| label3: |this column expands -->|
+-++
| label4: | |
+-++
| label5: | |
| | |
| | |
| | |
| |this row expands -+ |
| |V |
+-++
All the cells have borders around them.
The idea is that when you resize the panel, only the last row and the last column expands. Label1 and Label2 are 2-line multiline HTML labels. Labels 3, 4, and 5 are one-line labels. The column after all the labels are all JScrollPane/JTextPanes.
So first I created a JPanel containing all the labels, using a BoxLayout/Y-Axis. I put that panel in the main JPanel (which uses a BorderLayout), on the WEST side. That scrunches up the vertical box, and allows the vertical box to resize properly.
I wanted to do the same thing with the text pane column -- put them all in a JPanel using a BoxLayout/Y-Axis, and put it in the main JPanel in the CENTER, which I thought would just expand the box to take up the rest of the space.
However, it seems that no matter what I do, all of the JScrollPane/JTextPanes end up taking up an equal amount of space. I even tried setting the top scroll pane's maximum height to be equal to the top label's maximum height, but for whatever reason one or the other layout just ignored that.
Any ideas here?
Thanks!
--Rob
NOTE: In this code, jPanel1 is the label panel, and jPanel2 is the scrollpane panel.jScrollPane2 is the top scrollpane, and is supposed to take up only enough vertical space to equal the first label, while jScrollPane1 should take up the rest of the space. What happens in practice is that both scrollpanes take up an equal amount of vertical space.
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
publicclass PersonDataCardextends javax.swing.JPanel
{
/** Creates new form PersonDataCard */
public PersonDataCard()
{
initComponents();
}
/** 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.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
privatevoid initComponents()
{
jPanel1 =new javax.swing.JPanel();
jLabel1 =new javax.swing.JLabel();
jLabel2 =new javax.swing.JLabel();
jLabel3 =new javax.swing.JLabel();
jLabel4 =new javax.swing.JLabel();
jLabel5 =new javax.swing.JLabel();
jPanel2 =new javax.swing.JPanel();
jScrollPane2 =new javax.swing.JScrollPane();
jTextPane2 =new javax.swing.JTextPane();
jScrollPane1 =new javax.swing.JScrollPane();
jTextPane1 =new javax.swing.JTextPane();
setLayout(new java.awt.BorderLayout());
setName("rowLabels");
jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.Y_AXIS));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
jLabel1.setText("<html>燘irth:?lt;br>?lt;/html>");
jLabel1.setVerticalAlignment(javax.swing.SwingConstants.TOP);
jLabel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
jLabel1.setVerticalTextPosition(javax.swing.SwingConstants.TOP);
jPanel1.add(jLabel1);
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
jLabel2.setText("<html>燚eath:?lt;br>?lt;/html>");
jLabel2.setVerticalAlignment(javax.swing.SwingConstants.TOP);
jLabel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
jLabel2.setVerticalTextPosition(javax.swing.SwingConstants.TOP);
jPanel1.add(jLabel2);
jLabel2.getAccessibleContext().setAccessibleName("<html>Death:?lt;br>?lt;/html>");
jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
jLabel3.setText("<html>?lt;/html>");
jLabel3.setVerticalAlignment(javax.swing.SwingConstants.TOP);
jLabel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jLabel3.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
jLabel3.setVerticalTextPosition(javax.swing.SwingConstants.TOP);
jPanel1.add(jLabel3);
jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
jLabel4.setText("<html>?lt;/html>");
jLabel4.setVerticalAlignment(javax.swing.SwingConstants.TOP);
jLabel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jLabel4.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
jLabel4.setVerticalTextPosition(javax.swing.SwingConstants.TOP);
jPanel1.add(jLabel4);
jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
jLabel5.setText("<html>燦otes:?lt;/html>");
jLabel5.setVerticalAlignment(javax.swing.SwingConstants.TOP);
jLabel5.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jLabel5.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
jLabel5.setMaximumSize(new java.awt.Dimension(2147483647, 2147483647));
jLabel5.setVerticalTextPosition(javax.swing.SwingConstants.TOP);
jPanel1.add(jLabel5);
add(jPanel1, java.awt.BorderLayout.WEST);
jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.Y_AXIS));
jScrollPane2.setBackground(new java.awt.Color(102, 51, 255));
jScrollPane2.setMaximumSize(new Dimension(Short.MAX_VALUE, jLabel1.getPreferredSize().height));
jScrollPane2.setPreferredSize(new java.awt.Dimension(51, 34));
jTextPane2.setBackground(new java.awt.Color(192, 192, 192));
jScrollPane2.setViewportView(jTextPane2);
jPanel2.add(jScrollPane2);
jScrollPane1.setPreferredSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
jScrollPane1.setViewport(null);
jTextPane1.setEnabled(false);
jScrollPane1.setViewportView(jTextPane1);
jPanel2.add(jScrollPane1);
add(jPanel2, java.awt.BorderLayout.CENTER);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextPane jTextPane1;
private javax.swing.JTextPane jTextPane2;
// End of variables declaration
}
[/nobr]

