I have a horizontal panel with 3 buttons: Back, Next and Cancel. I've made horizontal alignment to the right, but only Next and Cancel buttons follow this alignment.
Here is the code:
HorizontalPanel buttons = new HorizontalPanel();
buttons.setWidth("100%");
cancel = new Button("Cancel");
next = new Button("Next");
back = new Button("Back");
buttons.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
buttons.add(back);
buttons.setCellWidth(back, "98%");
buttons.add(nextBtn);
buttons.setCellWidth(next, "1%");
buttons.add(cancel);
buttons.setCellWidth(cancel, "1%");
The problem is with this panel width. But if I delete it, all my buttons are left aligned. As a workaround I can use false button, that will be invisible and has cell width = 97%, the cell width of back button is 1%. But this is not what I need, because inspite of small amount of memory this invisible button consumes, it is still a memory :)
What is going wrong with this alignment? Maybe something is wrong with panel width? Hope for your help, thank you in advance.
There is setCellHorizontalAlignment() property in HorizontalPanel. With it you can give alignment to its widgets.
Try following:
buttons.setCellHorizontalAlignment(backButton, HasHorizontalAlignment.ALIGN_RIGHT);
buttons.setCellHorizontalAlignment(nextButton, HasHorizontalAlignment.ALIGN_RIGHT);
buttons.setCellHorizontalAlignment(cancelButton, HasHorizontalAlignment.ALIGN_RIGHT);
You can achieve this much easier using css:
FlowPanel panel = new FlowPanel();
panel.setWidth("100%");
cancel = new Button("Cancel");
cancel.getElement().getStyle().setFloat(Float.RIGHT);
next = new Button("Next");
next.getElement().getStyle().setFloat(Float.RIGHT);
back = new Button("Back");
back.getElement().getStyle().setFloat(Float.RIGHT);
panel.add(back);
panel.add(nextBtn);
panel.add(cancel);
Related
I have a very big problem. I need to do vertical menu in the center of window. What could be easier? What I did:
I create JFrame and set BorderLayout to it:
JFrame jfr = new JFrame("Frame");
Then I create 4 buttons:
JButton b1 = new JButton("b1");
JButton b2 = new JButton("b2");
JButton b3 = new JButton("b3");
JButton b4 = new JButton("b4");
I created panel and add all buttons to panel:
JPanel jpan = new JPanel();
jpan.setLayout(new BoxLayout(jpan, BoxLayout.Y_AXIS));
jpan.add(b1);
jpan.add(b2);
jpan.add(b3);
jpan.add(b4);
Aligned all buttons
b1.setAlignmentX(JComponent.CENTER_ALIGNMENT);
b2.setAlignmentX(JComponent.CENTER_ALIGNMENT);
b3.setAlignmentX(JComponent.CENTER_ALIGNMENT);
b4.setAlignmentX(JComponent.CENTER_ALIGNMENT);
And add panel to JFrame
jfr.add(jpan, BorderLayout.CENTER);
Please help me to understand this layouts!
Only say like this: "You should use this, when you use this layout"
And now main question: How can I change size of buttons?
There are a number of easy ways to change the size of buttons:
Give the buttons an icon of a different size.
Make the font of the buttons a different size.
Set more / less space between the button contents (the icon and text) and the border of the button.
Give the buttons more / less text.
Given the last is quite arbitrary, here is a demonstration of the first 3 techniques:
I am trying to make a 2x2 grid layout that has a JLabel on the top left, and three buttons on the other three spaces. When I do this, I get the unexpected result of one big button (filling up the entire JDialog) that says "Do you want to push me". I don't know why this result shows up, please help, Thanks!
public void sinceyoupressedthecoolbutton() {
JDialog replacementwindow = new JDialog(); //Like a window
JButton best = new JButton("best");
JButton first = new JButton("FIRST");
JButton second = new JButton("Second");
replacementwindow.setLayout(new GridLayout(2,3,0,0)); //Row, column, distance horizontally, distance vertical
JPanel panel = new JPanel();
replacementwindow.add(panel); //adding the JPanel itself
replacementwindow.add(first);
replacementwindow.add(second);
replacementwindow.add(best);
replacementwindow.setSize(500, 500);
replacementwindow.setTitle("NEW WINDOW!");
replacementwindow.setVisible(true);
}
It's because you set the layout of your JButton, and not of your JDialog
Change
label.setLayout(new GridLayout(2,2,0,0));
to
YES.setLayout(new GridLayout(2,2,0,0));
Also, your variable called label is a JButton, you probably want to change that.
Don't add components to a button. You add components to a panel.
So the basic code should be:
JDialog dialog = new JDialog(...);
JPanel panel = new JPanel( new GridLayout(...) );
panel.add(label);
panel.add(button1);
...
dialog.add(panel);
Also, variable names should NOT start with an upper case character! "Yes" does not follow Java standards. The other variables do. Be consistent!
I've been working on some app's using libgdx & I can't figure out how to position a button via coords.
public void create() {
batch = game.getSpriteBatch();
background = new Texture("data/textures/menuScreen_small.png");
bubbleArray = new Array<Bubble>();
Gdx.input.setInputProcessor(stage);
for (int i = 0; i < 20; i++) {
bubbleArray.add(new Bubble(MathUtils.random(0,
Gdx.graphics.getWidth()), MathUtils.random(0,
Gdx.graphics.getHeight())));
}
table = new Table();
stage.addActor(table);
table.setSize(1280, 720);
table.setPosition(0, 0);
//table.left().top();
// table.debug();
//table.row().minHeight(55).maxHeight(55);
skin = new Skin(Gdx.files.internal("data/ui/uiskin.json"));
/*
* HomeScreen [Button 1]
*/
final TextButton camButton = new TextButton("Camera", skin);
//Add button
table.add(camButton);//.left().expandX().fillX();
camButton.setPosition(0, 0);
//Button Height
//table.row().minHeight(55).maxHeight(55);
//Button Width
//table.row().minWidth(55).maxWidth(55);
In the code above I am trying to position my textButton "camButton" at 0,0 on the screen.
when I start the program it's in the very center of the screen.
If anyone has some guides or tips let me know, I need to learn this stuff.
a small 'fix'
Not exactly a fix; but by removing the table in general solved this problem. By doing this I'm adding it to the stage instead of stage > table > button. Any comments to that?
If you just want to be able to fix buttons to absolute positions like 0,0 there is no need to add the items to a table, just add you button to the stage
TextButton button = new TextButton("Floating Button", skin);
button.setPosition(10, 10);
stage.addActor(button);
Edit
If you still want the controlled positioning that the table provides check out the guide for the table layout it has many good examples Table Layout
If you want to position the table
table.left().bottom();
To position items inside a table
table.add(button).expand().bottom().right();
I had your problem. My requirement for the whole screen or part of the screen was. I solved my problem using WidgetGroup layout.
actor.setPosition(100,200);
yourWidgetGroup.addActor(actor);
I am facing issues in displaying notification count against an icon. It should be displayed at right top corner.
First created button with padding and margin as zero and set the image to button.
FlowLayout buttonLayout = new FlowLayout();
buttonLayout.setAlign(Component.CENTER);
buttonLayout.setValign(Component.TOP);
Container buttonContainer = new Container(buttonLayout);
buttonContainer.setUIID("IconContainer");
Button button = new Button(buttonImage);
button.setUIID("ButtonLabelNew");
buttonContainer.addComponent(button);
The created a notification count container and laid it over button
FlowLayout countLayout = new FlowLayout();
countLayout.setAlign(Component.RIGHT);
countLayout.setValign(Component.TOP);
Container countContainer = new Container(countLayout);
Label countLabel = new Label(displayCount);
countLabel.setUIID("backgroundLabel");
countContainer.addComponent(countLabel);
/*Adding Button and Notification Count to ItemContainer*/
Container itemContainer = new Container(new LayeredLayout());
itemContainer.addComponent(buttonContainer);
itemContainer.addComponent(countContainer);
Now added the text below the button
BoxLayout iconLayout = new BoxLayout(BoxLayout.Y_AXIS);
Container iconContainer = new Container(iconLayout);
Label iconText= new Label(buttonText);
iconText.setUIID("Label");
iconContainer.addComponent(0,itemContainer);
iconContainer.addComponent(1,iconText);
FInally created a grid and added icon containers to the grid
GridLayout gridLayout = new GridLayout(numRows, MAX_ITEMS_PER_ROW);
Container gridContainer = new Container(gridLayout);
gridContainer.setUIID("LogoContainer");
for (int indx = 0; indx < itemContainers.length; indx++)
{
gridContainer.addComponent(itemContainers[indx]);
}
approvalsWorklist.addComponent(BorderLayout.CENTER,gridContainer);
The image is positioned at the right hand side but the component is larger than the icon so you see the image positioned where the components are sized.
There are two ways to solve this for you.
Set a padding or margin on countContainer so the count will be spaced and placed in the right location. This has the drawback of having to tune based on a uniform icon size which might not be the case but its a relatively simple approach.
I'm assuming the components are placed in a GridLayout which means they all have the exact same size. You will wrap each element in the grid layout in a FlowLayout or a an absolute center BorderLayout they will take up their preferred size internally and then the badge will be positioned exactly within the icon edge.
at now i am trying to add a label beside a frame. And i have a text for the label and want to align it to center but it fails and always be left-align. Please help me!
Simulation sm = new Simulation(dm);
JFrame simulation = new JFrame();
simulation.setTitle("Traffic light and Car park Siumulation");
simulation.setSize(800,600);
simulation.setResizable(false);
simulation.setVisible(true);
simulation.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
simulation.add(sm, BorderLayout.CENTER);
//Carpark label
JLabel carparklb = new JLabel("abcd");
carparklb.setPreferredSize(new Dimension(200,600));
carparklb.setVerticalTextPosition(JLabel.CENTER);
carparklb.setHorizontalTextPosition(SwingConstants.CENTER);
simulation.add(carparklb, BorderLayout.EAST);
From the Java Docs
setVerticalTextPosition(int textPosition)
Sets the vertical position of the label's text, relative to its image.
Try JLabel.setVerticalAlignment and JLabel.setHorizontalAlignment instead
Try:
carparklb.setAlignmentY(JLabel.CENTER_ALIGNMENT);
carparklb.setAlignmentX(JLabel.CENTER_ALIGNMENT);
This will align the text to the center of the label both horizontally and vertically. If you only wanted to center it horizontally, you can try either:
JLabel carparklb = new JLabel("abcd", JLabel.CENTER_ALIGNMENT);
or
carparklb.setAlignmentX(JLabel.CENTER_ALIGNMENT);
Use html code as:
"<html><font size=\"5\"><P ALIGN =\"CENTER\">ARGHYA</P></font></html>"
and use it as the String in the label.