When I add a JPanel(1) into my GridBagLayout JPanel(2), my JPanel(1) is shrinked into a tiny square located in the middle. I don't know why this happened, because in theory it should work. I think it might have something to do with default BorderLayout? cause I read somewhere that BorderLayout puts your JPanel in the middle. But I have already set my Layout as GridBag, so I don't know why this is happening. Here's my code:
JFrame f = new JFrame("TEST");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
f.setSize(300, screenSize.height);
f.setLocation(screenSize.width - 300, 0);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagConstraints gbc = new GridBagConstraints();
JPanel panel = new JPanel(new GridBagLayout());
JPanel A = new JPanel();
Dimension d = new Dimension(270, 50);
A.setSize(d);
A.setBackground(Color.GREEN);
JPanel B = new JPanel();
B.setSize(d);
B.setBackground(Color.BLUE);
gbc.gridy = 0;
panel.add(A, gbc);
gbc.gridx = 1;
panel.add(B, gbc);
f.add(panel);
f.validate();
f.repaint();
More over, when I set the border for A and B to 100 pixels, both of them start showing up. So I am really confused!
Stop pulling your hair with GridBagLayout, try MigLayout instead, you'll save a lot of time:
http://www.miglayout.com
Related
I'm trying to use Skins with TextButtons. However I'm having an issue where the TextureRegions of the Skin are NOT being scaled down to the tablesize.
This is leading to an issue like in this image:
As you can see that is the e from the word Hello. I managed to scale that down to a more reasonable size (still wrong). However the Button textures are still way too large.
Any ideas? Because I am pulling my hair out. I am using a custom viewport with a custom screenspace width and height. Here is the create method for the menu.
public void create(){
menuStage = new Stage();
camera = new OrthographicCamera();
viewPort = new StretchViewport(SCREEN_SPACE_WIDTH, SCREEN_SPACE_HEIGHT, camera);
viewPort.setWorldSize(SCREEN_SPACE_WIDTH, SCREEN_SPACE_HEIGHT);
menuStage.setViewport(viewPort);
devTexture = new Sprite (new Texture(Gdx.files.internal("data/16by9.png"), true));
Image newActor = new Image(devTexture.getTexture());
newActor.setSize(
viewPort.getWorldWidth(),
viewPort.getWorldHeight()
);
menuStage.addActor(newActor);
skin = new Skin(Gdx.files.internal("data/uiskin.json"));
menuTable = new Table();
skin.getFont("default-font").setScale(0.08f);
menuTable.debugAll();
menuTable.setFillParent(true);
TextButton a,b,c;
a = new TextButton("hello", skin);
b = new TextButton("hello", skin);
c = new TextButton("hello", skin);
a.getStyle().up.setMinWidth(2.0f);
b.getStyle().up.setMinHeight(2.0f);
b.getStyle().up.setMinWidth(2.0f);
c.getStyle().up.setMinHeight(2.0f);
c.getStyle().up.setMinWidth(2.0f);
menuTable.row();
menuTable.add(a).size(2.0f, 1.0f);
menuTable.add(b).size(2.0f, 1.0f);
menuTable.add(c).size(2.0f, 1.0f);
menuStage.addActor(menuTable);
camera.translate(
newActor.getWidth() / 2,
newActor.getHeight() / 2
);
}
Trying to build about this GUI in Swing:
In my MainFrame i set a GridLayout like that to achieve 1 row, 2 columns:
setLayout(new GridLayout(1, 2));
In the Left Column i figures i would need a GridBagLayout as in the right column. Normal GridLayout doesn't work anymore because i want different sizes of each row. For the left column i tried this:
GridBagConstraints gbc = new GridBagConstraints();
mapPanel = new MapPanel(map);
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 10;
add(mapPanel, gbc);
controlPanel = new JPanel();
controlPanel.add(new JButton("Test"));
controlPanel.add(new JButton("Test 2"));
controlPanel.add(new JButton("Test 3"));
controlPanel.add(new JButton("Test 4"));
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridheight = 1;
add(controlPanel, gbc);
logPanel = new LogPanel();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridheight = GridBagConstraints.REMAINDER;
add(logPanel, gbc);
This however will result in everything "packed together" in the left column. It won't expand to the 100% height and 50% width the column has. How can i achieve a GUI as in the picture?
Thanks!
Look at JNodeLayout. RectNode is exactly what you Need.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import com.smartg.swing.layout.JNodeLayout;
import com.smartg.swing.layout.LayoutNode.RectNode;
public class RectNodeDemo {
public static void main(String[] args) {
String rootName = "root";
RectNode root = new RectNode(rootName);
JNodeLayout layout = new JNodeLayout(root);
layout.setHgap(1);
layout.setVgap(1);
JPanel target = new JPanel();
target.setBorder(new EmptyBorder(10, 10, 10, 10));
target.setLayout(layout);
addPanel(target, root, new Rectangle2D.Double(0, 0, .5, .5));
addPanel(target, root, new Rectangle2D.Double(0, .5, .5, .1));
addPanel(target, root, new Rectangle2D.Double(0, .6, .5, .4));
addPanel(target, root, new Rectangle2D.Double(.5, 0, .5, .9));
addPanel(target, root, new Rectangle2D.Double(.5, .9, .5, .1));
layout.syncNodes();
JFrame frame = new JFrame();
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(target, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
static void addPanel(JPanel target, RectNode node, Rectangle2D r) {
JPanel p = new JPanel();
target.add(p);
p.setBorder(new LineBorder(Color.BLACK, 1));
node.add(p, r);
}
}
You could use the Relative Layout. This layout allows you to specify the relative sizes of each component.
To create your panel on the left the code would be something like:
RelativeLayout rl = new RelativeLayout(RelativeLayout.Y_AXIS);
rl.setFill( true );
JPanel left = new JPanel( rl );
left.add(panel1, new Float(50));
left.add(panel2, new Float(10));
left.add(panel3, new Float(40));
Using only GridBagLayout will suffice.
By setting gbc.gridheight = 10 you tell GridBag to use 10 rows for the component. (Similar to rowspan in HTML.) This, however, says nothing about the actual height of the component since rows don't have a fixed height. Consequently, this constraint doesn't have any effect when you're only using a single column. The name of the constraint is IMHO confusing.
Use GridBagConstraints.weightx and GridBagConstraints.weighty to specify where extra space should go. See How to Use GridBagLayout for more.
In combination with component's preferred sizes you'll be able to obtain a panel that will show your components when the panel is small and distribute extra space when the panel is 'too big'.
Though, as Andrew Thompson pointed out, using setPreferredSize() is not a very good way to go. Often, components already have their preferred size just fine as constructed. Just play around with all of GridBagConstraints and see what happens.
This might in the end be a better looking solution.
I have a confusing ScrollPane issue that I hope someone has encountered before!
I have a movie clip called Inner_Area, which gets a number of other smaller movie clips loaded into it. The end effect is that it looks like a list of strings.
After the Inner_Area movieclip is populated with its additional information, I set the source of a ScrollePane object to it.
scroll_area.source = Inner_Area;
When I play the movieclip, the list loads up successfully, and a scroll bar appears on the right hand side. This is where the strange part happens.
If I click the scroll down arrow, to try to see the items at the bottom of the list, the Inner_Area actually seems to scroll to the RIGHT. Not down at all. I've looked over my actionscript and I cannot for the life of me see how this could be.
I've not included any code, of course, because I am initially hoping that someone may have experienced a similar situation before. How can I get a scrollpane area such that when you scroll down, it actually goes down instead of in a different direction?
Any hints, tips, or advice is much appreciated!
Some Code:
public class MenuBackground extends MovieClip
{
scroll_area = new ScrollPane();
scroll_area.x = -275;
scroll_area.y = -77;
scroll_area.width = 250;
scroll_area.height = 175;
addChild(scroll_area);
inner_area = new Inner_ZoneScrollArea();
spacing = 0;
for (i= 0; i < numthings; i++)
{
_field = new _Field();
_field.y = spacing;
spacing = spacing + 20;
inner_area.addChild(_field);
}
scroll_area.source = inner_area;
}
And in a different file, is the _Field code:
public function _Field()
{
_Format = new TextFormat();
_Format.size = 16;
_TextField = new TextField();
_TextField.x = 50;
_TextField.y = 4;
_TextField.textColor = 0xFFFFFF;
_TextField.defaultTextFormat = _Format;
_TextField.autoSize = "left";
_TextField.multiline = true; // Just added these last two on suggestion in this thread
_TextField.text = "Name"; // some text
addChild(_TextField);
}
not sure if you have got this one fixed yet mate but I think I had a similar issue trying to add Dynamic content to a ScrollPane if your having an issue with the scrollbars not coming up you need to do an update to the ScrollPane I had to put mine on a Timer so that it gave the content time to append and then update. like so
var timer:Timer = new Timer(3000,1);
timer.addEventListener(TimerEvent.TIMER, updateEvent);
timer.start();
function updateEvent(e:TimerEvent):void
{
ScrollPane.update();
timer.stop();
}
hope this helps any one else with a similar issue loading Dynamic content into a ScrollPane
The following simple example produces a ScrollPane that scrolls down:
var tf:TextField = new TextField();
tf.text = "Lorem \nIpsum dolor \nsit amet \nthese are \nmany\nmany \nmany \nmanylines of \ntext";
tf.autoSize = "left";
tf.multiline = true;
var sp:ScrollPane = new ScrollPane();
sp.source = tf;
addChild(sp);
To be accurate and brief:
Is it possible to Layout a frame container with group of components(like check boxes, radio buttons, etc...) instead of adding them one by one to a frame? So, positioning them in a frame would be a lot more easier.
private void initializaUI(){
setSize(700, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Panel container to wrap checkboxes and radio buttons
JPanel panel = new JPanel(null);
JPanel checkBoxPanel = new JPanel(new BoxLayout(panel, defaultCloseOperation, null));
JPanel radioPanel_1 = new JPanel(new GridLayout());
JPanel radioPanel_2 = new JPanel(new GridLayout());
//Text field to display order
JTextField orderField = new JTextField(20);
orderField.setBounds(100, 100, 100, 20);
//Button to process place the order
JButton button = new JButton("Process Selection");
button.setBounds(300, 100, 100, 40);
//toppings check boxes
checkBoxPanel.setVisible(true);
checkBoxPanel.setBounds(100, 200, 100, 50);
String Topping[] = {"Tomato", "Green Pepper", "Black Olives", "Mushrooms", "Extra Cheese", "Pepperoni", "Sausage"};
checkBoxPanel.add(new JCheckBox("Tomato"));
checkBoxPanel.add(new JCheckBox(Topping[1]));
checkBoxPanel.add(new JCheckBox(Topping[2]));
checkBoxPanel.add(new JCheckBox(Topping[3]));
checkBoxPanel.add(new JCheckBox(Topping[4]));
checkBoxPanel.add(new JCheckBox(Topping[5]));
checkBoxPanel.add(new JCheckBox(Topping[6]));
//sizes radio buttons
String size[] = {"Small:$6.50", "Medium:$8.50", "Large:$10.00"};
JRadioButton radio = new JRadioButton(size[0]);
radio.setBounds(100, 50, 100, 20);
//
panel.add(checkBoxPanel);
//
setContentPane(panel);
This is the code which supposed to perform some actions based on user entry. please help me make it clear and readable.
This is the error: "The field Component.x is not visible"
Yes, that's what a JPanel is for. It's an empty container that has its inner layout manager and that can be placed wherever you want inside a JFrame (or inside another JPanel). So, just to give you an example you can have:
JPanel checkBoxPanel = new JPanel(new GridLayout(..));
JPanel fieldsPanel = new JPanel(new BoxLayout(..));
checkBoxPanel.add(new JCheckBox(..));
fieldsPanel.add(new JTextField(..));
frame.setLayout(new BorderLayout());
frame.add(checkBoxPanel, BorderLayout.NORTH);
frame.add(..)
Something's not right here. I'm trying to get the rightmost button (labeled "help" in the example below) to be right-aligned to the JFrame, and the huge buttons to have their width tied to the JFrame but be at least 180px each. I got the huge button constraint to work, but not the right alignment.
I thought the right alignment was accomplished by gapbefore push (as in this other SO question), but I can't figure it out.
Can anyone help me?
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
public class RightAlignQuestion {
public static void main(String[] args) {
JFrame frame = new JFrame("right align question");
JPanel mainPanel = new JPanel();
frame.setContentPane(mainPanel);
mainPanel.setLayout(new MigLayout("insets 0", "[grow]", ""));
JPanel topPanel = new JPanel();
topPanel.setLayout(new MigLayout("", "[][][][]", ""));
for (int i = 0; i < 3; ++i)
topPanel.add(new JButton("button"+i), "");
topPanel.add(new JButton("help"), "gapbefore push, wrap");
topPanel.add(new JButton("big button"), "span 3, grow");
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new MigLayout("",
"[180:180:,grow][180:180:,grow]","100:"));
bottomPanel.add(new JButton("tweedledee"), "grow");
bottomPanel.add(new JButton("tweedledum"), "grow");
mainPanel.add(topPanel, "grow, wrap");
mainPanel.add(bottomPanel, "grow");
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
never mind, I got it: looks like there needs to be a gap constraint in the column spec, not at the component level:
topPanel.setLayout(new MigLayout("", "[][][]push[]", ""));
a much easier/cleaner way (IMOH) is using component constraints and doing
topPanel.add(new JButton("help"), "push, al right, wrap");
Push will push the cell out as the window stretches but you need to tell the component to bind itself to the right of the cell. You could achieve the above with the following code.
JPanel topPanel = new JPanel();
frame.setContentPane(topPanel);
for (int i = 0; i < 3; ++i)
topPanel.add(new JButton("button"+i), "");
topPanel.add(new JButton("help"), "push, al right, wrap");
topPanel.add(new JButton("big button"), "span 3, grow, wrap");
topPanel.add(new JButton("tweedledee"), "span, split2,grow, w 180, h 100");
topPanel.add(new JButton("tweedledum"), "w 180, h 100, grow");