Need "non-rectangular Card Layout Panel" or "Transparent component" - java

I've got a new requirement to change my display.
Currently I have a row of 3 radio buttons sitting above a Panel with a card layout. The buttons CONTROL which card of the Card Layout Panel is displayed. But this, it seems, wastes valuable screen real estate.
The row of buttons can easily be transformed to a column of buttons using a GridLayout(4,1) on the panel that owns them and their title label. This panel could then theoretically be placed in the upper left corner of the each of the cards displayed in the card layout panel, and the top row of each card could be moved up to the right of the button panel, allowing more space for what has to come below.
But what shall be the containment model for this? In this variant, the button panel must appear on each card. Who owns it? Logically it needs to be owned by the parent panel on which all this sits, but it won't display on the cards, which will cover it over - unless I could
add the button panel to each card at the moment of its display, which would probably be messy and maybe cause flicker.
make the top left corner of each card be transparent, allowing it to show the upper left button panel owned by its parent. But how would I do that?
Other solutions?
Maybe
Have two card layout panels controlled by the radio buttons. The upper right one that contains all but the the button panel and the lower component that sits below both.

It sounds like you could use a JLayeredPane as the parent component of both, the panel containing the radio buttons and the card panel.
JLayeredPane allows its child components to overlap, each child belonging to a layer.
To specify the layer, you can pass an Integer constant into the JLayeredPane's add(Component, Object) method as the constraint argument. The integer constants are defined in the JLayeredPane class itself. You can use JLayeredPane.DEFAULT_LAYER for the card panel and JLayeredPane.PALETTE_LAYER for the radio button panel.
Having set the layer, you'll still have to position the two panels correctly in x,y space. I would recommend just overriding the JLayeredPane's doLayout() method with something like this (haven't tested):
public void doLayout()
{
cardPanel.setBounds( new Rectangle( getSize() ));
radioButtonPanel.setBounds( new Rectangle( radtioButtonPanel.getPreferredSize() ));
}

You may be able to use OverlayLayout to display the control panel in the top-left of your card panel.

I would forget the transparency idea. Just put the options as a list to the left of (or right of, or over/under) the card panel. I would definitely NOT put the panel of controls on the CardPanel itself. It should be outside.
The list of selections could be radios, buttons, or in this example, a JList of items that can grow w/o messing up the layout. For example:
/*
* CardLayoutDemo.java
*/
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
#SuppressWarnings("unchecked")
public class CardLayoutDemo implements Runnable
{
final static String CARD1 = "Gray Panel";
final static String CARD2 = "Blue Panel";
final static String CARD3 = "Green Panel";
JPanel cards;
CardLayout cl;
public static void main(String[] args)
{
SwingUtilities.invokeLater(new CardLayoutDemo());
}
public void run()
{
final JList jList = new JList(new String[]{CARD1, CARD2, CARD3});
jList.setPrototypeCellValue("XXXXXXXXXXXX");
jList.setVisibleRowCount(5);
jList.setSelectedIndex(0);
jList.addListSelectionListener(new ListSelectionListener()
{
#Override
public void valueChanged(ListSelectionEvent e)
{
String name = jList.getSelectedValue().toString();
cl.show(cards, name);
}
});
JScrollPane scroll = new JScrollPane(jList);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
Dimension dim = new Dimension(300, 300);
JPanel card1 = new JPanel();
card1.setBackground(Color.GRAY);
card1.setPreferredSize(dim);
JPanel card2 = new JPanel();
card2.setBackground(Color.BLUE);
card2.setPreferredSize(dim);
JPanel card3 = new JPanel();
card3.setBackground(Color.GREEN);
card3.setPreferredSize(dim);
cl = new CardLayout();
cards = new JPanel(cl);
cards.add(card1, CARD1);
cards.add(card2, CARD2);
cards.add(card3, CARD3);
JFrame f = new JFrame("CardLayout Demo");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(scroll, BorderLayout.WEST);
f.add(cards, BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
If you wanted a label for the selections, just make a "selection panel" that contains the JLabel and the JScrollPane (or use your grid of buttons panel), and put it in Borderlayout.WEST (instead of the adding the JScrollPane directly).
Also, look into JTabbedPane as an alternative.

Related

buttons to change card in cardlayout

I have 2 panels in my frame, 1 is for buttons (I want to use radioButton, but for now it is easier using buttons) and the other one is for the card layout panel. My plan is to shuffle the chad when I press specific button. Like the move button will show me the move panel card. Move panel card has x0 label and text field, Line panel card has x0 and x1 both label and text field.
There are 2 classes, 1 is for the buttonpanel = Buttons
the other one is for the cards = PanelMiddle
Here's my code:
public class PanelMiddle{
JPanel controlPanel = new JPanel();
CardLayout cl = new CardLayout();
JPanel movePanel = new JPanel();
JPanel linePanel = new JPanel();
JLabel x0Label = new JLabel("x0");
JTextField x0TextField = new JTextField(3);
JLabel x1Label = new JLabel("x1");
JTextField x1TextField = new JTextField(3);
public PanelMiddle(){
controlPanel.setLayout(cl);
//move panel
movePanel.setLayout(new GridLayout (1,2));
movePanel.add(x0Label);
movePanel.add(x0TextField);
controlPanel.add(movePanel,"Move"); //add the keyword Move to show the move card
//line panel
linePanel.setLayout(new GridLayout (2,2));
//linePanel.add(x0Label);
linePanel.add(x1Label);
//linePanel.add(x0TextField);
linePanel.add(x1TextField);
controlPanel.add(linePanel,"Line"); // add the keyword Line to show the line card
}
}
In the other class I have:
public class Buttons extends PanelMiddle{
JPanel buttonPanel = new JPanel();
JButton moveB = new JButton ("Move");
JButton lineB = new JButton ("Line");
public Buttons(){
buttonPanel.setLayout(new GridLayout (2,1));
buttonPanel.add(moveB);
buttonPanel.add(lineB);
action();
}
public void action(){
moveB.addActionListener((e) -> {
cl.show(controlPanel,"Move");
});
lineB.addActionListener((e) -> { cl.show(controlPanel,"Line");});
}
}
The result that I got is weird. It doesn't show fully my panel. But when I tried commenting all the line panel, it works. Does someone have a fix here?
NB: Im sorry I dont know how to edit the text here so its a little bit messy.
edit 1 : as guleryuz says, I commented out the x0Label and x0TextField from the line panel
in swing component hierarchy a component can only be added to one container, you are adding x0Label and x0TextField two both panels. so when you add x0Labe two second panel (linePanel) it will be removed from movePanel (same case for x0TextField) so movePanel becomes empty.
more details here

Toggling between JPanels (hide/show)

I have two JPanels that sit on top of one another. The 'top' panel holds many widgets (JButtons, JTextFields, etc.). One of the buttons will initiate an action to display a number of images.
These images are displayed on the other JPanel. So, when this button is clicked, I want to hide the control panel and display the images panel. Sounds pretty simple.
Here is the code (I've omitted a lot of stuff that I don't think is relevant). In the constructor, if I switch which panel is visible when the app launches, it looks fine either way. When I click the button, I should go from my dark gray control panel to my blue images panel. Except that what happens is my dark gray control panel becomes an empty white panel. Any ideas?
public GUI() {
JFrame frame = new JFrame();
...
JPanel imagesPanel = new ImagesPanel();
imagesPanel.setBackground(Color.BLUE);
imagesPanel.setVisible(false);
frame.getContentPane().add(imagesPanel, BorderLayout.CENTER);
// make a JPanel to hold all of the buttons and text fields
JPanel imagesPanel = new ImagesPanel();
controlPanel.setBackground(Color.DARK_GRAY);
controlPanel.setVisible(true);
frame.getContentPane().add(controlPanel, BorderLayout.CENTER);
...
JButton btnDisplayImages = new JButton("Display Images");
btnDisplayImages.setPreferredSize(standardButtonSize);
btnDisplayImages.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
imagesPanel.setVisible(true);
controlPanel.setVisible(false);
frame.repaint();
frame.setVisible(true);
}
});
// button added to control panel
...
}
Use CardLayout. (docs.oracle.com/javase/tutorial/uiswing/layout/card.html)
final String IMAGES_PANEL = "Images Panel";
final String CONTROL_PANEL = "Control Panel";
CardLayout cardLayout;
JPanel cards;
//Where the components controlled by the CardLayout are initialized:
//Create the "cards".
JPanel card1 = new JPanel();
...
JPanel card2 = new JPanel();
...
//Create the panel that contains the "cards".
cardLayout = new CardLayout();
cards = new JPanel(cardLayout);
cards.add(card1, IMAGES_PANEL);
cards.add(card2, CONTROL_PANEL);
...
//Show images panel
cardLayout.show(cards,IMAGES_PANEL);
...
//Show control panel
cardLayout.show(cards, CONTROL_PANEL);

JFrame position three buttons one below another in the center

So I would like to have three JButtons all on top of each other, but not to large in width or height either. I am not too familiar with Java's layouts, and to be honest I am not too keen on them. Please view the image a code below to explain to me how, thanks.
package com.aqagame.harrykitchener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Main
{
private JButton playGame, playerNames, exitGame;
public Main()
{
JPanel mainCard = new JPanel(new BorderLayout(8, 8));
playGame = new JButton("Play Game");
playerNames = new JButton("Player Names");
exitGame = new JButton("Exit Game");
mainCard.add(playGame, BorderLayout.NORTH);
mainCard.add(playerNames, BorderLayout.CENTER);
mainCard.add(exitGame, BorderLayout.SOUTH);
JFrame window = new JFrame("Harry's AQA game");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(mainCard);
window.setSize(900, 800);
window.setLocationRelativeTo(null);
window.setVisible(true);
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Main();
}
});
}
}
Check out the Java Documentation for the different layout managers. I know you're not familiar with them, which is why you should probably start. Once you get used to them there is no end to their benefits. There is a lot of excellent information in the documentation and I am sure you will learn a lot. Personally, I recommend looking at the Box Layout:
Create JPanel that uses a GridLayout and add all the buttons to the panel. The GridLayout will automactially size the buttons to be the same size.
Set the layout manager of your main window to use a GridBagLayout.
add the panel to the main window using the default GridBagConststraints. Then the panel will automatically be centered both horizontally and vertically.
To not use Box or GridBag, I think a combination such as this may work out:
Have main panel (let's call it A) have a BorderLayout
Create another panel (let's call it B), with a FlowLayout, with constructor aligning components to the center
Create another panel (let's call it C), with a GridLayout, 1 column 3 rows
Add each button to a new JPanel with a FlowLayout (1 JPanel per button, so buttons are wrapped by a FlowLayout), and then add each of those JPanels to C
Add C to B
Add B to A (center position)
I think this should cause buttons to be on top of each other with small amount of padding while not being stretched widthwise and while appearing in the center of the screen.

JPanel Positioning using the Border Layout not working

I am trying to setup my JPanel's position to the right using i.add(jp, BorderLayout.EAST); but it is not working. Any ideas why? Thanks for the help in advance.
/* INSTANCE DECLARATIONS */
private JTextField tf;//text field instance variable
private JLabel jl2;//label instance variable
/*****************
* WINDOW METHOD *
* ***************/
public void window() {
LoadImageApp i = new LoadImageApp();//calling image class
JFrame gameFrame = new JFrame();//declaration
JPanel jp = new JPanel();
JLabel jl = new JLabel("Enter a Letter:");//prompt with label
tf = new JTextField(1);//length of text field by character
jl2 = new JLabel("Letters Used: ");
jp.add(jl);//add label to panel
jp.add(tf);//add text field to panel
jp.add(jl2);//add letters used
gameFrame.add(i); //adds background image to window
i.add(jp, BorderLayout.EAST); // adds panel containing label to background image panel
gameFrame.setTitle("Hangman");//title of frame window
gameFrame.setSize(850, 600);//sets size of frame
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//exit when 'x' button pressed
gameFrame.setIconImage(new ImageIcon("Hangman-Game-grey.png").getImage());//set the frame icon to an image loaded from a file
gameFrame.setLocationRelativeTo(null);//window centered
gameFrame.setResizable(false);//user can not resize window
gameFrame.setVisible(true);//display frame
}//end window method
What layout manager does i, your LoadImageApp instance, use? I'm betting it's not BorderLayout. I'm betting that the LoadImageApp class extends JPanel and if so and if you never explicitly set its layout, then it uses a FlowLayout by default, and as you're finding out, FlowLayout doesn't respect the BorderLayout.EAST int constant.
The solution is likely quite simple: make it use a BorderLayout:
setLayout(new BorderLayout());
Edit
You state in comment:
When I set the border layout of i to EAST, my background image shifts to the right also, is there a way to get around that?
No, you're missing the point. You need to set the layout of LoadImageApp to BorderLayout. You're not supposed to add i BorderLayout.EAST. This was never recommended to you.
i.e.,
public class LoadImageApp extends JPanel {
// in the constructor
public LoadImageApp() {
setLayout(new BorderLayout());
}
// .... etc....
}
THe LoadImageApp instance (which I would name loadImageApp, not i), should be added BorderLayout.CENTER, which you were doing before. Please read the layout manager tutorials which you can find here.

Change Background and size of panels in card layout

I have some panels in a card layout container (no idea if that is correct terminology). I can't find a way to set the location, or size of these panels inside the container. I tried setBounds and setLayout(null) and I still can't get anything to change.
These are my fields and the constructor. I've gotten my frame working and I can see and use the buttons to change cards, but I really can't change much else about the cards. I set the two card panels two have different backgrounds, but they only make a small boarder of color around the button and leave it in the centre of the screen.
I also don't understand why this isn't pasting my code properly... So sorry!
public class TestPanel extends JPanel implements ActionListener {
CardLayout cl = new CardLayout();
private JPanel panelCont = new JPanel();
private JPanel panel1 = new JPanel();
private JPanel panel2 = new JPanel();
private static JButton but1 = new JButton("Change panels");
private static JButton but2 = new JButton("Change back");
public TestPanel() {
panelCont.setLayout(cl);
panel1.add(but1);
panel2.add(but2);
panel1.setBackground(Color.black);
panel2.setBackground(Color.blue);
panelCont.add(panel1, "1");
panelCont.add(panel2, "2");
cl.show(panelCont, "1");
but1.addActionListener(this);
but2.addActionListener(this);
add(panelCont);
}
}
Thanks. I apologise in advance. I'm finding it hard to understand card layout.
A CardLayout respects the preferred size of the panels added to the layout. That is the size will be the size of the largest panel added to the layout.
I set the two card panels two have different backgrounds, but they only make a small boarder of color around the button and leave it in the centre of the screen.
The default layout for a panel is the FlowLayout. A FlowLayout by default has a 5 pixel horizontal/vertical gap around each component. So the preferred size of your panel is the size of the button plus the 5 pixel gap.
The panel is displaying correctly. When you add other components to the panel the size will change as required.
It's not clear where you pack() the enclosing Window. By default, pack() causes a panel having CardLayout to adopt the the size of the largest panel's preferred size, which is determined by the size of its contents. This example uses setPreferredSize() to specify an arbitrary size, but you can override getPreferredSize() as shown here.

Categories