S
Scott Steiner
Hi,
the following simple problem: I have a JFrame with a borderlayout. In
that JFrame I have a main JPanel in the CENTER and a footer JPanel in
the SOUTH. There is a JButton on the footer panel which when clicked
should simply resize the footer panel.
My problem is that the footer panel is not resizing unless I enforce a
repaint by say enlarging the JFrame with the mouse i.e make the window
larger, or smaller, or anything which will enforce a repaint. A
programatic repaint i.e this.repaint() is not producing the desired
results.
Here's the short sample code if anyone is interested, any help
appreciated.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JFrame implements ActionListener
{
private JPanel panelMain, panelFooter;
private JButton btn;
public Test()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300,300);
Container c = this.getContentPane();
c.setLayout(new BorderLayout());
panelMain = new JPanel();
panelMain.setBackground(Color.CYAN);
c.add(panelMain, BorderLayout.CENTER);
panelFooter = new JPanel();
panelFooter.setPreferredSize(new Dimension(0,100));
panelFooter.setBackground(Color.GREEN);
c.add(panelFooter, BorderLayout.SOUTH);
JButton btn = new JButton("Resize");
btn.setActionCommand("resize");
btn.addActionListener(this);
panelFooter.add(btn);
this.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
String cmd = ae.getActionCommand();
if (cmd.equals("resize"))
{
panelFooter.setPreferredSize(new Dimension(0,50));
this.repaint();
}
}
public static void main(String[] args)
{
new Test();
}
}
the following simple problem: I have a JFrame with a borderlayout. In
that JFrame I have a main JPanel in the CENTER and a footer JPanel in
the SOUTH. There is a JButton on the footer panel which when clicked
should simply resize the footer panel.
My problem is that the footer panel is not resizing unless I enforce a
repaint by say enlarging the JFrame with the mouse i.e make the window
larger, or smaller, or anything which will enforce a repaint. A
programatic repaint i.e this.repaint() is not producing the desired
results.
Here's the short sample code if anyone is interested, any help
appreciated.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JFrame implements ActionListener
{
private JPanel panelMain, panelFooter;
private JButton btn;
public Test()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300,300);
Container c = this.getContentPane();
c.setLayout(new BorderLayout());
panelMain = new JPanel();
panelMain.setBackground(Color.CYAN);
c.add(panelMain, BorderLayout.CENTER);
panelFooter = new JPanel();
panelFooter.setPreferredSize(new Dimension(0,100));
panelFooter.setBackground(Color.GREEN);
c.add(panelFooter, BorderLayout.SOUTH);
JButton btn = new JButton("Resize");
btn.setActionCommand("resize");
btn.addActionListener(this);
panelFooter.add(btn);
this.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
String cmd = ae.getActionCommand();
if (cmd.equals("resize"))
{
panelFooter.setPreferredSize(new Dimension(0,50));
this.repaint();
}
}
public static void main(String[] args)
{
new Test();
}
}