How to modify this view using GridBagLayout - swing

I would be grateful if you could help me to modify this interface. I just started learning GridBagLayout and I want to figure out how I can implement the drawn interface using GridBagLayout()
|-----------------------------------------------|
| Playing File: name of the file | //This JLabel should cover the entire space
|-----------------------------------------------|
| 00:00:00 ----#---------------------- 00:00:00 | //This has on the left and on the right side two labels and in the middle a JSpinner
|-----------------------------------------------|
| [Open] [Play] [Pause] [Rewind] [Save] | //This line contains 5 buttons
|-----------------------------------------------|
Please find in attachment a working example
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
public class Sample extends JPanel
{
private JLabel labelFileName = new JLabel("Playing File:");
private JLabel labelTimeCounter = new JLabel("00:00:00");
private JLabel labelDuration = new JLabel("00:00:00");
private JButton buttonOpen = new JButton(" Open ");
private JButton buttonPlay = new JButton(" Play ");
private JButton buttonPause = new JButton(" Pause ");
private JButton buttonRewind = new JButton(" Rewind ");
private JButton buttonSave = new JButton(" Save ");
private JSlider sliderTime = new JSlider();
GridBagConstraints constraints = new GridBagConstraints();
public Sample()
{
setLayout(new GridBagLayout());
constraints.insets = new Insets(5, 5, 5, 5);
sliderTime.setValue(0);
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 5;
constraints.fill = GridBagConstraints.HORIZONTAL;
add(labelFileName, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
constraints.gridwidth = 1;
add(labelTimeCounter, constraints);
constraints.gridx = 1;
constraints.gridy = 1;
constraints.gridwidth = 3;
add(sliderTime, constraints);
constraints.gridx = 4;
constraints.gridy = 1;
constraints.gridwidth = 1;
add(labelDuration, constraints);
JPanel panelButtons = new JPanel(new FlowLayout(FlowLayout.LEFT, 25, 5));
panelButtons.add(buttonOpen);
panelButtons.add(buttonPlay);
panelButtons.add(buttonPause);
panelButtons.add(buttonRewind);
panelButtons.add(buttonSave);
constraints.gridx = 0;
constraints.gridy = 2;
add(panelButtons, constraints);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
runApp();
}
});
}
public static void runApp()
{
JFrame frame = new JFrame("Frame");
frame.setVisible(true);
frame.setSize(800, 200);
frame.add( new Sample());
frame.setDefaultCloseOperation
}
}
Thanks

I would strongly suggest a one column grid with three rows.
The top row can be a panel set to flowlayout, or you could drop the label in directly. It is up to you to set the size of the label. Layout managers can't do that for you.
The middle row would be a borderlayout where the spinner is set to center, and the two labels are placed at east and west.
The bottom row should be a panel set with a flowlayout. To get the buttons nicely spaced out in your diagram, can only be done with a flowlayout.
My only concern would be that the slider panel could be resized in the vertical direction. In that case you might want to ditch the GridBaglayout, and just have a borderlayout on top where the top row is center, and the bottom two rows are packed into a panel that is south.

Related

multiple buttons not being able to set off different things

To clarify, the button is NOT a user interface element. It is an in-game object.
Here's a picture of the issue : It shows my main problem; I pressed the button that's circled, but it activated the one on the right.
I'm making a game where you have to complete 10 "chambers" of varying difficulty.
I have currently finished the first level and while finishing up I found an annoying bug I cant seem to fix.
If I have two buttons on a map, it seems that no matter which button I press, it will always do whatever the last one put on the map is going to do.
Also, if one button is activated, the other can't be activated.
I'm using the flashpunk engine/library/whatever and AS3.
The button class:
package Sprites
{
import net.flashpunk.Entity;
import net.flashpunk.graphics.Tilemap;
import net.flashpunk.masks.Grid;
import net.flashpunk.*;
/**
* ...
* #author Brad
*/
public class Button extends Entity
{
public var _tiles:Tilemap
private var _grid:Grid
private var xa:Number
private var ya:Number
private var conTiles:Array
public function Button(xN:Number, yN:Number, tiles:Array)
{
xa = xN
ya = yN
conTiles = []
_tiles = new Tilemap(Assets.Switch, 640, 480, 32, 32)
graphic = _tiles;
layer = 1
conTiles = tiles
_tiles.setTile(xN, yN, 0)
_grid = new Grid(640, 480, 32, 32, 0, 0)
mask = _grid
_grid.setTile(xN, yN, true)
name = "Button"
type = "button"
}
public function Activate():void
{
var Activated:Boolean = false
if (!Activated)
{
for (var i:Number = 0; i < conTiles.length; i++)
{
trace("Hi!")
FP.world["map"]["_tiles"].setTile(conTiles[i].x, conTiles[i].y, 3)
FP.world["map"]["_grid"].setTile(conTiles[i].x, conTiles[i].y, false)
}
_tiles.setTile(xa, ya, 1)
Activated = true
}
else
trace("Activated already!")
}
}
}
How I put the buttons into the world:
public class World1 extends World
{
public var startPos:Object = {"x": 45, "y": 135}
public var player:Player = new Player(startPos["x"], startPos["y"])
public var button:Button = new Button(5, 6, [new Point(10, 10), new Point(10, 11)])
public var button2:Button = new Button(7, 6, [new Point(10, 10), new Point(10, 11)])
public var map:Map1 = new Map1()
public var finish:Finish = new Finish(14,2,"map1")
public function World1()
{
add(map)
add(finish)
add(button)
add(button2)
add(player)
player.layer = 0
for (var i2:Number = 0; i2 < 9; i2++)
add(new Wall3D(i2+1, 1))
for (var i3:Number = 0; i3 < 11; i3++)
add(new Wall3D(i3, 13))
trace("Hi!")
}
}

