Add dynamically jpanels - java

I want to generate multiple panels (with title, desc and detail button), this is the general structure:
But when I put this code in a for loop it always adds the last item.
Panel panel_1 = new Panel();
panel_1.setBounds(23, 134, 378, 208);
JLabel lblNewLabel_2 = new JLabel("desc");
lblNewLabel_2.setBounds(0, 69, 189, 69);
JButton btnNewButton_2 = new JButton("Details");
btnNewButton_2.setBounds(104, 139, 189, 69);
panel_1.setLayout(null);
JLabel lblPrv = new JLabel("title");
lblPrv.setBounds(104, 0, 189, 69);
panel_1.add(lblPrv);
panel_1.add(lblNewLabel_2);
JLabel label_1 = new JLabel("");
label_1.setBounds(0, 69, 189, 69);
panel_1.add(label_1);
panel_1.add(btnNewButton_2);
Any suggestion?

Here is an example of how to place multiple panels next to each other:
public class Example extends JFrame {
Example() {
setLayout(new FlowLayout());
for (int i = 0; i < 4; i++)
add(new MyPanel());
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
class MyPanel extends JPanel {
MyPanel() {
setLayout(new BorderLayout());
add(new JLabel("title", SwingConstants.CENTER), BorderLayout.PAGE_START);
add(new JLabel("<html>WWWWWW WWWWWWW<p>WWWWWWWWW<p>RRRRRRR RRRRR RRRRRRR"));
add(new JButton("Details"), BorderLayout.PAGE_END);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Example());
}
}
You should pick the layout managers best suited for your situation.

Related

JButtons not visible until mouseover I also get this problem with JPanel and JLabel

I have an issue with my code below, the buttons and the Panel aren't displayed until mouseover
Could you please help me to identify the reason of this issue ?
Thank you in advance for your help.
Kind regards
JButton atelier;JButton examen;
private JPanel contentPane;
public void principe() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(50, 50,1000,700);
contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel Evenement = new JPanel();
Evenement.setBounds(3, 200, 251, 62);
contentPane.add(Evenement);
Evenement.setLayout(null);
Evenement.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
choix();}
});
JLabel lblNewLabel_5 = new JLabel("Activité");
lblNewLabel_5.setBounds(128, 13, 111, 36);
Evenement.add(lblNewLabel_5);
}
public void choix() {
JPanel m= new JPanel();
m.setBackground(new Color(70, 130, 180));
m.setBounds(260,20, 689, 75);
m.setLayout(null);
atelier = new JButton("Atelier");
atelier.setBounds(486,15, 137, 40);
atelier.setBackground(new Color(200, 180, 150));
m.add(atelier);
examen = new JButton("Examen");
examen.setBounds(344,15, 137, 40);
examen.setBackground(new Color(200, 180, 150));
m.add(examen);
contentPane.add(m);
}

How to layout this GUI?

I am creating a GUI in java. Currently i have an empty JFrame and am trying to add a JPanel to it. The JPanel contains buttons, text etc. However none of this is being properly displayed. My code is as follows:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class memoDisplayUI {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JTextArea jTextBox = new JTextArea();
JScrollPane scroll = new JScrollPane();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
memoDisplayUI frame = new memoDisplayUI();
frame.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public memoDisplayUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame.getContentPane().setBackground(new Color(255, 255, 255));
frame.getContentPane().setLayout(null);
frame.setBounds(100, 100, 270, 400);
frame.setUndecorated(true); //REMOVES MENU BAR
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lblMemos = new JLabel("MEMOS");
lblMemos.setForeground(new Color(100, 149, 237));
lblMemos.setFont(new Font("Moire", Font.BOLD, 30));
lblMemos.setBounds(16, 16, 234, 37);
panel.add(lblMemos);
JButton button = new JButton("");
button.setBackground(new Color(100, 149, 237));
button.setBounds(7, 350, 40, 40);
panel.add(button);
button.setIcon(new ImageIcon("back.png"));
JButton button_1 = new JButton("");
button_1.setBackground(new Color(100, 149, 237));
button_1.setBounds(113, 350, 40, 40);
panel.add(button_1);
button_1.setIcon(new ImageIcon("Edit.png"));
JButton button_2 = new JButton("");
button_2.setBackground(new Color(100, 149, 237));
button_2.setBounds(220, 350, 40, 40);
panel.add(button_2);
button_2.setIcon(new ImageIcon("memo.png"));
JButton btnExit = new JButton("");
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
btnExit.setBorder(null);
btnExit.setIcon(new ImageIcon("Exit.jpg"));
btnExit.setBounds(216, 19, 40, 40);
panel.add(btnExit);
jTextBox = new JTextArea();
scroll.setViewportView(jTextBox); // add scroll panel
jTextBox.setTabSize(4);
jTextBox.setLineWrap(true);
jTextBox.setBackground(new Color(192, 192, 192));
jTextBox.setBounds(8, 60, 255, 286);
panel.add(jTextBox);
frame.setContentPane(panel);
}
}
My desired layout is something very similar to this:
Could someone advise as to why this is.
Thanks for any help :)

