gridbaglayout

Java GridBagLayout - How to position my components gap-less and one by one?

左心房为你撑大大i 提交于 2019-12-07 18:31:29
I'm using GridBagLayout to place my GUI components by the following code, wanting the components lay one by one in a column, without any gaps : import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class TestGUI extends JFrame{ public TestGUI(){ JPanel bigPanel = new JPanel(new GridBagLayout()); JPanel panel_a = new JPanel(); JButton btnA = new JButton("button a"); panel_a.add(btnA); JPanel panel_b = new JPanel(); JButton btnB = new JButton("button b"); panel_b.add(btnB); GridBagConstraints c

How to set minimum sizes with GridBagLayout?

左心房为你撑大大i 提交于 2019-12-07 17:29:46
问题 I have a simple GUI using GridBagLayout with a button panel at the top, and a custom resizable component taking up the rest of the space, as shown in the following image: The custom component (the red one) has a preferred size of (400, 300) and a minimum size of (40, 30) and is happy to be resized to any size greater than that. However, I would like my frame to respect the minimum size of the button panel and not allow the frame to be resized such that any of the buttons are not fully shown

Skip a row GridBagLayout

余生颓废 提交于 2019-12-07 17:25:20
问题 I'm using the GridBagLayout on a JFrame and I'd like to be able to skip a row or two but have those rows show up as blank, and then have a button after those rows. I can't find any way in the documentation to do what I described, does anyone know of any way I can do this? 回答1: Figured it out much cleaner than adding empty components... there is a GridBagConstraint named insets that allows you to choose the white-space between the component you're adding and the other components around it in

jpanel not displaying well with jframe set to gridbaglayout

帅比萌擦擦* 提交于 2019-12-07 09:47:04
问题 the program below is to position a jpanel at the top left conner of jframe with gridbaglayout but instead a very small box is displayed in center of jframe. when I set the layout of jframe to null, the jpanel displays fine. can someone tell me why the jpanel is compressed to the center of frame with gridbaglayout? i really need to use gridbag. please help import java.awt.*; import javax.swing.*; //swing package public class Major { //defining the constructor public Major() { JFrame maFrame =

Why does GridBagLayout center my components instead of putting it in the corner?

故事扮演 提交于 2019-12-06 19:03:00
问题 So far I managed to avoid using the GridBagLayout (by hand code) as much as possible, but I could not avoid it this time and I am reading the SUN's tutorial GridBagLayout So far it is not going well. I think I am missunderstanding something. For example I try the following code (similar to the one in SUN's post): public class MainFrame extends JFrame { public static void main(String args[]) { EventQueue.invokeLater(new Runnable() { public void run() { try { MainFrame frame = new MainFrame();

GridBagLayout & JScrollPane: how to reduce row height?

牧云@^-^@ 提交于 2019-12-06 15:39:56
See below a simple test code using a GridBagLayout (2 rows, 2 component on row 0, 1 component on row 1). Although I have specified weighty to be 0.01 for first row and 1 for second row, the ratio on the screen looks more like 0.3 vs. 0.7. It seems that the height of the first row is resized so that the whole textarea fits in it. How can I reduce the height of the first row, so that the scroll bars of the JScrollPane will appear? public class Test { public static void main(String... args) { String text = "text\n\n\n\n\n\n\n\ntext"; JFrame frame = new JFrame(); JTextArea area; JScrollPane pane;

Java GridBagLayout : make component align to left

青春壹個敷衍的年華 提交于 2019-12-06 13:55:44
I have this layout using GridBagLayout : public class Example extends JFrame { public Example() { Border outline = BorderFactory.createLineBorder(Color.black); GridBagLayout gbl = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); JPanel pane = new JPanel(gbl); gbc.weighty = 1.0; gbc.weightx = 1.0; JLabel unitLbl = new JLabel("Unit"); unitLbl.setBorder(outline); gbc.gridx = 0; gbc.gridy = 0; gbc.ipadx = 30; gbc.ipady = 10; gbl.setConstraints(unitLbl, gbc); pane.add(unitLbl); JLabel typeLbl = new JLabel("Type"); typeLbl.setBorder(outline); gbc.gridx = 0; gbc.gridy = 1; gbc

GridBagLayout weighty alignment

╄→尐↘猪︶ㄣ 提交于 2019-12-06 13:30:21
I am dinamically generating a layout which requires to use relative sizes, the only way I found to do it without using an external Java layout library is GridBagLayout's weightx and weighty. At this point it has been working exactly as I need with one small exception. When I have one column containing two JPanels with a space distribution of 66.6% and 33.3% respectively and then another column with 3 JPanels using 33.3% of the space each one of them, the 33.3% of the first column is not the same as the 33.3% of the second. I need them to be perfectly aligned. Unfortunately this is my first

One column layout does not use full width of window

痞子三分冷 提交于 2019-12-06 12:38:35
I would like to create a layout: 2 rows, 1 column. 1st row should occupy 70% height of the window and 2nd row 30% of the window. I achieve this by using weighty attribute of GridBagConstraints . However I have problem with the width of my component, because when I resize application window the component remain in the center, its width is constant and I get a white spaces in the left and right of the component (even if I set fill to BOTH ). This problem does not occur when I change the height of the window (components resize very well and fill full height of the window). Below my constraints:

How do you stop a JLabel changing its size when its text changes?

笑着哭i 提交于 2019-12-06 04:48:30
问题 I'm generating some JComponents in code and using the GridBag layout to arrange them. My layout consists of 12 rows and 3 columns, with each row consisting of a JSlider, a JCheckBox and a JLabel. Here's the code I'm using to generate the UI: final int NUM_MOTORS = 12; // This is the panel I'm adding the components to. pnlMotorSliders.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); for (int i = 0; i < NUM_MOTORS; ++i) { c.gridy = i; // Create the slider JSlider