Positioning a button on a JPanel

I am trying to set the three buttons in the middle of this JPanel which is set above another panel.
Everything is working fine, but three buttons remains at the same position, no matter what.
How can I move the three buttons in the center of the panel2? Right now the three buttons are at the center Left of the panel2.
Code for my panel is here:
public AbcGeniusPanel()
{
//this.setVisible(false);
ImageIcon[] alphabets = new ImageIcon[26];
ImageIcon[] images = new ImageIcon[26];
setBackground(Color.yellow);
//Load the images for alphabet images into the alphabets array using a for loop
for(int i = 0; i < alphabets.length; i++)
{
alphabets[i] = new ImageIcon("C:\\Users\\Dip\\Desktop\\Java Projects\\AbcGeniusApp\\src\\Alphabets\\"+(i+1)+".png");
}
//Load the images images in the IMageIcon array
for(int i = 0; i < images.length; i++)
{
images[i] = new ImageIcon("C:\\Users\\Dip\\Desktop\\Java Projects\\AbcGeniusApp\\src\\Images\\"+(i+1)+".png");
}
//Create two JPanel objects
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
//Set a layoutManager on the panel
panel.setLayout(new GridLayout(2, 13, 5, 5)); //This is good for now
//Create an array for holdoing the buttons
buttons = new JButton[26];
/
//Try passing Images inside the JButton parameter later.
for(int i = 0; i < 26; i++)
{
buttons[i] = new JButton(alphabets[i]);
}
setLayout(new BorderLayout(2,0));
panel2.setLayout(new BoxLayout(panel2, BoxLayout.X_AXIS));
//add the panel to the Border layout
add(panel,BorderLayout.SOUTH);
add(panel2);
//Add evenHandling mechanism to all the buttons
for(int k = 0; k<26; k++)
{
buttons[k].addActionListener(this);
}
for(int count1 = 0; count1<26; count1++)
{
panel.add(buttons[count1]);
}
JButton button1 = new JButton();
JButton button2 = new JButton();
JButton button3 = new JButton();
panel2.add(button1);
panel2.add(button2);
panel2.add(button3);
}
You can use the BoxLayout (it may be easier just using Box.createHorizontalBox()) but you have to put vertical glue at each end of the box. You also may want to put horizontal struts between the buttons to give them some spacing.
It will be easier to just use a FlowLayout for your buttons, which does the equivalent of what I said without the extra code. There may be a potential drawback of the layout causing a button or 2 to flow over to the next line, but with your simple application it is probably not much of a chance.
Here are the two examples. Comment out one line and Comment in (???) the other line to see a different approach to the buttons:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class AlphabetExample {
public static void main(String[] args) {
AlphabetExample alphabetExample = new AlphabetExample();
JFrame frame = alphabetExample.createGui();
frame.setVisible(true);
}
private JFrame createGui() {
JFrame frame = new JFrame("Letters!");
frame.setSize(400, 300);
Container contentPane = frame.getContentPane();
contentPane.add(setupLetters(), BorderLayout.CENTER);
// contentPane.add(setupButtonsWithBox(), BorderLayout.NORTH); // <-- with a BoxLayout
contentPane.add(setupButtonsWithFlowPane(), BorderLayout.NORTH); // <-- with a FlowLayout
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return frame;
}
private JPanel setupLetters() {
String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
JPanel lettersPanel = new JPanel(new GridLayout(2, 13, 5, 5));
for (char x : letters.toCharArray()) {
final String letter = String.valueOf(x);
JButton button = new JButton(letter);
button.setActionCommand(letter);
lettersPanel.add(button);
}
return lettersPanel;
}
private JComponent setupButtonsWithBox() {
Box b = Box.createHorizontalBox();
b.add(Box.createHorizontalGlue());
b.add(new JButton("Left Button"));
b.add(Box.createHorizontalStrut(5));
b.add(new JButton("Center Button"));
b.add(Box.createHorizontalStrut(5));
b.add(new JButton("Right Button"));
b.add(Box.createHorizontalGlue());
return b;
}
private JComponent setupButtonsWithFlowPane() {
JPanel panel = new JPanel(); // default layout manager is FlowLayout
panel.add(new JButton("Left Button"));
panel.add(new JButton("Center Button"));
panel.add(new JButton("Right Button"));
return panel;
}
}
This solved my problem
for(int count1 = 0; count1<3; count1++)
{
panel2.add(Box.createHorizontalGlue());
panel2.add(imageButtons[count1]);
panel2.add(Box.createHorizontalGlue());
}

