Java AWT application window padding - java

I'm trying to build a simple AWT application in Java. I want all of the containers in the main window to be separated by bit. I can accomplish this by setting the Hgap and Vgap in the BorderLayout constructor (see below.)
However, I can't figure out how to put a cap between the containers and the edges of the main window. How do I add a few pixels of padding to the main window?
import java.awt.*;
import java.applet.Applet;
public class LayoutTest extends Applet {
public void init() {
BorderLayout layout = new BorderLayout(8, 8);
setLayout(layout);
add(new Button("Left"), BorderLayout.CENTER);
add(new Button("Right"), BorderLayout.EAST);
}
}

I agree with the other answers and would recommend using Swing (use JApplet instead), which would make all kinds of things easier (you could just call setBorder and use BorderFactory to create a border, for example), but in your case you can set insets by overriding getInsets:
#Override
public Insets getInsets()
{
return new Insets(10,10,10,10);
}
Replace 10 with whatever you like.
There doesn't appear to be a setter, or I would say to use that instead. If there is a better way to do this in the case of an AWT Applet, someone please correct me.
If you decide to use Swing, see: How to Use Borders

AWT is not the newest technology on the block. So unless you have a specific requirement to do work in AWT, I would recommend you to check out the modern replacements Swing or SWT - much more comfortable, flexible customizable and predictable in their behaviour than AWT.
One reason behind developing them was exactly that the kind of visual fine-tuning you are trying to do here is unnecessarily difficult (if not impossible) with AWT.

Whilst you could probably get away with setting the insets of the applet, I suggest moving to Swing (extend javax.swing.JApplet). Then set a JPanel as the content pane with a EmptyBorder set of the appropriate widths.
Also note, you will probably quickly have to move up to a more sophisticated layout manager, such as GridBagLayout.

Related

Java - Unable to move position of JTextField

I am trying to create a new JTextField in my Jframe. I want to play around with the positioning of the textfield. I tried using setBounds and setLocation to change the position of the text box but it doesn't change the location of the text box at all.
This is my code:
public class GUI_Tutorial extends JFrame {
public static void main(String[] args) {
GUI_Tutorial frame = new GUI_Tutorial();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(1000, 800);
frame.setVisible(true);
frame.setTitle("Calculator");
frame.setLayout(new FlowLayout());
}
public GUI_Tutorial() {
//frame.setLayout(new FlowLayout());
JTextField num1;
num1 = new JTextField(10);
add(num1);
num1.setVisible(true);
num1.setLocation(5, 5);
}
}
May I know what am I doing wrong?
Your problem is one of layout managers. When you add a component to a container, the layout manager will dictate where the component will go. A JFrame's contentPane (the JFrame sub-container that holds its components) uses a BorderLayout by default, and items added to this container in a default manner will fill the central portion of the container, will fill it completely if nothing else is added to other BorderLayout locations.
Possible solutions for placing items:
The worst suggestion, use a null layout. You would do this by calling getContentPane().setLayout(null);. But when you do this, you the programmer are 100% responsible for the exact position and size of all components added. This leads to hard to maintain GUI's that might not even work on other platforms -- so don't do this.
Use a GUI builder to help build your program: this has definite advantages, one being that it shields you from having to directly understand the layout managers, but this also is a disadvantage, because once you run into edge cases (and you usually will and quickly) that knowledge is essential. My own view is to initially avoid using this so as to better understand the layout managers, and then once you're familiar with the managers, then sure use this as needed.
Better is to learn and experiment with the layout managers, and Borders, playing with placement of components. Remember that you can nest containers (usually JPanels) and each can be given its own layout manager, allowing for complex GUI's that are created easily and maintained easily.
Note that
(again) JFrames (actually their contentPanes), JDialogs, and other top-level windows use BorderLayout by default, that JPanels use FlowLayout by default, that JScrollPanes use their own specialty layout, one that you will likely never want to muck with, and that most other components/containers, such as JComponent, JLabel,... have null layouts by default.
Best resource is the tutorial: Lesson: Laying Out Components Within a Container
Borders can also be useful here, especially the EmptyBorder which can allow you to put a blank buffer zone around your components.
Start playing with the simpler layout managers, FlowLayout, BorderLayout, GridLayout, then BoxLayout, before moving to the more complex
Try removing frame.setLayout(new FlowLayout());. You'll then need to use num1.setBounds(x, y, width, height) rather than setLocation()
But, as other users have pointed out, you should be using a layout manager. Read up on the different layouts and choose the best one for your GUI.

Resize JFrame and Components Based On Computer Resoultion

I've made a java application in netbeans and am wondering how to have the size of the jframe half the width and height of the computer resolution and also having the components comply with this change. I tried putting code and it did make the frame half the height of the computer resolution but my components, such as buttons and textfields, stopped showing. How can I achieve this? Thanks.
(EDITED)
Set the JFrame's layout manager to GridLayout. In the properties window of the GridLayout itself (select in the navigator window) set columns to 1 and rows to 2. This should give you what you want and you won't have to get into the code.
This is the key code being called within the initComponents() method of your JFrame subclass (created by NetBeans) but it is important to understand where it is:
getContentPane().setLayout(new java.awt.GridLayout(2, 1));
I love Netbeans but you do have to understand the basics.
Good luck with your project. Swing is an awesome toolset that was way ahead of it's time.
As usual in this situation the key is using the right combination of layout managers for your containers. You're probably using NetBeans generated code (something I recommend you avoid until you are very comfortable with Swing coding), and it's probably having you use GroupLayout, a fine layout, but one that might not behave as well as you'd like on resizing components. I suggest that you go through the layout manager tutorial and try to nest JPanel containers and play with different layouts that re-size well such as GridLayout, GridBagLayout and BorderLayout to try to create the best layout that can re-size well.

