I'm working on simple Java GUI Frame creation with buttons, it should show the frame with two buttons "OK" and "Clear" respectively. And two lines for writing the First Name and Last Name. I'm using VS Code editor. Following code gives compilation error, any help would be appreciated:
import java.awt.*;
import javax.swing.*;
import javafx.event.ActionEvent;
public class MainFrame extends JFrame{
final private Font mainFont = new Font("Segoe print", Font.BOLD, 18);
JTextField tfFirstName, tfLastName;
JLabel lbWelcome;
public void initialize(){
/********** Form Panel ***************/
JLabel lbFirstName = new JLabel("First Name");
lbFirstName.setFont(mainFont);
tfFirstName = new JTextField();
tfFirstName.setFont(mainFont);
JLabel lbLastName = new JLabel("Last Name");
lbLastName.setFont(mainFont);
tfLastName = new JTextField();
tfLastName.setFont(mainFont);
JPanel formPanel = new JPanel();
formPanel.setLayout(new GridLayout(4, 1, 5, 5));
formPanel.add(lbFirstName);
formPanel.add(tfFirstName);
formPanel.add(lbLastName);
formPanel.add(tfLastName);
/********** Welcome Label ***************/
lbWelcome = new JLabel();
lbWelcome.setFont(mainFont);
/********** Button Panel ***************/
JButton btnOK = new JButton("OK");
btnOK.setFont(mainFont);
btnOK.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
// TODO Auto-generated method stub
String firstName = tfFirstName.getText();
String lastName = tfLastName.getText();
lbWelcome.setText("Hello " + firstName + " " + lastName);
}
});
JButton btnClear = new JButton("Clear");
btnClear.setFont(mainFont);
btnClear.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
tfFirstName.setText("");
tfLastName.setText("");
lbWelcome.setText("");
}
});
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new BorderLayout());
mainPanel.setBackground(new Color(128, 128, 255));
mainPanel.add(formPanel, BorderLayout.NORTH);
mainPanel.add(lbWelcome, BorderLayout.CENTER);
mainPanel.add(buttonsPanel, BorderLayout.SOUTH);
add(mainPanel);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.setBackground(new Color(128, 128, 255));
mainPanel.add(formPanel, BorderLayout.NORTH);
mainPanel.add(lbWelcome, BorderLayout.CENTER);
setTitle("Welcome");
setSize(500, 600);
setMinimumSize(new Dimension(300, 400));
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
MainFrame myFrame = new MainFrame();
myFrame.initialize();
}
}
Compilation error message:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at JavaProjectTest.src.MainFrame.main(MainFrame.java:83)
Output result after fix:
There are two main problems:
(1) import javafx.event.ActionEvent; should be changed to import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
(2) The order of JPanel mainPanel = new JPanel(); should be before using mainPanel.setBackground(new Color(128, 128, 255));.
import javax.swing.*;
import java.awt.*;
//REMOVE THIS
//import javafx.event.ActionEvent;
//ADD THIS
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainFrame extends JFrame {
final private Font mainFont = new Font("Segoe print", Font.BOLD, 18);
JTexThere are two main problems, (1) `import javafx.event.ActionEvent;` should be changed to `import java.awt.event.ActionEvent;
import jThere are two main problems, (1) `import javafx.event.ActionEvent;` should be changed to `import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;` (2) The order of `JPanel mainPanel = new JPanel();` should be before using `mainPanel.setBackground(new Color(128, 128, 255));`.ava.awt.event.ActionListener;` (2) The order of `JPanel mainPanel = new JPanel();` should be before using `mainPanel.setBackground(new Color(128, 128, 255));`.There are two main problems, (1) `import javafx.event.ActionEvent;` should be changed to `import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;` (2) The order of `JPanel mainPanel = new JPanel();` should be before using `mainPanel.setBackground(new Color(128, 128, 255));`.There are two main problems, (1) `import javafx.event.ActionEvent;` should be changed to `import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;` (2) The order of `JPanel mainPanel = new JPanel();` should be before using `mainPanel.setBackground(new Color(128, 128, 255));`.tField tfFirstName, tfLastName;
JLabel lbWelcome;
public void initialize(){
/********** Form Panel ***************/
JLabel lbFirstName = new JLabel("First Name");
lbFirstName.setFont(mainFont);
There are two main problems, (1) `import javafx.event.ActionEvent;` should be changed to `import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;` (2) The order of `JPanel mainPanel = new JPanel();` should be before using `mainPanel.setBackground(new Color(128, 128, 255));`.There are two main problems, (1) `import javafx.event.ActionEvent;` should be changed to `import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;` (2) The order of `JPanel mainPanel = new JPanel();` should be before using `mainPanel.setBackground(new Color(128, 128, 255));`.tfFirstName = new JTextField();
tfFirstName.setFont(mainFont);
JLabel lbLastName = new JLabel("Last Name");
lbLastName.setFont(mainFont);
tfLastName = new JTextField();
tfLastName.setFont(mainFont);
JPanel formPanel = new JPanel();
formPanel.setLayout(new GridLayout(4, 1, 5, 5));
formPanel.add(lbFirstName);
formPanel.add(tfFirstName);
formPanel.add(lbLastName);
formPanel.add(tfLastName);
/********** Welcome Label ***************/
lbWelcome = new JLabel();
lbWelcome.setFont(mainFont);
/********** Button Panel ***************/
JButton btnOK = new JButton("OK");
btnOK.setFont(mainFont);
btnOK.addActionListener(new ActionListener(){
#Overrideimport javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainFrame extends JFrame {
final private Font mainFont = new Font("Segoe print", Font.BOLD, 18);
JTextField tfFirstName, tfLastName;
JLabel lbWelcome;
public void initialize(){
/********** Form Panel ***************/
JLabel lbFirstName = new JLabel("First Name");
lbFirstName.setFont(mainFont);
tfFirstName = new JTextField();
tfFirstName.setFont(mainFont);
JLabel lbLastName = new JLabel("Last Name");
lbLastName.setFont(mainFont);
tfLastName = new JTextField();
tfLastName.setFont(mainFont);
JPanel formPanel = new JPanel();
formPanel.setLayout(new GridLayout(4, 1, 5, 5));
formPanel.add(lbFirstName);
formPanel.add(tfFirstName);
formPanel.add(lbLastName);
formPanel.add(tfLastName);
/********** Welcome Label ***************/
lbWelcome = new JLabel();
lbWelcome.setFont(mainFont);
/********** Button Panel ***************/
JButton btnOK = new JButton("OK");
btnOK.setFont(mainFont);
btnOK.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
// TODO Auto-generated method stub
String firstName = tfFirstName.getText();
String lastName = tfLastName.getText();
lbWelcome.setText("Hello " + firstName + " " + lastName);
}
});
JButton btnClear = new JButton("Clear");
btnClear.setFont(mainFont);
btnClear.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
tfFirstName.setText("");
tfLastName.setText("");
lbWelcome.setText("");
}
});
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new BorderLayout());
//MOVE SEQUENCE JPanel mainPanel = new JPanel();
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.setBackground(new Color(128, 128, 255));
mainPanel.add(formPanel, BorderLayout.NORTH);
mainPanel.add(lbWelcome, BorderLayout.CENTER);
mainPanel.setBackground(new Color(128, 128, 255));
mainPanel.add(formPanel, BorderLayout.NORTH);
mainPanel.add(lbWelcome, BorderLayout.CENTER);
mainPanel.add(buttonsPanel, BorderLayout.SOUTH);
add(mainPanel);
setTitle("Welcome");
setSize(500, 600);
setMinimumSize(new Dimension(300, 400));
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
MainFrame myFrame = new MainFrame();
myFrame.initialize();
}
}
public void actionPerformed(ActionEvent e){
// TODO Auto-generated method stub
String firstName = tfFirstName.getText();
String lastName = tfLastName.getText();
lbWelcome.setText("Hello " + firstName + " " + lastName);
}
});
JButton btnClear = new JButton("Clear");
btnClear.setFont(mainFont);
btnClear.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
tfFirstName.setText("");
tfLastName.setText("");
lbWelcome.setText("");
}
});
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new BorderLayout());
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.setBackground(new Color(128, 128, 255));
mainPanel.add(formPanel, BorderLayout.NORTH);
mainPanel.add(lbWelcome, BorderLayout.CENTER);
mainPanel.setBackground(new Color(128, 128, 255));
mainPanel.add(formPanel, BorderLayout.NORTH);
mainPanel.add(lbWelcome, BorderLayout.CENTER);
mainPanel.add(buttonsPanel, BorderLayout.SOUTH);
add(mainPanel);
setTitle("Welcome");
setSize(500, 600);
setMinimumSize(new Dimension(300, 400));
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
MainFrame myFrame = new MainFrame();
myFrame.initialize();
}
}
Related
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;
}
}
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
}
});
So, I'm starting with the basic visual Java components, and have trouble to use them accurately.
Here is my new problem: I'm trying to implement a 3 parts panel: east, center, west, but can't have a proper display of the center panel. Here is my piece of code:
Basically 'panelUpMiddle' isn't visible, so I wonder why?...
public class TestCode_Web {
public static void main(String[] args) {
JFrame window = new JFrame("Test");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(400, 400);
JPanel innerPanel = new JPanel(new BorderLayout());
JPanel panelUp = new JPanel(new BorderLayout());
JPanel panelUpLeft = new JPanel();
JPanel panelUpMiddle = new JPanel();
window.add(innerPanel, BorderLayout.NORTH);
innerPanel.add(panelUp, BorderLayout.NORTH);
panelUp.add(panelUpLeft, BorderLayout.WEST);
panelUp.add(panelUpMiddle, BorderLayout.CENTER);
JLabel label1 = new JLabel("Label 1");
JLabel label11 = new JLabel("Label 11");
JLabel label12 = new JLabel("Label 12");
panelUp.add(label1);
panelUpLeft.add(label11);
panelUpMiddle.add(label12);
panelUp.setBackground(new Color(200, 240, 200));
panelUpLeft.setBackground(new Color(200, 240, 0));
panelUpMiddle.setBackground(new Color(100, 240, 200));
panelUp.setPreferredSize(new Dimension(window.getWidth(), 160));
panelUpLeft.setPreferredSize(new Dimension(160, 120));
window.setVisible(true);
}
}
Try this code. I updated the naming of the panels and the labels so it is more clear.
JFrame window = new JFrame("Test");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(400, 400);
JPanel centerPanel = new JPanel(new BorderLayout());
JPanel eastPanel = new JPanel(new BorderLayout());
JPanel westPanel = new JPanel(new BorderLayout());
window.add(centerPanel, BorderLayout.CENTER);
window.add(eastPanel, BorderLayout.EAST);
window.add(westPanel, BorderLayout.WEST);
JLabel centerLabel = new JLabel("Center");
JLabel eastLabel = new JLabel("East");
JLabel westLabel = new JLabel("West");
eastPanel.add(eastLabel);
westPanel.add(westLabel);
centerPanel.add(centerLabel);
centerPanel.setPreferredSize(new Dimension(200, 400));
eastPanel.setPreferredSize(new Dimension(100, 400));
westPanel.setPreferredSize(new Dimension(100, 400));
eastPanel.setBackground(new Color(200, 240, 200));
westPanel.setBackground(new Color(200, 240, 0));
window.setVisible(true);
A third panel is never created and nested within the other panel.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TestCode_Web {
public static void main(String[] args) {
JFrame window = new JFrame("Test");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(400, 400);
JPanel innerPanel = new JPanel(new BorderLayout());
JPanel panelUp = new JPanel(new BorderLayout());
JPanel panelUpLeft = new JPanel();
JPanel panelUpMiddle = new JPanel();
JPanel panelUpRight = new JPanel(); //Create third panel
window.add(innerPanel, BorderLayout.NORTH);
innerPanel.add(panelUp, BorderLayout.NORTH);
panelUp.add(panelUpRight, BorderLayout.EAST); //Add Third Panel
panelUp.add(panelUpLeft, BorderLayout.WEST);
panelUp.add(panelUpMiddle, BorderLayout.CENTER);
JLabel label1 = new JLabel("Label 1");
JLabel label11 = new JLabel("Label 11");
JLabel label12 = new JLabel("Label 12");
panelUpRight.add(label1); //Add label for third panel
panelUpLeft.add(label11);
panelUpMiddle.add(label12);
panelUp.setBackground(new Color(200, 240, 200));
panelUpLeft.setBackground(new Color(200, 240, 0));
panelUpMiddle.setBackground(new Color(100, 240, 200));
panelUp.setPreferredSize(new Dimension(window.getWidth(), 160));
panelUpLeft.setPreferredSize(new Dimension(160, 120));
window.setVisible(true);
}
}
Actually, you don't need the innerPanel:
public class TestCode_Web
{
public static void main(String[] args) {
JFrame window = new JFrame("Test");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new BorderLayout());
window.setSize(400, 400);
JPanel panelUp = new JPanel();
JPanel panelUpLeft = new JPanel();
JPanel panelUpMiddle = new JPanel();
window.add(panelUp, BorderLayout.NORTH);
window.add(panelUpLeft, BorderLayout.WEST);
window.add(panelUpMiddle, BorderLayout.CENTER);
JLabel label1 = new JLabel("Label 1");
JLabel label11 = new JLabel("Label 11");
JLabel label12 = new JLabel("Label 12");
panelUp.add(label1);
panelUpLeft.add(label11);
panelUpMiddle.add(label12);
panelUp.setBackground(new Color(200, 240, 200));
panelUpLeft.setBackground(new Color(200, 240, 0));
panelUpMiddle.setBackground(new Color(100, 240, 200));
panelUp.setPreferredSize(new Dimension(window.getWidth(), 160));
panelUpLeft.setPreferredSize(new Dimension(160, 120));
window.setVisible(true);
}
}
NiceCow gave the answer why yours is not working. This should work fine.
I would write on a JPanel a String much bigger respect than other element.which could be the way to paint a string in terms of simply?There is a method to do this?
You could add the text as a JLabel component and change its font size.
public static void main(String[] args) {
NewJFrame1 frame = new NewJFrame1();
frame.setLayout(new GridBagLayout());
JPanel panel = new JPanel();
JLabel jlabel = new JLabel("This is a label");
jlabel.setFont(new Font("Verdana",1,20));
panel.add(jlabel);
panel.setBorder(new LineBorder(Color.BLACK)); // make it easy to see
frame.add(panel, new GridBagConstraints());
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(NewJFrame1.EXIT_ON_CLOSE);
frame.setVisible(true);
}
The code will run and look like this :
see also Java layout manager vertical center for more info
JLabel supports HTML 3.2 formating, so you can use header tags if you don't want to mess with fonts.
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class HtmlHeadersSample extends JFrame {
public HtmlHeadersSample() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300,200);
setLocation(100, 100);
JLabel label1 = new JLabel();
label1.setText("simple text");
label1.setBounds(0, 0, 200, 50);
JLabel label2 = new JLabel();
label2.setText("<html><h1>header1 text</h1></html>");
label2.setBounds(0, 20, 200, 50);
JLabel label3 = new JLabel();
label3.setText("<html><h2>header2 text</h2></html>");
label3.setBounds(0, 40, 200, 50);
JLabel label4 = new JLabel();
label4.setText("<html><h3>header3 text</h3></html>");
label4.setBounds(0, 60, 200, 50);
add(label1);
add(label2);
add(label3);
add(label4);
setVisible(true);
}
public static void main(String[] args) {
new HtmlHeadersSample();
}
}
Here's how it looks like:
Just set the size of your font
JLabel bigLabel = new JLabel("Bigger text");
bigLabel.setFont(new Font("Arial", 0, 30));
This is a MigLayout-newbie question. I'd like to know how to draw a separator from the end of a label across the width of a panel.
Here's my example code:
package com.ndh.swingjunk;
import java.awt.*;
import javax.swing.*;
import net.miginfocom.swing.MigLayout;
class MyJFrame extends JFrame {
public MyJFrame() {
super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("foo");
label.setFont(new Font("Tahoma", Font.BOLD, 11));
JSeparator separator = new JSeparator();
JLabel anotherLabel = new JLabel("some other label");
anotherLabel.setFont(new Font("Tahoma", Font.PLAIN, 11));
JButton button1 = new JButton("button 1");
JButton button2 = new JButton("button 2");
JPanel panel = new JPanel(new MigLayout());
panel.add(label);
panel.add(separator, "growx, wrap");
panel.add(anotherLabel);
panel.add(button1);
panel.add(button2);
getContentPane().add(panel);
pack();
}
}
public class SeparatorLayoutQuestion {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override public void run() {new MyJFrame().setVisible(true);}});}}
and here's the result:
which is terribly lame. I'd like the separator to stretch from the end of "Foo" to the end of the panel, all the way across button 2.
Here's what worked for me, I had to use "split" so the label and separator could share the same cell:
JPanel panel = new JPanel(new MigLayout());
panel.add(label, "split 2, span");
panel.add(separator, "growx, wrap");
panel.add(anotherLabel);
panel.add(button1);
panel.add(button2);
try adding
"span 3"
to the first label