Can't access public properties of my Class

I'm still learning about object oriented programming...
I made my own simple button class to do horizontal buttons lists. I works fine, but...
package as3Classes {
import flash.display.Graphics;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.Event;
import flash.text.TextField;
import flash.events.MouseEvent;
import fl.motion.MotionEvent;
import flash.text.TextFormat;
public class simpleButton extends MovieClip {
//var nome:String = new String();
var sub:Sprite = new Sprite();
public var realce:Sprite = new Sprite();
public var titulo:String;
public function simpleButton(nomeId:String, texto:String) {
this.name = nomeId;
this.titulo = texto;
this.useHandCursor = true;
this.buttonMode = true;
this.mouseChildren = false;
var txtf:TextFormat = new TextFormat();
txtf.font = "Arial";
var txt:TextField = new TextField();
txt.wordWrap = false;
txt.multiline = false;
txt.text = texto;
txt.setTextFormat(txtf);
txt.width = txt.textWidth+4;
txt.height = 22;
txt.x = 4;
txt.y = 2;
txt.cacheAsBitmap = true;
var fundo:Sprite = new Sprite();
fundo.graphics.beginFill(0xCCCCCC);
fundo.graphics.drawRect(0, 0, txt.width+8, 22);
fundo.graphics.endFill();
//var realce:Sprite = new Sprite();
this.realce.graphics.beginFill(0x00CC00);
this.realce.graphics.drawRect(0, 0, txt.width+8, 22);
this.realce.graphics.endFill();
this.sub.graphics.beginFill(0xFF0000);
this.sub.graphics.drawRect(0, 0, txt.width+8, 2);
this.sub.graphics.endFill();
this.addChild(fundo);
this.addChild(this.realce);
this.addChild(this.sub);
this.addChild(txt);
this.sub.alpha = 0;
this.sub.y = 22;
this.addEventListener(MouseEvent.MOUSE_OVER, mouseover);
this.addEventListener(MouseEvent.MOUSE_OUT, mouseout);
}
private function mouseover(e:MouseEvent){
sub.alpha = 1;
}
private function mouseout(e:MouseEvent){
sub.alpha = 0;
}
}
}
... when I try to access the "titulo" or set "realce" as alpha=1 (to display it as clicked) it returns undefined. I can only set or read proprieties inherited, as the name, alpha, etc. What is my conceptual mistake?
Yes! I found a way to avoid this issue: Since (as it seams) I can't access the internal public objects of "simpleButton" by display objects list, I create a private object (btns:Object) in the root of the main class (can be seen everywhere) and add the simpleButtons simultaneously as adding at display. The name is the same, so I can change the realce.alpha by btns and it will reflect in the display.
This is bizarre, because the e.target leads to the child instead of the object itself!
If somebody knows a better way, please let me know.

Right Click Menu in Flex AIR

