Unable to leave vertical gap - java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.URI;
class MainPageTypo {
JFrame fr;
JButton easy, medium, tough;
JLabel Contact;
MainPageTypo() {
buildGUI();
hookUpEvents();
}
public void buildGUI() {
fr = new JFrame("TypoMaster");
JPanel mainP = new JPanel();
mainP.setLayout(new FlowLayout());
JPanel LevelPanel = new JPanel();
LevelPanel.setLayout(new GridLayout(3, 0, 50, 50));
easy = new JButton("Easy");
medium = new JButton("Medium");
tough = new JButton("Tough");
Contact = new JLabel("Visit my Blog");
fr.add(mainP);
LevelPanel.add(easy);
LevelPanel.add(medium);
LevelPanel.add(tough);
LevelPanel.setBackground(Color.magenta);
LevelPanel.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
mainP.add(LevelPanel);
mainP.setBackground(Color.lightGray);
fr.setSize(500, 500);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fr.setVisible(true);
// fr.setResizable(false);
}
public void hookUpEvents() {
}
public static void main(String args[]) {
new MainPageTypo();
}
}
This is my complete code.I want to leave vertical space from top of JPanel().I am using LevelPanel.setBorder(BorderFactory.createEmptyBorder(50,50,50,50));
but unable to get vertical gap.How can i get this?

From what you are saying I am only guessing that you might be after something you get by setting the border on the main panel:
mainP.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
Please give us more details. Because you are getting the gap. Maybe draw a quick and nasty picture. :)
Recommendation
Please follow Java naming conventions, i.e. variables names should start from lowercase letter.

Basically, you have to understand which part of the puzzle is responsible for what
setting a Border on a container (here levelPanel) adds space-requirement to the container itself (as #Boro already explained): the LayoutManager apply to that container will layout the children of the container only inside the insets as requested by the border. That's what you are seeing in levelPanel, the red above the first button, below the last button (and to the sides of all buttons)
setting x/y gap properties in a LayoutManager which support this, has effects that are entirely at the decision of the manager itself, no way around reading the api doc of the concrete manager.
API doc for GridLayout:
* In addition, the horizontal and vertical gaps are set to the
* specified values. Horizontal gaps are placed between each
* of the columns. Vertical gaps are placed between each of
* the rows.
API doc for FlowLayout:
* #param hgap the horizontal gap between components
* and between the components and the
* borders of the <code>Container</code>
* #param vgap the vertical gap between components
* and between the components and the
* borders of the <code>Container</code>
From your code, I guess you expected to achieve the GridLayout to have the same gap-behaviour as the FlowLayout :-)
As the LayoutManager of the levelPanel's parent (parent == mainP) is FlowLayout, you can - as an alternative to setting the a Border to mainP - set the gap of the FlowLayout:
mainP.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 50));

Related

How do I need to implement GridBagLayout

