Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Issue with BoxLayout using Panels

 
Sudha Lakshman
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
I am using BoxLayout. That seems to be fine as long as the components are not panel.
Here is what i am trying out. I have a panel (i have written it as a class called CustomPanel)
public class CustomPanel extends JPanel
{
private JLabel customLabel;
private JTextField customField;
private BoxLayout boxLayout;
public CustomPanel(String pLabelText,int pNumberOfChars)
{
customLabel = new JLabel(pLabelText);
customField = new JTextField(pNumberOfChars);
customField.setMaximumSize (customField.getPreferredSize());
boxLayout = new BoxLayout(this,BoxLayout.X_AXIS);
setLayout(boxLayout);
add(customLabel);
add(customField);
}
public void setPanelTitle(String pPanelTitle)
{
setBorder(BorderFactory.createTitledBorder(pPanelTitle));
}
}
now i use objects of this class on another panel which also is set to BoxLayout, as follows
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.util.*;
public class CustomTradingPanel extends JPanel
{
private CustomPanel customPanel;

public CustomTradingPanel()
{
setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
}
public void addComponents(String pLabelText,int pNumberOfChars)
{
CustomPanel localCustomPanel = new CustomPanel(pLabelText,pNumberOfChars);
customPanel = localCustomPanel;
customPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
add(customPanel);
add(Box.createRigidArea(new Dimension(0,5)));
}
public void setComponents(String pPanelTitle)
{
setBorder(BorderFactory.createTitledBorder(pPanelTitle));
}
}
now the problem i face is as follows :
I want to align the all the CustomPanel objects on the CustomTradingPanel such that all the customField objects are left aligned (on the same axis). How do i do that? or is there any other layout that would help me achieve this.
I do not want to use grid layout since the sizes of each component changes.
thanks in advance.
regards
Sudha
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if I do understand your problem then it's:
you are adding one panel with components aligned then you are adding the next panel. within this next one the components are aligned, but the components from the first are not aligned to the components from the next one?
you could try to work with EmptyBorder to set equal margins inside each of the panels. I don't think setting insets works outside GridBagLayout.
My preferred Layout for complex things is GridBagLayout. though, if you're not used to it you will take some time to grok it. I think, it helps creating dynamic views.
 
Sudha Lakshman
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Chantal Ackermann,
Thank you for your response.
You understood the problem( i was worried no one could...the way i had written).
But i kind of found out the solution even without using GridBagLayout.
All i did was set the 2nd component(the one to be aligned with its counterparts in the rest of the panels) to start from a fixed point(not using null layout).
All i did was use BoxLayout. I calculated the width of the rigid area between the two components (in my case the Label and the textfield) based on the width of the first component such that the textfield would start from the fixed point. I did not used gridbaglayout either.
Thanks for the suggestion.
regards
sudha
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!