Java frame trouble

I need help with my program because I'll want to put a password and username to my program, so if the username is test && password is 12345 my program will appear a new frame but unfortunately my second didn't work for my label, button and etc here is my code so far.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SwapFields extends JFrame{
public static void main(String[] args) {
SwapFields frameTabel = new SwapFields();
}
JButton blogin = new JButton("Login");
JPanel panel = new JPanel();
JTextField txuser = new JTextField(15);
JPasswordField pass = new JPasswordField(15);
JLabel lab = new JLabel("Username :");
JLabel pas = new JLabel("Password :");
JLabel cos;
// JPanel panel = new JPanel();
JButton y1;
JButton y2;
SwapFields() {
super("Enter Your Account !");
setSize(300, 200);
setLocation(500, 280);
panel.setLayout(null);
txuser.setBounds(90, 30, 150, 20);
pass.setBounds(90, 65, 150, 20);
blogin.setBounds(110, 100, 80, 20);
lab.setBounds(15, 28, 150, 20);
pas.setBounds(15, 63, 150, 20);
panel.add(lab);
panel.add(pas);
panel.add(blogin);
panel.add(txuser);
panel.add(pass);
getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
actionlogin();
}
public void actionlogin() {
blogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String puname = txuser.getText();
String ppaswd = pass.getText();
if (puname.equals("test") && ppaswd.equals("12345")) {
JFrame frame = new JFrame("Customer");
JPanel panel1 = new JPanel();
frame.setVisible(true);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cos = new JLabel("Do you have a Customer ?");
y1 = new JButton("Yes");
y2 = new JButton("No");
panel1.setLayout(null);
cos.setBounds(70, 30, 150, 20);
y1.setBounds(80, 65, 150, 20);
y2.setBounds(140, 65, 150, 20);
y1.setSize(55, 30);
y2.setSize(55, 30);
panel1.add(y1);
panel1.add(y2);
panel1.add(cos);
frame.getContentPane().add(panel1);
// this is the code ill change this is the second frame .
dispose();
} else {
JOptionPane.showMessageDialog(null, "Wrong Password / Username");
txuser.setText("");
pass.setText("");
txuser.requestFocus();
}
}
});
}
}
change
panel.setLayout(null);
to
panel.setLayout(new Flowlayout());

How should I make a "back button" to switch JPanels on a main JFrame class work with multiple JPanel classes?