How would I add gridbag layout to my code in order for output box to span the length of gui within the border parameters provided in the following code? I have two other classes that go along with this work fine. When the GUI populates it all works except the output text box doesn't span the length of gui so that the text in text box is cut off and I want to fix this, but I don't know how to do it as I never used gridbaglayout.
Below is the image showing how the GUI is supposed to look:
The code in question is given below:
public ATMGui() {
checkingAcc = new Account(1000);
savingAcc = new Account(2000);
currentSelect = new Account(0);
atmFrame = new JFrame("Automated Teller Machine");
output = new JTextField();
panel = new JPanel();
I believe that the problem may be because I didn't completely declare the size of the output box so it only spans one grid so-to-speak whereas I want it to span both. Can someone help me decide whether I declare the grid differently in the code above or if it goes below? Also, I've read about GridBagLayout and other methods but my problem is that I'm not sure how/where to implement it here.
atmWithdraw = new JButton("Withdraw");
atmDeposit = new JButton("Deposit");
transfer = new JButton("Transfer to");
balance = new JButton("Balance");
atmWithdraw.addActionListener(this);
panel.setLayout(new GridLayout(4, 2));
panel.setBorder(BorderFactory.createEmptyBorder(15, 15, 0, 15));
panel.add(atmWithdraw, 0);
panel.add(atmDeposit, 1);
panel.add(transfer, 2);
panel.add(balance, 3);
panel.add(checking, 4);
panel.add(savings, 5);
panel.add(output, BorderLayout.LINE_END);
atmFrame.add(panel);
atmFrame.setSize(300, 175);
atmFrame.setVisible(true);
i have never used gridbaglayout
Start by reading the section from the Swing tutorial on How to Use GridBagLayout for working examples.
The working example shows how to have a button span the entire width of the frame. You need to play with the "grid width" constraint. The tutorial explain how all the constraints are used.
The other option is to nest layout managers. So maybe you use the standard BorderLayout of the frame. Then you create a panel and add some components to the panel using your GridBagLayout and then add the panel to the CENTER. Then you can add your "output box" to the BorderLayout.PAGE_END. By default the component will fill the horizontal space.
The point is read the tutorials and learn the basics of each layout manager. Then use the appropriate combination of layout managers to get the job done.
My advice is not to use GridBagLayout but rather something a bit easier to use (but just as powerful) like TableLayout (http://www.oracle.com/technetwork/java/tablelayout-141489.html). To use include download the library and include in classpath or use the following maven include: -
<dependency>
<groupId>info.clearthought</groupId>
<artifactId>table-layout</artifactId>
<version>4.3.0</version>
</dependency>
(See https://github.com/nerro/table-layout for more details)
Once included, then you can define your table layout as a simple 2 dimensional array of numbers e.g.
import info.clearthought.layout.TableLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Test {
public static void main(String [] args) {
// Create JPanel using TableLayout as layout manager
double PREF = TableLayout.PREFERRED;
double BORDER = 10;
double size[][] = { { BORDER, 0.50, 10, 0.50, 10 }, { 10, PREF, 10, PREF, 10, PREF, 10, PREF, 10 } };
JPanel panel = new JPanel(new TableLayout(size));
panel.add(new JButton("Withdraw"), "1,1");
panel.add(new JButton("Deposit"), "3,1");
panel.add(new JButton("Transfer to"), "1,3");
panel.add(new JButton("Balance"), "3,3");
panel.add(new JRadioButton("Checking"), "1,5");
panel.add(new JRadioButton("Savings"), "3,5");
panel.add(new JTextField(), "1,7,3,7");
// Add to JFrame and Display
JFrame frame = new JFrame("ATM Machine");
frame.setSize(300, 200);
frame.setContentPane(panel);
frame.setVisible(true);
}
}
The previous code produces the following: -
NOTE that the layout is defined as follows: -
double size[][] = { { WIDTH1, WIDTH2, ... }, { HEIGHT1, HEIGHT2, ... } };`
The 2 column widths are specified as 50% or 0.50. All real numbers in the range [0.0..1.0) represents percentages e.g. see the BORDER variable. The constant FILL is allocated 100% of the scalable space and PREFERRED fills to the preferred size of the component. This allows the window to resize nicely.
To add then to this frame it's miles easier than GridBagLayout i.e. simply the cell X/Y positions e.g.
panel.add(new JButton("Withdraw"), "1,1");
panel.add(new JButton("Deposit"), "3,1");
We can span several columns as follows: -
panel.add(new JTextField(), "1,7,3,7");
This spans the JTextField from column 1 to column 3 in row 7.
For more advanced uses see: -
http://www.clearthought.info/sun/products/jfc/tsc/articles/tablelayout/Simple.html

JPanel taking up more space than it needs. How can I shrink it?

UPDATE: I have received justified criticism for posting non working code. I've taken that to heart and am updating this post with a complete working example. I'm also updating the description accordingly:
I have a very simple java swing GUI whose components take up what looks to be an equal amount of vertical (Y) space as is used by the largest Y extent component, but completely unnecessarily so. I have tried to shrink those components that don't need that much vertical space using preferredSize hints but to no avail.
The basic layout is simple: There's a main window and three vertical panels. The layout is a simple GridLayout (and I would prefer to keep it that way, unless someone shows me what I need cannot be done with GridLayout). All three panels seem to be occupying the same amount of vertical space, even though in the case of the sliders, this is massive waste of space. How can I get each of the sub-panes to only use as much space as they each need? i.e. I would like the two slider windows to be only as tall as the sliders and their description need to be.
The code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class test {
public static void main(String[] arg) {
JFrame mainWindow = new JFrame();
JSlider slider1 = new JSlider(0,100,50);
JSlider slider2 = new JSlider(0,100,50);
JPanel pnlSlider1 = new JPanel();
pnlSlider1.setLayout(new GridLayout(1,1)); // 1 row, 1 column
pnlSlider1.add(new JLabel("Description for slider1"));
pnlSlider1.add(slider1);
JPanel pnlSlider2 = new JPanel();
pnlSlider2.setLayout(new GridLayout(1,1)); // 1 row, 1 column
pnlSlider2.add(new JLabel("Description for slider2"));
pnlSlider2.add(slider2);
// label should now be to the left of slider
String content = "<html>Some rather long winded HTML content</html>";
JEditorPane ep = new JEditorPane("text/html", content);
// this is the main window panel
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3,1)); // 3 rows, 1 column
panel.add(ep);
panel.add(pnlSlider1);
panel.add(pnlSlider2);
// tie it all together and display the window
mainWindow.setPreferredSize(new Dimension(300, 600));
mainWindow.setLocation(100, 100);
mainWindow.getContentPane().add(panel);
mainWindow.pack();
mainWindow.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
mainWindow.setVisible(true);
}
}
(removed rant about not having seen any GUI coding advances in 30 years as that's not pertinent to the problem and likely won't be solved in this post either)
..components take up what looks to be an equal amount of vertical (Y) space as is used by the largest Y extent component, but completely unnecessarily so.
Yes, that is the way GridLayout is designed to work.
Use a GridBagLayout or BoxLayout or GroupLayout instead, each of which can do a single column or row of components of variable size (width and height).

BoxLayout refuses to honor preferred size of JButton

I have been working on a small project that is supposed to simulate a gambling game. Unfortunately, I ran into some odd issues while working with BoxLayout. To the best of my knowledge, LayoutManagers usually honor any component's preferred size. However, in the below code, BoxLayout does not.
Here is my code thus far:
import java.awt.*;
import javax.swing.*;
public class Main
{
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Suit-Up");
frame.setContentPane(makeGUI());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(900,450);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
}
public static JPanel makeGUI()
{
JPanel main = new JPanel();
main.setMinimumSize(new Dimension(900,450));
main.setBackground(Color.red);
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.LINE_AXIS));
infoPanel.setPreferredSize(new Dimension(900,60));
infoPanel.setBackground(Color.green);
main.add(infoPanel);
JPanel infoText = new JPanel();
infoText.setLayout(new BoxLayout(infoText, BoxLayout.PAGE_AXIS));
infoPanel.add(infoText);
JPanel moneyText = new JPanel();
moneyText.setLayout(new BoxLayout(moneyText, BoxLayout.LINE_AXIS));
infoText.add(moneyText);
JPanel lastGameText = new JPanel();
lastGameText.setLayout(new BoxLayout(lastGameText, BoxLayout.LINE_AXIS));
infoText.add(lastGameText);
JButton playAgain = new JButton("Play Again ($20)");
playAgain.setPreferredSize(new Dimension(200,60));
infoPanel.add(playAgain);
JButton finish = new JButton("End Session");
finish.setPreferredSize(new Dimension(200,60));
infoPanel.add(finish);
JPanel cardPanel = new JPanel();
cardPanel.setLayout(new BoxLayout(cardPanel, BoxLayout.LINE_AXIS));
main.add(cardPanel);
return main;
}
}
Despite specifying preferred sizes for both JButtons, they do not change their sizes. I have tried setMaximumSize() and setMinimumSize() as well, but neither had any effect.
Am I overlooking something obvious, or is this a limitation of BoxLayout?
"To the best of my knowledge, LayoutManagers usually honor any component's preferred size" - That's actually not true. preferred/min/max size are just "hints" that layout managers MAY use to determine how best to layout there contents. Layout managers are allowed to simply ignore them if they want to.
From the JavaDocs
BoxLayout attempts to arrange components at their preferred widths
(for horizontal layout) or heights (for vertical layout). For a
horizontal layout, if not all the components are the same height,
BoxLayout attempts to make all the components as high as the highest
component. If that's not possible for a particular component, then
BoxLayout aligns that component vertically, according to the
component's Y alignment. By default, a component has a Y alignment of
0.5, which means that the vertical center of the component should have the same Y coordinate as the vertical centers of other components with
0.5 Y alignment.
Similarly, for a vertical layout, BoxLayout attempts to make all
components in the column as wide as the widest component. If that
fails, it aligns them horizontally according to their X alignments.
For PAGE_AXIS layout, horizontal alignment is done based on the
leading edge of the component. In other words, an X alignment value of
0.0 means the left edge of a component if the container's ComponentOrientation is left to right and it means the right edge of
the component otherwise.

Java Swing - JPanel and GridLayout Margins/Padding

I'm working on building a chess game in Java, and I'm currently having a bit of trouble getting the GUI exactly the way I want it with Swing. I'm using a GridLayout to organize a grid of 8x8 ChessButtons (which override the JButton so that I can store extra information inside of them such as coordinates). Originally, the ChessButtons wouldn't appear unless I moused over them, but I solved that problem by placing each ChessButton inside a separate JPanel and setting each button's setPreferredSize() to a set height and width.
Now, my problem is that there seems to be a small margin or padding above (and/or below?) each button. I've made sure to set setHgap(0) and setVgap(0) for the GridLayout, so I'm pretty sure the mysterious margin is coming from either the buttons or the JPanels. But, I can't seem to get rid of them, and they seem to be causing each ChessButton to shift a little bit up/down whenever I mouse of them.
I realize this description of the problem might be a little hard to visualize, so I've taken a screenshot (using JButtons rather than ChessButtons so the gaps are slightly easier to recognize): http://img3.imageshack.us/img3/6656/jbuttonmargins.png
Here is the code I used to initialize each ChessButton:
chessBoard = new JPanel(new GridLayout(8, 8, 0, 0));
chessBoard.setBorder(BorderFactory.createEmptyBorder());
for (int i = 0; i <= 65; i++) {
//Create a new ChessButton
ChessButton button = new ChessButton("hi");
button.setBorder(BorderFactory.createEmptyBorder());
button.setPreferredSize(new Dimension(75, 75));
button.setMargin(new Insets(0, 0, 0, 0));
//Create a new JPanel that the ChessButton will go into
JPanel buttonPanel = new JPanel();
buttonPanel.setPreferredSize(new Dimension(75, 75));
buttonPanel.setBorder(BorderFactory.createEmptyBorder());
buttonPanel.add(button);
//Add the buttonPanel to the grid
chessBoard.add(buttonPanel);
}
So, how can I get rid of these vertical spaces between buttons? I'm relatively new to Swing, so I'm sorry if the answer is extremely obvious, but I'd appreciate any help anyone might have to offer! Thanks in advance!
Don't add an empty border; do use setBorderPainted(false).
Addendum: As #camickr notes, the panel's layout may include default gaps. The example below uses no-gap GridLayout accordingly.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/** #see http://stackoverflow.com/questions/4331699 */
public class ButtonBorder extends JPanel {
private static final int N = 8;
private static final int SIZE = 75;
public ButtonBorder() {
super(new GridLayout(N, N));
this.setPreferredSize(new Dimension(N * SIZE, N * SIZE));
for (int i = 0; i < N * N; i++) {
this.add(new ChessButton(i));
}
}
private static class ChessButton extends JButton {
public ChessButton(int i) {
super(i / N + "," + i % N);
this.setOpaque(true);
this.setBorderPainted(false);
if ((i / N + i % N) % 2 == 1) {
this.setBackground(Color.gray);
}
}
}
private void display() {
JFrame f = new JFrame("ButtonBorder");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new ButtonBorder().display();
}
});
}
}
Originally, the ChessButtons wouldn't appear unless I moused over them, but I solved that problem by placing each ChessButton inside a separate JPanel and setting each button's setPreferredSize() to a set height and width
That is not the proper solution. There is no reason to use a JPanel to hold the buttons. In fact, this is probably the cause of the problem. The buttons should show up when you add them to a GridLayout. If they don't show up its probably because you added the buttons to the GUI after making the GUI visible. Components should be added to the GUI BEFORE it is made visible.
Now, my problem is that there seems to be a small margin or padding above (and/or below?) each button
I don't understand why there also isn't a horizontal gap. When you create a JPanel, by default it uses a FlowLayout which also contains a horizontal/vertical gap of 5 pixels. So I understand why you might have the vertical gap of 10 pixels. I don't understand why there is no horizontal gap.
If you need more help post your SSCCE demonstrating the problem. And the SSCCE should use regular JButtons. Get the basics working with standard components before you start playing with custom components. That way you know if the problem is with your custom code or not.
Try adding chessBoard.setPreferredSize(600, 600) to create a JPanel for the board that only has room to fit the buttons (8 buttons each way * 75 size each way on the buttons).

Swing - how to mix JTextField and JTextAreas and have same visual appearance?

I am using miglayout to create a form in which there are JTextFields (short input answers) as well as JTextAreas (Longer answers). The problem is twofold.
The border placed around a Scrollpane wrapped text area does not match that of a Text Field.
The width and placement of the textarea/textfield differ, causing them not to line up correctly.
alt text http://grab.by/3O0V
After changing from right/left to right/fill:
alt text http://grab.by/3RMk
You can see that the bounds line up, but that there are still gaps. I tried setting novisualpadding but this did not fix it.
Source code:
package test2;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
public class Test extends JPanel {
private static final int NUM_CHARACTERS_WIDTH = 20;
private static final int NUM_ROWS = 5;
public Test() {
setLayout(new MigLayout(
"wrap 2",
// Align text labels on the so their right edge meets left edge of the text fields
"[right][left]"
));
add(new JLabel("Text field:"));
add(new JTextField(NUM_CHARACTERS_WIDTH));
add(new JLabel("No scrollpane text area:"));
add(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH));
add(new JLabel("Scrollpane text area:"));
add(new JScrollPane(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH)));
add(new JLabel("Text field:"));
add(new JTextField(NUM_CHARACTERS_WIDTH));
}
public static void main(String[] args) {
JFrame frame = new JFrame("");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new Test();
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
What's the preferred way to mix and match jtextfield and jtextareas, while still maintaining visual harmony? I notice now that the text field has a blue highlight around it when focus is in it, as opposed to the text area... another source of visual discontinuity.
I know this question is pretty old, but to get the border for a TextArea to match that of a TextField:
(myTextArea).setBorder(new JTextField().getBorder());
That should give a border to your TextArea like the one around a TextField.
Not sure how you can fix your border problem but to fix your layout situation I would just use springlayout. Springlayout is just a way to better layout your elements within the JPanel. You can find out more about it Java Sun Tutorial
Specifically you use it by setting where you want your North, South, West and East borders of each element. To do this you would have to first take your label calls out of the add so each one can be named. So instead of:
add(new JLabel("Text field:"));
Do:
JLabel myLabelName = new JLabel("Text field:");
add(myLabelName);
For each of your elements (JLabels, JTextAreas and JTextField). Once this is done you can easily set the layout.
Springlayout layout = new SpringLayout();
setLayout(layout);
Then for each of the elements you have to set any of the borders you want. They have to be in the specific order South, the North, West then East. Though you don't have to use all four borders if you don't want to. Here is an example on how to set your first text area, the one on the top.
layout.putConstraint(SpringLayout.NORTH, FirstTextAreaName, 10, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, FirstTextAreaName, this.getWidth()/2, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.EAST, FirstTextAreaName, -10, SpringLayout.EAST, this);
This example doesn't set the south of the text area but if you did want to it would have to be first. The first line sets the north side of the text area to be 10 pixels away from the top. When setting the other areas you but the previous (above) areas name instead of this and say it is 10 pixels away from the south of the previous one:
layout.putConstraint(SpringLayout.NORTH, SecondTextAreaName, 10, SpringLayout.SOUTH, FirstTextAreaName);
The second line in the above example sets the east side of the text area to start halfway through your main panel. The last, third, line sets the east side of the text area to be 10 pixels from the east side of your main panel.
I know that MiGLAyout (which I love, BTW) has the ability to do special handling for visual alignment vs strict pixel alignment. You may be running into this... The 'al' unit identifier is used for this, but I haven't had to use it so can't provide examples. It would probably be worth downloading the MiG sample project and see if they have the same alignment issue (I'm sure they have panels similar to yours).
For what it's worth, we mix text fields and areas in the same panel quite frequently and don't run into this... We do have to set the border of the scroll pane to be the same as the border of the text field as suggested by Noel Ang.
Also, instead of specifying constraints in the layout constructor, we generally specify them as we add each component - not sure if that makes a difference or not...
For the layout problem, try a columnConstraints value of [right][fill] instead of [right][left].
For the other issue, this appears to be a look-and-feel inconsistency. I ran your code in Windows, and the differences are there too, but less flagrant. My suggestion would be to set identifical borders explicitly for text fields and text areas.
setLayout(new MigLayout(
"wrap 2",
"[right][fill]"
));
JTextField textField;
JScrollPane scrollPane;
add(new JLabel("Text field:"));
textField = new JTextField(NUM_CHARACTERS_WIDTH);
textField.setBorder( new EtchedBorder( EtchedBorder.LOWERED ) );
add(textField);
add(new JLabel("No scrollpane text area:"));
add(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH));
add(new JLabel("Scrollpane text area:"));
scrollPane = new JScrollPane(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH));
scrollPane.setBorder( new EtchedBorder( EtchedBorder.LOWERED ) );
add(scrollPane);
add(new JLabel("Text field:"));
textField = new JTextField(NUM_CHARACTERS_WIDTH);
textField.setBorder( new EtchedBorder( EtchedBorder.LOWERED ) );
add(textField);
If you can't get MigLayout to align your components, considering using java.awt.GridBagLayout:
import static java.awt.GridBagConstraints.*;
setLayout( new GridBagLayout() );
GridBagConstraints leftCons = new GridBagConstraints();
leftCons.anchor = NORTHEAST;
leftCons.fill = NONE;
leftCons.weightx = 1.0;
leftCons.gridy = RELATIVE;
leftCons.gridx = 0;
leftCons.insets = new Insets( 4, 8, 4, 8 );
GridBagConstraints rightCons = new GridBagConstraints();
rightCons.anchor = NORTHWEST;
rightCons.fill = HORIZONTAL;
rightCons.weightx = 1.0;
rightCons.gridy = RELATIVE;
rightCons.gridx = 1;
rightCons.insets = leftCons.insets;
add(new JLabel("Text field:"), leftCons);
add(new JTextField(NUM_CHARACTERS_WIDTH), rightCons);
add(new JLabel("No scrollpane text area:"), leftCons);
add(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH), rightCons);
add(new JLabel("Scrollpane text area:"), leftCons);
add(new JScrollPane(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH)), rightCons);
add(new JLabel("Text field:"), leftCons);
add(new JTextField(NUM_CHARACTERS_WIDTH), rightCons);
First off +1 for screen shots.
Since you are using Mac, did you try Quaqua Look And Feel? It renders the textboxes/areas properly.
The answer is that MiG Layout folks are working on a fix for their next version.
Hello,
Apple has a nasty habbit of compensating by default and not let the developer decide. This is such a case where they have added a border to make it more visually like OS X. This should be the choice of the layout manager...
MigLayout can compensate for visual bounds like this but it is only done for JTabbedPane in Windows XP. I'm not sure it can be done 100% good in OS X though. I'll have to check. We don't want the text field to just grow into the bounds.
I have added this to the todo list for the next version.

Categories