Reputation:
How to resize the height of JWindow
from JButton
actions (i tried setSize and repaint but it does not work) ?
public class Gui extends JWindow implements KeyListener
{
public static Gui mystatic;
private static JPanel panelBgImg;
private final Dimension screen;
public static int w = 0;
public static int h = 0;
public void paintComponent(Graphics g)
{
}
public Dimension getPreferredSize()
{
return new Dimension(100, h);
}
public Gui()
{
....
/* Button 1 */
JButton jb2 = new JButton("")
{
public Dimension getPreferredSize()
{
return new Dimension(50,50);
}
};
jb2.setOpaque(false);
jb2.setContentAreaFilled(false);
jb2.setBorderPainted(false);
jb2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
h = 100;
/* HERE i am trying to resize the JWindow itself but not working
return new Dimension(100, h); */
mystatic.setSize(screen.width, 100);
mystatic.repaint();
}
});
...
}
Upvotes: 2
Views: 784
Reputation: 24626
Here is a small program, where setSize() method is working using Event Dispatcher Thread.
import java.awt.event.*;
import javax.swing.*;
public class WindowTest extends JWindow implements ActionListener
{
private JPanel panel;
private JButton sizeButton;
private int height = 220;
private int width = 220;
public WindowTest()
{
panel = new JPanel();
sizeButton = new JButton("Set Size");
sizeButton.addActionListener(this);
panel.add(sizeButton);
add(panel);
setSize(width, height);
setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent ae)
{
height += 10;
width += 10;
this.setSize(width, height);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new WindowTest().setVisible(true);
}
});
}
}
Hopefully this be of some help, You Just need to Schedule a Job for Event Dispatcher Thread, for changes to be made to the GUI display without hickups, as done in the main method.
Regards
Upvotes: 2
Reputation: 109823
for example
please don't use Thread.sleep(int)
durring EDT, as demonstrated in this code example, there is used for closing current JVM instance, otherwise this instance never gone from PC's RAM
import java.util.logging.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.*;
public class SSCCE {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
final JWindow window = new JWindow();
final JPanel windowContents = new JPanel();
JLabel label = new JLabel("A window that is pushed into view..........");
windowContents.add(label);
window.add(windowContents);
window.pack();
window.setLocationRelativeTo(null);
final int desiredWidth = window.getWidth();
window.getContentPane().setLayout(null);
window.setSize(0, window.getHeight());
window.setVisible(true);
Timer timer = new Timer(15, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int newWidth = Math.min(window.getWidth() + 1, desiredWidth);
window.setSize(newWidth, window.getHeight());
windowContents.setLocation(newWidth - desiredWidth, 0);
if (newWidth >= desiredWidth) {
((Timer) e.getSource()).stop();
//restore original layout
window.getContentPane().setLayout(new BorderLayout());
window.validate();
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(SSCCE.class.getName()).log(Level.SEVERE, null, ex);
}
System.exit(0);
}
}
});
timer.start();
}
private SSCCE() {
}
}
Upvotes: 3