Packed JFrame Too Tall? - java

I am trying to create a simple Java Swing GUI that consists of a panel on top and tabs in the center. I want the top panel to remain its preferred size and the tabs to take up the remaining space so I used a BorderLayout. The content of the tab can be tall so I put the tab component into a scroll pane.
Everything seems to work the way I expect (with respect to component sizing and scroll bar behavior when I resize the frame) except that my packed frame is 12 pixels too tall (and possibly 16 pixels too wide). Would someone please explain what is going on and how to resolve it. Somehow when the pack is sizing all of the components, something is smart enough to (mostly) respect the screen size. I am using Java 8 on Windows 7 with a screen resolution of 1920 x 1200.
Below is my test code and the output it produces.
Code:
package test;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
public final class SizeTest
{
public static void main(final String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
createAndShowGui();
}
});
}
private static void createAndShowGui()
{
final JPanel topPanel = new JPanel();
topPanel.setBorder(BorderFactory.createTitledBorder("Top"));
topPanel.setPreferredSize(new Dimension(800, 150));
final JPanel centerPanel = new JPanel();
centerPanel.setBorder(BorderFactory.createTitledBorder("Center"));
centerPanel.setPreferredSize(new Dimension(800, 1300));
final JScrollPane scrollPane = new JScrollPane(centerPanel);
final JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Tab", scrollPane);
final JPanel mainPanel = new JPanel(new BorderLayout(0, 10));
mainPanel.add(topPanel, BorderLayout.NORTH);
mainPanel.add(tabbedPane, BorderLayout.CENTER);
final JFrame mainFrame = new JFrame("Size Test");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.add(mainPanel);
mainFrame.pack();
System.err.println("***** Frame Size: " + mainFrame.getSize() + ", Screen Size: "
+ Toolkit.getDefaultToolkit().getScreenSize() + ", Maximum Window Bounds: "
+ GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds());
mainFrame.setVisible(true);
}
}
Output:
***** Frame Size: java.awt.Dimension[width=816,height=1212], Screen Size: java.awt.Dimension[width=1920,height=1200], Maximum Window Bounds: java.awt.Rectangle[x=0,y=0,width=1920,height=1156]

If you're going to stuff around with setPreferredSize, be prepared for things to go astray.
The first thing I would do, is seriously reconsider using setPreferredSize.
Because of the way the API works, JScrollPane will use the preferredSize of the component to make determinations about it's own size. You can change this by implementing the Scrollable interface, which allows you to return the preferredScrollableViewportSize, which JScrollPane will use instead when determing how large it needs to be
You see Scrollable demonstrated here and here and lots of other places if you do some searching

Related

How to draw two JPanels on top of each other?

I am currently working on a N-Body simulation and I have made particles move on a black screen. My current problem is that there is no way of controlling it.
My plan:
Each color stands for a different JPanel. The blue one should contain the buttons and text fields, the red one the viewport.
But with my small knowledge in Java, I didn't succeed in creating this. I first tried with setBounds and setLayoutManager(null), in vain.
My structure goes like that:
Window class extends JFrame
Simulation class creating blueJPanel class (extends JPanel) and redJPanel,
adds them to the window.
But this is garbage code... So how would you draw these simple panels on top of each other?
Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section. Pay particular attention to the Laying Out Components Within a Container section.
As I said in my comment, you create two JPanels. Here's an example.
Here's the complete runable code to create this example.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class BorderLayoutExampleGUI implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new BorderLayoutExampleGUI());
}
#Override
public void run() {
JFrame frame = new JFrame("BorderLayout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createButtonPanel(), BorderLayout.NORTH);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createButtonPanel() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
panel.setPreferredSize(new Dimension(600, 100));
panel.setBackground(Color.blue);
// Add the buttons and text fields
return panel;
}
private JPanel createMainPanel() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
panel.setPreferredSize(new Dimension(600, 380));
panel.setBackground(Color.red);
// Add the drawing code
return panel;
}
}

setPreferredSize() dosent work in my JButton