intellij and swing - changing jpanel size

I am using intellij with swing.
My application runs on different computers with different monitors.
I want to display my form in different sizes.
I have a JPanel (not the main . inner jpanel ) set to (-1,670) in the intellij gui editor.
And I try to change it with this code :
MyFormUI myform = new MyFormUI();
if (thisIsTheCase){
myform.setLongView()
}
and in MyFormUI ->
public void setLongView(){
myPanel.setPrefferedSize(new Dimension(-1, 1000))
myPanel.repaint() ;
revalidate();
// I tried also repaint and revalidate on a higher jpanel in the hierarchy
}
When I change it in the gui editor - it does change, but not through code.
any suggestions?
myPanel.setPrefferedSize(new Dimension(-1, 1000))
myPanel.repaint();
revalidate();
Not sure what the -1 does.
The code should be:
myPanel.revalidate();
myPanel.repaint();
The revalidate() actually redoes the layout of the component and the repaint just paints it. In your code you are repainting before redoing the layout.
Preferred size is, as you may guess, only preferred. If the underlying layout manager does not want to use this setting, it is free to discard it. IntelliJ IDEA is using its own layout manager, so you need to check if the panels Horizontal and Vertical size policies allow resizing.
It will be easier to diagnose the problem if you describe your layout in more details.

Java: remove margin / padding on a JTabbedPane

I'd like to know how to remove the margins between my JtabbedPane and my JFrame content pane and between my JTabbedPane and its internal JPanel.
I circled the margins I want to remove.
the green line is here to show the gap between the jpanel inside the jtabbedpane.
I tried to look at some method named setMargin but it doesn't exist on a JTabbedPane. I also checked the Hgap and Vgap (both = 0) on the different layout (root content pane, my jpanel, etc).
So any suggestions are welcome.
Thanks.
.
I can't post images yet.
It is up to the look and feel to decide how much space is around components inside a tabbed pane - generally it will do this based on whatever is the default for your desktop. JTabbedPane does not have methods for setting the insets around internal components.
You can set this globally for all tabbed panes (caveat: Works on MetalLookAndFeel, will probably work for Windows L&F as well, probably won't work for GTK or Nimbus look and feel which are not based on BasicLookAndFeel). This will change the appearance of all tabbed panes in the VM:
UIManager.getDefaults().put("TabbedPane.contentBorderInsets", new Insets(0,0,0,0));
UIManager.getDefaults().put("TabbedPane.tabsOverlapBorder", true);
You probably also want to make sure your JTabbedPane has an EmptyBorder(0,0,0,0) and so do the components you put in it.
If this doesn't work on your target desktop, the alternatives are
if you don't care about your tabbed panes looking different from native application tabbed panes, the (unpleasant) alternative is to write your own TabbedPaneUI
set the UI delegate for the single JTabbedPane you want to look like this to MetalTabbedPaneUI or some other UI delegate that does respond to these properties
I just struck the same problem, and nothing anyone else said seemed to be a complete solution, so I came up with this:
import javax.swing.plaf.basic.BasicTabbedPaneUI;
tabbedPane.setUI(new BasicTabbedPaneUI() {
private final Insets borderInsets = new Insets(0, 0, 0, 0);
#Override
protected void paintContentBorder(Graphics g, int tabPlacement, int selectedIndex) {
}
#Override
protected Insets getContentBorderInsets(int tabPlacement) {
return borderInsets;
}
});
It works the same without overriding the paintContentBorder method, however doing so probably makes the UI slightly more efficient during resizes or similar, as the standard one seems to delegate out to a number of other methods.
Tested on Oracle Java 6 u43 for Linux in WindowMaker, Mac OS X 10.6.7 with Mac Java 6 u37 and Windows 7 with Java 7 u07, hopefully this helps someone :-)
Margins are added by setting borders on UI elements. Have a look at the settings of the border of your JTabbedPane.

Best way of subclassing a JPanel in Swing

I am currently trying to build an expanding panel in Swing (akin the WPF's Expander control) and I'd like to retain the usual methods for manipulating it (i. e. setLayout, add, etc.). Only they should be routed to an embedded panel (the one being shown or hidden).
How would one do that? Overriding every method of JComponent and re-routing that to an embedded JPanel would be cumbersome, but that's the only way I see.
Or should I rather make the embedded panel visible to the outside and force users to use something like ExpanderPanel.getInnerPanel() instead. But then it's no drop-in replacement for JPanel which I think would be nice to have.
Take a look at the JXTaskPane from Swingx project. It already does what you need.
In 1.5(ish) Swing routed a few methods to the content pane in JFrame, JApplet, etc. Whilst there appeared to be some usability benefits for those just starting, it doesn't actually fix the problem. So everyone has to deal with a very strangely behaving API. So my advice is to avoid this approach.
If you have a Container widget which holds a panel you want to show and hide, why not layout your inner panel however you want, then add it to the Container panel, then use static methods against the Container to say
JPanel p = new JPanel();
//do something with the JPanel...
ContainerWidget.setContent(p);
ContainerWidget.expandPanel(p,true);
Would somethign like this work?

Categories