I have never used layouts before and from what I understand GridBagLayout is probably not the easiest layout to start on, however I am trying to use it anyway. I am trying to use the GridBagLayout for my JPanel so that when I re-size it the components grow. I only need it to keep the natural height of the components but I am trying to use GridBagConstraints.HORIZONTAL to make the width of a component re-size automatically. I have the code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.font.*;
import java.io.*;
public class NumberConverterDesignTest
{
JFrame numberConversionWindow = new JFrame("Number Conversion");
JPanel numberPanel = new JPanel();
////////COMPONENTS////////
JLabel lblTitle;
JLabel lblBase2;
JLabel lblBase8;
JLabel lblBase10;
JLabel lblBase16;
JTextField txtBase2;
JTextField txtBase8;
JTextField txtBase10;
JTextField txtBase16;
///////////Font///////////
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String []fontFamilies = ge.getAvailableFontFamilyNames();
Font FontT5 = new Font("SansSerif", Font.BOLD, 45);
public void numberConvertGUI()
{
numberConversionWindow.setBounds(10, 10, 420, 300);
numberConversionWindow.setMinimumSize(new Dimension(420, 300));
numberConversionWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
numberConversionWindow.setLayout(new GridLayout(1,1));
createNumberConversionPanel();
numberConversionWindow.getContentPane().add(numberPanel);
numberConversionWindow.setVisible(true);
}
public void createNumberConversionPanel()
{
numberPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
lblTitle = new JLabel();
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
lblTitle.setText("Number Converter");
lblTitle.setHorizontalAlignment(JLabel.CENTER);
lblTitle.setFont(FontT5);
numberPanel.add(lblTitle, c);
lblBase2 = new JLabel();
c.weightx = 0.0;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
lblBase2.setText("Binary: ");
lblBase2.setHorizontalAlignment(JLabel.RIGHT);
numberPanel.add(lblBase2);
lblBase8 = new JLabel();
//c.weightx = 0.5;
c.weighty = 1.0;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
lblBase8.setText("Octal: ");
lblBase8.setHorizontalAlignment(JLabel.RIGHT);
numberPanel.add(lblBase8);
lblBase10 = new JLabel();
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 3;
lblBase10.setText("Decimal: ");
lblBase10.setHorizontalAlignment(JLabel.RIGHT);
numberPanel.add(lblBase10);
lblBase16 = new JLabel();
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 4;
lblBase16.setText("Hexadecimal: ");
lblBase16.setHorizontalAlignment(JLabel.RIGHT);
numberPanel.add(lblBase16);
txtBase2 = new JTextField();
c.weightx = 0.0;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
txtBase2.setText("");
numberPanel.add(txtBase2);
txtBase8 = new JTextField();
//c.weightx = 0.5;
c.weighty = 1.0;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 2;
txtBase8.setText("");
numberPanel.add(txtBase8);
txtBase10 = new JTextField();
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 3;
txtBase10.setText("");
numberPanel.add(txtBase10);
txtBase16 = new JTextField();
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 4;
txtBase16.setText("");
numberPanel.add(txtBase16);
}
public static void main(String[] args)
{
NumberConverterDesignTest nC = new NumberConverterDesignTest();
nC.numberConvertGUI();
}
}
and when this is compiled I get
My aim is to try and get
I believe the problem to be what I have set weightx to be, however I am not sure what to set it to, to get the result that I would like. Any help would be greatly appreciated
You need to pass the updated constraint each time you call the numberPanel.add method.
Related
I am using the gridbag layout to create my first application in Java. My code is completed and is located in another class. The problem I am having is being able to control buttons. I have the Jtextfield that outputs text on very first row to the bottom right. I want that Jtextfield to take 3 of the 4 spaces but its only taking the space in the end only. I want to be able to show entry spanning from from 3 of the cells in the top. I reserved one cell to reserve an image of the operator. Also, I set up some text constraints so that it doesn't resize the whole right hand size as output is being shown. But I wanted to keep the right hand side even. Can anyone tell me what I am doing wrong?
public class MyFrame extends JFrame{
static JLabel display;
static JLabel displayOperator;
public static String entry;
public static String repeatEntry;
public static String repeatEntry2;
public static String num1 = "";
public static String num2 = "";
public static String total = "";
public static String operator = "";
// public values for buttons
public JButton zero;
public JButton one;
public JButton two;
public JButton three;
public JButton four;
public JButton five;
public JButton six;
public JButton seven;
public JButton eight;
public JButton nine;
public JButton equal;
public JButton divide;
public JButton multiply;
public JButton minus;
public JButton plus;
public JButton clear;
public JButton negative;
public JButton decimal;
//attributes for bag layout
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
public MyFrame() {
super();
init();
//added strings to constructor frame
}
private void init() {
// creating a grid bag layout
/* FYI
* weight x and weight y refer to the space taking in the frame or space it takes
* grid x and y refers to location of button
* constraints sets limits to avoid things stretching out too much
*/
this.setLayout(new GridBagLayout());
this.setTitle("Calculator");
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}
//all output will shown in this JLabel
display = new JLabel("",SwingConstants.RIGHT); //aligns right
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 3;
c.weighty = 1;
c.gridx = 3;
c.gridy = 0;
this.add(display, c);
// this will display an indicator that show the operator currently in use
displayOperator = new JLabel("",SwingConstants.LEFT); //aligns LEFT
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0;
c.weighty = 0;
c.gridx = 0;
c.gridy = 0;
this.add(displayOperator, c);
display.setFont(new Font("Times Roman", Font.PLAIN, 20));
FontMetrics fm = display.getFontMetrics(display.getFont());
int w = fm.stringWidth("00000000"); //set display of numbers by these amount of digits to the text
int h = fm.getHeight();
Dimension size = new Dimension(w, h);
//locks perspective so that JBagged buttons are not autoresized
display.setMinimumSize(size);
display.setPreferredSize(size);
// all input buttons with Button Listeners assigned to them
JButton seven = new JButton("7");
this.add(seven);
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 4;
c.weightx = 1;
c.weighty = 1;
this.add(seven, c);
seven.addActionListener(new MyButtonListener(this));
JButton eight = new JButton("8");
this.add(eight);
c.fill = GridBagConstraints.BOTH;
c.gridx = 1;
c.gridy = 4;
c.weightx = 1;
c.weighty = 1;
this.add(eight, c);
eight.addActionListener(new MyButtonListener(this));
JButton nine = new JButton("9");
this.add(nine);
c.fill = GridBagConstraints.BOTH;
c.gridx = 2;
c.gridy = 4;
c.weightx = 1;
c.weighty = 1;
this.add(nine, c);
nine.addActionListener(new MyButtonListener(this));
JButton divide = new JButton("/");
this.add(divide);
c.fill = GridBagConstraints.BOTH;
c.gridx = 3;
c.gridy = 4;
c.weightx = .75;
c.weighty = .75;
this.add(divide, c);
divide.addActionListener(new MyButtonListener(this));
JButton four = new JButton("4");
this.add(four);
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 5;
c.weightx = 1;
c.weighty = 1;
this.add(four, c);
four.addActionListener(new MyButtonListener(this));
JButton five = new JButton("5");
this.add(five);
c.fill = GridBagConstraints.BOTH;
c.gridx = 1;
c.gridy = 5;
c.weightx = 1;
c.weighty = 1;
this.add(five, c);
five.addActionListener(new MyButtonListener(this));
JButton six = new JButton("6");
this.add(six);
c.fill = GridBagConstraints.BOTH;
c.gridx = 2;
c.gridy = 5;
c.weightx = 1;
c.weighty = 1;
this.add(six, c);
six.addActionListener(new MyButtonListener(this));
JButton multiply = new JButton("*");
this.add(multiply);
c.fill = GridBagConstraints.BOTH;
c.gridx = 3;
c.gridy = 5;
c.weightx = 1;
c.weighty = 1;
this.add(multiply, c);
multiply.addActionListener(new MyButtonListener(this));
JButton one = new JButton("1");
this.add(one);
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 6;
c.weightx = 1;
c.weighty = 1;
this.add(one, c);
one.addActionListener(new MyButtonListener(this));
JButton two = new JButton("2");
this.add(two);
c.fill = GridBagConstraints.BOTH;
c.gridx = 1;
c.gridy = 6;
c.weightx = 1;
c.weighty = 1;
this.add(two, c);
two.addActionListener(new MyButtonListener(this));
JButton three = new JButton("3");
this.add(three);
c.fill = GridBagConstraints.BOTH;
c.gridx = 2;
c.gridy = 6;
c.weightx = 1;
c.weighty = 1;
this.add(three, c);
three.addActionListener(new MyButtonListener(this));
JButton minus = new JButton("-");
this.add(minus);
c.fill = GridBagConstraints.BOTH;
c.gridx = 3;
c.gridy = 6;
c.weightx = 1;
c.weighty = 1;
this.add(minus, c);
minus.addActionListener(new MyButtonListener(this));
JButton zero = new JButton("0");
this.add(zero);
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 7;
c.weightx = 1;
c.weighty = 1;
this.add(zero, c);
zero.addActionListener(new MyButtonListener(this));
JButton decimal = new JButton(".");
this.add(decimal);
c.fill = GridBagConstraints.BOTH;
c.gridx = 1;
c.gridy = 7;
c.weightx = 1;
c.weighty = 1;
this.add(decimal, c);
decimal.addActionListener(new MyButtonListener(this));
JButton equal = new JButton("=");
this.add(equal);
c.fill = GridBagConstraints.BOTH;
c.gridx = 2;
c.gridy = 7;
c.weightx = 1;
c.weighty = 1;
this.add(equal, c);
equal.addActionListener(new MyButtonListener(this));
JButton plus = new JButton("+");
this.add(plus);
c.fill = GridBagConstraints.BOTH;
c.gridx = 3;
c.gridy = 7;
c.weightx = 1;
c.weighty = 1;
this.add(plus, c);
plus.addActionListener(new MyButtonListener(this));
JButton clear = new JButton("C");
this.add(clear);
c.fill = GridBagConstraints.BOTH;
c.gridx = 1;
c.gridy = 8;
c.weightx = 1;
c.weighty = 1;
this.add(clear, c);
clear.addActionListener(new MyButtonListener(this));
JButton negative = new JButton("+/-");
this.add(negative);
c.fill = GridBagConstraints.BOTH;
c.gridx = 2;
c.gridy = 8;
c.weightx = 1;
c.weighty = 1;
this.add(negative, c);
negative.addActionListener(new MyButtonListener(this));
JButton percentage = new JButton("%");
this.add(percentage);
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 8;
c.weightx = 1;
c.weighty = 1;
this.add(percentage, c);
percentage.addActionListener(new MyButtonListener(this));
// sets dimensions for window and locks resizing
this.setPreferredSize(new Dimension(400,500));
this.setMaximumSize(new Dimension(400,500));
this.setMinimumSize(new Dimension(400,500));
this.pack();
}
}
I have created a swing UI in java using a gridbag layout. Here is the code for adding the components;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.gridwidth=2;
this.add(loggedInLabel, c);
// c.fill = GridBagConstraints.HORIZONTAL;
c.anchor =GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 1;
this.add(inputCommand, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
this.add(confirmCommandButton, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 3;
this.add(replyField,c);
// c.fill = GridBagConstraints.VERTICAL;
c.anchor =GridBagConstraints.EAST;
c.gridx = 1;
c.gridy = 1;
this.add(screens,c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx=0;
c.gridy=4;
c.gridwidth=2;
c.anchor = GridBagConstraints.SOUTHWEST;
c.weightx = 0.25;
c.weighty = 0.25;
this.add(scrollPane,c);
For some reason, the screens object (JComboBox) is overlapping the input command object (JTextField). Does anyone know why? They are meant to be on different x co-ordinates
Analyze your code, first you set gridwidth to 2, this means that all subsequent components have an x grid span of 2, including inputCommand:
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.gridwidth=2; //Span 2 columns
this.add(loggedInLabel, c);
c.anchor =GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 1;
this.add(inputCommand, c); //inputCommand is two columns wide, cells (0,1)-(1,1)
.....
c.anchor =GridBagConstraints.EAST;
c.gridx = 1;
c.gridy = 1;
this.add(screens,c); //Screens is also two columns wide, starting from 1 so
//it will partially overlap inputCommand on cell (1,1)
...
After adding all of the missing lines to your code, I came up with the following GUI.
I had to make so many guesses as to what the rest of your code looks like, I have no idea if this matches what you did.
I added an Insets instance to separate the components.
Here's the complete runnable code I used.
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class GridBagLayoutExample implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new GridBagLayoutExample());
}
#Override
public void run() {
JFrame frame = new JFrame("GirdBagLayout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(5, 5, 5, 5);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
JLabel loggedInLabel = new JLabel("Logged in");
panel.add(loggedInLabel, c);
// c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 1;
JTextField inputCommand = new JTextField(10);
panel.add(inputCommand, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
JButton confirmCommandButton = new JButton("Confirm Command");
panel.add(confirmCommandButton, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 3;
JTextField replyField = new JTextField(10);
panel.add(replyField, c);
// c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.EAST;
c.gridx = 1;
c.gridy = 1;
JPanel screens = new JPanel();
panel.add(screens, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 4;
c.gridwidth = 2;
c.anchor = GridBagConstraints.SOUTHWEST;
c.weightx = 0.25;
c.weighty = 0.25;
JScrollPane scrollPane = new JScrollPane();
panel.add(scrollPane, c);
return panel;
}
}
I'm trying to create some UI but no idea how to use LayoutManger for it.
Like showing in the image Purple color component have fixed width and height but stick to the corners. 8 and 4 have fixed with and variable height, 2 and 6 have fixed height and variable width. the middle component (9) need variable width and height.
These 8 component work like a border and middle one is need to resize according to parent component size. I could do this coding absolute positions using null layout. but I have suggested to not to use null layout.
How could I do this with a Layout manager witch layout I can use for this? Do I need to use more layouts than one?
UPDATE
i have tried something with GridBagLayout as Andrew's suggestion but I still need a little help to understand how it works. Here is my code
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GridLayoutTest {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel jPanel1 = new JPanel();
jPanel1.setLayout(new GridBagLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button;
GridBagConstraints c = new GridBagConstraints();
button = new JButton("1");
c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.FIRST_LINE_START;
c.gridx = 0;
c.gridy = 0;
jPanel1.add(button, c);
button = new JButton("2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
c.gridx = 1;
c.gridy = 0;
jPanel1.add(button, c);
button = new JButton("3");
c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.FIRST_LINE_END;
c.gridx = 2;
c.gridy = 0;
jPanel1.add(button, c);
button = new JButton("4");
c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.LINE_START;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 1;
jPanel1.add(button, c);
//Panel
JPanel panel = new JPanel();
panel.setBackground(Color.green.darker());
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.0;
//c.weightx = 1.0;
c.gridx = 1;
c.gridy = 1;
jPanel1.add(panel, c);
button = new JButton("5");
c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.LINE_END;
c.weighty = 1.0;
c.gridx = 2;
c.gridy = 1;
jPanel1.add(button, c);
button = new JButton("6");
c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.LAST_LINE_START;
c.gridx = 0;
c.gridy = 2;
jPanel1.add(button, c);
button = new JButton("7");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
c.gridx = 1;
c.gridy = 2;
jPanel1.add(button, c);
button = new JButton("8");
c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.LAST_LINE_END;
c.gridx = 2;
c.gridy = 2;
jPanel1.add(button, c);
frame.add(jPanel1);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
I created the following GUI.
I set all six of the GridBagConstraints (anchor, fill, gridx, gridy, weightx, weighty) for each element of the grid. That way, I could more easily keep track of what the values were for each element.
Here's the complete runnable code I used. In other words a minimal, reproducible example.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class GridBagLayoutGUI implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new GridBagLayoutGUI());
}
#Override
public void run() {
JFrame frame = new JFrame("GridBagLayout GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new GridBagLayout());
panel.setPreferredSize(new Dimension(300, 300));
JButton button;
GridBagConstraints c = new GridBagConstraints();
button = new JButton("1");
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.FIRST_LINE_START;
c.weightx = 0.0d;
c.weighty = 0.0d;
c.gridx = 0;
c.gridy = 0;
panel.add(button, c);
button = new JButton("2");
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.CENTER;
c.weightx = 1.0d;
c.weighty = 0.0d;
c.gridx = 1;
c.gridy = 0;
panel.add(button, c);
button = new JButton("3");
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.FIRST_LINE_END;
c.weightx = 0.0d;
c.weighty = 0.0d;
c.gridx = 2;
c.gridy = 0;
panel.add(button, c);
button = new JButton("4");
c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.LINE_START;
c.weightx = 0.0d;
c.weighty = 1.0d;
c.gridx = 0;
c.gridy = 1;
panel.add(button, c);
// Panel
JPanel innerPanel = new JPanel();
innerPanel.setBackground(Color.green.darker());
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.CENTER;
c.weightx = 1.0d;
c.weighty = 1.0d;
c.gridx = 1;
c.gridy = 1;
panel.add(innerPanel, c);
button = new JButton("5");
c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.LINE_END;
c.weightx = 0.0d;
c.weighty = 1.0d;
c.gridx = 2;
c.gridy = 1;
panel.add(button, c);
button = new JButton("6");
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.LAST_LINE_START;
c.weightx = 0.0d;
c.weighty = 0.0d;
c.gridx = 0;
c.gridy = 2;
panel.add(button, c);
button = new JButton("7");
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.CENTER;
c.weightx = 1.0d;
c.weighty = 0.0d;
c.gridx = 1;
c.gridy = 2;
panel.add(button, c);
button = new JButton("8");
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.LAST_LINE_END;
c.weightx = 0.0d;
c.weighty = 0.0d;
c.gridx = 2;
c.gridy = 2;
panel.add(button, c);
return panel;
}
}
Another option would be JGoodies FormLayout. I have been using this in combination with JFormDesigner for all my layouting needs since basically forever. It covers 95% percent of my use cases. The remaining 5% is BorderLayout and absolute positioning (null layout).
A common strategy to solve complex computing tasks, is to break them into small, well defined manageable tasks. Divide and conquer.
This also applies to gui: you can break the design into small, easy to layout containers.
In this case the needed layout can be achieved by using BoxLayout and BorderLayout:
import java.awt.*;
import javax.swing.Box.Filler;
import javax.swing.*;
public class FramedContent extends JPanel{
private static final int FRAME_WIDTH = 35, CONTENT_SIZE = 300, BORDER_SIZE = 1;
private static final Color CORNER_COLOR = Color.MAGENTA, RULLER_COLOR = Color.BLUE,
CONTENT_COLOR = Color.GREEN, BORDER_COLOR = Color.WHITE;
FramedContent(){
setLayout(new BorderLayout());
add(header(), BorderLayout.NORTH);
add(center(), BorderLayout.CENTER);
add(header(), BorderLayout.SOUTH);
}
private JPanel header(){
JPanel hExpandPane = new JPanel();
hExpandPane.setLayout(new BoxLayout(hExpandPane, BoxLayout.X_AXIS));
hExpandPane.add(corner());
hExpandPane.add(hRuller());
hExpandPane.add(corner());
return hExpandPane;
}
private JComponent corner(){
Dimension d = new Dimension(FRAME_WIDTH, FRAME_WIDTH);
Filler filler = new Filler(d, d, d);
filler.setBackground(CORNER_COLOR);
filler.setBorder(BorderFactory.createLineBorder(BORDER_COLOR, BORDER_SIZE));
filler.setOpaque(true);
return filler;
}
private JComponent hRuller(){
Dimension d = new Dimension(0,FRAME_WIDTH);
Filler filler = new Filler(d, d, new Dimension(Short.MAX_VALUE, FRAME_WIDTH));
filler.setBackground(RULLER_COLOR);;
filler.setOpaque(true);
return filler;
}
private JPanel center(){
JPanel vExpandPane = new JPanel();
vExpandPane.setLayout(new BorderLayout());
vExpandPane.add(vRuller(), BorderLayout.WEST);
vExpandPane.add(content(), BorderLayout.CENTER);
vExpandPane.add(vRuller(), BorderLayout.EAST);
return vExpandPane;
}
private JComponent vRuller(){
Dimension d = new Dimension(FRAME_WIDTH, 0);
Filler filler = new Filler(d, d, new Dimension(FRAME_WIDTH, Short.MAX_VALUE));
filler.setBackground(RULLER_COLOR);
filler.setOpaque(true);
return filler;
}
private JPanel content(){
JPanel content = new JPanel(new GridBagLayout());// GridBagLayout used just to center the content
content.setBackground(CONTENT_COLOR);
content.add(new JLabel("Your Content Goes Here"));
content.setPreferredSize(new Dimension(CONTENT_SIZE,CONTENT_SIZE));
return content;
}
public static void createWindow() {
JFrame frame = new JFrame("Framed content");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.add(new FramedContent());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
createWindow();
}
}
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
In a JFrame with a BorderLayout, I have a "control panel" (with buttons and stuff) on the bottom of the window. In the JPanel of this "control panel" I'd like to use a GridBagLayout.
This is the result I have right now.
I was thinking to divide the layout in a 3 rows x 8 columns table.
In this configuration, the "+" symbol should take just one square and all the buttons should fill the panel.
The code is this:
buttonsPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
removeCost = new JButton("-");
c.gridx = 5;
c.gridy = 0;
buttonsPanel.add(removeCost, c);
addCost = new JButton("+");
c.gridx = 7;
c.gridy = 0;
buttonsPanel.add(addCost, c);
text = new JLabel("Incasso");
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 3;
buttonsPanel.add(text, c);
cost = new JTextArea();
cost.setBorder(BorderFactory.createLineBorder(Color.black, 1, true));
cost.setPreferredSize(new Dimension(80, 18));
c.gridx = 5;
c.gridy = 1;
c.gridwidth = 3;
buttonsPanel.add(cost, c);
cancel = new JButton("Cancella");
c.anchor = GridBagConstraints.LINE_START;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 3;
c.insets = new Insets(0, 2, 2, 30);
buttonsPanel.add(cancel, c);
enter = new JButton("Accetta");
c.anchor = GridBagConstraints.LINE_END;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 5;
c.gridy = 2;
c.gridwidth = 3;
c.insets = new Insets(0, 30, 2, 2);
buttonsPanel.add(enter, c);
What am I doing wrong?
You can not create a new instance of the GridBagConstrains. I did so.
I execute your code. Please see screenshot.
import javax.swing.*;
import java.awt.*;
public class app {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel buttonsPanel = new JPanel(new GridBagLayout());
frame.add(buttonsPanel);
// =====================
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
JButton removeCost = new JButton("-");
c.gridx = 5;
c.gridy = 0;
buttonsPanel.add(removeCost, c);
JButton addCost = new JButton("+");
c.gridx = 7;
c.gridy = 0;
buttonsPanel.add(addCost, c);
JLabel text = new JLabel("Incasso");
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 3;
buttonsPanel.add(text, c);
JTextArea cost = new JTextArea();
cost.setBorder(BorderFactory.createLineBorder(Color.black, 1, true));
cost.setPreferredSize(new Dimension(80, 18));
c.gridx = 5;
c.gridy = 1;
c.gridwidth = 3;
buttonsPanel.add(cost, c);
JButton cancel = new JButton("Cancella");
c.anchor = GridBagConstraints.LINE_START;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 3;
c.insets = new Insets(0, 2, 2, 30);
buttonsPanel.add(cancel, c);
JButton enter = new JButton("Accetta");
c.anchor = GridBagConstraints.LINE_END;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 5;
c.gridy = 2;
c.gridwidth = 3;
c.insets = new Insets(0, 30, 2, 2);
buttonsPanel.add(enter, c);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setSize(new Dimension(400, 400));
}
}
You need to have this behavior?
You need to create a new instance of the GridBagConstraints (c = new GridBagConstraints) every time you use it as a parameter to the add method.