Who should layout components ?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Dmitry_Zakharov
Posted On:   Tuesday, November 26, 2002 12:59 AM

I have panel (for example, JPanel) with FlowLayout. I add several buttons on it. The panel has sufficient width and so all buttons are placed in one row. When I recieve some event (some button was pressed), I resize the panel (make it too narrow). I thought that the panel relayouts its children and buttons will be places in several rows. But I was wrong. There is my example below. How should I fix this example ? The panel might be in another container and container can resize the panel by itself. How does AWT suppose to solve such situations ? Thanks in advance Here is my example: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Test { public stat   More>>

I have panel (for example, JPanel) with FlowLayout. I add several buttons on it. The panel has sufficient width and so all buttons are placed in one row.


When I recieve some event (some button was pressed), I resize the panel (make it too narrow). I thought that the panel relayouts its children and buttons will be places in several rows. But I was wrong.


There is my example below. How should I fix this example ? The panel might be in another container and container can resize the panel by itself. How does AWT suppose to solve such situations ?


Thanks in advance


Here is my example:

			
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test {
public static void main(String[]args) {
JFrame frame = new JFrame("test");
Container pane = frame.getContentPane();
pane.setLayout(null);

JButton b = new JButton("test");
pane.add(b);
b.setBounds(0,0,35,15);

final JPanel panel = new JPanel(new FlowLayout());
for (int i=0;i <10;i++) panel.add(new JButton(""+i));
pane.add(panel);
panel.setBounds(0,30,300,30);

b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.setBounds(0,30,50,200);
}
});

frame.setSize(400,300);
frame.show();
}
}
   <<Less

Re: Who should layout components ?

Posted By:   Christopher_Schultz  
Posted On:   Tuesday, November 26, 2002 07:09 AM

After calling setBounds, try calling invalidate on the component, too. This usually triggers the LayoutManager to re-layout the component.



-chris
About | Sitemap | Contact