I use a GridBagLayout and place a JLabel on it as the picture shows. I managed to set the size of the JLabel to the desired one via ipadx and ipady but I don't seem to work with the position of it. It seems it is always centered on the middle while I'd like to start from the red dot and don't reposition itself as I resize the window. What should I do?
Thanks. The code I use is also:
GridBagLayout gbl = new GridBagLayout();
setLayout(gbl);
GridBagConstraints gbc = new GridBagConstraints();
JLabel jl = new JLabel("This is a jlabel!", SwingConstants.CENTER);
jl.setBorder(BorderFactory.createLineBorder(Color.black));
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.ipadx = 87;
gbc.ipady = 220;
add(jl, gbc);
I improved it... now you can work with insets to position that label around JFrame, but after some research I would recommend to choose different layout manager.
So your code would look like:
Dimension d = new Dimension(350, 400);
GridBagLayout gbl = new GridBagLayout();
JFrame frame = new JFrame("Heloo");
frame.setLayout(gbl);
GridBagConstraints gbc = new GridBagConstraints();
JLabel jl = new JLabel("This is a jlabel!", SwingConstants.CENTER);
jl.setBorder(BorderFactory.createLineBorder(Color.black));
gbc.ipadx = 87;
gbc.ipady = 220;
gbc.insets = new Insets(0, 0, 360, 340);// here work with JFrame size!!
Hope this code will help you :)
Related
Created a JFrame and tried to change the background but it does not work.
I have looked at so many solutions here and on the Internet and it doesnt work , What is wrong in my code
JFrame frame = new JFrame("Process");
JLabel label = new JLabel("Please wait...");
JProgressBar pb = new JProgressBar();
pb.setIndeterminate(true);
pb.setBackground(new java.awt.Color(248, 201, 171));
frame.setBackground(new java.awt.Color(242, 186, 152));
frame.setSize(400, 200);
frame.setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setBackground(new java.awt.Color(242, 186, 152));
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(10,10,10,10); // make spaces between components on screen
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 20;
panel.add(label, c);
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 20;
panel.add(pb, c);
frame.add(panel);
frame.setVisible(true);
try frame.getContentPane().setBackground(new java.awt.Color(242, 186, 152));
don't work on the frame, work on the frame.getContentPane(). This is also where your sub panel and layout should be set. Besides, you use gridbag constraints but you never set a gridbag layout first. Meanwhile, you add to the frame and even if you did add to the content pane, you also didn't specify a content pane layout.
After doing changes in my code I found that the problem is caused by the GridBagConstraints c = new GridBagConstraints();
so i changed this by using panel.setLayout(null); then used setBounds to adjust the position of the components label.setBounds(100, 30, 250, 10);
and everything os ok
I know there are some other questions similar to this but I haven't seen my specific question answered. I want to change the size of a JButton WITHOUT changing the look. For example if I use setPreferredSize, I lose the white background color and rounded corners. I just want to remove the surrounding padding so the button is only slightly bigger then it's label.
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame jFrame = new JFrame("Label");
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(10, 10, 10, 10);
JPanel jPanel = new JPanel(new GridBagLayout());
JButton jButtonUp = new JButton("Upd");
//jButtonUp.setPreferredSize(new Dimension(30, 20));
jPanel.add(jButtonUp, gbc);
gbc.gridy = 1;
JButton jButtonDn = new JButton("Del");
//jButtonDn.setPreferredSize(new Dimension(30, 20));
jPanel.add(jButtonDn, gbc);
jFrame.add(jPanel);
jFrame.pack();
jFrame.setVisible(true);
Using Mac OSX Sierra.
What I have is this:
What I get with setPreferredSize is this:
What I want is this:
Check this answer here, Simply do
After the line:
JButton jButtonUp = new JButton("Upd");
Add:
jButtonUp.setBorder(null);
jButtonUp.setMargin(new Insets(0,0,0,0));
Then add the button to the panel:
jPanel.add(jButtonUp, gbc);
I have 5 JPanels lined up vertically. Each JPanel is filled with the same elements, but different values (JPanel, JButton, JLabel). I want them to look like this:
Panel Button Label
Panel Button Label
Buts it's turning out like this
Panel Button Label
Panel Button Label
The spacing is a little off, but the code is exactly the same for each container. How can I fix this?
public class AnswerChoice extends JPanel {
private static final long serialVersionUID = 1L;
private AnswerButton button;
private JLabel answerLabel;
public AnswerChoice(String imageURL) {
setBackground(Color.RED);
setLayout(new GridBagLayout());
button = new AnswerButton(imageURL);
answerLabel = new JLabel();
answerLabel.setFont(new Font("Times New Roman", Font.PLAIN, 32));
GridBagConstraints gbc = new GridBagConstraints();
JPanel panel = new JPanel();
panel.setBackground(Color.ORANGE);
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 1;
gbc.weightx = 0.2;
add(panel, gbc);
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 2;
gbc.weightx = 0.0;
gbc.insets = new Insets(0, 0, 0, 30);
add(button, gbc);
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 3;
gbc.weightx = 0.8;
add(answerLabel, gbc);
}
}
The JLabel does have text in it but I set it somewhere else.
but the code is exactly the same for each container.
The layout is done independently for each container based on the components added to the containter. So the size of each component on the container matters. Each container doesn't know that you have 4 other containers.
I have 5 JPanels lined up vertically.
So then you need to create a single panel using a GridBagLayout and add all 15 components to that panel. Then all 3 columns will be sized based on the components in each of the 5 rows.
Or I see in your logic that you try to assign relative sizes to each of the 3 components as .2, 0, .8. In this case you could use the Relative Layout on each of the panels. Using the Relative Layout you would display the button at its preferred size and then use 0.2f and 0.8f as the contraints for the panel and label respectively.
I'm making a program that uses a GridBagLayout in a container. My primary use for this is to have two JPanels, which occupy 75% and 25% of the horizontal space of the window. For some reason though, the two panels look more like 90/10, and when resizing, the smaller one rapidly changes in size, between it's apparent minimum size, and what I believe is the desired 25%.
Here is the relevant code.
frmReedreadV = new JFrame();
frmReedreadV.setBounds(x, y, width, height);
frmReedreadV.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmReedreadV.getContentPane().setLayout(new BoxLayout(frmReedreadV.getContentPane(), BoxLayout.Y_AXIS));
JPanel stretchyPanel = new JPanel();
frmReedreadV.getContentPane().add(stretchyPanel);
stretchyPanel.setLayout(new CardLayout(0, 0));
JPanel textAndUsers = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.weighty = 1;
textArea = new JTextArea();
textArea.setMargin(new Insets(2, 5, 5, 2));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
scrollPane = new JScrollPane(textArea);
scrollPane.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
gbc.weightx = 0.8;
textAndUsers.add(scrollPane, gbc);
list = new FriendsList(listUpdate);
gbc.weightx = 0.2;
textAndUsers.add(list.frmUserList, gbc);
stretchyPanel.add(textAndUsers);
FriendsList is a JList contained in a JPanel.
There are other buttons and text fields in the main CardLayout content pane, but those shouldn't affect what is inside of this GridBagLayout, correct?
I made another copy of this JPanel as a standalone application, and it displays and resizes perfectly. See here:
JFrame frame = new JFrame();
frame.getContentPane().setLayout((new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS)));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 550, 600);
JPanel stretchyPane = new JPanel();
frame.getContentPane().add(stretchyPane);
stretchyPane.setLayout(new CardLayout(0, 0));
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JTextArea text = new JTextArea();
text.setMargin(new Insets(2, 5, 5, 2));
JScrollPane panel1 = new JScrollPane(text);
FriendsList panel2 = new FriendsList(new Object());
c.fill = GridBagConstraints.BOTH;
c.weightx = .8;
c.weighty = 1;
panel.add(panel1, c);
c.weightx = .2;
//c.fill = GridBagConstraints.HORIZONTAL;
panel.add(panel2.frmUserList, c);
stretchyPane.add(panel);
frame.setVisible(true);
What could be causing the difference between the two, since I've replicated my original line by line into the copy?
The weightx and weighty properties might appear to act as proportional sizes, but that is not what they do. In fact they determine the distribution of extra space in the layout.
If you set everything to its preferred size by calling pack() on your JFrame, there will be no extra space. Which means the weightx and weighty properties have no effect while it's in that state.
Once the user starts resizing the window to be larger, there will be extra space, and only then will GridBagLayout consult the weightx and weighty properties to determine how to apportion that extra space to each column and row. Until then, it's entirely possible for a component with a small weightx to be wider than a component with a larger weightx, if their preferred sizes dictate it.
Hopefully this simple program will demonstrate this concept. Try using the mouse (or keyboard) to resize the window to be wider, and observe how each of the textfields grows:
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GridBagProportions {
static void buildAndShowWindow() {
JTextField small = new JTextField("small (0.8)", 5);
JTextField large = new JTextField("LARGE (0.2)", 30);
small.setMinimumSize(small.getPreferredSize());
large.setMinimumSize(large.getPreferredSize());
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets.left = 6;
gbc.insets.top = 6;
gbc.insets.bottom = 6;
gbc.weightx = 0.8;
panel.add(small, gbc);
gbc.weightx = 0.2;
gbc.insets.right = 6;
panel.add(large, gbc);
JFrame frame = new JFrame("GridBagLayout Proportions");
frame.getContentPane().add(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
buildAndShowWindow();
}
});
}
}
So what can be done about it? Well, this is one layout scenario that GridBagLayout cannot do. I would try using a SpringLayout:
SpringLayout layout = new SpringLayout();
JPanel textAndUsers = new JPanel(layout);
SpringLayout.Constraints scrollPaneConstraints =
new SpringLayout.Constraints(scrollPane);
Spring scrollPaneWidth = scrollPaneConstraints.getWidth();
SpringLayout.Constraints listConstraints =
new SpringLayout.Constraints(scrollPaneWidth,
scrollPaneConstraints.getY(),
Spring.scale(scrollPaneWidth, 0.25f),
scrollPaneConstraints.getHeight());
layout.putConstraint(SpringLayout.EAST, textAndUsers, 0,
SpringLayout.EAST, frmUserList);
layout.putConstraint(SpringLayout.SOUTH, textAndUsers, 0,
SpringLayout.SOUTH, scrollPane);
textAndUsers.add(scrollPane, scrollPaneConstraints);
textAndUsers.add(frmUserList, listConstraints);
Notice that the creation of listConstraints specifies a width argument which is Spring.scale(scrollPaneWidth, 0.25f). This ensures the list is always one-fourth as wide as the scrollPane containing the JTextArea.
SpringLayout is tricky to use, in that you have to make sure to link the far edges of the layout container to child components explicitly, because SpringLayout won't grow to accommodate all the child components automatically. That's what the putConstraint calls are doing.
I have written the following code to add a JLabel to a JPanel but it displays it in the center, while I expected it to be placed at the top of the JPanel.
Here is the piece of code I'm referring to:
JPanel pnlProjects = new JPanel();
pnlProjects.setMinimumSize(new Dimension(10, 300));
GridBagLayout gridBagLayout = new GridBagLayout();
pnlProjects.setLayout(gridBagLayout);
GridBagConstraints gridBagConstraints = new GridBagConstraints();
// add multiple label dynamically;
for (int count = 0; count < project.length; count++) {
lblProjects[count] = new JLabel("Project"+count );
lblProjects[count].setHorizontalAlignment(SwingConstants.LEFT);
lblProjects[count].setHorizontalTextPosition(SwingConstants.LEFT);
lblProjects[count].setBorder(BorderFactory.createBevelBorder(0));
lblProjects[count].setPreferredSize(new Dimension(100, 20));
gridBagConstraints.fill = GridBagConstraints.VERTICAL;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = count;
pnlProjects.add(lblProjects[count], gridBagConstraints);
}
// Add project panel in to the scorllPan
JScrollPane jspProjectlist = new JScrollPane(pnlProjects);
Would anyone be ablt to explain to me how to change it as per my requirement?
You can use next trick: add next code after your loop:
gridBagConstraints.weighty=1;
gridBagConstraints.gridy++;
pnlProjects.add(new JLabel(" "), gridBagConstraints);
That dummy JLabel will grab all space under your project JLabel's.