I have JFrame form which must contain TextField somewhere around center of the form, I am using GridBagLayout to set TextField position on the frame.
The code like this below:
public static JFrame enterFrameDraw(JFrame frame){
JButton btnEnter = new JButton("Sign in!");
JLabel loginLabel = new JLabel("Login!");
GridBagLayout gbl = new GridBagLayout();
JTextField textFieldLogin = new JTextField();
textFieldLogin.setBackground(Color.GRAY);
btnEnter.addActionListener(e -> {
try{
Chat.btnEnterHandler(frame);
} catch (Exception e2){
e2.printStackTrace();
}
});
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setTitle(FRM_TITLE);
frame.setLocation(FRM_LOC_X, FRM_LOC_Y);
frame.setSize(FRM_WIDTH, FRM_HEIGHT);
frame.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.NORTH;
c.fill = GridBagConstraints.NONE;
c.gridheight = 1;
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridx = GridBagConstraints.RELATIVE;
c.gridy = GridBagConstraints.RELATIVE;
c.insets = new Insets(40, 0, 0, 0);
c.ipadx = 0;
c.ipady = 0;
c.weightx = 0.0;
c.weighty = 0.0;
gbl.setConstraints(textFieldLogin, c);
frame.add(textFieldLogin, c);
frame.setResizable(false);
frame.setVisible(true);
return frame;
}
And result seems like:
Image
Could the problem lies in the "frame.add()" instead of simple "add()"?
UPDATE:
Method enterFrameDraw() after modification. Still doesn't work.
public static JFrame enterFrameDraw(JFrame frame){
JButton btnEnter = new JButton("Sign in!");
JLabel loginLabel = new JLabel("Login!");
GridBagLayout gbl = new GridBagLayout();
JTextField textFieldLogin = new JTextField();
textFieldLogin.setBackground(Color.GRAY);
btnEnter.addActionListener(e -> {
try{
Chat.btnEnterHandler(frame);
} catch (Exception e2){
e2.printStackTrace();
}
});
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setTitle(FRM_TITLE);
frame.setLocation(FRM_LOC_X, FRM_LOC_Y);
frame.setSize(FRM_WIDTH, FRM_HEIGHT);
frame.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.NORTH;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridheight = 1;
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridx = GridBagConstraints.RELATIVE;
c.gridy = GridBagConstraints.RELATIVE;
c.insets = new Insets(0, 40, 0, 40);
c.ipadx = 0;
c.ipady = 0;
c.weightx = 0.5;
c.weighty = 0.0;
gbl.setConstraints(textFieldLogin, c);
frame.add(textFieldLogin);
frame.setResizable(false);
frame.setVisible(true);
return frame;
}
UPDATE 2:
What i want to have on the form.
Image: This one
As far as I can tell there are two problems.
You need to set your constraints' fill to horizontal:
c.fill = GridBagConstraints.HORIZONTAL;
You need to set the constraints' weightx to a non-zero value:
c.weightx = 0.5;
Edit 1:
There's 2 ways you can centralize the content:
Define the constraints' insets to push it in from the left and right, like so:
c.insets = new Insets(0, 40, 0, 40);
The value 40 here is just an example. You could change the parameters to whatever suits.
You can use javax.swing.Box to create empty grid cells. To keep your content in the center, you can create an empty 'strut' on either side of the middle components:
c.gridx = 0;
c.gridy = 0;
c.weightx = 0.5;
frame.add(Box.createHorizontalStrut(100), c);
c.gridx = 1;
frame.add(textFieldLogin, c);
c.gridx = 2;
frame.add(Box.createHorizontalStrut(100), c);
Again, the value of 100 here is just an example, you should tweak the parameters to your needs.
It would be worthwhile reading up on things like Struts and Glues in the context of gridbaglayouts, they're really useful in organizing your content the way you want it.
Edit 2:
To add more components below the text field that's in the center, you should use the second approach of adding horizontal struts to the cells in the first row.
The idea is that putting a strut in a cell fills up space that would be used by a component, if there was a component in that cell. So by specifying the width of a horizontal strut in the first row, any following components with the same x value as the strut will share the same width as the strut.
So in this case, your method might look something like this:
public static JFrame enterFrameDraw(JFrame frame){
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(500, 500);
frame.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
frame.add(Box.createHorizontalStrut(80), c);
JTextField textFieldLogin = new JTextField();
textFieldLogin.setBackground(Color.GRAY);
c.gridx = 1;
c.gridy = 0;
frame.add(textFieldLogin, c);
c.gridx = 2;
c.gridy = 0;
frame.add(Box.createHorizontalStrut(80), c);
JLabel label = new JLabel("Label");
c.gridx = 0;
c.gridy = 1;
frame.add(label, c);
frame.setVisible(true);
return frame;
}
Here a label is added to the second row, and it does not appear in the center.
Related
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 want to create a layout like this
This was my code (does not work):
outer.setLayout(new BorderLayout());
panel1 = new JPanel();
...
outer.add(panel1, BorderLayout.PAGE_START);
outer.add(panel2, BorderLayout.LINE_START);
outer.add(panel3, BorderLayout.CENTER);
outer.add(panel4, BorderLayout.LINE_END);
outer.add(panel5, BorderLayout.PAGE_END);
note: panel5 above should contain 2 more panels inside it
In the above code, I can get them on the correct places but the center one (panel3) is very big so that all others are squashed to the side.
How can i get some ratio of size in these eg 2:10:2 etc?
Should i change my layout?
If you want to do something like that, using BorderLayout is a good start. So yes I would use BorderLayout as well here.
However you should change they way you are adding the panels:
outer.add(panel1, BorderLayout.NORTH);
outer.add(panel2, BorderLayout.WEST);
outer.add(panel3, BorderLayout.CENTER);
outer.add(panel4, BorderLayout.EAST);
//Create a additional Panel for the two at the bottom
JPanel southPanelContainer = new JPanel(new BorderLayout());
southPanelContainer.add(panel5, BorderLayout.EAST);
southPanelContainer.add(panel6, BorderLayout.WEST);
outer.add(southPanelContainer, BorderLayout.SOUTH);
This should already look somewhat decent, however if you still want to change the way it looks then you should add some components to those panels. The layout manager will automatically resize the panels so everything fits.
Use a GridBagLayout for a table/matrix like layout, where some "cells" occupy more than one slot.
BorderLayout is for one central panel having some bordering panels around.
public MainFrame() {
JPanel outer = new JPanel(new GridBagLayout());
outer.setPreferredSize(new Dimension(800, 600));
JPanel panel1 = createPanel("1");
JPanel panel2 = createPanel("2");
JPanel panel3 = createPanel("3");
JPanel panel4 = createPanel("4");
JPanel panel5 = createPanel("5");
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 3; // col span
c.gridheight = 1;
outer.add(panel1, c);
c.weightx = 0.33;
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
outer.add(panel2, c);
c.weightx = 0.33;
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
outer.add(panel2, c);
c.weightx = 0.33;
c.gridx = 2;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
outer.add(panel3, c);
c.weightx = 0.33;
c.gridx = 3;
c.gridy = 1;
c.gridwidth = 3;
c.gridheight = 1;
outer.add(panel4, c);
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 3;
c.gridheight = 1;
outer.add(panel5, c);
setContentPane(outer);
pack();
}
private JPanel createPanel(String title) {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder(title));
setPreferredSize(new Dimension(300, 300));
return panel;
}
There is a GridBagConstraints constructor setting all fields. Not so readable here however.
It is also a quite error prone layout.
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
GridBagLayout is not working as expected. I can not find where I'm making the mistake. I know that somewhere I'm doing some silly mistake but can't find it.
Here I have made 3 buttons, 4 text fields and 3 combo boxes, and I want to align them in proper order using GridBagLayout.
Code is as follows :
GridBagConstraints c = new GridBagConstraints();
JPanel secondPanel = new JPanel();
f=new JFrame("Server Side");
b=new JButton("3DES ALGO");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 0;
secondPanel.add(b, c);
b1=new JButton("Browse");
c.gridx = 2;
c.gridy = 0;
secondPanel.add(b1, c);
b2=new JButton("Color");
c.gridx = 0;
c.gridy = 0;
secondPanel.add(b2, c);
comboBox1 = new JComboBox(s);
c.gridx = 0;
c.gridy = 1;
secondPanel.add(b, c);
comboBox2 = new JComboBox(s);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
secondPanel.add(b, c);
comboBox3 = new JComboBox(s);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 1;
secondPanel.add(b, c);
b10=new JButton("OK");
tf=new TextField();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 1;
secondPanel.add(tf, c);
tf1=new TextField();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
secondPanel.add(tf1, c);
tf2=new TextField();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 2;
secondPanel.add(tf2, c);
tf3=new TextField();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 2;
secondPanel.add(tf3, c);
b.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b.setBounds(200,0,330,50);
b1.setBounds(500,100,90,30);
b2.setBounds(0,0,70,30);
Container content = f.getContentPane();
content.setBackground(Color.RED);
b.setForeground(Color.white);
b.setBackground(Color.black);
b1.setForeground(Color.white);
b1.setBackground(Color.black);
b2.setForeground(Color.white);
b2.setBackground(Color.black);
tf.setBackground(Color.CYAN);
tf1.setBackground(Color.GRAY);
tf1.setForeground(Color.white);
tf2.setBackground(Color.GRAY);
tf2.setForeground(Color.white);
tf3.setBackground(Color.GRAY);
tf3.setForeground(Color.white);
f.add(b);
f.add(b1);
f.add(b2);
secondPanel.setLayout(new GridBagLayout());
secondPanel.add(comboBox1);
secondPanel.add(comboBox2);
secondPanel.add(comboBox3);
f.add(tf);
f.add(tf1);
f.add(tf2);
f.add(tf3);
f.add(secondPanel);
f.setLayout(new GridBagLayout());
f.addWindowListener(new WindowEventListener());
f.pack();
f.setVisible(true);
The grids are 0 based, so your basic code would be:
c.gridx = 0;
c.gridy = 0;
panel.add(b1, c);
c.gridx = 1;
panel.add(b2, c);
c.gridx = 2;
panel.add(b3, c);
c.gridx = 0;
c.gridy = 1;
panel.add(combo1, c)
c.gridx = 1;
panel.add(combo2, c);
...
I'm pretty sure you need to set the layout to a GridBagLayout before adding your components to secondPanel.
I also see that you add your JComboBoxes without any constraints at all, which almost certainly is not what you want to do.
Also, I see you are calling setBounds. Don't do that. It is a LayoutManager's job to set child components' bounds, not yours.
Although the documentation isn't as clear as it could be, GridBagConstraints is a lot easier to use when you make use of GridBagConstraints.RELATIVE and GridBagConstraints.REMAINDER. gridx and gridy are set to RELATIVE by default, probably because it's usually the easiest use case.
I find my code is easiest to read if I create all of my components first, then add them to a layout.
So, if you want to lay out your buttons and fields in three rows, you could do this:
b = new JButton("3DES ALGO");
b1 = new JButton("Browse");
b2 = new JButton("Color");
b.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
comboBox1 = new JComboBox(s);
comboBox2 = new JComboBox(s);
comboBox3 = new JComboBox(s);
tf = new JTextField(10);
tf1 = new JTextField(10);
tf2 = new JTextField(10);
tf3 = new JTextField(10);
GridBagConstraints c = new GridBagConstraints();
JPanel secondPanel = new JPanel(new GridBagLayout());
c.fill = GridBagConstraints.HORIZONTAL;
// Components are easier to see and use if there is some space between them.
c.insets.right = 6;
c.insets.bottom = 6;
// At this point, c.gridx and c.gridy are both GridBagConstraints.RELATIVE.
// c.gridwidth and c.gridheight are 1.
secondPanel.add(b, c);
secondPanel.add(b1, c);
// A gridwidth of REMAINDER means "This is the last component on this row."
c.gridwidth = GridBagConstraints.REMAINDER;
secondPanel.add(b2, c);
// Since the last component was added with a width of REMAINDER, the next
// component is automatically on the next row, as long as gridx and gridy
// are still set to GridBagConstraints.RELATIVE.
c.gridwidth = 1;
secondPanel.add(tf, c);
secondPanel.add(tf1, c);
secondPanel.add(tf2, c);
c.gridwidth = GridBagConstraints.REMAINDER;
secondPanel.add(tf3, c);
c.gridwidth = 1;
secondPanel.add(comboBox1, c);
secondPanel.add(comboBox2, c);
c.gridwidth = GridBagConstraints.REMAINDER;
secondPanel.add(comboBox3, c);