0

Recently I created an internal window template that every window realize. I used GridLayout for the components but I don't like the outcome. So, I decided to create a template using generated code from NetBeans (template pattern so far). A plain window with nothing in it.

private void initComponents() {

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 517, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 307, Short.MAX_VALUE)
    );
}

While reading more about GroupLayout I understood that I cannot add components as easily as with GridLayout or FlowLayout. Maybe I have to replace the components everytime, or change visibility etc...Supposing I still want to use GroupLayout, which is the best pattern to add components to that window? Should I "migrate" from template pattern to decorator? If so, how am I supposed to do this? What is your opinion? Thank you in advance.

EDIT: My former code did those things:

public abstract class InternalWindowTemplate{
private int internalWindowCounter = 0;
private final int xOffset = 30;
private final int yOffset = 30;
Container container;
GridLayout gridLayout;
JInternalFrame internalFrame;
GroupLayout internalFrameLayout;

//    overriden methods
public InternalWindowTemplate(String title, boolean isResizable, boolean isClosable, boolean isMaximizable, boolean isIconifiable, int width, int height, int gridRows, int gridColumns){
    internalFrame = new JInternalFrame(title, isResizable, isClosable, isMaximizable, isIconifiable);
    setContainer();
    setGridLayout(gridRows, gridColumns);
    setInternalWindowSize(width, height);
    setInternalWindowLocation(xOffset, yOffset);
}

public void setContainer(){
     container = internalFrame.getContentPane();
}

public Container getContainer(){
    return container;
}

public void setInternalWindowCloseOperation(){
    internalFrame.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
}

public JInternalFrame showInternalFrame(){
    internalFrame.setVisible(true);
    setInternalWindowCloseOperation();
    return internalFrame;
}

public void closeInternalFrame(){
    internalFrame.dispose();
}


public void addComponentToInternalWindow(Component component){
    getContainer().add(component);
    pack();
}

public void pack(){
    internalFrame.pack();
}

public abstract void addComponentsToInternalWindow();

public void setGridLayout(int rows, int columns){
//        I use gridLayout for internal windows
    gridLayout = new GridLayout(rows, columns);
    getContainer().setLayout(gridLayout);
}
}
Vassilis De
  • 363
  • 1
  • 3
  • 21
  • Read [How to Use GroupLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/group.html). There is no hard and fast rule to say it best pattern to add components to the window. It completely depends on the UI of the application. Use any layout that fits as per the design. Please share snapshot of UI. – Braj Jul 05 '14 at 09:35
  • *I used GridLayout for the components but I don't like the outcome.* Can we help you to sort out the issue of the outcome? – Braj Jul 05 '14 at 09:38
  • If I had an internal window with several components that I want to put in order? How would I change those "adds" to GroupLayout everytime? – Vassilis De Jul 05 '14 at 09:42
  • Use `FlowLayout` instead of GroupLayout in that case if you want to add in order from left to right. I suggest to visit at [A Visual Guide to Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) – Braj Jul 05 '14 at 09:43
  • Yes @Braj, I want to have exact order of components to my outcome. GridLayout didn't help me as much as GroupLayout but I cannot understand how to handle adding components. – Vassilis De Jul 05 '14 at 09:47
  • I can't help you until and unless you show us minimal testable code along with expected output. Just follow the links and spend some time. – Braj Jul 05 '14 at 09:48
  • Sorry @Braj I cannot share snapshots, because of my low reputation... – Vassilis De Jul 05 '14 at 09:49
  • post it on any online snapshot sites and share the link. – Braj Jul 05 '14 at 09:51
  • ok I'll try it. For now I edited the code I wrote so far for the internal window template – Vassilis De Jul 05 '14 at 09:55
  • as you see, I can add one component at a time, and I don't know how to make it work with group layout so far (if it does). Unfortunately I cannot recover the former internal window I created, because I changed many things..I can only show you what I am thinking to have now. A play window that every window add its components http://tardis1.tinygrab.com/grabs/577b9224027832e912e649fa20371ecf27547b05cd.jpg – Vassilis De Jul 05 '14 at 10:18
  • You don't need to use a single layout manager. Usually a form is designed with multiple panels each using a different layout manager to achieve the desired layout. GroupLayout is complicated because is forces you to use a single layout. – camickr Jul 05 '14 at 14:17
  • Thank you guys, I'll try it with `flowLayout` instead – Vassilis De Jul 05 '14 at 14:20

0 Answers0