Two JPanels inside another panel should be equal in width
Hi everybody,
I have two JPanels which both have a titledborder. I want them to have the same with, but I can not get it done. You can see how it looks here: http://jborsje.nl/jpanels.png. As you can see the JPanels are not equal in width. Here is the code I used (please note that p_hopsControl = true).
/**
* Initialize the sidebar of the graph panel.
* @param p_hopsControl Indicates whether or not a widget for controlling the
* hops in the graph should be added to the panel.
*/
private JPanel getSideBar(boolean p_hopsControl)
{
// Create the panel.
JPanel panel =new JPanel(new GridBagLayout());
GridBagConstraints constraints =new GridBagConstraints();
constraints.anchor = GridBagConstraints.FIRST_LINE_START;
// Add the label and the spinner to the panel.
constraints.gridx = 0;
if (!p_hopsControl) constraints.weighty = 1;
constraints.gridy = 0;
panel.add(getLegend(), constraints);
if (p_hopsControl)
{
constraints.gridy = 1;
constraints.weighty = 1;
constraints.weightx = 1;
panel.add(getHopsWidgets(), constraints);
}
// Set the background of the panel.
panel.setBackground(m_display.getBackground());
return panel;
}
private JEditorPane getLegend()
{
String content ="<html><body>" +
"<table>" +
"<tr><td bgcolor=\"" + Integer.toHexString(Constants.NODE_COLOR_CLASS & 0x00ffffff) +"\" width=\"20px\"></td><td>OWL class</td></tr>" +
"<tr><td bgcolor=\"" + Integer.toHexString(Constants.NODE_COLOR_INDIVIDUAL & 0x00ffffff) +"\"></td><td>OWL individual</td></tr>" +
"<tr><td bgcolor=\"" + Integer.toHexString(Constants.NODE_COLOR_SELECTED & 0x00ffffff) +"\"></td><td>Node selected</td></tr>" +
"<tr><td bgcolor=\"" + Integer.toHexString(Constants.NODE_COLOR_HIGHLIGHTED & 0x00ffffff) +"\"></td><td>Node highlighted</td></tr>" +
"<tr><td bgcolor=\"" + Integer.toHexString(Constants.NODE_COLOR_SEARCH & 0x00ffffff) +"\"></td><td>Node in search result set</td></tr>" +
"<tr><td bgcolor=\"" + Integer.toHexString(Constants.NODE_DEFAULT_COLOR & 0x00ffffff) +"\"></td><td>Node in search result set</td></tr>" +
"</body></html>";
JEditorPane legend =new JEditorPane("text/html", content);
legend.setEditable(false);
legend.setBorder(new TitledBorder("Legend"));
JPanel panel =new JPanel();
panel.setBorder(new TitledBorder("Legend"));
panel.add(legend);
return legend;
}
/**
* Create a panel containing a JSpinner which can be used to set the number
* of hops, used in the graph distance filter.
* @return A JPanel containing the hops widgets.
*/
private JPanel getHopsWidgets()
{
// Get the GraphDistanceFilter.
GraphDistanceFilter filter = m_display.getDistanceFilter();
// Create the panel.
JPanel panel =new JPanel(new GridBagLayout());
GridBagConstraints constraints =new GridBagConstraints();
constraints.anchor = GridBagConstraints.FIRST_LINE_START;
// Create the label.
JLabel label =new JLabel("Number of hops: ");
// Create the spinner and its model.
SpinnerNumberModel model =new SpinnerNumberModel(filter.getDistance(), 0, null, 1);
m_spinner =new JSpinner();
m_spinner.addChangeListener(this);
m_spinner.setModel(model);
// Add the label and the spinner to the panel.
constraints.gridx = 0;
constraints.gridy = 0;
panel.add(label, constraints);
constraints.gridx = 1;
constraints.weighty = 1;
panel.add(m_spinner, constraints);
// Set the background of the panel.
panel.setBackground(m_display.getBackground());
// Add a titled border to the panel.
panel.setBorder(new TitledBorder("Hops control"));
return panel;
}
Does anybody know how this can be done?