Im creating a Tic Tac Toe game out of Java and i seem to be stucj in one problem i cant get out of :( . I cant rezize my button. I tried both tried both setSize and setPreferredSize but dosent seem to work:
Here is the setPreferedSize pic:
And Code:
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import sun.org.mozilla.javascript.internal.xml.XMLLib.Factory;
public class TicTacToe {
JFrame frame;
JPanel contentPane;
JButton row1col1;
JButton row1col2;
JButton row1col3;
JButton row2col1;
JButton row2col2;
JButton row2col3;
JButton row3col1;
JButton row3col2;
JButton row3col3;
public TicTacToe() {
// TODO Auto-generated constructor stub
frame = new JFrame("Fds");
contentPane = new JPanel();
contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.Y_AXIS));
row1col1 = new JButton();
row1col1.setPreferredSize(new Dimension(600,600));
contentPane.add(row1col1);
row1col2 = new JButton();
row1col2.setPreferredSize(new Dimension(600,600));
contentPane.add(row1col2);
frame.setContentPane(contentPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
private static void runGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
TicTacToe greeting = new TicTacToe();
}
public static void main(String[] args) {
/* Methods that create and show a GUI should be
run from an event-dispatching thread */
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
runGUI();
}
});
}
}
Thank you so much for your help!
The BoxLayout respects the maximum size of the component.
In your case the maximum size is less than the preferred size.
However, the solution is NOT to play with the preferred/maximum sizes.
Instead you can use:
button.setMargin( new Insets(10, 10, 10, 10) );
to control the size of your button, then normal layout management can be done as the preferred size will be calculated correctly.
As Camickr states, BoxLayout respects maximal size, but having said that, why use BoxLayout? Instead I suggest:
If you want to create a grid of JButtons, use GridLayout as it excels at creating grids.
It somewhat respects preferred sizes as long as you pack your GUI, and all the components are the same size,
But having said that, don't set the size. For tic tac toe, set the JLabel's font to something large, or use JLabels with ImageIcons that are large, so that your GUI sets its own size correctly.
You should be using an array or 2D array of JButtons for ease of coding.
For example please check out my code here: Java: Drawing using Graphics outside the class which draws the main canvas which uses programmer created ImageIcons and creates this GUI:
Or my code in this answer: How to wait for a MouseListener mouse press? which uses Font sizing to create this GUI:

How can I keep the vlcj player from taking up all the room?

I am writing a small Swing program which involves embedding a video player in the interface. In order to achieve this, I am using vlcj.
While the GUI itself has some more components, here is an analog code structure:
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import javax.swing.*;
import java.awt.*;
public class MyTestClass extends JFrame {
public MyTestClass(){
EmbeddedMediaPlayerComponent playerCmpt =
new EmbeddedMediaPlayerComponent();
playerCmpt.setPreferredSize(new Dimension(200, 100));
JPanel leftPane = new JPanel();
leftPane.setPreferredSize(new Dimension(100, 100));
JSplitPane mainSplit = new JSplitPane(
JSplitPane.HORIZONTAL_SPLIT,
leftPane, playerCmpt);
this.add(mainSplit);
this.pack();
this.setVisible(true);
}
public static void main(String[] args){
new MyTestClass();
}
}
(I tried this code separately, and I'm facing the same problem as in my GUI)
Basically, the window has two parts: a left panel in which I display some data, and a right panel in which the video is to be embedded. The panels are put together in a JSplitPane is order to allow the user to allocate the amount of room he desires for the player (and therefore, for the video itself). At first, the components get 100 and 200 pixels in width, respectively.
The problem is: EmbeddedMediaPlayerComponent is getting a little too confortable. While I have set it to a preferred size of 200x100, it refuses to shrink down once the vertical split has been moved. That is, I can't bring the player below 200 pixels in width, and once I've enlarge it, I can't bring it back to 200 pixels... Setting a maximum size doesn't change anything. This little problem is annoying because it forces my left panel to shrink again and again, until it becomes practically invisible.
Is there any way I could have the media player follow the constraints set by JSplitPane as the user tries to resize the components? If it's any use, the left pane contains a JTree in my application, which also gets crushed by the player.
This one works for me. Just improve the code to fit for your purpose.
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import java.awt.*;
import javax.swing.*;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
public class MyTestClass extends JFrame {
public MyTestClass() {
String vlcPath = "C:\\Program Files (x86)\\VideoLAN\\VLC";
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), vlcPath);
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
EmbeddedMediaPlayerComponent playerCmpt = new EmbeddedMediaPlayerComponent();
playerCmpt.setPreferredSize(new Dimension(200, 100));
JPanel leftPane = new JPanel();
leftPane.setPreferredSize(new Dimension(100, 100));
JPanel playerPanel = new JPanel(new BorderLayout());
playerPanel.add(playerCmpt);
JSplitPane mainSplit = new JSplitPane(
JSplitPane.HORIZONTAL_SPLIT,
leftPane, playerPanel);
playerPanel.setMinimumSize(new Dimension(10, 10));
this.add(mainSplit);
this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MyTestClass();
}
}

Wrapping JPanel around JCheckBox without any space - Swing

