Line up swing components by edges - java

Is it possible to line up swing components?
The components are in separate panels which both use flow layout. These two panels are in another panel which is using a grid layout.
As you can see there is a subtle difference and I find it annoying. I know that all of the jlabels [the rectangles in blue/purple all have the same size, so i think it might be because of the '+' and '*', but I'm not sure because the left sides of the first two boxes aren't lined up.
the panels
JPanel panel2 = new JPanel(new GridLayout(4, 1));
JPanel panel2a = new JPanel(new FlowLayout());
JPanel panel2b = new JPanel(new FlowLayout());
the first two rectangles (purple)
add1 = new JLabel("", JLabel.CENTER);
add1.setTransferHandler(new TransferHandler("text"));
add1.setBorder(b2);
add2 = new JLabel("", JLabel.CENTER);
add2.setTransferHandler(new TransferHandler("text"));
add2.setBorder(b2);
the two blue rectangles
textFieldA = new JTextField();
textFieldA.setHorizontalAlignment(JTextField.CENTER);
textFieldA.setEditable(false);
textFieldA.setBorder(new LineBorder(Color.blue));
textFieldM = new JTextField();
textFieldM.setHorizontalAlignment(JTextField.CENTER);
textFieldM.setEditable(false);
textFieldM.setBorder(new LineBorder(Color.blue));
the + and *
opA = new JLabel("+", JLabel.CENTER);
opS = new JLabel("*", JLabel.CENTER);
Showing that the rectangles are the same size
Dimension d = card1.getPreferredSize();
int width = d.width + 100;
int height = d.height + 50;
add1.setPreferredSize(new Dimension(width, height));
add2.setPreferredSize(new Dimension(width, height));
mult1.setPreferredSize(new Dimension(width, height));
mult2.setPreferredSize(new Dimension(width, height));
textFieldA.setPreferredSize(new Dimension(width, height));
textFieldM.setPreferredSize(new Dimension(width, height));
Adding to the panels
panel2a.add(add1);
panel2a.add(opA);
panel2a.add(add2);
panel2a.add(enterA);
panel2a.add(textFieldA);
panel2c.add(mult1);
panel2c.add(opM);
panel2c.add(mult2);
panel2c.add(enterM);
panel2c.add(textFieldM);
panel2.add(panel2a);
panel2.add(panel2c);

AFAIU this could be achieved using GroupLayout. This layout would require 5 horizontal groups and 2 vertical groups.
See How to Use GroupLayout for examples (including discussion of that image).
See also this answer for an MCVE.

Cross container layout management isn't really possible (or at least I've never seen a layout manager that does it).
You can, however, think carefully about your layout requirements and produce some interesting effects using compound components and layout managers...
public class TestLayout {
public static void main(String[] args) {
new TestLayout();
}
public TestLayout() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(2, 0));
frame.add(new CalculatePane("+"));
frame.add(new CalculatePane("x"));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class CalculatePane extends JPanel {
public CalculatePane(String operator) {
setLayout(new GridBagLayout());
Font font = UIManager.getFont("TextField.font").deriveFont(Font.BOLD, 24);
JTextField field1 = new JTextField(2);
JTextField field2 = new JTextField(2);
JTextField field3 = new JTextField(2);
field1.setFont(font);
field2.setFont(font);
field3.setFont(font);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(2, 2, 2, 2);
gbc.fill = GridBagConstraints.BOTH;
add(field1, gbc);
gbc.gridx++;
add(new JLabel(operator), gbc);
gbc.gridx++;
add(field2, gbc);
gbc.gridx++;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(new JButton("="), gbc);
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx++;
add(field3, gbc);
}
}
}

Related

Fix the size of a JScrollPane of JPanels in a JFrame

