Java: remove margin / padding on a JTabbedPane - java

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.

Related

GWT TabLayoutPanel inside ScrollPanel

I'm trying very hard to understand GWT's layout system that they introduced in 2.0. It's supposedly recommended, but it seems much more complex that the old way.
What I want to do, (which I feel is a very basic design) is build an application with a header, left vertical menu, footer, and a scrolling content section.
So, to do that, you do this:
DockLayoutPanel panel = new DockLayoutPanel();
FlowPanel header = new FlowPanel();
FlowPanel menu = new FlowPanel();
FlowPanel footer = new FlowPanel();
ScrollPanel content = new ScrollPanel();
panel.addNorth(header);
panel.addWest(menu);
panel.addSouth(footer);
panel.add(content);
RootLayoutPanel.get().add(panel);
That works perfectly fine. Except, the ScrollPanel (where all content goes) apparently breaks the layout flow. So, in my activity, I have:
#Override
public void start(AcceptsOneWidget panel, EventBus eventBus)
{
FlowPanel viewPanel = new FlowPanel();
panel.add(viewPanel);
}
If I add a TabLayoutPanel to the viewPanel, it will not display correctly. I have to add it to a LayoutPanel that is attached to a LayoutPanel that is attached to a LayoutPanel, etc. all the way up to the RootLayoutPanel.
So, I try this:
#Override
public void start(AcceptsOneWidget panel, EventBus eventBus)
{
LayouPanel viewPanel = new LayoutPanel();
panel.add(viewPanel);
}
And in my ui:binder, I have:
<g:LayoutPanel>
<g:layer>
<g:TabLayoutPanel barHeight="3.33" barUnit="EM" height="500px">
<!-- tabs go here -->
</g:TabLayoutPanel>
</g:layer>
</g:LayoutPanel>
So 2 things here.
I don't want to set the height. I want the tab panel to be as big as the content. I don't get why you have to set heights in this "better" method of laying out your app.
It still doesn't show on the screen at all. I'm assuming because it's being attached to the ScrollPanel. What confuses me is that ScrollPanel does indeed implement RequiresSize and ProvidesResize, so is it not a valid LayoutPanel? That's literally all LayoutPanel does. It's a ComplexPanel that implements those interfaces
When I inspect the page, and hover over where the TabLayoutPanel should be, I get a blue highlight (in Chrome) as if it were there, but it seems the
<g:layer>
that wraps it has no height, so it hides the TabLayoutPanel. In fact, if I edit it in the viewer to have a height, the TabLayoutPanel does show.
Am I doing something fundamentally wrong here? I would very much like to do:
RootLayoutPanel > DockLayoutPanel > ScrollPanel > *Some Container > TabLayoutPanel
And for things to just... work. I'm trying to convert my current app to this new layout stuff to get rid of all the tables that get generated under the hood, but it seems to cause more problems than it fixes.
*This container has things above and below the Tab Panel
ScrollPanel in your proposed layout will occupy space given to it by DockLayoutPanel. If "Some Container" is never larger than space allocated to ScrollPanel, ScrollPanel will never scroll. If "Some Container" may become larger (i.e. the need for scrolling), then this container cannot get its size from ScrollPanel - its height should either be set explicitly, or it should be a regular FlowPanel which grows with its content. In both of these cases, this "Some Container" will not be able to provide any size to its children, including TabLayoutPanel.
It's hard to give advice without seeing the requirements, but if you need to include a TabLayoutPanel inside a panel that grows with its own content, you must set its height yourself - in code or CSS.

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.

Double-byte system locale causes Swing component resizing

I'm working on a Swing app for Solaris, and I have a problem that shows up only when the system LANG variable is set to a double-byte language such as Korean. The problem is that components are being resized and this is messing up the layout of the app:
1) LANG set to Korean
2) LANG set to English
Changing the Locale in the Java code itself does not affect the display (if LANG=English and Locale is set to Korean, everything is fine. If LANG=Korean and Locale is set to English, the issue happens).
Is this caused by the LayoutManager (I'm using BorderLayout)? Is there any way to prevent this from happening? So far the only workaround I can use is changing the system locale to english right before launching the app.
Here is some example code on what I'm doing:
Box container = Box.createVerticalBox();
container.add(label1);
container.add(label2);
container.add(label3);
container.add(label4);
container.add(label5);
Border border1 = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
TitledBorder titledBorder = BorderFactory.createTitledBorder(border1, LocalizationTools.getString("STR_1"));
Dimension lDim = new Dimension(550, 100);
mypanel = new JPanel(new BorderLayout());
mypanel.setPreferredSize(lDim);
mypanel.setMaximumSize(lDim);
mypanel.setMinimumSize(lDim);
mypanel.setBorder(titledBorder);
mypanel.add(container);
Here are some strings used in the example:
1) "\ud648 \ub514\ub809\ud1a0\ub9ac"
2) "\ubcc0\uacbd \uc2dc\uac04"
3) "\ub9c8\uc9c0\ub9c9 \uc791\uc5c5"
Here are a few things to try:
Ensure that you are calling pack() on the parent Window and not interfering with any component's preferred size.
Use a compound border, adding an EmptyBorder to your TitledBorder.
While debugging, add padding to the BorderLayout and use color for highlight.
Edit your question to include an sscce that exhibits the problem; a native user may see the problem (text cut-off at bottom) more clearly.

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 AWT application window padding

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.

Categories