Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with gridbaglayout  RSS feed

 
swarna latha
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have taken 2 JScrollpanes for that i have added 2 jlist.I have written some code regarding that.Please check in down side.in that left i have added one scrollpane,middle 2 buttons rigtht another scrollpane.
if the length of the text is more the scroll width is increased .But for me both the lists size should be same .it wont be the matter with the text length.How can i solve that .Please any of you people help me on this.

This is the code i have written.
private JPanel getSecondPanel()
{
JPanel listPanel = new JPanel(new GridBagLayout());
listPanel.setBorder(BorderFactory.createTitledBorder (null, "Select the groups for the user"));
listPanel.add(getJScrollPane1(), new GridBagConstraints(0, 0, 1, 2, 0.5, 1.0,
GridBagConstraints.SOUTHWEST, GridBagConstraints.BOTH,
new Insets(0, 10, 10, 5), 0, 0));
listPanel.add(getJButton2(), new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.PAGE_START, GridBagConstraints.HORIZONTAL,
new Insets(60, 0, 0, 0), 0, 0));
listPanel.add(getJButton3(), new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.PAGE_END, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 60, 0), 0, 0));
listPanel.add(getJScrollPane(), new GridBagConstraints(2, 0, 1, 2, 0.5, 1.0,
GridBagConstraints.SOUTHEAST, GridBagConstraints.BOTH,
new Insets(0, 10, 10, 5), 0, 0));
return listPanel;
}

Thanks and regards
swarna
 
satya sahu
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this code. May be this will solver your problem.
If this is not ok, then you can try setting
minimumsize and preferred size of the scrollPane.


public class JScrollPaneList extends javax.swing.JFrame {

/** Creates new form JScrollPaneList */
public JScrollPaneList() {
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 ">
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;

listOneJPanel = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList();
buttonPanel = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
listTwoJPanel = new javax.swing.JPanel();
jScrollPane2 = new javax.swing.JScrollPane();
jList2 = new javax.swing.JList();

getContentPane().setLayout(new java.awt.GridBagLayout());

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jList1.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 3Item 3Item 3Item 3Item 3Item 3" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jScrollPane1.setViewportView(jList1);

org.jdesktop.layout.GroupLayout listOneJPanelLayout = new org.jdesktop.layout.GroupLayout(listOneJPanel);
listOneJPanel.setLayout(listOneJPanelLayout);
listOneJPanelLayout.setHorizontalGroup(
listOneJPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 100, Short.MAX_VALUE)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
);
listOneJPanelLayout.setVerticalGroup(
listOneJPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 100, Short.MAX_VALUE)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
getContentPane().add(listOneJPanel, gridBagConstraints);

buttonPanel.setLayout(new java.awt.GridBagLayout());

jButton1.setText("jButton1");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
buttonPanel.add(jButton1, gridBagConstraints);

jButton2.setText("jButton2");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
buttonPanel.add(jButton2, gridBagConstraints);

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
getContentPane().add(buttonPanel, gridBagConstraints);

jList2.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jScrollPane2.setViewportView(jList2);

org.jdesktop.layout.GroupLayout listTwoJPanelLayout = new org.jdesktop.layout.GroupLayout(listTwoJPanel);
listTwoJPanel.setLayout(listTwoJPanelLayout);
listTwoJPanelLayout.setHorizontalGroup(
listTwoJPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 100, Short.MAX_VALUE)
.add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
);
listTwoJPanelLayout.setVerticalGroup(
listTwoJPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 100, Short.MAX_VALUE)
.add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
getContentPane().add(listTwoJPanel, gridBagConstraints);

pack();
}// </editor-fold>

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JScrollPaneList().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JPanel buttonPanel;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JList jList1;
private javax.swing.JList jList2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JPanel listOneJPanel;
private javax.swing.JPanel listTwoJPanel;
// End of variables declaration

}
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!