I am a beginner in Java Swing and I am trying to put a multiple JPanels in a JScrollPanel. The matter is, the JSCrollPannel (named jp in the code) should not fill all the JFrame but it does even if I fix a size with setSize() and a maximal size with setMaximalSize(). What is the trouble? How can I make the JSCrollPane smaller than the JFrame?
package GUI;
import java.awt.*;
import javax.swing.*;
public class MultiPanels {
private JScrollPane getContent() {
Dimension d = new Dimension(300,200);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc= new GridBagConstraints();
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
panel.add(getPanel(d, 6, Color.red), gbc);
panel.add(getPanel(d, 4, Color.green.darker()), gbc);
panel.add(getPanel(d, 4, Color.orange), gbc);
panel.add(getPanel(d, 12, Color.blue), gbc);
panel.add(getEmptyPanel(d), gbc);
return new JScrollPane(panel);
}
private JScrollPane getPanel(Dimension d, int rows, Color color) {
JPanel panel = new JPanel(new GridBagLayout());
panel.setBackground(color);
GridBagConstraints gbc= new GridBagConstraints();
gbc.insets = new Insets(10,5,10,5);
gbc.weightx = 1.0;
for(int i = 0, j = 1; i < rows; i++) {
gbc.gridwidth = GridBagConstraints.RELATIVE;
panel.add(new JButton(String.valueOf(j++)), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
panel.add(new JButton(String.valueOf(j++)), gbc);
}
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setPreferredSize(d);
return scrollPane;
}
private JScrollPane getEmptyPanel(Dimension d) {
JPanel panel = new JPanel() {
protected void paintComponent(Graphics g) {
int w = getWidth();
int h = getHeight();
GradientPaint gp = new GradientPaint(0,0,Color.red,
0,h,Color.cyan);
((Graphics2D)g).setPaint(gp);
g.fillRect(0,0,w,h);
}
};
panel.setPreferredSize(new Dimension(300,400));
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setPreferredSize(d);
return scrollPane;
}
public static void main(String[] args) {
JFrame f = new JFrame();
JScrollPane jp = new MultiPanels().getContent();
jp.setSize(new Dimension(200, 200));
jp.setMaximumSize(new Dimension(200,200));
jp.setPreferredSize(new Dimension(200,200));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(jp);
f.setSize(400,400);
f.setLocation(200,200);
f.setResizable(false);
f.setVisible(true);
}
}
Any time things don't size or arrange correctly, you have to look into Layouts.
Generally, spend more time on:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
To be specific, the default layout of a JPanel and JFrame is BorderLayout which is a very simple layout manager indeed. When you add to a component managed by BorderLayout without saying where, it is automatically added to the center and fills to use all available space:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html#border
It is possible to use "none" (Absolute Positioning) as the layout, but this is almost always a bad idea and you want to think about what you really want to do with the rest of the space in the JFrame: perhaps by letting new child components, with their own size demands, take up some of the space that the main panel is now swallowing up.

How to make jLabels stay attached to the corners of a window form, despite resizing the form in java?

I have 4 jLabels in my java program, which i placed in 4 corners I want them to stay there despite user resizing the window. I have written the code for the labels, but cannot seem to figure out how to keep them glued to each corner.
here is my code for the jLabels
JLabel label_1 = new JLabel("");
label_1.setEnabled(false);
label_1.setBounds(0, 0, 19, 19);
contentPane.add(label_1);
JLabel label_2 = new JLabel("");
label_2.setEnabled(false);
label_2.setBounds(0, 242, 19, 19);
contentPane.add(label_2);
JLabel label_3 = new JLabel("");
label_3.setEnabled(false);
label_3.setBounds(549, 242, 19, 19);
contentPane.add(label_3);
JLabel label_4 = new JLabel("");
label_4.setEnabled(false);
label_4.setBounds(549, 0, 19, 19);
contentPane.add(label_4);
Thanks
Don't use null layouts
Don't use setBounds(...)
Do use proper layout managers. Read the Layout Manager Tutorials for all the gory details.
Note that by using a null layout and setBounds, you ham-string your application's layout to being very rigid, very difficult to debug, enhance, and modify, and you also create a GUI that might look good on your box, but likely will not look good on another box using a different OS, or even the same OS with a slightly different screen resolution.
For example, using a GridBagLayout:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;
import static java.awt.GridBagConstraints.*;
public class LabelLayout extends JPanel {
private static final int[] ANCHORS = {NORTHWEST, SOUTHWEST, NORTHEAST, SOUTHEAST};
public LabelLayout() {
setLayout(new GridBagLayout());
for (int i = 0; i < ANCHORS.length; i++) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = i / 2;
gbc.gridy = i % 2;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.anchor = ANCHORS[i];
add(new JLabel("Label " + (i + 1)), gbc);
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("Labels");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new LabelLayout());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
A couple other notes:
I try to avoid using GridBagLayouts since they are one of the more complex layouts, but for your problem, they work nicely and simply.
Your problem can also be solved by using nested JPanels each using a simpler layout such as a BorderLayout.
Demo program, iteration number 2 that shows two GUI's, one using GridBagLayout and the other using nested JPanels, each using BorderLayout:
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;
import static java.awt.GridBagConstraints.*;
public class LabelLayout {
private static final int[] ANCHORS = { NORTHWEST, NORTHEAST, SOUTHWEST,
SOUTHEAST };
private JPanel gridBagPanel = new JPanel(new GridBagLayout());
private JPanel borderPanel = new JPanel(new BorderLayout());
public LabelLayout() {
for (int i = 0; i < ANCHORS.length; i++) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = i % 2;
gbc.gridy = i / 2;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.anchor = ANCHORS[i];
gridBagPanel.add(new JLabel("Label " + (i + 1)), gbc);
}
JPanel northPanel = new JPanel(new BorderLayout());
JPanel southPanel = new JPanel(new BorderLayout());
northPanel.add(new JLabel("Label 1"), BorderLayout.WEST);
northPanel.add(new JLabel("Label 2"), BorderLayout.EAST);
southPanel.add(new JLabel("Label 3"), BorderLayout.WEST);
southPanel.add(new JLabel("Label 4"), BorderLayout.EAST);
borderPanel.add(northPanel, BorderLayout.NORTH);
borderPanel.add(southPanel, BorderLayout.SOUTH);
}
public JPanel getGridBagPanel() {
return gridBagPanel;
}
public JPanel getBorderPanel() {
return borderPanel;
}
private static void createAndShowGui() {
LabelLayout labelLayout = new LabelLayout();
JFrame frame = new JFrame("Label GridBagLayout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(labelLayout.getGridBagPanel());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
frame = new JFrame("Label BorderLayout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(labelLayout.getBorderPanel());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

Layout Manager for background images and text

I'm trying to think of the best layout manager to achieve the picture below. I know absolute positioning is what I am used to, but I can't get the background image using this. GridBagLayout is excellent, but horrifically hard when ever I try I get a separate image for each grid.
Does anyone know an easy way out of this, or easy code to achieve the following?
There are several ways to do it. These are what I can think of at the moment:
Create a subclass of JComponent.
Override the paintComponent(Graphics g) method to paint the image that you want to display.
Set the content pane of the JFrame to be this subclass.
Some sample code:
class ImagePanel extends JComponent {
private Image image;
public ImagePanel(Image image) {
this.image = image;
}
#Override
protected void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, null);
}
}
// elsewhere
BufferedImage myImage = ImageIO.read(...);
JFrame myJFrame = new JFrame("Image pane");
myJFrame.setContentPane(new ImagePanel(myImage));
Note that this code does not handle re-sizing the image to fit the JFrame, if that's what you wanted.
There are a number of ways you can achieve this.
The simplest might be to just use what's already available...
If you don't need the background to be scaled at run-time (ie you can get away with a non-resizable window), simply using a JLabel as the primary container could make your life significantly easier.
public class LabelBackground {
public static void main(String[] args) {
new LabelBackground();
}
public LabelBackground() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new LoginPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class LoginPane extends JLabel {
public LoginPane() {
try {
setIcon(new ImageIcon(ImageIO.read(getClass().getResource("/background.jpg"))));
} catch (IOException ex) {
ex.printStackTrace();
}
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.EAST;
gbc.insets = new Insets(2, 2, 2, 2);
gbc.gridx = 0;
gbc.gridy = 0;
JLabel nameLabel = new JLabel("Name: ");
nameLabel.setForeground(Color.WHITE);
JLabel passwordLabel = new JLabel("Password: ");
passwordLabel.setForeground(Color.WHITE);
add(nameLabel, gbc);
gbc.gridy++;
add(passwordLabel, gbc);
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx++;
gbc.gridy = 0;
add(new JTextField(20), gbc);
gbc.gridy++;
add(new JTextField(20), gbc);
gbc.gridy++;
gbc.insets = new Insets(10, 2, 2, 2);
gbc.anchor = GridBagConstraints.EAST;
add(new JButton("Submit"), gbc);
}
}
}
Updated with example of left alignment
At the end of the constructor, add...
JPanel filler = new JPanel();
filler.setOpaque(false);
gbc.gridx++;
gbc.weightx = 1;
add(filler, gbc);
You might like to take a look at How to use GridBagLayout for more details

How to position components with GridBagLayout?

I am kind of new to Java Programming and I am trying to make a window that contains two buttons and a text area, as seen in the image below.
The problem I encountered though was positioning the components. I tried using GridLayout and separating the window into 9 rows and 16 cells, but then found I couldn't make components occupy more than a cell. I know I should be using GridBagLayout but I don't know how exactly. Help would be appreciated. :)
You have a number of choices. Instead of trying to layout the whole component in one, try using a compound layout, where by you layout sections of the UI in separate panes and focus on the individual requirements of each section...
public class TestLayout11 {
public static void main(String[] args) {
new TestLayout11();
}
public TestLayout11() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new ExamplePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected class ExamplePane extends JPanel {
public ExamplePane() {
setLayout(new GridBagLayout());
JPanel buttonPane = new JPanel(new GridBagLayout());
JButton btnOkay = new JButton("Ok");
JButton btnCancel = new JButton("Cancel");
JTextArea textArea = new JTextArea(5, 20);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.CENTER;
buttonPane.add(btnOkay, gbc);
gbc.gridy++;
gbc.insets = new Insets(50, 0, 0, 0);
buttonPane.add(btnCancel, gbc);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(100, 100, 100, 100);
add(buttonPane, gbc);
gbc.insets = new Insets(150, 100, 150, 100);
gbc.gridx++;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
add(new JScrollPane(textArea), gbc);
}
}
}

How to place components beneath tabs in right oriented JTabbedPane

So I just stumbled across placement of tabs in a JTabbedPane to the right and left (i.e. setTabPlacement(JTabbedPane.RIGHT)) which I love the look of. What I need is to utilize the space this leaves beneath the tabs. I currently have a column of JButtons, but they get pushed to the side, leaving a lot of blank space.
Any thoughts on how to do this? Some kind of custom overlay or something?
Here's a screenshot. In the code I basically have one horizontally aligned Box, with the JTabbedPane over a JTree, then the column of buttons after that.
boxOfEverything.add(tabbedPane);
boxOfEverything.add(boxColumnButtons);
Screenshot here.
I made this community wiki because this answer is not mine. #cheesecamera seems to have posted the same question on another forum and got an answer there. I copied the answer so that people coming here looking for an answer can get an answer.
The idea is to use swing's glasspane.
import java.awt.*;
import javax.swing.*;
public class RightTabPaneButtonPanel {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new RightTabPaneButtonPanel().makeUI();
}
});
}
public void makeUI() {
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setTabPlacement(JTabbedPane.RIGHT);
JPanel panel = new JPanel(new GridLayout(0, 1));
for (int i = 0; i < 3; i++) {
JPanel tab = new JPanel();
tab.setName("tab" + (i + 1));
tab.setPreferredSize(new Dimension(400, 400));
tabbedPane.add(tab);
JButton button = new JButton("B" + (i + 1));
button.setMargin(new Insets(0, 0, 0, 0));
panel.add(button);
}
JFrame frame = new JFrame();
frame.add(tabbedPane);
frame.pack();
Rectangle tabBounds = tabbedPane.getBoundsAt(0);
Container glassPane = (Container) frame.getGlassPane();
glassPane.setVisible(true);
glassPane.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.NONE;
int margin = tabbedPane.getWidth() - (tabBounds.x + tabBounds.width);
gbc.insets = new Insets(0, 0, 0, margin);
gbc.anchor = GridBagConstraints.SOUTHEAST;
panel.setPreferredSize(new Dimension((int) tabBounds.getWidth() - margin,
panel.getPreferredSize().height));
glassPane.add(panel, gbc);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

Categories

Resources