I am having a really hard time getting a contextmenu or some sort of NativeMenu coming up on right click in my AIR app. Can someone point me in the right direction or provide a small bit of sample code so I clarify it in my mind?
Thanks!
This is nice example of context menu item in air application
Adobe air - add context menu on right click of tree node
Sure,
Take a look at this: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/ui/ContextMenu.html
Here is the example:
package {
import flash.ui.ContextMenu;
import flash.ui.ContextMenuItem;
import flash.ui.ContextMenuBuiltInItems;
import flash.events.ContextMenuEvent;
import flash.display.Sprite;
import flash.display.Shape;
import flash.text.TextField;
public class ContextMenuExample extends Sprite {
private var myContextMenu:ContextMenu;
private var menuLabel:String = "Reverse Colors";
private var textLabel:String = "Right Click";
private var redRectangle:Sprite;
private var label:TextField;
private var size:uint = 100;
private var black:uint = 0x000000;
private var red:uint = 0xFF0000;
public function ContextMenuExample() {
myContextMenu = new ContextMenu();
removeDefaultItems();
addCustomMenuItems();
myContextMenu.addEventListener(ContextMenuEvent.MENU_SELECT, menuSelectHandler);
addChildren();
redRectangle.contextMenu = myContextMenu;
}
private function addChildren():void {
redRectangle = new Sprite();
redRectangle.graphics.beginFill(red);
redRectangle.graphics.drawRect(0, 0, size, size);
addChild(redRectangle);
redRectangle.x = size;
redRectangle.y = size;
label = createLabel();
redRectangle.addChild(label);
}
private function removeDefaultItems():void {
myContextMenu.hideBuiltInItems();
var defaultItems:ContextMenuBuiltInItems = myContextMenu.builtInItems;
defaultItems.print = true;
}
private function addCustomMenuItems():void {
var item:ContextMenuItem = new ContextMenuItem(menuLabel);
myContextMenu.customItems.push(item);
item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemSelectHandler);
}
private function menuSelectHandler(event:ContextMenuEvent):void {
trace("menuSelectHandler: " + event);
}
private function menuItemSelectHandler(event:ContextMenuEvent):void {
trace("menuItemSelectHandler: " + event);
var textColor:uint = (label.textColor == black) ? red : black;
var bgColor:uint = (label.textColor == black) ? black : red;
redRectangle.graphics.clear();
redRectangle.graphics.beginFill(bgColor);
redRectangle.graphics.drawRect(0, 0, size, size);
label.textColor = textColor;
}
private function createLabel():TextField {
var txtField:TextField = new TextField();
txtField.text = textLabel;
return txtField;
}
}
}

1180: Call to a possibly undefined method Point

I'm very very new on ActionScript 3.0 development for Blackberry Playbook.
I'm working with Double Sided 3D Plane in FP10 and I'm having troubles with this code:
package
{
import flash.display.Sprite;
import flash.events.Event;
public class FlipCard extends Sprite
{
private var myPaperSprite:PaperSprite;
public function FlipCard()
{
super();
}
private function Flip():void
{
/*
Create a new PaperSprite:
If your front and back faces already exist, you can pass them straight
into the constructor, like so:
myPaperSprite = new PaperSprite( myFrontMc, myBackMc );
*/
myPaperSprite = new PaperSprite();
/*
Optionally specify a pivot point, in this example the centre is used
(this is also the default so there is no need to set this normally).
To pivot around the top left use:
myPaperSprite.pivot = new Point(0,0);
or for bottom right:
myPaperSprite.pivot = new Point(1,1);
and so on...
*/
myPaperSprite.pivot = new Point(0.5,0.5);
line myPaperSprite.pivot = new Point(0.5,0.5); is throwing the following error:
1180: Call to a possibly undefined method Point.
And pivot member is defined as follows:
package
{
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
import flash.geom.Rectangle;
public class PaperSprite extends Sprite
{
//___________________________________________________________
//————————————————————————————————————————————— CLASS MEMBERS
private static const POINT_A:Point = new Point(0, 0);
private static const POINT_B:Point = new Point(100, 0);
private static const POINT_C:Point = new Point(0, 100);
private var _isFrontFacing:Boolean = true;
private var _autoUpdate:Boolean = true;
private var _pivot:Point = new Point(0.5,0.5);
private var _front:DisplayObject;
private var _back:DisplayObject;
private var _p1:Point;
private var _p2:Point;
private var _p3:Point;
//___________________________________________________________
//————————————————————————————————————————— GETTERS + SETTERS
/**
* A Point Object used to determine the pivot, or registration point of the
* PaperSprite. The values should be between 0 and 1 and are relative to the
* dimensions of each face, 0 being far left (on the x axis) or top (y axis)
* and 1 being the far right (x axis) or bottom (y axis).
*
* The default value is { x:0.5, y:0.5 } meaning that both faces will pivot
* around their respective centres
*
* Examples:
*
* To pivot from the centre use: new Point( 0.5, 0.5 );
* To pivot from the top left use: new Point( 0.0, 0.0 );
* To Pivot from the bottom right use: new Point( 1.0, 1.0 );
*
*/
public function get pivot():Point
{
return _pivot;
}
public function set pivot( value:Point ):void
{
_pivot = value;
alignFaces();
}
Where is the problem? What am I doing wrong?
add import flash.geom.Point to FlipCard.as as well, as it is not visible in that class