I'm trying to use GridBagLayout but the GridBagConstraints objects doesn't show any effect. I want a button to fill the horizontal space. Any Ideas?
static class Five extends JFrame {
public Five() {
setSize(300, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
JButton button = new JButton("Long-Named Button 4");
add(button, c);
setVisible(true);
}
This works, details in comments:
import java.awt.*;
import javax.swing.*;
public class Five extends JFrame {
public Five() {
setSize(300, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// best to do all important stuff in a panel added to the frame
JPanel gui = new JPanel(new GridBagLayout());
setContentPane(gui);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1d; // fixes the problem
JButton button = new JButton("Long-Named Button 4");
add(button, c);
pack(); // should always be done after all components are added
setVisible(true);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
new Five();
}
};
SwingUtilities.invokeLater(r);
}
}
Further tips:
There is no good case for extending JFrame in this case, just do the relevant additions etc. to an instance of a standard frame.
To make the button larger, set a large icon, large insets, or large font. To make the frame bigger, add an EmptyBorder around the gui panel.
Related
I am having issue where my JPanel is not setting up the size. I am not sure if is something to do with my JTab or JFrame. I am using GridBagLayout layout management. And for some reason are not able to set the size.
Here is a dummy code, following the same logic to my original source code:
FirstPanel.java
import javax.swing.*;
import java.awt.*;
public class FirstPanel extends JPanel {
private JLabel label1 = new JLabel("Label 1");
private JTextField textField1 = new JTextField();
private GridBagConstraints c = new GridBagConstraints();
public FirstPanel() {
//Size is not overriding
Dimension size = getPreferredSize();
size.width = 100;
setPreferredSize(size);
setBorder(BorderFactory.createTitleBorder("Border Title");
setLayout(new GridBagLayout());
addComponents();
}
private void addComponents() {
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.NORTHWEST;
c.insets = new Insets(5, 0, 0, 0);
add(label1, c);
c.gridx = 1;
add(textField1, c);
c.weightx = 1;
c.weighty = 1;
add(new JLabel(""), c);
}
}
MainPanel.java
import javax.swing.*;
import java.awt.*;
public class MainPanel {
private JFrame frame = new JFrame("App");
private JPanel panel1 = new JPanel(new GridBagLayout());
private GridBagConstraints c = new GridBagConstraints();
private JTabbedPane tabPane = new JTabbedPane();
public MainPanel() {
addComponents();
frame.add(tabPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 350);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
}
private void addComponents() {
tabPane.addTab("Tab 1", new FirstPanel());
}
}
Main.java
public class Main {
public static void main(String[] args) {
new MainPanel();
}
}
Or at least have two JPanels,
Exactly.
Frist you create a main panel using a BorderLayout that you add to the tabbed pane.
Then you have a second panel for your labels and text fields (using whatever layout manager you want). Then you add this panel to the BorderLayout.LINE_START.
Then you add your scrollpane containing the JTable to the BorderLayout.CENTER of the main panel.
Read the tutorial on Layout Manager. Nest panels with different layout managers as required.
want to have JTable taking 50% of the other side.
Picking a random number like 50% is not the way to design a GUI. What happens if the frame is made smaller/larger. What happens to the space? Design the layout with flexibility in mind, just like your browser window is designed. There are always fixed areas where the size is determined by the components added and there is a flexible area that grows/shrinks as desired.
How can i align everything to the center of the frame ? In my example code, the JLabel doesnt occupies the same % of space as the button.Its about 10% label and 90% button. How can i make them both have the same amount of space ? This is my code:
class Animation extends JPanel {
JLabel lab = new JLabel("A");
JButton but = new JButton("BUTTTTTTTOOOOOOOOOOOOON");
GridBagConstraints c = new GridBagConstraints();
public Animation(){
setLayout(new GridBagLayout());
c.gridx = 0 ;
c.gridy = 0;
add(lab,c);
c.gridx = 1;
add(but, c);
}
public static void main(String[] args) {
JFrame fram = new JFrame();
fram.add(new Animation());
fram.pack();
fram.setVisible(true);
}
}
Put the two of them into a 2x1 GridLayout -- it forces the same dimensions on each cell.
public Animation()
{
setLayout(new GridLayout());
add(lab);
add(but);
}
I was wondering if I could get some help on my application. I can not find the right values without stuffing everything up. Its using GridBagConstraint, JPanel, JScrollPane (Doesn't Work on one of the panels), JButton, JTabbedPane and JTextArea. The relevant code that is stuffing the display up is down below. TabBar and FileViewer extends of JPanel and Window extends of JFrame;
My init for the tabs and how I add tabs. (This isn't the JScrollPane stuffup part)
private TabBarComponent() {
super(new GridLayout(0, 1, 5, 0));
instance = this;
tabbedPane = new JTabbedPane();
add(tabbedPane);
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
public void addBar(String text, JScrollPane s) {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(s);
tabbedPane.addTab(text, panel);
}
This is the JScrollPane stuff up one... right now it doesn't bother me but will eventually. I add to the panel by panel.add(button).
public FileViewerComponent() {
super(new GridLayout(0, 1, 5, 0));
instance = this;
panel = new JPanel();
panel.setLayout(new GridLayout(0, 1, 5, 0));
scrollArea = new JScrollPane(panel);
addButtons();
add(scrollArea);
}
Here is where I create the Window. This is where the help is required. I don't know what values I should set. I have looked at the documentation and how the classes work.
private WindowComponent() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
System.exit(1);
}
setSize(new Dimension(1024, 900));
setLocationRelativeTo(null);
setTitle("GridBagConstraints");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
setJMenuBar(new MenuBarComponent());
c.gridx = 0;
c.gridy = 0;
c.weightx = 0.5;
c.weighty = 1;
c.anchor = GridBagConstraints.NORTHWEST;
add(FileViewerComponent.getInstance(), c);
c = new GridBagConstraints();
c.gridwidth = 2;
c.gridx = 1;
c.gridy = 0;
c.weightx = 0.5;
c.weighty = 1;
c.anchor = GridBagConstraints.NORTHEAST;
add(TabBarComponent.getInstance(), c);
addWindowListener(this);
setVisible(true);
}
Thank you for the help I hope to get.
http://imgur.com/a/JJ1kX#0
The first picture is original size, the second is when I make the window smaller. It works fine when I enlarge the window.
EDIT: Is there an API part from the one created by java that I could use or a tool I could use?
I am trying to make an application in java, to start with i have problems in the GUI.
I have put Jpanel inside Jframe ,but i am gettingf problems when i use setMaximumSize and moreover i want to fix the size of the Jpanel, so that even if user tries to change the size of the window , jpanel remains at center.
i have tried this a solution at StackOverflow
How can I properly center a JPanel ( FIXED SIZE ) inside a JFrame?
Please guide me through.This is my first post, i dont have enough reputations to post an image .Thanks
import javax.swing.*;
import java.awt.*;
class App_Demo4 extends JFrame {
public App_Demo4()
{
JFrame frm=new JFrame("Application");
JPanel pane=new JPanel();
frm.setVisible(true);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setLayout(new BorderLayout());
frm.add(pane,BorderLayout.CENTER);
pane.setSize(400,400);
Dimension dim=new Dimension(400,400);
pane.setMinimumSize(dim);
Box box = new Box(BoxLayout.Y_AXIS);
box.add(Box.createVerticalGlue());
box.add(pane);
box.add(Box.createVerticalGlue());
frm.add(box);
frm.setSize(500,500);
JButton dbtn1=new JButton("Download File");
pane.add(dbtn1);
JTextField txt1=new JTextField(20);
pane.add(txt1);
JButton bbtn1=new JButton("Browse");
pane.add(bbtn1);
JButton dbtn2=new JButton("Download Mail");
pane.add(dbtn2);
JTextField txt2=new JTextField(20);
pane.add(txt2);
JButton bbtn2=new JButton("Browse");
pane.add(bbtn2);
JButton cbtn1=new JButton("Compile File");
pane.add(cbtn1);
JTextField txt3=new JTextField(20);
pane.add(txt3);
JButton bbtn3=new JButton("Browse");
pane.add(bbtn3);
JButton cbtn2=new JButton("Cancel");
pane.add(cbtn2,BorderLayout.EAST);
}
public static void main(String[] args)
{
new App_Demo4();
}
}
Don't try to set the size of a panel. That is the job of the layout manager.
You need to change the layout manager of your panel. By default a JPanel uses a FlowLayout. In this case the components just flow to a new line depending on the width of the frame.
You might want to look at a GridBagLayout. See the section from the Swing tutorial on How to Use a GridBagLayout for more information and examples.
Also, make sure you add all the components to the frame BEFORE packing the frame and making the frame visible. So you basic code should be:
frame.add(....)l
frame.pack();
frame.setVisible(true);
Thats how you do it :
Hope it helps...
import javax.swing.*;
import java.awt.*;
public class App_Demo4 extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public App_Demo4()
{
super("Application");
JPanel pane=new JPanel();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(500,500));
GridBagLayout gl = new GridBagLayout();
pane.setLayout(gl);
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1;
c.weighty = 1;
c.anchor = GridBagConstraints.CENTER;
c.fill = GridBagConstraints.NONE;
c.insets = new Insets(6, 6, 6, 6);
c.gridx = 0;
c.gridy = 0;
JButton dbtn1=new JButton("Download File");
pane.add(dbtn1, c);
JTextField txt1=new JTextField(20);
c.gridx = 1;
pane.add(txt1, c);
JButton bbtn1=new JButton("Browse");
c.gridx = 2;
pane.add(bbtn1, c);
JButton dbtn2=new JButton("Download Mail");
c.gridy = 1;
c.gridx = 0;
pane.add(dbtn2, c);
JTextField txt2=new JTextField(20);
c.gridx = 1;
pane.add(txt2, c);
JButton bbtn2=new JButton("Browse");
c.gridx = 2;
pane.add(bbtn2, c);
c.gridy = 2;
c.gridx = 0;
JButton cbtn1=new JButton("Compile File");
pane.add(cbtn1, c);
JTextField txt3=new JTextField(20);
c.gridx = 1;
pane.add(txt3, c);
JButton bbtn3=new JButton("Browse");
c.gridx = 2;
pane.add(bbtn3, c);
c.gridy = 3;
c.gridx = 2;
JButton cbtn2=new JButton("Cancel");
pane.add(cbtn2,c);
this.add(pane);
this.pack();
this.setVisible(true);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new App_Demo4();
}
});
}
}
I'm using a JTable and adding it to a panel which uses a gridbaglayout like so:
JTable qdbs = new JTable(rowData, columnNamesVector);
qdbs.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
panel.add(qdbs, c);
I don't want the table to be in a scroll pane, but I do want the table to take up the entire width of the panel. How would I accomplish this?
An SSCCE as requested:
import javax.swing.*;
import java.awt.*;
public class Main{
public static void main(String[] args) {
new TestFrame();
}
public static class TestFrame extends JFrame{
public TestFrame() {
this.setTitle("SSCCE");
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(10,10,10,10);
JTable testTable = new JTable(10,2);
panel.add(testTable, c);
this.add(panel);
this.pack();
this.setVisible(true);
}
}
}
I would like this table to always take up the entire width of the panel (except the insets). Currently the table does not change size when the frame is resized.
You need to add constrains to tell the layout what to do with more space. In your SSCCE add these items:
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0;
import java.awt.*;
import javax.swing.*;
public class Test
{
public static void main(String args[]) throws Exception
{
JPanel panel = new JPanel( new GridBagLayout() );
JTable table = new JTable(5, 5);
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(table, gbc);
JFrame frame = new JFrame();
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
If you need more help post a SSCCE that demonstrates the problem.
c is a GridBagConstraint or something along those lines, I imagine? The very simplest thing to do would be to set the LayoutManager of the JPanel to a BorderLayout, then just add with the constraint BorderLayout.CENTER .
hmmm
Alex Bliskovsky wrote panel.add(qdbs, c);
that's wrong, not, never do that, you are forgot wrap you JTable to the ScrollPane and then you can play with some of LayoutManagers, for related examples for LayoutManagers check GridBagConstraints for GrigBagLayout
The following frame will correctly resize the table when the frame is resized.
public class Sandbox {
public static void main(String[] args) {
new TestFrame();
}
public static class TestFrame extends JFrame {
public TestFrame() {
this.setTitle("SSCCE");
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(10, 10, 10, 10);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 1;
JTable testTable = new JTable(10, 2);
panel.add(testTable, c);
this.add(panel);
this.pack();
this.setVisible(true);
}
}
}
Perhaps with:
GridBagConstraints.fill = GridBagContraints.BOTH;