Iam trying to find out how GridBagLayout works,because i have had never used it. I will post source code and picture of that gui. i have problem with components to allign. i want to put button to be in the middle of two components(on one side checkboxgroup and on the other is JTextArea. and i managed but it is alligned in the bottom and i want to allign it to be in the middle of the height of both components.
public class GUI extends JFrame {
public GUI(){
setSize(600,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setTitle("ridBagLayout[![enter image description here][1]][1]");
JPanel panel=new JPanel(new GridBagLayout());
GridBagConstraints c=new GridBagConstraints();
String[]niz={"C Sharp","Java","PHP","VisualBasic"};
c.anchor=GridBagConstraints.PAGE_START;
c.weighty=1.0;
c.gridx=0;
c.gridy=0;
c.insets=new Insets(30,30,0,0);
panel.add(new JComboBox(niz),c);
c.gridx=1;
//c.insets=new Insets(10,0,0,0);
panel.add(new JButton("Get drop down item"),c);
c.gridx=2;
c.ipadx=20;
panel.add(new JTextField(15),c);
JCheckBox visualBasic=new JCheckBox("Visual Basic");
JCheckBox cSharp=new JCheckBox("C Sharp");
JCheckBox java=new JCheckBox("Java");
JCheckBox php=new JCheckBox("PHP");
JPanel checkbox=new JPanel();
checkbox.setLayout(new GridLayout(4,0,5,5));
checkbox.add(visualBasic);
checkbox.add(cSharp);
checkbox.add(java);
checkbox.add(php);
javax.swing.border.Border raisedetched = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
checkbox.setBorder(raisedetched);
c.insets=new Insets(0,20,0,0);
c.gridx=0;
c.gridy=1;
panel.add(checkbox,c);
c.gridx=1;
c.ipadx=30;
c.anchor=GridBagConstraints.CENTER;
panel.add(new JButton("Selected Item"),c);
c.gridx=2;
c.gridy=1;
c.ipadx=20;
c.anchor=GridBagConstraints.NORTH;
panel.add(new JTextArea(7,15),c);
add(panel);
setVisible(true);
}
}
GridBagLayout is one of the most powerful layouts to use in Java, but it requires some nesting in order to obtain a good result.
In your case, maybe you need to put that button inside a panel and set its alignments properties to CENTER.
There is lot of documentation about GridBagLayout but another way to see how it works could be to create a new project in an IDE like NetBeans and create that GUI through its graphical editor and see the generated code.
Related
In most of the GUI programs, when the user resizes it, the components of the program, such as text fields, buttons, etc. tend to increase or decrease their size depending on the decisions of the user. I'm trying to implement this idea into my GUI program. I'm a little bit lost about how I can do it. By the way, I created my program without the usage/help of the Eclipse Swing or Netbeans' GUI.
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
public class CodeReviewerFrame extends JFrame {
EditorAreaPanel display = new EditorAreaPanel();
// FileOptionsPanel fileOptionsPanel = new FileOptionsPanel( display );
JPanel p = new JPanel();
JPanel panel = new JPanel();
public CodeReviewerFrame(String title) throws IOException {
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(1500, 1000));
setLayout(new BorderLayout());
ImageIcon img = new ImageIcon("icon.png");
setIconImage(img.getImage());
p.setLayout(new BorderLayout());
p.add(new HomeOptionsPanel(display), BorderLayout.LINE_START);
p.add(new NewCommentPanel(display), BorderLayout.CENTER);
p.add(new CommentOptionsPanel(display), BorderLayout.LINE_END);
add(p, BorderLayout.PAGE_START);
panel.setLayout(new BorderLayout());
panel.add(new FileExplorerPanel(), BorderLayout.LINE_START);
panel.add(new FileOptionsPanel(display), BorderLayout.CENTER);
panel.add(new CommentShowPanel(display), BorderLayout.LINE_END);
add(panel, BorderLayout.CENTER);
pack();
setResizable(true);
setVisible(true);
/**
* Everything Under This is experimental
*/
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
//add ( fileOptionsPanel, constraints );
}
}
There are components on each Panel that have been added, such as buttons in HomeOptionsPanel, huge JTextArea in the center of FileOptionsPanel plus four buttons up on the JTextArea, etc. Should I use new Layout type, or commands known as "repaint/revalidate," or implement changeListener? And should I only implement the code to the JFrame, or do it for each of the JPanels?
The behaviour of your UI upon window resizing depends (also) on the Layout Manager you are using.
Some Layout Managers (like BorderLayout) resize the components when the windows is resized, while others (like FlowLayout) don't.
It is not clear what LM you are using inside your panels, but most likely your issue stands in there.
analysisProgressBar = new JProgressBar();
analysisProgressLabel = new JLabel("Analysing...");
add(analysisProgressBar, BorderLayout.NORTH);
add(analysisProgressLabel, BorderLayout.CENTER);
How can I add JProgressBar and JLabel vertically in JToolBar?
I want it to look like this:
[==== ]
Analysing...
How it looks now:
It's easy
JPanel p = new JPanel(new BorderLayout());
p.add(analysisProgressBar, BorderLayout.NORTH);
p.add(analysisProgressLabel, BorderLayout.CENTER);
toolbar.add(p);
If you want more control over the layout, maybe consider using a GridBagLayout
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(analysisProgressBar, gbc);
add(analysisProgressLabel, gbc);
this may generate a better result when the container is resized
Have a look at A Visual Guide to Layout Managers, Laying Out Components Within a Container and How to Use GridBagLayout
I have a box layout that has a JLabel and a button next to it, as such:
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
panel.add(new JLabel("Reference "));
panel.add(new JButton("HI"));
frame.add(panel);
frame.setSize(500,300);
frame.setVisible(true);
This correctly presents a label called reference and a button right next to it. But if I want to present the same thing right below it (a new label, and another button), how would I do that?
Because simply creating another panel, and emulating what I did before, doesn't seem to work.
I.e
JPanel newPanel = new JPanel();
newPanel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
Or using HTML in a new label like
JLabel s = new JLabel("<html> <br>newLaberl </html>");
adding this to the panel still would print it on the same line, after the button, not the next, any ideas?
You just have to set the layout of the JFrame get it working the way you said.
I used your example setting the frame layout to use a GridLayout
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
frame.setLayout(new GridLayout(2,1));
panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
panel.add(new JLabel("Reference "));
panel.add(new JButton("HI"));
panel2.setLayout(new BoxLayout(panel2,BoxLayout.X_AXIS));
panel2.add(new JLabel("Reference2 "));
panel2.add(new JButton("HI2"));
frame.add(panel);
frame.add(panel2);
frame.setSize(500,300);
frame.setVisible(true);
boxlayout isn't maybe the best layout for such thing, i suggest you to go to
https://docs.oracle.com/javase/tutorial/uiswing/layout/layoutlist.html
and take a look on the various layout and tutorial documented on Oracle's docs website.
this one could be usefull for your application
https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
I am trying to design a layout which contains a form and couple of items. but I found it too hard to put items in right places.
In the following image, the right frame is what I am aiming to design and the left on is what I could made.
And this is the code for the right frame:
public class GUI extends JFrame{
public GUI(){
JFrame frame = new JFrame("frame");
frame.setSize(600, 600);
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JLabel("Title"), BorderLayout.NORTH);
JPanel formPanel = new JPanel(new GridLayout(1,2));
panel.add(formPanel);
TitledBorder formPanelTitle = BorderFactory.createTitledBorder("GridLayout(1,2)");
formPanel.setBorder(formPanelTitle);
//LEFT PANEL
JPanel labelsPanel = new JPanel(new GridLayout(4,1));
TitledBorder labelsPanelTitle = BorderFactory.createTitledBorder("GridLayout(4,1)");
labelsPanel.setBorder(labelsPanelTitle);
labelsPanel.add(new JLabel("Label 1"));
labelsPanel.add(new JLabel("Label 2"));
labelsPanel.add(new JLabel("Label 3"));
labelsPanel.add(new JLabel("Label 4"));
formPanel.add(labelsPanel);
//RIGHT PANEL
JPanel fieldsPanel = new JPanel(new GridLayout(4,1));
TitledBorder fieldsPanelTitle = BorderFactory.createTitledBorder("GridLayout(4,1)");
fieldsPanel.setBorder(fieldsPanelTitle);
fieldsPanel.add(new JTextField("Label 1"));
fieldsPanel.add(new JTextField("Label 2"));
fieldsPanel.add(new JTextField("Label 3"));
fieldsPanel.add(new JTextField("Label 4"));
formPanel.add(fieldsPanel);
//BOTTOM PANEL
JPanel bottomPanel = new JPanel(new GridLayout(2,1));
TitledBorder BottomPanelTitle = BorderFactory.createTitledBorder("GridLayout(2,1)");
bottomPanel.setBorder(BottomPanelTitle);
panel.add(bottomPanel, BorderLayout.SOUTH);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(new JButton("Browse"));
buttonPanel.add(new JLabel("Label"));
TitledBorder buttonPanelTitle = BorderFactory.createTitledBorder("FlowLayout()");
buttonPanel.setBorder(buttonPanelTitle);
bottomPanel.add(buttonPanel);
JPanel secondButtonPanel = new JPanel(new GridLayout(1,2));
secondButtonPanel.add(new JButton("Back"));
secondButtonPanel.add(new JButton("Next"));
TitledBorder secondButtonPanelTitle = BorderFactory.createTitledBorder("GridLayout(1,2)");
secondButtonPanel.setBorder(secondButtonPanelTitle);
bottomPanel.add(secondButtonPanel);
frame.add(panel);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
// TODO code application logic here
new GUI();
}
}
I am not sure if the code is really optimal, since there are a lot of inner panels and made it too complicated. Also I could not put items in the places I wanted to. Is there any suggestion or idea to make this layout look better?
Create a JPanel, using GridBagLayout and add your labels/fields to it, this forms the "center" portion of your layout.
Create a JPanel and add the Browse button a JLabel to it. Using GridBagConstraints#gridwidth set to REMAINDER, add this to your first panel
Create a JPanel, using BorderLayout, add the first panel to the CENTER position. Add the title Label to the NORTH position, you may need to adjust it's horizontalAlignment property
Create a JPanel using FlowLayout, aligned to the RIGHT and add your "Back" and "Next" buttons to it. Add this to the SOUTH position of the previous panel.
Check out Laying Out Components Within a Container for more details
I can't make the table span up to the JFrame border. I tried using setMinimumSize but it didn't work. What am I missing?
ADDED: I'm not interested in adding JScrollPane to the table (yet). I just want to know exactly how to make GridBagLayout resize a particular component to JFrame's borders.
public class Ost extends JFrame{
OstBridge bridge;
public Ost() {
Container cp=this.getContentPane();
GridBagConstraints c=new GridBagConstraints();
cp.setLayout(new GridBagLayout());
// Add button
JButton b1=new JButton("Button");
c.gridx=0;
c.gridy=0;
c.insets=new Insets(20,0,20,0);
cp.add(b1,c);
// Table data
String[] columns={"Album","Url"};
Object[][] data={
{"test1","tes2"}
};
// Add Table
JTable table=new JTable(data,columns);
c.gridx=0;
c.gridy=1;
c.weightx=1;
c.weighty=1;
c.gridwidth=c.REMAINDER;
table.setBackground(Color.RED);
table.setMinimumSize(new Dimension(400,400));
cp.add(table, c);
// Show All
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(320, 240);
this.setTitle("Test");
this.setVisible(true);
}
This is what I get:
And this is what I would like to get:
In the constraints for the table I should've used GridBagConstraints.fill:
// Add Table
JTable table=new JTable(data,columns);
c.gridx=0;
c.gridy=1;
c.weightx=1;
c.weighty=1;
c.gridwidth=c.REMAINDER;
c.fill=GridBagConstraints.HORIZONTAL; // this line solves the problem
table.setBackground(Color.RED);
table.setMinimumSize(new Dimension(400,400));
cp.add(table, c);
Hope this answer helps someone else who's trying to give firsts steps into java as me! :)