hello guys hope so all are doing well I am new to java swing and and trying build very simple app and I am stocked in problem that I was trying to open jpanal that contain a form in main dashboard I am attaching some pics of gui please see and let me know is there any way to add that form to main dashboard
my program structure is:
one dashboard class that contain button of add and a jpanal that can hold that form
one form class that contain form
I am trying to display this form into main dashboard jpanal
package school_management_system;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class dashboard extends JFrame implements ActionListener {
JLabel header_label=new JLabel();
JPanel panel_header=new JPanel();
JPanel panel_left=new JPanel();
JPanel panel_center=new JPanel();
JPanel panel_left_sub1=new JPanel();
JPanel panel_left_sub2=new JPanel();
JPanel panel_left_sub3=new JPanel();
JButton add_item_bt=new JButton();
JButton update_item_bt=new JButton();
JButton add_customer_bt=new JButton();
dashboard()
{
setlocandsize();
designcomponents();
this.setVisible(true);
this.setSize(620, 620);
this.setLayout(new BorderLayout());
this.add(panel_header, BorderLayout.NORTH);
this.add(panel_left, BorderLayout.WEST);
this.add(panel_center, BorderLayout.CENTER);
add_item_bt.addActionListener(this);
panel_header.add(header_label);
panel_left.add(panel_left_sub1, BorderLayout.NORTH);
panel_left.add(panel_left_sub2, BorderLayout.NORTH);
panel_left.add(panel_left_sub3, BorderLayout.NORTH);
panel_left_sub1.add(add_item_bt);
panel_left_sub2.add(update_item_bt);
panel_left_sub3.add(add_customer_bt);
}
public void setlocandsize()
{
panel_header.setPreferredSize(new Dimension(100,100));
panel_left.setPreferredSize(new Dimension(150,150));
panel_center.setPreferredSize(new Dimension(100,100));
panel_left_sub1.setPreferredSize(new Dimension(130,50));
panel_left_sub2.setPreferredSize(new Dimension(130,50));
panel_left_sub3.setPreferredSize(new Dimension(130,50));
add_item_bt.setPreferredSize(new Dimension(120,30));
update_item_bt.setPreferredSize(new Dimension(120,30));
add_customer_bt.setPreferredSize(new Dimension(120,30));
panel_left.setLayout(new FlowLayout(FlowLayout.CENTER,10,20));
panel_left_sub1.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
panel_left_sub2.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
panel_left_sub3.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
panel_header.setLayout(new FlowLayout(FlowLayout.CENTER,10,20));
}
public void designcomponents()
{
panel_header.setBackground(Color.red);
panel_left.setBackground(Color.blue);
panel_center.setBackground(Color.DARK_GRAY);
panel_left_sub1.setBackground(Color.yellow);
panel_left_sub2.setBackground(Color.green);
panel_left_sub3.setBackground(Color.orange);
add_item_bt.setText("Add Item");
update_item_bt.setText("Update item");
add_customer_bt.setText("Add Customer");
header_label.setText("Staff Dashboard");
header_label.setFont(new Font("MV Boli",Font.BOLD,20));
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==add_item_bt)
{
new add_item();
}
}
}
'
'
package school_management_system;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class add_item extends JFrame {
JPanel add_item_panel=new JPanel();
JLabel item_panel_label=new JLabel();
JLabel item_name_label=new JLabel();
JTextField item_name_tf=new JTextField();
JLabel item_price_label=new JLabel();
JTextField item_price_tf=new JTextField();
JButton add_item_bt=new JButton();
JDialog add_item_dialog=new JDialog();
add_item()
{
JDialog d1;
d1=new JDialog();
setlocandsize();
designcomponents();
d1.setLayout(new BorderLayout());
d1.add(add_item_panel);
d1.setVisible(true);
d1.setSize(500, 500);
add_item_panel.add(item_panel_label);
add_item_panel.add(item_name_label);
add_item_panel.add(item_name_tf);
add_item_panel.add(item_price_label);
add_item_panel.add(item_price_tf);
add_item_panel.add(add_item_bt);
}
public void setlocandsize()
{
add_item_panel.setBounds(100, 50, 400, 400);
add_item_panel.setLayout(null);
add_item_panel.setBackground(Color.gray);
item_panel_label.setBounds(130, 0, 200, 100);
item_name_label.setBounds(90, 60, 100, 100);
item_name_tf.setBounds(210, 95, 100, 25);
item_price_label.setBounds(90, 100, 100, 100);
item_price_tf.setBounds(210, 135, 100, 25);
add_item_bt.setBounds(230, 175, 80, 25);
}
public void designcomponents()
{
item_panel_label.setText("Add Item");
item_panel_label.setFont(new Font("MV Boli",Font.BOLD,30));
item_name_label.setText("Item Name:");
item_name_label.setFont(new Font("MV Boli",Font.BOLD,15));
item_price_label.setText("Item Name:");
item_price_label.setFont(new Font("MV Boli",Font.BOLD,15));
add_item_bt.setText("Add");
}
}
'
First, let's fix some issues with the GUI. Read the comment to follow the changes :
//rename to follow naming conventions
public class Dashboard extends JFrame implements ActionListener {
//rename to follow naming conventions
JLabel headerLabel=new JLabel();
JPanel panelHeader=new JPanel();
JPanel panelLeft=new JPanel();
JPanel panelCenter=new JPanel();
JPanel panelLeftSub1=new JPanel();
JPanel panelLeftSub2=new JPanel();
JPanel panelLeftSub3=new JPanel();
JButton addItemBtn=new JButton();
JButton updateItemBtn=new JButton();
JButton addCustomerBtn=new JButton();
Dashboard()
{
setlocandsize();
designcomponents();
//this.setSize(620, 620); let pack() size the window to its children
this.setLayout(new BorderLayout());
this.add(panelHeader, BorderLayout.NORTH);
this.add(panelLeft, BorderLayout.WEST);
this.add(panelCenter, BorderLayout.CENTER);
addItemBtn.addActionListener(this);
panelHeader.add(headerLabel);
//JPanel uses FlowLayout as default so BorderLayout.NORTH has no effect;
panelLeft.add(panelLeftSub1);
panelLeft.add(panelLeftSub2);
panelLeft.add(panelLeftSub3);
panelLeftSub1.add(addItemBtn);
panelLeftSub2.add(updateItemBtn);
panelLeftSub3.add(addCustomerBtn);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);//always at the end
}
public void setlocandsize()
{
//todo override getPreferredSize instead of setting proffered size
panelHeader.setPreferredSize(new Dimension(100,100));
panelLeft.setPreferredSize(new Dimension(150,250));
panelCenter.setPreferredSize(new Dimension(100,100));
panelLeftSub1.setPreferredSize(new Dimension(130,50));
panelLeftSub2.setPreferredSize(new Dimension(130,50));
panelLeftSub3.setPreferredSize(new Dimension(130,50));
//typically there is no need to set size of buttons
//addItemBtn.setPreferredSize(new Dimension(120,30));
//updateItemBtn.setPreferredSize(new Dimension(120,30));
//addCustomerBtn.setPreferredSize(new Dimension(120,30));
panelLeft.setLayout(new FlowLayout(FlowLayout.CENTER,10,20));
panelLeftSub1.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
panelLeftSub2.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
panelLeftSub3.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
panelHeader.setLayout(new FlowLayout(FlowLayout.CENTER,10,20));
}
public void designcomponents()
{
panelHeader.setBackground(Color.red);
panelLeft.setBackground(Color.blue);
panelCenter.setBackground(Color.DARK_GRAY);
panelLeftSub1.setBackground(Color.yellow);
panelLeftSub2.setBackground(Color.green);
panelLeftSub3.setBackground(Color.orange);
addItemBtn.setText("Add Item");
updateItemBtn.setText("Update item");
addCustomerBtn.setText("Add Customer");
headerLabel.setText("Staff Dashboard");
headerLabel.setFont(new Font("MV Boli",Font.BOLD,20));
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==addItemBtn)
{
new AddItem();
}
}
public static void main(String[] args) {
new Dashboard();
}
}
//rename to follow naming conventions and use JDialog
class AddItem extends JDialog {
JPanel addItemPanel=new JPanel();
JLabel itemPanelLabel=new JLabel();
JTextField itemNameTf=new JTextField(15);//set size of TF by setting columns
JTextField itemPriceTf=new JTextField(15);
JButton additemBtn=new JButton();
AddItem()
{
designcomponents();
setLayout(new BorderLayout());
add(addItemPanel);
addItemPanel.setLayout(new BoxLayout(addItemPanel, BoxLayout.PAGE_AXIS));
addItemPanel.add(itemPanelLabel);
addItemPanel.add(makeEntryPanel("Item Name:", itemNameTf));
addItemPanel.add(makeEntryPanel("Item Price:", itemPriceTf));
addItemPanel.add(additemBtn);
addItemPanel.setBackground(Color.gray);
pack();
setVisible(true);
}
public void designcomponents()
{
itemPanelLabel.setText("Add Item");
itemPanelLabel.setFont(new Font("MV Boli",Font.BOLD,30));
additemBtn.setText("Add");
}
private JPanel makeEntryPanel(String text, JTextField tf ){
JPanel entry = new JPanel();
JLabel label = new JLabel(text);
label.setFont(new Font("MV Boli",Font.BOLD,15));
entry.add(label);
entry.add(tf);
return entry;
}
}
Next introduce an Item object:
class Item {
private String name;
private double price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
Now modify Dashboard and AddItem to use and share an Item:
Modify Dashboard to construct an Item object, and open AddItem dialog that configures that object:
public class Dashboard extends JFrame {
JLabel headerLabel=new JLabel();
JPanel panelHeader=new JPanel();
JPanel panelLeft=new JPanel();
JPanel panelCenter=new JPanel();
JPanel panelLeftSub1=new JPanel();
JPanel panelLeftSub2=new JPanel();
JPanel panelLeftSub3=new JPanel();
JButton addItemBtn=new JButton();
JButton updateItemBtn=new JButton();
JButton addCustomerBtn=new JButton();
Dashboard()
{
setlocandsize();
designcomponents();
this.setLayout(new BorderLayout());
this.add(panelHeader, BorderLayout.NORTH);
this.add(panelLeft, BorderLayout.WEST);
this.add(panelCenter, BorderLayout.CENTER);
addItemBtn.addActionListener(e-> addItem());
panelHeader.add(headerLabel);
panelLeft.add(panelLeftSub1);
panelLeft.add(panelLeftSub2);
panelLeft.add(panelLeftSub3);
panelLeftSub1.add(addItemBtn);
panelLeftSub2.add(updateItemBtn);
panelLeftSub3.add(addCustomerBtn);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);//always at the end
}
public void setlocandsize()
{
//todo override getPreferredSize instead of setting proffered size
panelHeader.setPreferredSize(new Dimension(100,100));
panelLeft.setPreferredSize(new Dimension(150,250));
panelCenter.setPreferredSize(new Dimension(100,100));
panelLeftSub1.setPreferredSize(new Dimension(130,50));
panelLeftSub2.setPreferredSize(new Dimension(130,50));
panelLeftSub3.setPreferredSize(new Dimension(130,50));
panelLeft.setLayout(new FlowLayout(FlowLayout.CENTER,10,20));
panelLeftSub1.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
panelLeftSub2.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
panelLeftSub3.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
panelHeader.setLayout(new FlowLayout(FlowLayout.CENTER,10,20));
}
public void designcomponents()
{
panelHeader.setBackground(Color.red);
panelLeft.setBackground(Color.blue);
panelCenter.setBackground(Color.DARK_GRAY);
panelLeftSub1.setBackground(Color.yellow);
panelLeftSub2.setBackground(Color.green);
panelLeftSub3.setBackground(Color.orange);
addItemBtn.setText("Add Item");
updateItemBtn.setText("Update item");
addCustomerBtn.setText("Add Customer");
headerLabel.setText("Staff Dashboard");
headerLabel.setFont(new Font("MV Boli",Font.BOLD,20));
}
private void addItem(){
Item item = new Item();
new AddItem(item); //invoke AddItem that updates item
//after AddItem is closed, print details for testing
System.out.println(item.getName() + " price:" + item.getPrice());
//todo display details in gui
}
public static void main(String[] args) {
new Dashboard();
}
}
the modal AddItem configures the shared Item object and closes when done:
class AddItem extends JDialog {
private final Item item;
JPanel addItemPanel=new JPanel();
JLabel itemPanelLabel=new JLabel();
JTextField itemNameTf=new JTextField(15);//set size of TF by setting columns
JTextField itemPriceTf=new JTextField(15);
JButton additemBtn=new JButton();
AddItem(Item item)
{
this.item = item;
setModal(true);
designcomponents();
setLayout(new BorderLayout());
add(addItemPanel);
addItemPanel.setLayout(new BoxLayout(addItemPanel, BoxLayout.PAGE_AXIS));
addItemPanel.add(itemPanelLabel);
addItemPanel.add(makeEntryPanel("Item Name:", itemNameTf));
addItemPanel.add(makeEntryPanel("Item Price:", itemPriceTf));
addItemPanel.add(additemBtn);
addItemPanel.setBackground(Color.gray);
pack();
setVisible(true);
}
public void designcomponents()
{
itemPanelLabel.setText("Add Item");
itemPanelLabel.setFont(new Font("MV Boli",Font.BOLD,30));
additemBtn.setText("Add");
additemBtn.addActionListener(e->updateItemDetails());
}
private void updateItemDetails() {
//todo check input
//configure item
item.setName(itemNameTf.getText());
item.setPrice(Double.valueOf(itemPriceTf.getText()));
dispose();//close dialog
}
private JPanel makeEntryPanel(String text, JTextField tf ){
JPanel entry = new JPanel();
JLabel label = new JLabel(text);
label.setFont(new Font("MV Boli",Font.BOLD,15));
entry.add(label);
entry.add(tf);
return entry;
}
}
Related
I'll try to explain the problem as best i can.
Basically what I have is a system that takes in customer data (Customer is an object) and then adds it to an array. I have that working no problems. What I'm trying to get working is that I also have an option that should enable the user to edit a customers details. The way I'm trying to implement this is; in a separate GUI, I have a comboBox which will be populated with all of the customers first names that are currently in the arrayList. This also works.
Here is my problem - what I want to happen is that when one of the customers first names is chosen from the comboBox, it should find that object, and then fill out the JTextFields with its respective data, e.g. that customers first name should pop into the first name textField and his second name into the second name TextField etc..
I cant figure out this problem so any help is appreciated!
This is only one of a few classes, the rest of which work.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.MaskFormatter;
import javax.swing.JLabel;
import java.awt.SystemColor;
import java.text.ParseException;
import java.util.ArrayList;
import java.awt.Font;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.UIManager;
import java.awt.GridLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
public class editCustomers extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
ArrayList<Customers> customerList;
private MaskFormatter mask = null;
/**
* Create the frame.
*/
public editCustomers(ArrayList<Customers> aList) {
customerList = aList;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 700, 600);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(new GridLayout(6, 0, 0, 0));
JPanel panel_12 = new JPanel();
panel.add(panel_12);
panel_12.setLayout(new BorderLayout(0, 0));
JLabel lblSelectACustomer = new JLabel("Select a Customer to Edit");
panel_12.add(lblSelectACustomer, BorderLayout.WEST);
JComboBox comboBox = new JComboBox();
//comboBox.setModel(new DefaultComboBoxModel());
for (Customers temp : customerList) {
comboBox.addItem(temp.getfName());
}
panel_12.add(comboBox, BorderLayout.EAST);
JPanel panel_3 = new JPanel();
panel.add(panel_3);
panel_3.setLayout(new BorderLayout(0, 0));
JLabel lbl1 = new JLabel("Edit Customers First Name");
lbl1.setVerticalAlignment(SwingConstants.TOP);
Dimension d = lbl1.getPreferredSize();
lbl1.setPreferredSize(new Dimension(d.width + 50, d.height));
panel_3.add(lbl1, BorderLayout.WEST);
JPanel panel_8 = new JPanel();
panel_3.add(panel_8, BorderLayout.CENTER);
textField = new JTextField();
panel_8.add(textField);
textField.setColumns(30);
JPanel panel_4 = new JPanel();
panel.add(panel_4);
panel_4.setLayout(new BorderLayout(0, 0));
JLabel lbl2 = new JLabel("Edit Customers Second Name");
lbl2.setVerticalAlignment(SwingConstants.TOP);
lbl2.getPreferredSize();
lbl2.setPreferredSize(new Dimension(d.width + 50, d.height));
panel_4.add(lbl2, BorderLayout.WEST);
JPanel panel_9 = new JPanel();
panel_4.add(panel_9, BorderLayout.CENTER);
textField_1 = new JTextField();
panel_9.add(textField_1);
textField_1.setColumns(30);
JPanel panel_5 = new JPanel();
panel.add(panel_5);
panel_5.setLayout(new BorderLayout(0, 0));
JLabel lbl3 = new JLabel("Edit Customers Address");
lbl3.setVerticalAlignment(SwingConstants.TOP);
lbl3.getPreferredSize();
lbl3.setPreferredSize(new Dimension(d.width + 50, d.height));
panel_5.add(lbl3, BorderLayout.WEST);
JPanel panel_10 = new JPanel();
panel_5.add(panel_10, BorderLayout.CENTER);
textField_2 = new JTextField();
panel_10.add(textField_2);
textField_2.setColumns(30);
JPanel panel_6 = new JPanel();
panel.add(panel_6);
panel_6.setLayout(new BorderLayout(0, 0));
JLabel lbl4 = new JLabel("Edit Customers Date of Birth");
lbl4.setVerticalAlignment(SwingConstants.TOP);
lbl4.getPreferredSize();
lbl4.setPreferredSize(new Dimension(d.width + 50, d.height));
panel_6.add(lbl4, BorderLayout.WEST);
JPanel panel_11 = new JPanel();
panel_6.add(panel_11, BorderLayout.CENTER);
textField_3 = new JTextField();
try {
mask = new MaskFormatter("##/##/####");
mask.setPlaceholderCharacter(' ');
} catch (ParseException e) {
e.printStackTrace();
}
JFormattedTextField textField_3 = new JFormattedTextField(mask);
textField_3.setText("dd/mm/yyyy");
panel_11.add(textField_3);
textField_3.setColumns(30);
JPanel panel_2 = new JPanel();
panel.add(panel_2);
panel_2.setLayout(new BorderLayout(0, 0));
JPanel panel_7 = new JPanel();
panel_2.add(panel_7, BorderLayout.CENTER);
JButton btnConfirm = new JButton("Confirm");
btnConfirm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
panel_7.add(btnConfirm);
JButton btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
panel_7.add(btnCancel);
JPanel panel_1 = new JPanel();
panel_1.setBackground(SystemColor.activeCaption);
contentPane.add(panel_1, BorderLayout.NORTH);
JLabel lblEditACurrent = new JLabel("Edit a Current Customer");
lblEditACurrent.setForeground(Color.BLACK);
lblEditACurrent.setFont(new Font("Arial", Font.PLAIN, 19));
lblEditACurrent.setBackground(UIManager.getColor("InternalFrame.activeTitleBackground"));
panel_1.add(lblEditACurrent);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
editCustomers frame = new editCustomers(new ArrayList<Customers>());
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
You can try something like this ....
Change line 63 - 67 as following. Put the customers into the combobox instead of just names.
JComboBox<Customer> comboBox = new JComboBox<>();
//comboBox.setModel(new DefaultComboBoxModel());
for (Customers temp : customerList) {
comboBox.addItem(temp);
}
Then implement an ActionListener for the combobox as following.
comboBox.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Customer customer = comboBox.getSelectedItem();
// Use the above customer object's fields to populate your text fields
}
});
I'm having a problem were a can't make any of the created buttons visible in my frame.
I am trying to create a Frame with 2 layouts the top part is a blank layout and the bottom layout will have three buttons.
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.*;
public class Game_Initiliasation extends JFrame {
int Time;
int Difficulty;
JPanel TopPanel;
JPanel BottomPanel;
Game_Initiliasation(){
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(300, 300);
this.setVisible(true);
TopPanel = new JPanel();
BottomPanel = new JPanel();
TopPanel.setLayout(new BorderLayout());
add(TopPanel, BorderLayout.CENTER);
add(BottomPanel, BorderLayout.SOUTH);
JButton Easy = new JButton("Easy");
JButton Medium = new JButton("Medium");
JButton Difficult = new JButton("Difficult");
/*Easy.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
actionPerformed(evt);
}
});*/
Easy.setVisible(true);
BottomPanel.add(Easy);
BottomPanel.add(Medium);
BottomPanel.add(Difficult);
}
}
this.add(BottomPanel); // Add this line, you will be able to see the buttons
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.*;
public class Game_Initiliasation extends JFrame {
int Time;
int Difficulty;
JPanel TopPanel;
JPanel BottomPanel;
Game_Initiliasation(){
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(300, 300);
this.setVisible(true);
TopPanel = new JPanel();
BottomPanel = new JPanel();
TopPanel.setLayout(new BorderLayout());
add(TopPanel, BorderLayout.CENTER);
add(BottomPanel, BorderLayout.SOUTH);
JButton Easy = new JButton("Easy");
JButton Medium = new JButton("Medium");
JButton Difficult = new JButton("Difficult");
/*Easy.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
actionPerformed(evt);
}
});*/
Easy.setVisible(true);
BottomPanel.add(Easy);
BottomPanel.add(Medium);
BottomPanel.add(Difficult);
this.add(BottomPanel); // Add this line, you will be able to see the buttons
}
}
I am adding dynamic JTextField and JLabel in panel1 but I am not able to set the layout of JTextField and JLabel. I need to add JTextfield and JLabel to panel1 and I add panel1 to panel. I need to add JTextFields and JLabels in a Top-Bottom manner and set layout. panel1 and panel are instances of JPanel.
My code :
public class MakeScrollablePanel extends JFrame implements ActionListener
{
static JButton jButton11,jButton12;
static JPanel panel,panel1;
static JTextField jTextFields;
static JLabel label;
static JComboBox<String> jComboBox;
static Dimension dime,dime1,dime2,dime3,dime4,dime5;
static JScrollPane scroll;
private GridBagConstraints panelConstraints = new GridBagConstraints();
BoxLayout bx=null;// #jve:decl-index=0:
int count=1,i=0;
public MakeScrollablePanel()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Show();
add(jButton11);
add(scroll);
dime=new Dimension(600,550);
setSize(dime);
setTitle("Priyank Panel");
setLayout(new FlowLayout());
setLocationRelativeTo(null);
setVisible(true);
setResizable(true);
}
private void Show()
{
jButton11=new JButton("Add Designation");
panel=new JPanel();
bx=new BoxLayout(panel,BoxLayout.Y_AXIS);
scroll=new JScrollPane(panel);
dime1=new Dimension(500,3000);
dime5=new Dimension(500,450);
panelConstraints = new GridBagConstraints();
scroll.setPreferredSize(dime5);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
panel.setLayout(bx);
panel.add(Box.createHorizontalBox());
panel.setBorder(LineBorder.createBlackLineBorder());
panel.setBackground(new Color(204, 230 , 255));
jButton11.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==jButton11)
{
label=new JLabel("Add Designation "+count +" :-");
jTextFields=new JTextField(30);
panel1=new JPanel();
panel1.setBackground(new Color(204, 230 , 255));
panel1.add(label);
panel1.add(jTextFields);
panel.add(panel1);
panel1.revalidate();
panel.revalidate();
panel.updateUI();
count++;
i++;
}
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new MakeScrollablePanel();
}
});
}
}
still not working
still I can't see thre any problem with (depsite fact that there is used BoxLayout instead of GridLayout, but result could be very similair in the case that is used many JTextFields)
.
.
from (little bit) modifyied OPs code
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
public class MakeScrollablePanel extends JFrame implements ActionListener {
private JButton jButton11, jButton12;
private JPanel panel, panel1;
private JTextField jTextFields;
private JLabel label;
private JComboBox<String> jComboBox;
private Dimension dime, dime1, dime2, dime3, dime4, dime5;
private JScrollPane scroll;
private GridBagConstraints panelConstraints = new GridBagConstraints();
private BoxLayout bx = null;// #jve:decl-index=0:
private int count = 1, i = 0;
public MakeScrollablePanel() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Show();
add(jButton11, BorderLayout.NORTH);
add(scroll);
setTitle("Priyank Panel");
pack();
setVisible(true);
setLocationRelativeTo(null);
setResizable(true);
}
private void Show() {
jButton11 = new JButton("Add Designation");
panel = new JPanel();
bx = new BoxLayout(panel, BoxLayout.Y_AXIS);
scroll = new JScrollPane(panel);
dime5 = new Dimension(500, 150);
panelConstraints = new GridBagConstraints();
scroll.setPreferredSize(dime5);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
panel.setLayout(bx);
panel.add(Box.createHorizontalBox());
panel.setBorder(LineBorder.createBlackLineBorder());
panel.setBackground(new Color(204, 230, 255));
jButton11.addActionListener(this);
}
#Override
public void actionPerformed(ActionEvent event) {
if (event.getSource() == jButton11) {
label = new JLabel("Add Designation " + count + " :-");
jTextFields = new JTextField(30);
panel1 = new JPanel();
panel1.setBackground(new Color(204, 230, 255));
panel1.add(label);
panel1.add(jTextFields);
panel.add(panel1);
panel.revalidate();
panel.repaint();
count++;
i++;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new MakeScrollablePanel();
}
});
}
}
I'm making a flash card application, and I'm trying to get it to where is has a JLabel at the top that says what the active set of flash cards is. The way I have it set up though, is that when the user clicks the "Add Set" button, it creates a button at the bottom with no real label to be called on.
So I was using ActionListener to try and get the label of the button itself, but the JLabel just shows the title of the Add Set button and doesn't change whenever a different button is clicked.
Heres my code:
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
public class GraphicsUI extends JPanel {
private DeckList setsList;
CardActions action = new CardActions();
JButton addSetButton, addCardButton;
private JLabel label;
private JPanel actives, optionsPanel,cardPanel, flashCardsPanel, bottomPanel;
private String name;
public GraphicsUI(){
this.setPreferredSize(new Dimension(1000,825));
this.setBackground(Color.LIGHT_GRAY);
actives = new JPanel();
actives.setPreferredSize(new Dimension(1000, 50));
actives.setBackground(Color.blue);
this.add(actives);
label = new JLabel();
actives.add(label);
optionsPanel = new JPanel();
optionsPanel.setPreferredSize(new Dimension(200, 350));
optionsPanel.setBackground(Color.cyan);
this.add(optionsPanel);
cardPanel = new JPanel();
cardPanel.setPreferredSize(new Dimension(580, 350));
cardPanel.setBackground(Color.green);
this.add(cardPanel);
flashCardsPanel = new JPanel();
flashCardsPanel.setPreferredSize(new Dimension(200, 350));
flashCardsPanel.setBackground(Color.white);
this.add(flashCardsPanel);
bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout());
bottomPanel.setPreferredSize(new Dimension(1000, 400));
bottomPanel.setBackground(Color.black);
this.add(bottomPanel);
this.addSetButton = new JButton("Add Set");
addSetButton.setPreferredSize(new Dimension(150, 30));
optionsPanel.add(addSetButton, BorderLayout.WEST);
addSetButton.addActionListener(new ButtonListener());
}
public class ButtonListener implements ActionListener{
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
label.setText("Active set: " + e.getActionCommand());
if(e.getSource() ==addSetButton){
action.setCommand(CardActions.Command.ADDSET);
action.setList(getSetInfo());
}
}
}
private CardList getSetInfo(){
CardList cl = new CardList();
String setName = JOptionPane.showInputDialog("Enter the name of your set.");
if(setName.isEmpty()){
JOptionPane.showMessageDialog(this, "Cannot have an empty set.");
}
else{
cl.setSetName(setName);
ImageIcon img = new ImageIcon("setIcon.png");
JButton newset = new JButton(setName);
newset.setIcon(img);
newset.setBackground(Color.white);
bottomPanel.add(newset);
bottomPanel.revalidate();
}
return cl;
}
}
i think you forgot to add an ActionListener to your newSet Button, below you'll find a working example. I commented the two lines with the icon, so uncomment and try it.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
*
* #author ottp
* #version 1.0
*/
public class CardPanel extends JPanel {
final private JLabel label;
private JButton addSetButton;
private JPanel actives, optionsPanel,cardPanel, flashCardsPanel, bottomPanel;
public CardPanel() {
actives = new JPanel();
actives.setPreferredSize(new Dimension(1000, 50));
actives.setBackground(Color.BLUE);
this.add(actives);
label = new JLabel();
actives.add(label);
optionsPanel = new JPanel();
optionsPanel.setPreferredSize(new Dimension(200, 350));
optionsPanel.setBackground(Color.cyan);
this.add(optionsPanel);
cardPanel = new JPanel();
cardPanel.setPreferredSize(new Dimension(580, 350));
cardPanel.setBackground(Color.green);
this.add(cardPanel);
flashCardsPanel = new JPanel();
flashCardsPanel.setPreferredSize(new Dimension(200, 350));
flashCardsPanel.setBackground(Color.white);
this.add(flashCardsPanel);
bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout());
bottomPanel.setPreferredSize(new Dimension(1000, 400));
bottomPanel.setBackground(Color.black);
this.add(bottomPanel);
this.addSetButton = new JButton("Add Set");
addSetButton.setPreferredSize(new Dimension(150, 30));
optionsPanel.add(addSetButton, BorderLayout.WEST);
addSetButton.addActionListener(new ButtonListener());
}
public class ButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
label.setForeground(Color.WHITE);
label.setText("Active set: " + e.getActionCommand());
if(e.getSource().equals(addSetButton)) {
getSetInfo();
}
}
}
private void getSetInfo() {
String newSetName =
JOptionPane.showInputDialog(cardPanel, "Enter the name of your set", JOptionPane.INFORMATION_MESSAGE);
JButton newSet = new JButton(newSetName);
newSet.setBackground(Color.WHITE);
newSet.addActionListener(new ButtonListener());
bottomPanel.add(newSet);
bottomPanel.revalidate();
}
}
Greets
Patrick
I want to know how to add components dynamically to a JDialog. I know there is a similar question on SO here, but as you can see, I have his solution as a part of my code.
So the idea is that on click of the button, I need to add a component on the dialog. The sample code is below:
import java.awt.Container;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Test extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String args[]) {
Test test = new Test();
test.createDialog();
}
public void createDialog() {
DynamicDialog dialog = new DynamicDialog(this);
dialog.setSize(300, 300);
dialog.setVisible(true);
}
}
class DynamicDialog extends JDialog {
/**
*
*/
private static final long serialVersionUID = 1L;
public DynamicDialog(final JFrame owner) {
super(owner, "Dialog Title", Dialog.DEFAULT_MODALITY_TYPE);
final JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
final JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(Box.createRigidArea(new Dimension(3, 10)));
panel.add(createLabel("Click on add"));
panel.add(Box.createRigidArea(new Dimension(23, 10)));
panel.add(createLabel("To add another line of text"));
panel.add(Box.createHorizontalGlue());
mainPanel.add(panel);
mainPanel.add(Box.createRigidArea(new Dimension(3, 10)));
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
buttonPanel.add(Box.createHorizontalGlue());
JButton button = new JButton();
button.setText("Add another line");
buttonPanel.add(button);
mainPanel.add(buttonPanel);
mainPanel.add(Box.createRigidArea(new Dimension(3, 10)));
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
Container contentPane = owner.getContentPane();
JPanel _panel = new JPanel();
_panel.setLayout(new BoxLayout(_panel, BoxLayout.X_AXIS));
_panel.add(Box.createHorizontalGlue());
_panel.add(createLabel("Added!"));
contentPane.add(_panel);
contentPane.validate();
contentPane.repaint();
owner.pack();
}
});
pack();
setLocationRelativeTo(owner);
this.add(mainPanel);
}
JLabel createLabel(String name) {
JLabel label = new JLabel(name);
return label;
}
}
If you add it to the main panel it will work, you were adding it to the content pane of the frame which it seems it does not show up anywhere.
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
JPanel _panel = new JPanel();
_panel.setLayout(new BoxLayout(_panel, BoxLayout.X_AXIS));
_panel.add(Box.createHorizontalGlue());
_panel.add(createLabel("Added!"));
mainPanel.add(_panel);
mainPanel.validate();
mainPanel.repaint();
owner.pack();
}
})