Java GUI - Positioning - java

I am trying to position two buttons to be on the left top side. They are always in the center top though.
I have tried this:
jp = new JPanel();
jp.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//c.anchor = GridBagConstraints.BASELINE_TRAILING;
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 0;
jp.add(test, c);
c.gridy++;
jp.add(atest, c);
add(jp);
But its still at center, not on left side (http://i.imgur.com/MYF8dqr.png).
This is an image I took. The red is a scetch of how I wish the buttons to be.
Updated:
ArrayList<String> atest = new ArrayList<String>();
JLabel[ ] asd = new JLabel[100];
int temp = 0;
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.FIRST_LINE_START;
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 0;
atest.add("Hello");
atest.add("haelp");
atest.add("yummy");
atest.add("whats wrong");
for(String server : servers)
{
asd[temp] = new JLabel();
asd[temp].setText(server);
jp.add(asd[temp], c);
c.weighty++;
c.gridy++;
temp++;
}
Im trying to read string from array and add it as label one after other on left side.
Doesn't work out too good, here's the result:
http://prntscr.com/26a9rb
If gridbaglayout is bad way of doing it, which way should I go for?

Read the section from the Swing tutorial on How to Use GridBagLayout for an explanation of how the contraints work. The section on weightx, weighty should solve your problem.

If you are using Eclipse you can use a plugin called WindowBuilder to build the frames:
http://www.eclipse.org/windowbuilder/
You have to apply spring layout to your window in WindowBuilder to drag and drop buttons from the toolbar like in the .NET designer in VisualStudio.

To solve your problem:
set the weightx and weighty of GridBagConstraint to some other value than 0 as #camickr mentioned.
you should set the anchor to FIRST_LINE_START. The WEST anchor will put the component on the left side of its display area but the components will be centered vertically. It is equivalent to LINE_START for horizontal, left-to-right orientations of components
Edit:
Try to look into the BoxLayout. Cleaver use of preferredSize with alignment setting can achieve what you are expecting. I have written a SSCCE for you. However, reading about the size behavior with BoxLayout will help you understand the example better.
class AComponent extends JPanel
{
public AComponent(Color c) {
setBackground(c);
setAlignmentX(LEFT_ALIGNMENT);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 100);
}
#Override
public Dimension getMinimumSize() {
return new Dimension(getPreferredSize());
}
#Override
public Dimension getMaximumSize() {
Dimension dim = getPreferredSize();
return new Dimension(200, dim.height);
}
}
class MyWindow extends JFrame
{
public MyWindow ()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel container = new JPanel()
{
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 350);
}
};
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
container.setBackground(Color.WHITE);
container.add(new AComponent(new Color(0xFFAA00)));
container.add(new AComponent(new Color(0x359DBD)));
container.add(new AComponent(new Color(0xFFD47E)));
add(container);
pack();
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new MyWindow().setVisible(true);
}
});
}
}

You need to set jp like this: jp.setLayout(null);

Related

Setting Size is not overriding on Java

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.

GridBagLayout - GridBagConstraints not working

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.

GridBagLayout Horizontal alignment

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);
}

Java - I cannot find values that would work

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?

How to place components beneath tabs in right oriented JTabbedPane

So I just stumbled across placement of tabs in a JTabbedPane to the right and left (i.e. setTabPlacement(JTabbedPane.RIGHT)) which I love the look of. What I need is to utilize the space this leaves beneath the tabs. I currently have a column of JButtons, but they get pushed to the side, leaving a lot of blank space.
Any thoughts on how to do this? Some kind of custom overlay or something?
Here's a screenshot. In the code I basically have one horizontally aligned Box, with the JTabbedPane over a JTree, then the column of buttons after that.
boxOfEverything.add(tabbedPane);
boxOfEverything.add(boxColumnButtons);
Screenshot here.
I made this community wiki because this answer is not mine. #cheesecamera seems to have posted the same question on another forum and got an answer there. I copied the answer so that people coming here looking for an answer can get an answer.
The idea is to use swing's glasspane.
import java.awt.*;
import javax.swing.*;
public class RightTabPaneButtonPanel {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new RightTabPaneButtonPanel().makeUI();
}
});
}
public void makeUI() {
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setTabPlacement(JTabbedPane.RIGHT);
JPanel panel = new JPanel(new GridLayout(0, 1));
for (int i = 0; i < 3; i++) {
JPanel tab = new JPanel();
tab.setName("tab" + (i + 1));
tab.setPreferredSize(new Dimension(400, 400));
tabbedPane.add(tab);
JButton button = new JButton("B" + (i + 1));
button.setMargin(new Insets(0, 0, 0, 0));
panel.add(button);
}
JFrame frame = new JFrame();
frame.add(tabbedPane);
frame.pack();
Rectangle tabBounds = tabbedPane.getBoundsAt(0);
Container glassPane = (Container) frame.getGlassPane();
glassPane.setVisible(true);
glassPane.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.NONE;
int margin = tabbedPane.getWidth() - (tabBounds.x + tabBounds.width);
gbc.insets = new Insets(0, 0, 0, margin);
gbc.anchor = GridBagConstraints.SOUTHEAST;
panel.setPreferredSize(new Dimension((int) tabBounds.getWidth() - margin,
panel.getPreferredSize().height));
glassPane.add(panel, gbc);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

Categories

Resources