Jframe. adding 2 Jpanel components in one frame. Doesnt work - java

Problem : i'm trying to add a 2nd Jpanel in my frame but when i add this latter, it overwrites the previous one. The purpose is to have 2 components (Jpanels) in the same frame but it seem to accept only one but not both. The order of appearence should be in one column and two rows:
1: Enter name:
2: TextField
import javax.swing.*;
import java.awt.*;
public class Money2 extends JFrame {
public Money2() {
// setLayout(new GridLayout(2,2));
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout(FlowLayout.CENTER));
p1.add(new JLabel("Enter name:"));
// -------------------------------------------------------------------------
// p2.setLayout(new FlowLayout(FlowLayout.CENTER));
JPanel p2 = new JPanel(new FlowLayout());
p2.add(new JTextField(8));
add(p1); // add to Jframe
add(p2);
}
/** Main method */
public static void main(String[] args) {
Money2 frame = new Money2();
frame.setTitle("Money Converter App");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350, 400);
frame.setVisible(true);
}
}

Both panels cannot occupy the same location in a BorderLayout. You could place panel p1 at a different location:
add(p1, BorderLayout.PAGE_START);

Related

Is there an option to add ScrollPane without disabling CardLayout?

I have a card layout where I switch panels with a button. However, the code (switching panels) works only when lines:
JScrollPane scrPane = new JScrollPane(card1);
frame.add(scrPane);
are removed. In other case, clicking button achieves nothing. Is there an option to keep the scrolling (I need this, since the main application will have a lot of wrapped text) without disabling an option to switch cards?
package com.code;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.awt.event.*;
public class Card {
public static void main(String[] args) {
JFrame frame = new JFrame("App");
frame.setVisible(true);
frame.setSize(1200, 800);//Give it a size
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel mainPanel = new JPanel(new CardLayout());
frame.add(mainPanel);
JPanel menu = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel card1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel card2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
mainPanel.add(menu, "menu");
mainPanel.add(card1, "card1");
mainPanel.add(card2, "card2");
JLabel l1 = new JLabel("label 1");
JLabel l2 = new JLabel("label 2");
card1.add(l1);
card2.add(l2);
JButton click = new JButton("Click!");
menu.add(click);
JScrollPane scrPane = new JScrollPane(card1);
frame.add(scrPane);
click.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
CardLayout cardLayout = (CardLayout) mainPanel.getLayout();
cardLayout.show(mainPanel, "card1");
}
});
}
}
A JFrame (its content pane) uses BorderLayout by default. That means you can have only 1 component at BorderLayout.CENTER. When you frame.add(component) the default constraints is BorderLayout.CENTER.
Now, you frame.add(mainPanel); and then frame.add(scrPane);. So main panel is removed, since scrPane is being added after it.
Doing JScrollPane scrPane = new JScrollPane(card1); it means you add a scrollpane to card1, and not in content pane. I guess that you want it to the content pane (the whole frame). So the fix is to delete frame.add(mainPanel); and do the following:
JScrollPane scrPane = new JScrollPane(mainPanel);
frame.add(scrPane);
Now, the main panel is added to scrPane and scrPane is added to the frame.
However, your GUI will be empty after that, because you frame.setVisible(true); before you are finished adding components to it. Take a look at Why shouldn't I call setVisible(true) before adding components?
Eventually, full code is:
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("App");
frame.setSize(1200, 800);//Give it a size
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel mainPanel = new JPanel(new CardLayout());
JPanel menu = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel card1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel card2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
mainPanel.add(menu, "menu");
mainPanel.add(card1, "card1");
mainPanel.add(card2, "card2");
JLabel l1 = new JLabel("label 1");
JLabel l2 = new JLabel("label 2");
card1.add(l1);
card2.add(l2);
JButton click = new JButton("Click!");
menu.add(click);
JScrollPane scrPane = new JScrollPane(mainPanel);
frame.add(scrPane);
click.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
CardLayout cardLayout = (CardLayout) mainPanel.getLayout();
cardLayout.show(mainPanel, "card1");
}
});
frame.setVisible(true);
});
}
Some good links I suggest you to read are the Initial Threads and What does .pack() do?

align left and right two JLabels in a "North" BorderLayout