I'm currently working on a game for school, and I've hit a brick wall. I've created a main class that sets up the JFrame, and in that JFrame have JPanel buttons that open a server JPanel class, a client JPanel class, and buttons for options, and exiting the game. Now where I'm stuck is how I should make buttons to go back to the main JPanel using a back button on the server/client JPanel class. Here's the code I have at the moment:
MainUI.class (a different class runs this):
public class MainUI extends JFrame {
private JPanel contentPane;
private JTextField textField;
public MainUI() {
// Sets up the frame
setTitle("Pong Legacy | Prototype v0.1.0");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 500);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
// Starts the Server window
JButton btnStartServer = new JButton("Start Server");
btnStartServer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JPanel server = new ServerUI();
getContentPane().removeAll();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(server);
getContentPane().validate();
getContentPane().repaint();
}
});
btnStartServer.setBounds(97, 364, 100, 25);
contentPane.add(btnStartServer);
// Starts the Client window
JButton btnStartClient = new JButton("Start Client");
btnStartClient.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JPanel client = new ClientUI();
getContentPane().removeAll();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(client);
getContentPane().validate();
getContentPane().repaint();
}
});
btnStartClient.setBounds(97, 400, 100, 25);
contentPane.add(btnStartClient);
// Opens the Options menu
// (To Do)
JButton btnOptions = new JButton("Options");
btnOptions.setBounds(37, 436, 100, 25);
contentPane.add(btnOptions);
// Quits the game
JButton btnQuitGame = new JButton("Quit Game");
btnQuitGame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnQuitGame.setBounds(157, 436, 100, 25);
contentPane.add(btnQuitGame);
// Username Field
JLabel lblUsername = new JLabel("Username:");
lblUsername.setBounds(121, 45, 52, 14);
contentPane.add(lblUsername);
textField = new JTextField();
textField.setBounds(104, 67, 86, 20);
contentPane.add(textField);
textField.setColumns(10);
}
}
ServerUI.class:
public class MainUI extends JFrame {
private JPanel contentPane;
private JTextField textField;
public MainUI() {
// Sets up the frame
setTitle("Pong Legacy | Prototype v0.1.0");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 500);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
// Starts the Server window
JButton btnStartServer = new JButton("Start Server");
btnStartServer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JPanel server = new ServerUI();
getContentPane().removeAll();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(server);
getContentPane().validate();
getContentPane().repaint();
}
});
btnStartServer.setBounds(97, 364, 100, 25);
contentPane.add(btnStartServer);
// Starts the Client window
JButton btnStartClient = new JButton("Start Client");
btnStartClient.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JPanel client = new ClientUI();
getContentPane().removeAll();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(client);
getContentPane().validate();
getContentPane().repaint();
}
});
btnStartClient.setBounds(97, 400, 100, 25);
contentPane.add(btnStartClient);
// Opens the Options menu
// (To Do)
JButton btnOptions = new JButton("Options");
btnOptions.setBounds(37, 436, 100, 25);
contentPane.add(btnOptions);
// Quits the game
JButton btnQuitGame = new JButton("Quit Game");
btnQuitGame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnQuitGame.setBounds(157, 436, 100, 25);
contentPane.add(btnQuitGame);
// Username Field
JLabel lblUsername = new JLabel("Username:");
lblUsername.setBounds(121, 45, 52, 14);
contentPane.add(lblUsername);
textField = new JTextField();
textField.setBounds(104, 67, 86, 20);
contentPane.add(textField);
textField.setColumns(10);
}
}
ClientUI.class:
public class ClientUI extends JPanel {
private JTextField textField;
public ClientUI() {
setLayout(null);
JButton btnConnect = new JButton("Connect");
btnConnect.setBounds(47, 400, 200, 25);
add(btnConnect);
JButton btnBack = new JButton("Back");
btnBack.setBounds(117, 436, 60, 25);
add(btnBack);
JRadioButton rdbtnSelectAServer = new JRadioButton("Select a server from the list:");
rdbtnSelectAServer.setBounds(66, 25, 161, 25);
add(rdbtnSelectAServer);
JRadioButton rdbtnManualConnection = new JRadioButton("Manual Connection:");
rdbtnManualConnection.setBounds(87, 325, 120, 25);
add(rdbtnManualConnection);
textField = new JTextField();
textField.setBounds(47, 355, 200, 25);
add(textField);
textField.setColumns(10);
}
}
I've heard that I could use a CardLayout, too, but I want to see if I can do it this way.

Adding a custom component manually in swing group layout

I have this class which creates a grid:
class GridPane extends JPanel{
public GridPane(int row,int col){
setLayout(new GridLayout(row,col));
setBorder(BorderFactory.createEmptyBorder(1,1,1,1));
for (int i =1; i<=(row*col); i++)
{
JPanel pan = new JPanel();
pan.setBackground(Color.RED);
pan.setPreferredSize(new Dimension(3,3));
pan.setBorder(BorderFactory.createLineBorder(Color.BLACK));
add(pan);
}
}
And this which sets up the empty main frame with the group layout:
public MainFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGap(0, 440, Short.MAX_VALUE)
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGap(0, 268, Short.MAX_VALUE)
);
contentPane.setLayout(gl_contentPane);
}
I am not very experienced with SWING and I have been trying to add a GridPane object to the Right of the Main Frame for some time. It keeps telling me something like :
java.lang.IllegalStateException: GridPane[,0,0,0x0,invalid,layout=java.awt.GridLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.EmptyBorder#6ba7508a,flags=9,maximumSize=,minimumSize=,preferredSize=] is not attached to a vertical/horizontal group
Does anyone know what is going on? What should I do to accomplish what I need? Sorry for posting lots of code. Thanks for the help.
I would suggest that in this case you make use of BorderLayout.
contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
contentPane.add(pan, BorderLayout.LINE_END);
Here is the full code. I hope this is what you are looking for.
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
GridPane gp = new GridPane(5, 5);
frame.add(gp, BorderLayout.EAST);
frame.setVisible(true);
frame.setSize(250, 250);
}
}
class GridPane extends JPanel {
public GridPane(int row, int col) {
setLayout(new GridLayout(row, col));
setPreferredSize(new Dimension(125, 125));
setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
for (int i = 1; i <= (row * col); i++) {
JPanel pan = new JPanel();
pan.setBackground(Color.RED);
pan.setPreferredSize(new Dimension(3, 3));
pan.setBorder(BorderFactory.createLineBorder(Color.BLACK));
add(pan);
}
}
}
Useful links:
A Visual Guide to Layout Managers
How to Use BorderLayout
How to Use GroupLayout

Categories