Related to GUI creation using Swing in Java - java

I am facing problem during event handling.
The problem is like this:
My GUI has mainPanel (JPanel) which in turn consists of a panel with three buttons (namely btn1, btn2, btn3) at its WEST position.
I have created 3 more panels namely pnl1, pnl2, pnl3 (each panel has one label and one text area) using three different functions of same class.
My requirement is that if I click btn1 / btn2 / btn3 then pnl1 / pnl2 / pnl3 respectively must appear at mainPanel's CENTER position.

You will have to add action listener to the btn1, btn2, btn3. Then when the respective button is clicked you write a function that will display the required respective panels to the center of GUI.
To do so you can use cardLayout.
If you add detail to your question then we can help you with better answer or suggestions.
What's the purpose of the label and text area?
Add a screenshot of your GUI and some code that you have written.

Sounds like you want to put a CardLayout in the center, here's a tutorial.

You could consider creating a JPanel with a CardLayout for the CENTER panel. The CardLayout could contain 4 UI's ( the pnl1,pnl2,pnl3 and an empty panel ), and clicking on those buttons could activate the correct panel on the CardLayout

Related

Centering a JButton using Box Layout.X_AXIS

I have a JPanel that can have either 1 or 2 buttons, depending on what's going on in the program at the time. I'm using Box Layout.X_AXIS to line up the 2 buttons configuration, and it works great. When I switch to 1 button however, the single button is on the far left of the window. I've tried a bunch of different things to get the button centered, but the only thing that seems to work is this:
JButton yesBtn = new JButton("Continue");
btnPane.setLayout(new BoxLayout(btnPane, BoxLayout.Y_AXIS));
yesBtn.setAlignmentX(Component.CENTER_ALIGNMENT);
The problem with this is that the vertical location of the button changes doing it this way, so it doesn't match where the 2 buttons would sit vertically. Is there a way to center the button using the X_AXIS layout?
I've tried a bunch of different things to get the button centered,
The easiest way is to add "glue" BEFORE the first component and AFTER the last component. Then it will work for 1 component or multiple components.
Read the section from the Swing tutorial on Invisible Components as Filler for more information and examples.

Create and show a jRadioButton when I push a button

I'm writing a code that, when I push a jButton, create and show in a jPannel a jRadioButton. In NetBeans I have writted, in the action method of the jButton, this part of code:
javax.swing.JRadioButton birdButton = new javax.swing.JRadioButton("ciao");
birdButton.setMnemonic(KeyEvent.VK_B);
birdButton.setActionCommand("ciao");
birdButton.setSelected(true);
jPanel1.add(birdButton);
jPanel1.revalidate();
jPanel1.repaint();
but, when I push the button the jRadioButton doesn't appear. The jPanel1 there is. What's the trouble? Thanks.
when you drag and drop jpanel to jframe in netbeans from visual component section,they set your panel layout to somelayout for example group layout
so when you add component you will not see it probably because there is different way to add components to different layout .not just .add() .you should add appropriate layout according to your expected design.
for example if you set layout to flowlayout you will see the jradiobuton as you expect.
this is how you can set layout to a component in netbean ide.

How to do a main menu layout in Java?

I'm working with a few people to design a game for a project in class. While making the main menu GUI, I searched all over for some way to get the design the way I wanted (here), but I couldn't figure it out. CardLayout, BoxLayout, BorderLayout, GridLayout, none of those have helped in what I'm trying to do, at least not the way I was implementing each layout. Is there some other layout that could help me with aligning the title text and 4 buttons to the left (horizontally) and aligned with each other vertically. Is it possible to use GridLayout with maybe 5 rows and 3 columns and just fill the 2nd and 3rd columns with nothing? I also want there to be sufficient padding between each object and from the bounds of the window.
This is what I've got so far, but this spreads the width of all the buttons across the entire window:
JLabel title = new JLabel("Inkball", JLabel.LEFT);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 3, 0, 25));
panel.add(title);
panel.add(playNow);
panel.add(highScore);
panel.add(rules);
panel.add(exitGame);
Container c = this.getContentPane();
c.add(panel, BorderLayout.CENTER);
Start with a BorderLayout.
To this, add a JPanel that uses either a GridLayout or GridBagLayout, to the BorderLayout.WEST position of the parent container, this panel will be used to layout the options on the right
Add a JPanel with a CardLayout to the BorderLayout.CENTER position of the first container. This will will allow you to switch between views based on the selection of the user.
You will need to have the ability to obtain notification about user selection from the "menu pane" and update the "view pane". You simply use a ActionListener, registered to the "menu pane", which acts a proxy for the buttons actions and provide information about what the action menus via the actionCommand property of the JButtons and ActionEvent
Have a look at How to Use Buttons, Check Boxes, and Radio Buttons and Laying Out Components Within a Container for more details

Overlap JPanels with WindowBuilder for eclipse

I am using WindowBuilder Pro for eclipse, and I would like to have two Jpanels that perfectly overlap each other. I would then be able to toggle their visibilty based on the selection of a combox box. When I try and acheive this in the gui builder, the first panel gets displaced by the second panel. And advice please?
It is possible using groupLayout, according to the tutorial .
What you must do is add the components to a mother JPanel , and set that panel to use GroupLayout.
Then add the components to the layout as a ParallelGroup in both the horizontal and vertical spacing. This means they will occupy the same X and Y space. Then disable/enable as needed, hiding the JPanels as well.
I believe the way it would work is so:
JPanel panel1, panel2, panel3;
//initialize panel3, etc
panel1=new JPanel();
panel2 = new JPanel();
panel1.add(new JTextField("Panel1"));
panel2.add(new JTextField("PANEL2"));
groupLayout = new GroupLayout(panel3);
panel3.setLayout(groupLayout);
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(panel1)
.addComponent(panel2)
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(panel1)
.addComponent(panel2)
);
panel1.setEnabled(false);
panel1.setVisible(false);
then add a jCheckBox with an ActionPerformed method containing:
if(panel1.isEnabled()) {
panel1.setEnabled(false);
panel1.setVisible(false);
panel2.setEnabled(true);
panel2.setVisible(true);
}else
if(panel2.isEnabled()) {
panel2.setEnabled(false);
panel2.setVisible(false);
panel1.setEnabled(true);
panel1.setVisible(true);
}
That produced the desired behaviour for me. You should be able to switch the JComboBox for the JCheckBox fairly easily.
EDIT: Removed the necessity of having "Jpanel of their own". That should not be the case, and the above method allows you to get the benefits of both GroupLayout and CardLayout.
I would like to have two Jpanels that perfectly overlap each other. I would then be able to toggle their visibilty based on the selection of a combox box
See: How to Use Card Layout for an example that does exactly this.
I would like to have two Jpanels that perfectly overlap each other.
I believe the CardLayout is there exactly for that reason.
Basically, you can nest different panels or 'Cards' using the CardLayout and set the appropriate card to be displayed programmatically (on some event).

Is it possible to have 2 JPanels in a Border layout at the same location?

I'm writing a game which uses a border layout with a JPanel using BorderLayout.CENTER. What I'd like to be able to do is sometimes hide this panel and replace it with another panel with different information. I added both to the container and set visibility of one of them to false.
Then later I try:
panel1.setVisible(false);
panel2.setVisible(true);
but this doesn't display the new panel. I just see gray. Any ideas?
TIA
Use a nested JPanel with a CardLayout for that.

Categories