What Does .Pack() Do

How does pack() work and what does it looks for

pack makes use of the layout manager API and "asks" the frame's contentPane for it's preferredSize, this allows pack to "pack" the windows decorations around the content. You have to remember, a windows size includes the windows decorations, so a window of 800x600 will have a viewable which is noticeably smaller

If you add a component to the frame whose size is undefined (in the case of JComponent and JPanel which return a preferredSize of 0x0, the frame will be packed to its smallest possible size, not really what you want.

So, for example, something like...

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

public TestPane() {
}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.fillOval(10, 10, 30, 30);
g2d.dispose();
}

}

}

generates a window of 200x222 on my Mac, but the TestPane will be 200x200

You might also like to have a look at Laying Out Components Within a Container, Painting in AWT and Swing and Performing Custom Painting for some more details

What does pack(B*, $s) do In Perl?

pack:

B A bit string (descending bit order inside each byte)

It takes a bit string, and produces the corresponding bytes.

For example, pack "B*", "0100000101000010" is equivalent to "\x41\x42" and chr(65).chr(66).

$ perl -Mv5.10 -e'say sprintf "%vX", pack "B*", "0100000101000010"'
41.42
```

pack() method of JFrame doesn't work (java,ubuntu)

From the order of your code:

JFrame frame = new JFrame("test");
JPanel panel = new JPanel();
JLabel label1= new JLabel("");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();

You did not add anything into the frame before you pack() it. pack() means let the frame decide its size based on the components being added to it.

Since you have no components added to it before you pack() it, you receive a small window with visually nothing inside (until you resize the window).

When the frame is being resized, paintManager will be consulted to paint the contentPane, hence if you add before pack(), not only the frame will be resized nicely for you, the components within it will be painted as well.


To see the components within the JFrame:

public static void main (String [] args){
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel label1= new JLabel("");
panel.add(label1); //Add label to panel
frame.add(panel); //Add panel (with label) to frame
frame.pack(); //Let the frame adjust its size based on the added components
frame.setVisible(true);
}

How do I use JFrame.pack()?

The behaviour of the method is specified in the documentation (the javadoc).

Here's the documentation for Window.pack.

Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. The resulting width and height of the window are automatically enlarged if either of dimensions is less than the minimum size as specified by the previous call to the setMinimumSize method.

Can someone explain to me the pack() function in PHP?

Those represent how you want the data you are packing to be represented in binary format:

so

$bin = pack("v", 1); => 0000000000000001 (16bit)

where

$bin = pack("V", 1) => 00000000000000000000000000000001 (32 bit)

It tells pack how you want the data represented in the binary data.
The code below will demonstrate this. Note that you can unpack with a different
format from what you packed the data as.

<?php

$bin = pack("S", 65535);
$ray = unpack("S", $bin);
echo "UNSIGNED SHORT VAL = ", $ray[1], "\n";

$bin = pack("S", 65536);
$ray = unpack("S", $bin);
echo "OVERFLOW USHORT VAL = ", $ray[1], "\n";

$bin = pack("V", 65536);
$ray = unpack("V", $bin);
echo "SAME AS ABOVE BUT WITH ULONG VAL = ", $ray[1], "\n";
?>

Is it a bad idea to call pack() in JFrame.invalidate?

Your issue is not that revalidate() or repaint() aren't working.

The issue here is that your JFrame has been pack()ed already and thus it has a preferred size set. If you want to change its size you need to call pack() on it again. Not necessarily to call it on invalidate().

I made some changes to your code to compile (typos) and I came with this:

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

public class TestFrame {
JFrame jframe;
NewPanel jpanel;

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new TestFrame());
}

public TestFrame(){
jframe = new JFrame(); // without above addition frame won't resize
jpanel = new NewPanel();
jframe.add(jpanel);
jframe.pack();
jframe.setVisible(true);
}

class NewPanel extends JPanel implements ActionListener{
public NewPanel(){
JTextField textField = new JTextField (10);
textField.addActionListener(this);
this.add(textField);
}

// Adds a label when action is performed on textfield
@Override
public void actionPerformed(ActionEvent ae){
System.out.println("WOLOLO");
JPanel extraPanel = new JPanel();
extraPanel.add(new JLabel("hi"));
this.add(extraPanel);
this.revalidate();
this.repaint();
jframe.pack();
}
}
}

Another way to solve this is to override getPreferredSize method from the JPanel:

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

And you can delete jframe.pack() in the previous code.



Related Topics



Leave a reply



Submit