whilst positioning my JLabels I found a problem which till now I could not resolve. The JLabels were overlapping.
This is a snippet from the code regarding the gridbaglayout:
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 3;
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20;
c.weightx = 3;
c.weighty = 1;
c.insets = new Insets(0,5,5,5);
all.add(header, c);
c.gridx = 0;
c.gridy = 1;
c.weightx = 2;
c.weighty = 4;
c.insets = new Insets(5,5,5,5);
all.add(sts, c); //this label overlapped
c.gridx = 1;
c.gridy = 1;
c.weightx = 1;
c.weighty = 5;
c.insets = new Insets(5,5,5,5);
all.add(cl, c); //this label overlapped
Thanks in advance
Having a gridwidth of 3 means the component will take up 3 places across the x grid, please read the grid bag layout tutorial
http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
You have the first component at x0 this will extend 3 columns to x2 hence the next avail grid location is x3
Related
I want the image and the title (JLabel) to both me centered and be at the top of the page but instead the image is at the top of the page and centered but the title is 3/4ths of the way down the page and centered.
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.NORTH;
c.insets = new Insets(10, 0, 0, 0);
c.weighty = 1;
c.gridy = 0;
panel.add(image,c);
c.gridy = 1;
panel.add(title, c);
c.weighty = 1;
c.gridy = 0;
panel.add(image,c);
c.gridy = 1;
panel.add(title, c);
You don't reset the "weighty" constraint so it is used for both components. Therefore, the "extra" space in the frame is allocated equally to each component. So you see the extra space between the two components.
Add a Border to each component and you will see the actual size of each component.
The solution is to assign set the "weighty" constraint to only the second component so the first component is displayed at its preferred size.
//c.weighty = 1;
c.gridy = 0;
panel.add(image,c);
c.gridy = 1;
c.weighty = 1;
panel.add(title, c);
I am wondering (as said in the title) if there is any way to resize rows when resizing the window. What happens to me when I use weighty function in GridBagLayout is that the rows split and there becomes space in between them. Is there any way to resize the rows instead of the spacing?
(Sorry if this is a bit unclear; here is a picture of what I mean)
Here is the important part of my code when I tried coding GridBagLayout:
JTextField display = new JTextField();//This is the top Text Field as seen in the picture
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.VERTICAL;
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
panel.add(one, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 0;
panel.add(two, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 0;
panel.add(three, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 3;
c.gridy = 0;
panel.add(plus, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
panel.add(four, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
panel.add(five, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 1;
panel.add(six, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 3;
c.gridy = 1;
panel.add(minus, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
panel.add(seven, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 2;
panel.add(eight, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 2;
panel.add(nine, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 3;
c.gridy = 2;
panel.add(multiply, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 3;
panel.add(zero, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 2;
panel.add(equals, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 3;
c.gridy = 3;
c.gridwidth = 1;
panel.add(divide, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 4;
panel.add(decimal, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 4;
panel.add(negative, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 4;
panel.add(modulus, c);
BorderLayout bl = new BorderLayout();
frame.add(display, bl.NORTH);//This is the top display,not very important.
frame.add(panel, bl.CENTER);
Looking to the image you attached, it seems you're using Java Swing. A little deep dive into the code would be useful(if you can paste the code here).
I figured it out! My mistake was that I typed c.fill = GridBagConstraints.HORIZONTAL so that it only resized horizontally and not vertically. What i figured out (it is very obvious but I couldn't think of it when I posted the question) is that I use c.fill = GridBadConstraints.BOTH so that it resizes both vertically and horizontally.
what I want to achieve is something like this:
This is my code:
JDialog messageDialog = new JDialog();
messageDialog.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
messageDialog.setBounds(0, 0, 350, 250);
messageDialog.setLocationRelativeTo(null);
messageDialog.setVisible(true);
JPanel btnPanel = new JPanel();
JPanel clearPanel = new JPanel();
JPanel labelsPanel = new JPanel();
JPanel txtPanel = new JPanel();
JButton newMessage = new JButton("New");
JButton recievedMessages = new JButton("Recieved");
JButton sendMessages = new JButton("Sent");
JButton refreshMessages = new JButton("Refresh");
JLabel recievedMessLab = new JLabel("Messages get:");
JTextPane txtToSend = new JTextPane();
btnPanel.setLayout(new GridLayout(4, 1));
btnPanel.add(newMessage);
btnPanel.add(recievedMessages);
btnPanel.add(sendMessages);
btnPanel.add(refreshMessages);
c.gridx = 0;
c.gridy = 0;
messageDialog.add(clearPanel, c);
c.gridx = 1;
c.gridy = 0;
messageDialog.add(labelsPanel, c);
c.gridx = 0;
c.gridy = 1;
messageDialog.add(btnPanel, c);
c.gridx = 1;
c.gridy = 1;
messageDialog.add(txtPanel, c);
labelsPanel.add(recievedMessLab);
I don't know why I get some free space around all the panels and I can't figure out how to resize the grids. Oracle tutorial doesn't help too. What is the easiest way to resize this? How to rid of that free space?
You need to add weight and fill information to your GridBagConstraints so the layout manager knows which components to strech over the available space.
Try the following:
c.gridx = 0;
c.gridy = 0;
c.fill = c.NONE; // dont fill (strech)
messageDialog.add(clearPanel, c);
c.gridx = 1;
c.gridy = 0;
c.weightx = 1; // horizontal weight: 1
c.fill = c.HORIZONTAL; // fill (strech) horizontally
messageDialog.add(labelsPanel, c);
c.gridx = 0;
c.gridy = 1;
c.weightx = 0; // horizontal weight: back to 0
c.weighty = 1; // vertical weight: 1
c.fill = c.VERTICAL; // fill (strech) vertically
messageDialog.add(btnPanel, c);
c.gridx = 1;
c.gridy = 1;
c.weightx = 1; // both weights: 1
c.weighty = 1; // both weights: 1
c.fill = c.BOTH; // and fill both ways, vertically and horizontally
messageDialog.add(txtPanel, c);
Revisit the part about weightx, weighty and fill in the tutorial to get a clue how they work.
PS: txtPanel is empty and txtToSend is never used?
i new to java and swing (came from c#) and try to add scrollbar to my textarea with no success.
I tried all kinds of techniques , it is very difficult to me
please take a look on the following code, I would be very happy
package firmwareUpdate;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
public class firmwareUpdate {
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
new firmwareUpdate().createUI();
}
};
EventQueue.invokeLater(r);
}
private void createUI() {
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
// row #1
JButton btnUpload = new JButton("Browse hex/bin file...");
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
frame.getContentPane().add(btnUpload, c);
JLabel lblFileName = new JLabel("aaa");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 4;
frame.getContentPane().add(lblFileName, c);
// row #2
JButton btnConfig = new JButton("Config");
// c.fill = GridBagConstraints.HORIZONTAL;
// c.ipady = 40; //make this component tall
// c.weightx = 0.0;
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
frame.getContentPane().add(btnConfig, c);
JButton btnHex2Bin = new JButton("hex2bin");
// c.fill = GridBagConstraints.HORIZONTAL;
// c.ipady = 40; //make this component tall
// c.weightx = 0.0;
c.gridx = 1;
c.gridy = 2;
c.gridwidth = 1;
frame.getContentPane().add(btnHex2Bin, c);
JButton btnFirmwareUpdate = new JButton("Firmware Update");
// c.fill = GridBagConstraints.HORIZONTAL;
// c.ipady = 40; //make this component tall
// c.weightx = 0.0;
c.gridx = 2;
c.gridy = 2;
c.gridwidth = 1;
frame.getContentPane().add(btnFirmwareUpdate, c);
JButton btnFirmwareUpdateStop = new JButton("Firmware Update Stop");
//c.fill = GridBagConstraints.HORIZONTAL;
// c.ipady = 40; //make this component tall
//c.weightx = 0.0;
c.gridx = 3;
c.gridy = 2;
c.gridwidth = 1;
frame.getContentPane().add(btnFirmwareUpdateStop, c);
// row #3
JButton btnCheckSum = new JButton("Check Checksum");
c.fill = GridBagConstraints.HORIZONTAL;
// c.ipady = 40; //make this component tall
c.weightx = 0.0;
c.gridx = 0;
c.gridy = 3;
c.gridwidth = 1;
frame.getContentPane().add(btnCheckSum, c);
JButton btnStartBootloader = new JButton("Start Bootloader");
c.fill = GridBagConstraints.HORIZONTAL;
// c.ipady = 40; //make this component tall
c.weightx = 0.0;
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 1;
frame.getContentPane().add(btnStartBootloader, c);
JButton btnStartApplication = new JButton("Start Application");
c.fill = GridBagConstraints.HORIZONTAL;
// c.ipady = 40; //make this component tall
c.weightx = 0.0;
c.gridx = 2;
c.gridy = 3;
c.gridwidth = 1;
frame.getContentPane().add(btnStartApplication, c);
JTextArea txtFileContent = new JTextArea(30,68);
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0; //reset to default
c.weighty = 1.0; //request any extra vertical space
c.anchor = GridBagConstraints.PAGE_START; //bottom of space
c.insets = new Insets(10,0,0,0); //top padding
c.gridx = 0; //aligned with button 2
c.gridy = 4; //third row
c.gridwidth = 4; //4 columns wide
frame.getContentPane().add(txtFileContent, c);
//txtFileContent.setVisible(false);
////////////// JScrollPane scrollPane = new JScrollPane(txtFileContent);
/////////////// scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
// middlePanel.add(scrollPane);
// scrollPane.setBounds(10,60,780,500);
//scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
////////////////// frame.getContentPane().add(scrollPane);
txtFileContent.setLineWrap(true);
txtFileContent.setWrapStyleWord(true);
JScrollPane scrolltxt = new JScrollPane();
scrolltxt.setViewportView(txtFileContent);
scrolltxt.setWheelScrollingEnabled(true);
frame.getContentPane().add(scrolltxt, c);
frame.setIconImage(new ImageIcon(getClass().getResource("/resources/fwup.png")).getImage());
frame.setTitle("Firmware Update");
//frame.setSize(600,800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
Try This
JTextArea txtFileContent = new JTextArea(30,68);
JScrollPane scroll = new JScrollPane(txtFileContent);
frame.add(scroll);
I'm not too sure if this will solve your problem, but have you considered that not setting any column widths or heights in your layout may be your concern (line 30 of the code)? Allow me to give you an example:
GridBagLayout layout=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
layout.columnWidths=new int[] {198, 198};
layout.rowHeights=new int[] {70};
panel.setLayout(layout);
Wherein, panel is a panel that is pushed onto the JFrame.
NB: I am personally against using the built-in content pane of a JFrame, because of its tendancies to default out on the user; however, for all intents and purposes, it shouldn't be a problem if done right.
I hope this helps, and best of luck!
When I tested your code, the scroll bar for the JTextArea shows up as expected when you exceed the row count of the initial JTextArea (The default value for the scrollbars' visibility are only to be shown when needed: JScrollPane API under verticalScrollBarPolicy and horizontalScrollBarPolicy). If you are asking to have it default as visible, then use either setVerticalScrollBarPolicy(...) or setHorizontalScrollBarPolicy(...) on your JScrollPane variable. There are several options available for ScrollPaneConstants
I am running into a problem with GridBagLayouts.
I have a few components laid out as shown in the picture below:
I want the "center" component to be able to move above the bottom edge of "top-right" if the "top" component shrinks small enough. However, this obviously causes a problem since it would have to be in a different row in the grid to be able to do so.
To solve this - I define the entire right column as its own container, with its own layout manager. I'd expect the two columns to be able to behave independently, however I get the same problem! I don't see how the two layout managers could possibly be interacting still. Can anyone possibly explain what my problem is?
Here is the relevant code (in my class extending Applet):
public void init(){
addComponents();
}
private void addComponents(){
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
Left left = new Left();
Top top = new Top();
Center center = new Center();
TopRight topRight = new TopRight();
BottomRight bottomRight = new BottomRight();
Bottom bottom = new Bottom();
topRight.setPreferredSize(new Dimension(200,200));
top.setPreferredSize(new Dimension(0,200));
center.setPreferredSize(new Dimension(0,100));
container.setPreferredSize(new Dimension(200,200));
c.fill = GridBagConstraints.BOTH;
c.gridx = 0; c.gridy = 0;
c.gridwidth = 1; c.gridheight = 3;
c.weightx = 0.2; c.weighty = 1.0;
this.add(left,c);
c.gridx = 1; c.gridy = 0;
c.gridwidth = 1; c.gridheight = 1;
c.weightx = 0.8; c.weighty = 0.8;
this.add(top,c);
c.gridx = 1; c.gridy = 1;
c.gridwidth = 1; c.gridheight = 1;
c.weightx = 0.8; c.weighty = 0.0;
this.add(center,c);
c.gridx = 1; c.gridy = 2;
c.gridwidth = 1; c.gridheight = 1;
c.weightx = 0.8; c.weighty = 0.2;
this.add(bottom,c);
JPanel container = new JPanel();
container.setLayout(new GridBagLayout());
c.gridx = 2; c.gridy = 0;
c.gridwidth = 1; c.gridheight = 3;
c.weightx = 0.0; c.weighty = 1.0;
this.add(container,c);
GridBagConstraints c2 = new GridBagConstraints();
c2.fill = GridBagConstraints.BOTH;
c2.gridx = 0; c2.gridy = 0;
c2.gridwidth = 1; c2.gridheight = 1;
c2.weightx = 0.0; c2.weighty = 0.0;
container.add(topRight,c2);
c2.gridx = 0; c2.gridy = 1;
c2.gridwidth = 1; c2.gridheight = 1;
c2.weightx = 0.0; c2.weighty = 1.0;
container.add(bottomRight,c2);
}
Note: Each of the components shown (left, top, etc) is an extension of JPanel - each with its paint(Graphics g) method overridden to fill it with some color.
Thanks in advance, and sorry for the long read,
Jonathan
I fixed the problem. It had something to do with me setting the preferred height of the Top component unnecessarily - when I removed that line it worked like I was wanting.