Java GUI Swing programming - java

I am beginner . Can I have multiple layouts in a single JFrame?
I want to make a tic-tac-toe project in java and add a GUI to it, so apart from a 3x3 grid, I want a JLabel and a Jbutton. So how can I build a grid as well as add menus and radio buttons?
Which layout should I use?

You must deal with containers and layouts. In containers you can add as many children as you want and Layouts are associated to Containers to provide a way to graphically arrange the children.
Containers provide logical aggregation. Layouts provide graphical appearance for an aggregation.
You probably need to have a container (main) in which there could be a toolbar containing labels and buttons and another container (secondary) with a grid inside. In such a case, there will be three containers : 2 panels and a toolbar. The toolbar has its own layout, but you have to specify layouts for the panels. In the main one a border layout seems to be appropriate (on the north the toolbar, in the center the panel/grid). In the secondary panel a grid layout should do the trick with buttons inside.

I would suggest to use TableLayout as LayoutManager: It allows you with very easy configuration to specify where the various components should be placed inside your container.
Be aware of the fact, that you do not need to solve your Layout with a single layout for your root container: You can always add a JPanel to your Container that has it's own Layout and its own child components.

Related

GridBagLayout or BorderLayout for proposed project in Swing/AWT?

I'm attempting to create a GUI as pictured:
I'm having some trouble deciding which layout would be the best for this.
I've attempted to use a GridBagLayout, but can't figure out how to make items different sizes, and how to position them.
I've also considered using a BorderLayout and creating an eastpanel, westpanel and centerpanel these using the respective layout constraints to put them there, but this feels incorrect.
I've also read the how to use GridBagLayout Java tutorial, but still can't figure out how to achieve the goal.
You should not force yourself to use only a single panel with a single layout. Nest several panels inside each other with each panel possibly having different layouts.
You could have 1 root panel with a BorderLayout. For the top you have a panel with a FlowLayout (or maybe a gridbaglayout). And the center can be another panel with a GridBagLayout or perhaps a combination of more nested panels.
There is no one magic layout that can do everything. Composition is the key.

Make children of JLayeredPane fill available space of container

I have two JPanel instances in a JLayeredPane, on different z-orders. I want both of my child JPanels to always fill the space of the LayeredPane.
The idea is for me to toggle the display of a 2nd panel over top of the first to display a modal-like dialog. Yes, I could just use a JDialog, but I thought it would be fun to try and create some transparancy overtop of the covered JPanel for a nice effect.
I find that using a layout manager on the JLayeredPane, like BorderLayout, and trying to set both children to CENTER conflicts since both panels can't be in the Center.
Is there a trick that I'm not seeing?
The idea is for me to toggle the display of a 2nd panel over top of the first
The easiest way to do this is to use a Glass Pane.
Check out the Disabled Glass Pane for an example of this approach.
There are two ways to create some "Glass Panel like" overlay for JPanels with JLayeredPane:
Add a ComponentListener to the JLayeredPane and update the sizes of all child components whenever the size of the JLayeredPane changes
Create a simple FillLayout, which expands the size of its child Components to the size of the Layout Container (In our case the JLayeredPane). You need to keep a list of children Components. During layoutContainer you copy the dimensions of the Container to these child Components. I wrote this and its really simple, but unfortunately I can't post it, since it's corporate. But if anyone is interested just ask in the comments. The implementation basically consists of one-liners.
For both solutions you need to make sure, that the panels on top are transparent, by setting setOpaque to false. This ensures that underlying panels render their content.

Put 2 FlowLayouts one under another with a margin

I'm trying to dynamically create 2 FlowLayout instances (one of them has about 50 dynamically created buttons, other has about 10) so that there is a slight gap between them. I'm stuck with this, I tried to use BoxLayout and put flow layouts in it, and then create gap with Box class and its methods, but it didn't work. I tried with BorderLayout, and GridBagLayout, but that didn't work either.
You can see what I'm trying to do on the image below. I would appreciate if anyone has an idea how to do this. The actual question is: How can I create a gap between the first 50 buttons and other 10 buttons, where both groups of buttons have to be set in flow layout.
This effect can be achieved by adding an EmptyBorder to each of the containers with FlowLayout.
Many layouts allow us to set a gap between components, depending on which layout the 2 containers with flow layout are being added to, that might be a possibility here.

How to align buttons using a Grid Layout?

Is it possible to use the Grid Layout for just your text area within the program and have your buttons outside of the grid layout for them to align how you want them ??
I'm struggling to align my buttons and I was wondering if this sort of problem is possible to solve using the grid layout or would I have to change my layout all together to see the results I'm looking for.
I can suggest you to use extra Composites with own layouts (e.g. one composite for text area with fill layout and other one for buttons with grid layout).
You can read this good article about SWT layouts.

JavaFX layout equivalent to GridLayout

I'm used to working with Swing to create GUIs but for a recent project I've chosen to switch to JavaFX. I'm having some trouble with recreating a certain layout I used to make using a GridLayout.
I desire the following behavior:
2 columns that scale proportionally with the size of their parent that center their contents.
Using Swing, I would make JPanel with a GridLayout (1 row, 2 columns) and add 2 JPanels with a BorderLayout, adding the actual content to those panels with the centered constraint.
Then I could add the first panel to any container that has a layout that stretches with the frame and all would be well.
I seem to be unable to recreate this behavior in JavaFX in a simple way. I can think of ways to do it using bindings and combining several panes but I was hoping there is a layout that does this automatically. I've tried using TilePane, HBox, GridPane, AnchorPane, SplitPane and even BorderPane but none of them seem to do what I want them to.
Is there a recommended way to accomplish this? I would much prefer not to embed Swing into the application. Basically what I want is to be able to split the content into two columns that automatically stretch with the Stage/Scene (JFrame).
GridPane? Some references here
http://docs.oracle.com/javafx/2/layout/builtin_layouts.htm#CHDGHCDG
http://docs.oracle.com/javafx/2/api/javafx/scene/layout/GridPane.html

Categories

Resources