I am using BorderLayout for my application.
setLayout(new BorderLayout());
I need to align two JLabels in left and right in the "NORTH" of the JPanel.
Here is my code:
JPanel top = new JPanel();
top.add(topTxtLabel);
top.add(logoutTxtLabel);
add(BorderLayout.PAGE_START, top);
So I need topTxtLabel in left and logoutTxtLabel in right.
I tried to implement Border Layout again to use "WEST" and "EAST", but it didn't worked. Ideas?
Assuming your application consists of a JFrame with BorderLayout you could try this: Set the layout mode of your JPanel again to BorderLayout. Add the panel in the north of the frame. Then add the 2 JLabels in the east and west. You can also replace the JFrame with another JPanel.
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main
{
public static void main(String[] args)
{
new Main();
}
Main()
{
JFrame frame = new JFrame("MyFrame");
frame.setLayout(new BorderLayout());
JPanel panel = new JPanel(new BorderLayout());
JLabel left = new JLabel("LEFT");
JLabel right = new JLabel("RIGHT");
JPanel top = new JPanel(new BorderLayout());
top.add(left, BorderLayout.WEST);
top.add(right, BorderLayout.EAST);
panel.add(top, BorderLayout.NORTH);
frame.add(panel, BorderLayout.NORTH);
frame.add(new JLabel("Another dummy Label"), BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Multiple JPanels - All buttons same size

I have a a main panel that contains multiple panels inside. Each 'children' panel contains one (or more) JButtons. Since I am displaying all the panels at the same time, I would like to make all the buttons the same size (to have consistency).
This code illustrates my problem:
public class Test
{
public static void main(String[] args)
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(new Dimension(200, 200));
// 1st Panel
JPanel panel1 = new JPanel(new MigLayout());
panel1.add(new JButton("button in panel 1"));
// 2nd Panel
JPanel panel2 = new JPanel(new MigLayout());
panel2.add(new JButton("2nd button"));
JPanel parent = new JPanel(new MigLayout("wrap"));
parent.add(panel1, "pushx, growx");
parent.add(new JSeparator(), "pushx, growx");
parent.add(panel2, "pushx, growx");
f.add(parent);
f.setVisible(true);
}
}
The size of the "button in panel 1" is different from the button in the other panel. Is there an 'easy' way to set their size using the layout? (Hardcoding the size is NOT an option).
I think you didn't read the document carefully, to demonstrate the use I have written a code. You should not copy the same snippet showed in that link. They have specified that the component you have updating must be passed into updateComponentTreeUI() method. You can replace the size by replacing the "large" with "small" or "mini".
import javax.swing.*;
import java.awt.*;
public class Demo {
/**
* #param args
*/
JFrame frame ;
JButton btn;
public Demo()
{
frame = new JFrame("Example");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setLayout(new FlowLayout());
btn = new JButton("Example");
btn.putClientProperty("JComponent.sizeVariant", "large");
SwingUtilities.updateComponentTreeUI(btn);
frame.add(btn);
frame.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}
catch(Exception e)
{
e.printStackTrace();
}
Demo d = new Demo();
}
}

Switching JPanels in a JFrame without CardLayout

How can I switch Panels with ScrollPanes in a Frame? I've tried many possible ways but cannot come up with a solution to.
Actually this is one of the Java Problems my professor gave me and I needed to accomplish this by not using other layouts (such as CardLayout) and I should use the null layout only. Additional classes are allowed as long as I maintain these three classes and the scroll pane.
public class MainDriver {
public static void main(String[] args) {
JFrame frame = new JFrame("Frame");
panel1 p1 = new panel1();
panel2 p2 = new panel2();
JScrollPane jsp = new JScrollPane(panel1.panel);
Container c = frame.getContentPane();
jsp.getVerticalScrollBar().setUnitIncrement(10);
c.add(jsp);
//codes for panel switching from panel1 to panel2 vice versa
frame.setDefaultCloseOperation(JFrame.exit_ON_CLOSE);
frame.setSize(1058, 600);
frame.setLocation(100, 50);
frame.setVisible(true);
}
}
---------------------------------------------
public class panel1{
public JPanel panel(){
JPanel fore = new JPanel();
fore.setLayout(null);
fore.setPreferredSize(new Dimension (1024, 600));
fore.setBackground(Color.decode("#004050"));
fore.setVisible(true);
JButton but = new JButton();
but.setLocation(425, 300);
but.setSize(100, 35);
//button action/mouse listener
fore.add(but);
return fore;
}
}
---------------------------------------------
public class panel2{
public JPanel panel(){
JPanel fore = new JPanel();
fore.setLayout(null);
fore.setPreferredSize(new Dimension (1024, 600));
fore.setBackground(Color.decode("#004050"));
fore.setVisible(true);
JButton but = new JButton();
but.setLocation(425, 300);
but.setSize(100, 35);
//button action/mouse listener
fore.add(but);
return fore;
}
}
How can I switch Panels with ScrollPanes in a Frame?
scrollPane.setViewportView( anotherPanel );

Create a frame using FlowLayout

Okay, so I'm having some trouble with my Programming Exercise today.
The Exercise text goes like this:
(Use the FlowLayout manager) Write a program that meets the following requirements:
Create a frame and set its layout to FlowLayout
Create two panels and add them to the frame
Each panel contains three buttons. The panel uses FlowLayout
The buttons should be named "Button 1", "Button 2" and so on.
I think I'm having some trouble with adding the panels to the frame because when i run the program, it shows an empty frame.
Here is the code i have.
import javax.swing.*;
import java.awt.*;
public class Exercise12_1 extends JFrame {
public Exercise12_1() {
setLayout(new FlowLayout());
JFrame frame = new JFrame(" Exercise 12_1 ");
frame.setLayout(new FlowLayout());
// Create two panels
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
panel1.setLayout(new FlowLayout());
panel2.setLayout(new FlowLayout());
// Add three buttons to each panel
panel1.add(new JButton(" Button 1 "));
panel1.add(new JButton(" Button 2 "));
panel1.add(new JButton(" Button 3 "));
panel2.add(new JButton(" Button 4 "));
panel2.add(new JButton(" Button 5 "));
panel2.add(new JButton(" Button 6 "));
// Add panels to frame
frame.add(panel1);
frame.add(panel2);
}
public static void main(String[] args) {
Exercise12_1 frame = new Exercise12_1();
frame.setTitle(" Exercise 12_1 ");
frame.setSize(600, 100);
frame.setLocationRelativeTo(null); // center frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
I would greatly appreciate it if some of you took your time to help me out here.
Thanks.
Your main method creates a frame:
Exercise12_1 frame = new Exercise12_1();
and then makes it visible.
And the constructor of this 'Exercise12_1' frame creates another frame, and adds panel to this other frame:
JFrame frame = new JFrame(" Exercise 12_1 ");
frame.setLayout(new FlowLayout());
The constructor shouldn't create another frame. It should add the panels to this: the frame being constructed, and that will then be made visible.
Also, you should not use setSize(), but pack(), to make the frame have the most appropriate size based on the preferred size of all the components it contains.
Check this:
import javax.swing.*;
import java.awt.*;
public class Exercise12_1 extends JFrame {
public Exercise12_1() {
setLayout(new FlowLayout());
//JFrame frame = new JFrame(" Exercise 12_1 ");
this.setLayout(new FlowLayout());
// Create two panels
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
panel1.setLayout(new FlowLayout());
panel2.setLayout(new FlowLayout());
// Add three buttons to each panel
panel1.add(new JButton(" Button 1 "));
panel1.add(new JButton(" Button 2 "));
panel1.add(new JButton(" Button 3 "));
panel2.add(new JButton(" Button 4 "));
panel2.add(new JButton(" Button 5 "));
panel2.add(new JButton(" Button 6 "));
// Add panels to frame
this.add(panel1);
this.add(panel2);
}
public static void main(String[] args) {
Exercise12_1 frame = new Exercise12_1();
frame.setTitle(" Exercise 12_1 ");
frame.setSize(600, 100);
frame.setLocationRelativeTo(null); // center frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
You use use such code like this.getContentPane().add(panel1); to add Panel.
Change
frame.add(panel1);
frame.add(panel2);
To
this.getContentPane().add(panel1);
this.getContentPane().add(panel2);
and it will be working then.

Categories