I want to display a vertical list of JCheckBox-es in a JPanel. But I have decided to wrap each JCheckBox with a JPanel, (thinking that i would need it in future). and i am going to stack these JPanels(containing one JCheckBox each) vertically in another JPanel using BoxLayout.
The BoxLayout is working as expected without any vertical space in between the JPanels added to them. My problem is with these inner JPanels containing individual checkbox. there is a considerable amount of space around the checkbox for each panel. I want to wrap these checkboxes tightly, so that the size of jcheckbox and that of the corresponding jpanel containing them are equal.
for these individual jpanel i have tried layouts - flowlayout and default layout. both didnt help.
How can i fix this? i just simply set the flowlayout and add the jcheckbox directly to the jpanel. no other code i have used.
I am adding an example code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class TryBox implements Runnable{
/**
* #param args
*/
public static void main(String args[])
{
SwingUtilities.invokeLater(new TryBox());
}
public void run()
{
class InnerPanel extends JPanel
{
public InnerPanel(int i) {
// TODO Auto-generated constructor stub
setLayout(new FlowLayout());
setBackground(new Color(i*50, i*50, i*50));
add(new JCheckBox("CheckBox "+i));
}
}
JPanel outerPanel = new JPanel();
outerPanel.setLayout(new BoxLayout(outerPanel, BoxLayout.Y_AXIS));
for(int i=0;i<5;i++)
{
outerPanel.add(new InnerPanel(i));
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane sp = new JScrollPane(outerPanel);
frame.setPreferredSize(new Dimension(200, 600));
frame.getContentPane().add(new JScrollPane(outerPanel));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
here frame.setPreferredSize(new Dimension(200, 600)); is making the difference. i want the jpanels to reduce to the size of jcheckboxes. i dont want the jcheckboxes to fit into jpanel's size. if any extra vertical space is there in the container i want it to be left free.
The default layout of JPanel is FlowLayout, and the default constructor creates "a default 5-unit horizontal and vertical gap." You can specify no gap:
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
In addition, the JCheckBox UI delegate adds Insets that vary by L&F. I would be wary of changing these: the size is negligible and typically required to accommodate selection indication.
Did you try setting the margin and border insets to 0 (on the Swing components)?

MigLayout row-height and changing font-size

I have a problem using MigLayout in combination with dynamically changing the font-size of the components which are shown in the MigLayout cells.
In detail: I added a JCheckBox via MigLayout to a JPanel. The font-size of the JCheckBox is default (12pt?) and the row which contains the JCheckBox has a preferred height of 17lp. That all works fine.
(View here: http://www.bilderload.com/bild/227327/migproblemcellheight1UQXP2.png)
Now I change the font-size to e.g. 20pt and start the program again. Now the text of the JCheckBox is cut because the row has also the height of 17lp.
(View here: http://www.bilderload.com/bild/227328/migproblemcellheight2DDPGJ.png)
If I for example let the row definition empty ("[]") the text shows correctly with both font sizes - the normal and the large one. But in this case the row will sadly never reach a minimum of 17lp. (It will always have a minimum of 23lp or so)
How can I change the MigLayout definition to get a minimum row-height of 17lp and to let the cell grow correctly with the components font-size/text etc.?
Or maybe this is a L&F problem?
Thanks & best regards,
Philipp
Here is my sample code (working example):
import java.awt.Font;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
public class TestMigLayoutFontSize extends JFrame {
public TestMigLayoutFontSize() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600, 400);
setContentPane(getTestPanel());
setVisible(true);
}
private JPanel getTestPanel() {
JCheckBox testBox = new JCheckBox("Program argument");
Font normalFont = testBox.getFont();
Font largeFont = new Font(testBox.getFont().getName(), testBox.getFont().getStyle(), 20);
// testBox.setFont(normalFont);
testBox.setFont(largeFont);
JPanel tempPanel = new JPanel(new MigLayout("debug", "0lp![grow,fill]0lp!", "[17lp:17lp:n]"));
tempPanel.add(testBox);
JPanel testPanel = new JPanel(new MigLayout("", "[grow,fill]", "[grow,fill]"));
testPanel.add(tempPanel);
return testPanel;
}
public static void main(String[] args) {
new TestMigLayoutFontSize();
}
}
You may reduce the space around your checkbox by reducing the border size, e.g. put
testBox.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
directly after the assignment of testBox. You may then leave the row definition empty and still get a reasonable height for your panel.
The following works for me. I think the problem is , that you specify the preferred size.
Regards
Roger
package de.test;
import java.awt.Font;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
public class MigTest extends JFrame {
public MigTest() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600, 400);
setContentPane(getTestPanel());
setVisible(true);
}
private JPanel getTestPanel() {
JCheckBox testBox = new JCheckBox("Program argument");
Font normalFont = testBox.getFont();
Font largeFont = new Font(testBox.getFont().getName(), testBox.getFont().getStyle(), 90);
// testBox.setFont(normalFont);
testBox.setFont(largeFont);
JPanel tempPanel = new JPanel(new MigLayout("debug", "0lp![grow,fill]0lp!", "[80:n:]"));
tempPanel.add(testBox);
JPanel testPanel = new JPanel(new MigLayout("", "[grow,fill]", "[grow,fill]"));
testPanel.add(tempPanel);
return testPanel;
}
public static void main(String[] args) {
new MigTest();
}
}

Categories