WindowBuilder Source Code Layout and Database Implementation - java
Hi I am a newbie java programmer, I am currently creating a java gui application using WindowBuilder within eclipse.
I have the layout that I like however, the source code is all in one class, and I can't seem to navigate through the panels correctly.
A link to the image for how the layout of panels is in WindowBuilder - http://postimg.org/image/xmnm9ilm1/
I was wondering how can I best layout the code, and how best to navigate through the program.
Bear in mind I don't know many techical terms.
Also what is the best way to implement a microsoft access backend database that can be editied within the program for login usernames, passwords and adding and removing items? I have a seperate database class at the moment.
Thank You
Source code of program is shown below
import java.awt.EventQueue;
import java.awt.Toolkit;
import javax.swing.JFrame;
import java.awt.CardLayout;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Color;
import javax.swing.JList;
import javax.swing.Box;
import java.awt.Panel;
import javax.swing.AbstractListModel;
import javax.swing.ListSelectionModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class UnivEPOS {
private JFrame frmUniveposV;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UnivEPOS window = new UnivEPOS();
window.frmUniveposV.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public UnivEPOS() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmUniveposV = new JFrame();
frmUniveposV.setTitle("UnivEPOS v1.0");
frmUniveposV.setSize(Toolkit.getDefaultToolkit().getScreenSize());
frmUniveposV.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmUniveposV.getContentPane().setLayout(new CardLayout(0, 0));
JPanel Login = new JPanel();
Login.setBackground(new Color(128, 0, 0));
frmUniveposV.getContentPane().add(Login, "name_244778779455709");
Login.setLayout(null);
JLabel lblLogin = new JLabel("LOGIN");
lblLogin.setForeground(new Color(255, 255, 255));
lblLogin.setFont(new Font("Tahoma", Font.BOLD, 18));
lblLogin.setBounds(10, 0, 60, 50);
Login.add(lblLogin);
textField = new JTextField();
textField.setBounds(518, 282, 262, 20);
Login.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(518, 319, 262, 20);
Login.add(textField_1);
JLabel lblNewLabel = new JLabel("Username");
lblNewLabel.setForeground(new Color(255, 255, 255));
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 16));
lblNewLabel.setBounds(416, 283, 92, 14);
Login.add(lblNewLabel);
JLabel lblPassword = new JLabel("Password");
lblPassword.setForeground(new Color(255, 255, 255));
lblPassword.setFont(new Font("Tahoma", Font.BOLD, 16));
lblPassword.setBounds(416, 320, 92, 14);
Login.add(lblPassword);
JButton btnNewButton = new JButton("ENTER");
btnNewButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
}
});
btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 11));
btnNewButton.setBounds(691, 396, 89, 23);
Login.add(btnNewButton);
JButton btnNewButton_1 = new JButton("EXIT");
btnNewButton_1.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
}
});
btnNewButton_1.setFont(new Font("Tahoma", Font.BOLD, 11));
btnNewButton_1.setBounds(518, 396, 89, 23);
Login.add(btnNewButton_1);
JPanel Home = new JPanel();
Home.setBackground(new Color(128, 0, 0));
frmUniveposV.getContentPane().add(Home, "name_244789265918213");
Home.setLayout(null);
JLabel lblCurrentOrders = new JLabel("CURRENT ORDERS");
lblCurrentOrders.setForeground(new Color(255, 255, 255));
lblCurrentOrders.setBounds(535, 0, 346, 31);
lblCurrentOrders.setFont(new Font("Tahoma", Font.BOLD, 35));
Home.add(lblCurrentOrders);
JButton btnNewButton_2 = new JButton("Previous");
btnNewButton_2.setBounds(614, 598, 95, 31);
Home.add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("Next");
btnNewButton_3.setBounds(731, 598, 95, 31);
Home.add(btnNewButton_3);
JButton btnNewButton_6 = new JButton("NEW ORDER");
btnNewButton_6.setBounds(10, 598, 244, 112);
btnNewButton_6.setFont(new Font("Tahoma", Font.BOLD, 18));
Home.add(btnNewButton_6);
JButton btnNewButton_4 = new JButton("Show Paid");
btnNewButton_4.setBounds(614, 632, 95, 31);
Home.add(btnNewButton_4);
JButton btnNewButton_5 = new JButton("Show Unpaid");
btnNewButton_5.setBounds(731, 632, 95, 31);
Home.add(btnNewButton_5);
JButton btnShowAll = new JButton("SHOW ALL");
btnShowAll.setBounds(658, 663, 131, 31);
Home.add(btnShowAll);
JButton btnAdmin = new JButton("ADMIN");
btnAdmin.setBounds(1245, 682, 95, 37);
Home.add(btnAdmin);
JLabel lblCurrentUser = new JLabel("Current User:");
lblCurrentUser.setForeground(new Color(255, 255, 255));
lblCurrentUser.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblCurrentUser.setBounds(1081, 12, 83, 14);
Home.add(lblCurrentUser);
JLabel lblNewLabel_1 = new JLabel("Date & Time:");
lblNewLabel_1.setForeground(new Color(255, 255, 255));
lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblNewLabel_1.setBounds(10, 14, 80, 14);
Home.add(lblNewLabel_1);
JPanel Order = new JPanel();
Order.setBackground(new Color(128, 0, 0));
frmUniveposV.getContentPane().add(Order, "name_244796279334447");
Order.setLayout(null);
JLabel lblOrder = new JLabel("ORDER:");
lblOrder.setForeground(new Color(255, 255, 255));
lblOrder.setFont(new Font("Tahoma", Font.BOLD, 40));
lblOrder.setBounds(10, 11, 166, 43);
Order.add(lblOrder);
JLabel lblDateTime = new JLabel("DATE & TIME:");
lblDateTime.setForeground(new Color(255, 255, 255));
lblDateTime.setFont(new Font("Tahoma", Font.BOLD, 40));
lblDateTime.setBounds(771, 11, 282, 43);
Order.add(lblDateTime);
JLabel lblTable = new JLabel("TABLE:");
lblTable.setForeground(new Color(255, 255, 255));
lblTable.setFont(new Font("Tahoma", Font.BOLD, 40));
lblTable.setBounds(391, 11, 146, 43);
Order.add(lblTable);
Panel panel = new Panel();
panel.setBounds(10, 75, 650, 489);
Order.add(panel);
panel.setLayout(null);
JLabel lblNewLabel_2 = new JLabel("TOTAL:");
lblNewLabel_2.setForeground(new Color(255, 255, 255));
lblNewLabel_2.setFont(new Font("Tahoma", Font.BOLD, 40));
lblNewLabel_2.setBounds(10, 421, 157, 57);
panel.add(lblNewLabel_2);
JLabel lblCurrentOrder = new JLabel("CURRENT ORDER");
lblCurrentOrder.setForeground(new Color(255, 255, 255));
lblCurrentOrder.setBounds(251, 5, 198, 31);
lblCurrentOrder.setFont(new Font("Tahoma", Font.PLAIN, 25));
panel.add(lblCurrentOrder);
Panel panel_1 = new Panel();
panel_1.setBounds(690, 75, 650, 489);
Order.add(panel_1);
panel_1.setLayout(null);
JLabel lblCategories = new JLabel("CATEGORIES");
lblCategories.setForeground(new Color(255, 255, 255));
lblCategories.setFont(new Font("Tahoma", Font.PLAIN, 25));
lblCategories.setBounds(254, 11, 153, 31);
panel_1.add(lblCategories);
JButton btnPrevious = new JButton("PREVIOUS");
btnPrevious.setBounds(462, 455, 89, 23);
panel_1.add(btnPrevious);
JButton btnNext = new JButton("NEXT");
btnNext.setBounds(551, 455, 89, 23);
panel_1.add(btnNext);
JButton btnNewButton_7 = new JButton("SAVE");
btnNewButton_7.setFont(new Font("Tahoma", Font.BOLD, 30));
btnNewButton_7.setBounds(10, 582, 254, 137);
Order.add(btnNewButton_7);
JButton btnSavePay = new JButton("SAVE & PAY");
btnSavePay.setFont(new Font("Tahoma", Font.BOLD, 30));
btnSavePay.setBounds(274, 582, 274, 137);
Order.add(btnSavePay);
JButton btnFinish = new JButton("FINISH");
btnFinish.setFont(new Font("Tahoma", Font.BOLD, 30));
btnFinish.setBounds(558, 582, 253, 137);
Order.add(btnFinish);
JButton button = new JButton("ADMIN");
button.setFont(new Font("Tahoma", Font.BOLD, 11));
button.setBounds(1245, 682, 95, 37);
Order.add(button);
JButton btnExit = new JButton("EXIT");
btnExit.setFont(new Font("Tahoma", Font.BOLD, 11));
btnExit.setBounds(1114, 682, 95, 37);
Order.add(btnExit);
JPanel Reports = new JPanel();
Reports.setBackground(new Color(128, 0, 0));
frmUniveposV.getContentPane().add(Reports, "name_244798869260892");
Reports.setLayout(null);
Panel panel_2 = new Panel();
panel_2.setBounds(0, 180, 1350, 459);
Reports.add(panel_2);
panel_2.setLayout(null);
JLabel lblSales = new JLabel("SALES");
lblSales.setForeground(new Color(255, 255, 255));
lblSales.setFont(new Font("Tahoma", Font.PLAIN, 25));
lblSales.setBounds(600, 0, 78, 52);
panel_2.add(lblSales);
JLabel lblReports = new JLabel("REPORTS");
lblReports.setForeground(new Color(255, 255, 255));
lblReports.setFont(new Font("Tahoma", Font.BOLD, 35));
lblReports.setBounds(556, 0, 174, 52);
Reports.add(lblReports);
JButton btnNewButton_8 = new JButton("SALES");
btnNewButton_8.setFont(new Font("Tahoma", Font.PLAIN, 25));
btnNewButton_8.setBounds(10, 97, 174, 77);
Reports.add(btnNewButton_8);
JButton btnCancellations = new JButton("CANCELLATIONS");
btnCancellations.setFont(new Font("Tahoma", Font.PLAIN, 25));
btnCancellations.setBounds(194, 97, 221, 77);
Reports.add(btnCancellations);
JButton btnItemsSold = new JButton("ITEMS SOLD");
btnItemsSold.setFont(new Font("Tahoma", Font.PLAIN, 25));
btnItemsSold.setBounds(425, 97, 221, 77);
Reports.add(btnItemsSold);
JLabel lblNewLabel_3 = new JLabel("PAGE");
lblNewLabel_3.setForeground(new Color(255, 255, 255));
lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 40));
lblNewLabel_3.setBounds(610, 645, 106, 74);
Reports.add(lblNewLabel_3);
JButton btnNewButton_9 = new JButton("PREVIOUS");
btnNewButton_9.setFont(new Font("Tahoma", Font.BOLD, 11));
btnNewButton_9.setBounds(860, 645, 163, 74);
Reports.add(btnNewButton_9);
JButton btnNext_1 = new JButton("NEXT");
btnNext_1.setFont(new Font("Tahoma", Font.BOLD, 11));
btnNext_1.setBounds(1025, 645, 164, 74);
Reports.add(btnNext_1);
JButton btnNewButton_10 = new JButton("EXIT");
btnNewButton_10.setFont(new Font("Tahoma", Font.BOLD, 11));
btnNewButton_10.setBounds(1199, 645, 141, 74);
Reports.add(btnNewButton_10);
JPanel Settings = new JPanel();
Settings.setBackground(new Color(128, 0, 0));
frmUniveposV.getContentPane().add(Settings, "name_244813941995636");
Settings.setLayout(null);
JLabel lblNewLabel_4 = new JLabel("SYSTEM SETTINGS");
lblNewLabel_4.setForeground(new Color(255, 255, 255));
lblNewLabel_4.setFont(new Font("Tahoma", Font.BOLD, 35));
lblNewLabel_4.setBounds(500, 0, 339, 54);
Settings.add(lblNewLabel_4);
JList list = new JList();
list.setForeground(new Color(255, 255, 255));
list.setBackground(new Color(128, 0, 0));
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setFont(new Font("Tahoma", Font.PLAIN, 40));
list.setModel(new AbstractListModel() {
String[] values = new String[] {"USER 1", "USER 2", "USER 3", "USER 4", "USER 5"};
public int getSize() {
return values.length;
}
public Object getElementAt(int index) {
return values[index];
}
});
list.setBounds(10, 82, 634, 481);
Settings.add(list);
JButton btnNewButton_11 = new JButton("ADD USER");
btnNewButton_11.setFont(new Font("Tahoma", Font.BOLD, 11));
btnNewButton_11.setBounds(10, 617, 196, 102);
Settings.add(btnNewButton_11);
JButton btnChangeSuperuserPassword = new JButton("CHANGE SUPERUSER PASSWORD");
btnChangeSuperuserPassword.setFont(new Font("Tahoma", Font.BOLD, 11));
btnChangeSuperuserPassword.setBounds(433, 617, 211, 102);
Settings.add(btnChangeSuperuserPassword);
JButton button_1 = new JButton("EXIT");
button_1.setFont(new Font("Tahoma", Font.BOLD, 11));
button_1.setBounds(1245, 682, 95, 37);
Settings.add(button_1);
Panel panel_3 = new Panel();
panel_3.setBounds(650, 84, 690, 479);
Settings.add(panel_3);
panel_3.setLayout(null);
JLabel lblNewLabel_5 = new JLabel("RECEIPT OF SALE");
lblNewLabel_5.setForeground(new Color(255, 255, 255));
lblNewLabel_5.setFont(new Font("Tahoma", Font.PLAIN, 20));
lblNewLabel_5.setBounds(260, 11, 178, 44);
panel_3.add(lblNewLabel_5);
JLabel lblNewLabel_6 = new JLabel("COMPANY");
lblNewLabel_6.setForeground(new Color(255, 255, 255));
lblNewLabel_6.setFont(new Font("Tahoma", Font.PLAIN, 22));
lblNewLabel_6.setBounds(24, 136, 125, 44);
panel_3.add(lblNewLabel_6);
JLabel lblSalesRepresentative = new JLabel("SALES REP");
lblSalesRepresentative.setForeground(new Color(255, 255, 255));
lblSalesRepresentative.setFont(new Font("Tahoma", Font.PLAIN, 22));
lblSalesRepresentative.setBounds(24, 213, 125, 44);
panel_3.add(lblSalesRepresentative);
JLabel lblAddress = new JLabel("ADDRESS");
lblAddress.setForeground(new Color(255, 255, 255));
lblAddress.setFont(new Font("Tahoma", Font.PLAIN, 22));
lblAddress.setBounds(24, 171, 125, 48);
panel_3.add(lblAddress);
JLabel lblMessage = new JLabel("MESSAGE");
lblMessage.setForeground(new Color(255, 255, 255));
lblMessage.setFont(new Font("Tahoma", Font.PLAIN, 22));
lblMessage.setBounds(24, 256, 125, 38);
panel_3.add(lblMessage);
textField_2 = new JTextField();
textField_2.setBounds(154, 151, 404, 26);
panel_3.add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setColumns(10);
textField_3.setBounds(154, 188, 404, 26);
panel_3.add(textField_3);
textField_4 = new JTextField();
textField_4.setColumns(10);
textField_4.setBounds(154, 228, 404, 26);
panel_3.add(textField_4);
textField_5 = new JTextField();
textField_5.setColumns(10);
textField_5.setBounds(154, 268, 404, 26);
panel_3.add(textField_5);
JPanel Administration = new JPanel();
Administration.setBackground(new Color(128, 0, 0));
frmUniveposV.getContentPane().add(Administration, "name_244816080502214");
Administration.setLayout(null);
JLabel lblNewLabel_7 = new JLabel("ADMINISTRATION");
lblNewLabel_7.setForeground(new Color(255, 255, 255));
lblNewLabel_7.setFont(new Font("Tahoma", Font.BOLD, 35));
lblNewLabel_7.setBounds(506, 0, 335, 43);
Administration.add(lblNewLabel_7);
JLabel lblCategories_1 = new JLabel("CATEGORIES");
lblCategories_1.setForeground(new Color(255, 255, 255));
lblCategories_1.setFont(new Font("Tahoma", Font.PLAIN, 35));
lblCategories_1.setBounds(90, 88, 214, 43);
Administration.add(lblCategories_1);
JLabel lblSubcategories = new JLabel("SUB-CATEGORIES");
lblSubcategories.setForeground(new Color(255, 255, 255));
lblSubcategories.setFont(new Font("Tahoma", Font.PLAIN, 35));
lblSubcategories.setBounds(528, 88, 292, 43);
Administration.add(lblSubcategories);
JLabel label = new JLabel("SUB-CATEGORIES");
label.setForeground(new Color(255, 255, 255));
label.setFont(new Font("Tahoma", Font.PLAIN, 35));
label.setBounds(1008, 88, 292, 43);
Administration.add(label);
JList list_1 = new JList();
list_1.setBounds(55, 142, 292, 404);
Administration.add(list_1);
JList list_2 = new JList();
list_2.setBounds(528, 142, 292, 404);
Administration.add(list_2);
JList list_3 = new JList();
list_3.setBounds(1008, 142, 292, 404);
Administration.add(list_3);
JButton btnAdd = new JButton("ADD");
btnAdd.setBounds(55, 557, 89, 23);
Administration.add(btnAdd);
JButton button_2 = new JButton("ADD");
button_2.setBounds(528, 557, 89, 23);
Administration.add(button_2);
JButton button_3 = new JButton("ADD");
button_3.setBounds(1008, 557, 89, 23);
Administration.add(button_3);
JButton btnRemove = new JButton("REMOVE");
btnRemove.setBounds(258, 557, 89, 23);
Administration.add(btnRemove);
JButton button_4 = new JButton("REMOVE");
button_4.setBounds(731, 557, 89, 23);
Administration.add(button_4);
JButton button_5 = new JButton("REMOVE");
button_5.setBounds(1211, 557, 89, 23);
Administration.add(button_5);
JButton btnReports = new JButton("REPORTS");
btnReports.setFont(new Font("Tahoma", Font.BOLD, 30));
btnReports.setBounds(10, 638, 180, 81);
Administration.add(btnReports);
JButton btnSettings = new JButton("SETTINGS");
btnSettings.setFont(new Font("Tahoma", Font.BOLD, 30));
btnSettings.setBounds(200, 638, 185, 81);
Administration.add(btnSettings);
JButton button_6 = new JButton("EXIT");
button_6.setFont(new Font("Tahoma", Font.BOLD, 11));
button_6.setBounds(1245, 682, 95, 37);
Administration.add(button_6);
}
}
Source code of database class is shown below
import java.awt.event.*;
import java.sql.*;
public class database {
Connection con;
Statement st;
ResultSet rs;
public database()
{
connect();
frame();
}
public void connect()
{
try{
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver);
String db = "jdbc:odbc:db";
con = DriverManager.getConnection(db);
st = con.createStatement();
}
catch(Exception ex){
}
}
public void frame()
{
new UnivEPOS();
}
}
Related
Java Gui cannot scroll through scrollPane
I am creating a Gui in java and I wanted to have a list of selectable checkboxes in a smaller pane that allows you to scroll through it. I have it set up but I may have messed up somewhere that it does not allow me to scroll, any help is appreciated. GUI: Code: private void initialize() { frame = new JFrame(); frame.setResizable(false); frame.getContentPane().setBackground(new Color(255, 255, 255)); frame.getContentPane().setLayout(null); frame.setVisible(true); frame.setSize(801, 500); JPanel panel_1 = new JPanel(); panel_1.setBorder(null); panel_1.setBackground(new Color(66, 69, 65)); panel_1.setBounds(0, 0, 194, 483); frame.getContentPane().add(panel_1); String[] sports = { "Select a sport", "NBA", "NFL", "NHL", "ATL", "MLB", "NCAA", "PGA", "CKE" }; // list of panel_1.setLayout(null); JButton btnNewButton = new JButton("Submit"); btnNewButton.setBounds(52, 382, 89, 25); panel_1.add(btnNewButton); btnNewButton.setFont(new Font("Leelawadee UI Semilight", Font.BOLD, 12)); // items // contained // in drop // down JComboBox comboBox_1 = new JComboBox(sports); comboBox_1.setBounds(17, 345, 155, 22); panel_1.add(comboBox_1); comboBox_1.setMaximumRowCount(10); comboBox_1.setBackground(new Color(255, 255, 255)); comboBox_1.addActionListener(comboBox_1); comboBox_1.addActionListener(comboBox_1); comboBox_1.addActionListener(comboBox_1); JLabel lblSport = new JLabel("Sport\r\n"); lblSport.setBounds(0, 321, 188, 18); lblSport.setForeground(Color.WHITE); panel_1.add(lblSport); lblSport.setHorizontalAlignment(SwingConstants.CENTER); lblSport.setFont(new Font("Leelawadee UI Semilight", Font.BOLD, 14)); lblSport.setBackground(SystemColor.activeCaption); JScrollPane scrollPane = new JScrollPane(); scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); scrollPane.setBounds(0, 0, 188, 291); panel_1.add(scrollPane); JPanel panel_2_1 = new JPanel(); scrollPane.setViewportView(panel_2_1); panel_2_1.setToolTipText( "This is where you input the URL of a gambling site that will be used in the calculator."); panel_2_1.setBorder(null); panel_2_1.setBackground(new Color(66, 69, 65)); panel_2_1.setLayout(null); JLabel lblNewLabel = new JLabel("Sportsbook Selection"); lblNewLabel.setBounds(0, 0, 0, 0); lblNewLabel.setForeground(Color.WHITE); panel_2_1.add(lblNewLabel); lblNewLabel.setFont(new Font("Calibri", Font.PLAIN, 16)); lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel.setLabelFor(frame); lblNewLabel.setBackground(SystemColor.activeCaption); JCheckBox chckbxNewCheckBox = new JCheckBox("888Bets"); chckbxNewCheckBox.setBounds(6, 43, 89, 23); chckbxNewCheckBox.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox); JCheckBox chckbxNewCheckBox_2 = new JCheckBox("Bovada"); chckbxNewCheckBox_2.setBounds(97, 43, 89, 23); chckbxNewCheckBox_2.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_2); JCheckBox chckbxNewCheckBox_1 = new JCheckBox("BlueBet"); chckbxNewCheckBox_1.setBounds(6, 69, 89, 23); chckbxNewCheckBox_1.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_1); JCheckBox chckbxNewCheckBox_3 = new JCheckBox("Coral"); chckbxNewCheckBox_3.setBounds(97, 69, 89, 23); chckbxNewCheckBox_3.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_3); JCheckBox chckbxNewCheckBox_4 = new JCheckBox("BoyleSports"); chckbxNewCheckBox_4.setBounds(6, 95, 89, 23); chckbxNewCheckBox_4.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_4); JCheckBox chckbxNewCheckBox_5 = new JCheckBox("Circa Sports"); chckbxNewCheckBox_5.setBounds(97, 95, 89, 23); chckbxNewCheckBox_5.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_5); JCheckBox chckbxNewCheckBox_6 = new JCheckBox("Betclic"); chckbxNewCheckBox_6.setBounds(6, 121, 89, 23); chckbxNewCheckBox_6.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_6); JCheckBox chckbxNewCheckBox_2_1 = new JCheckBox("Casumo"); chckbxNewCheckBox_2_1.setBounds(97, 121, 89, 23); chckbxNewCheckBox_2_1.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_2_1); JCheckBox chckbxNewCheckBox_1_1 = new JCheckBox("Betfair"); chckbxNewCheckBox_1_1.setBounds(6, 147, 89, 23); chckbxNewCheckBox_1_1.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_1_1); JCheckBox chckbxNewCheckBox_3_1 = new JCheckBox("DraftKings"); chckbxNewCheckBox_3_1.setBounds(97, 147, 89, 23); chckbxNewCheckBox_3_1.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_3_1); JCheckBox chckbxNewCheckBox_4_1 = new JCheckBox("BetMGM"); chckbxNewCheckBox_4_1.setBounds(6, 173, 89, 23); chckbxNewCheckBox_4_1.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_4_1); JCheckBox chckbxNewCheckBox_5_1 = new JCheckBox("FanDuel"); chckbxNewCheckBox_5_1.setBounds(97, 173, 89, 23); chckbxNewCheckBox_5_1.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_5_1); JCheckBox chckbxNewCheckBox_7 = new JCheckBox("BetOnline"); chckbxNewCheckBox_7.setBounds(6, 199, 89, 23); chckbxNewCheckBox_7.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_7); JCheckBox chckbxNewCheckBox_2_2 = new JCheckBox("FOX Bet"); chckbxNewCheckBox_2_2.setBounds(97, 199, 89, 23); chckbxNewCheckBox_2_2.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_2_2); JCheckBox chckbxNewCheckBox_1_2 = new JCheckBox("BetRivers"); chckbxNewCheckBox_1_2.setBounds(6, 225, 89, 23); chckbxNewCheckBox_1_2.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_1_2); JCheckBox chckbxNewCheckBox_3_2 = new JCheckBox("GTbets"); chckbxNewCheckBox_3_2.setBounds(97, 225, 89, 23); chckbxNewCheckBox_3_2.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_3_2); JCheckBox chckbxNewCheckBox_4_2 = new JCheckBox("Bet Victor"); chckbxNewCheckBox_4_2.setBounds(6, 251, 89, 23); chckbxNewCheckBox_4_2.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_4_2); JCheckBox chckbxNewCheckBox_5_2 = new JCheckBox("Intertops"); chckbxNewCheckBox_5_2.setBounds(97, 251, 89, 23); chckbxNewCheckBox_5_2.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_5_2); JCheckBox chckbxNewCheckBox_6_1 = new JCheckBox("BetUS"); chckbxNewCheckBox_6_1.setBounds(6, 277, 89, 23); chckbxNewCheckBox_6_1.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_6_1); JCheckBox chckbxNewCheckBox_2_1_1 = new JCheckBox("Ladbrokes"); chckbxNewCheckBox_2_1_1.setBounds(97, 277, 89, 23); chckbxNewCheckBox_2_1_1.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_2_1_1); JCheckBox chckbxLeovegas = new JCheckBox("LeoVegas"); chckbxLeovegas.setBounds(6, 304, 89, 23); chckbxLeovegas.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxLeovegas); JCheckBox chckbxNewCheckBox_2_3 = new JCheckBox("LiveScore"); chckbxNewCheckBox_2_3.setBounds(97, 304, 89, 23); chckbxNewCheckBox_2_3.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_2_3); JCheckBox chckbxNewCheckBox_1_3 = new JCheckBox("LowVig"); chckbxNewCheckBox_1_3.setBounds(6, 330, 89, 23); chckbxNewCheckBox_1_3.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_1_3); JCheckBox chckbxNewCheckBox_3_3 = new JCheckBox("Matchbook"); chckbxNewCheckBox_3_3.setBounds(97, 330, 89, 23); chckbxNewCheckBox_3_3.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_3_3); JCheckBox chckbxNewCheckBox_4_3 = new JCheckBox("MarathonkBet "); chckbxNewCheckBox_4_3.setBounds(6, 356, 89, 23); chckbxNewCheckBox_4_3.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_4_3); JCheckBox chckbxNewCheckBox_5_3 = new JCheckBox("Mr Green"); chckbxNewCheckBox_5_3.setBounds(97, 356, 89, 23); chckbxNewCheckBox_5_3.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_5_3); JCheckBox chckbxNewCheckBox_6_2 = new JCheckBox("MyBookie"); chckbxNewCheckBox_6_2.setBounds(6, 382, 89, 23); chckbxNewCheckBox_6_2.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_6_2); JCheckBox chckbxNewCheckBox_2_1_2 = new JCheckBox("Neds"); chckbxNewCheckBox_2_1_2.setBounds(97, 382, 89, 23); chckbxNewCheckBox_2_1_2.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_2_1_2); JCheckBox chckbxNewCheckBox_1_1_1 = new JCheckBox("NordicBet"); chckbxNewCheckBox_1_1_1.setBounds(6, 408, 89, 23); chckbxNewCheckBox_1_1_1.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_1_1_1); JCheckBox chckbxNewCheckBox_3_1_1 = new JCheckBox("PaddyPower"); chckbxNewCheckBox_3_1_1.setBounds(97, 408, 89, 23); chckbxNewCheckBox_3_1_1.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_3_1_1); JCheckBox chckbxNewCheckBox_4_1_1 = new JCheckBox("PointsBet(AU)"); chckbxNewCheckBox_4_1_1.setBounds(6, 434, 89, 23); chckbxNewCheckBox_4_1_1.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_4_1_1); JCheckBox chckbxNewCheckBox_5_1_1 = new JCheckBox("PointsBet(US)"); chckbxNewCheckBox_5_1_1.setBounds(97, 434, 89, 23); chckbxNewCheckBox_5_1_1.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_5_1_1); JCheckBox chckbxNewCheckBox_7_1 = new JCheckBox("Pinnacle"); chckbxNewCheckBox_7_1.setBounds(6, 460, 89, 23); chckbxNewCheckBox_7_1.setFont(new Font("Calibri", Font.PLAIN, 11)); panel_2_1.add(chckbxNewCheckBox_7_1); I was hoping to make the checkboxes able to be scrolled through.`
See the below runnable code to see one way this can be done using a JDialog for a window and Layout managers. Be sure to read the comments in code. You should be able to adapt the concept to your specific application: import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.util.ArrayList; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; public class SportBookingsDemo { //Class member variables: private JDialog dia; // JDialog object (class global): private JCheckBox[] chks; // JCheckBox object Array (class global): private JComboBox sportCombo; // JDialog object (class global): private JButton submit; // JButton object (class global): // Captions for All JCheckBoxes (class global): String[] sportsBookNames = {"888Bets", "Bovada", "BlueBet", "Coral", "BoyleSports", "Circa Sports", "Betclic", "Casumo", "Betfair", "DraftKings", "BetMGM", "FanDuel", "BetOnline", "FOX Bet", "BetRivers", "GTbets", "Bet Victor", "Intertops", "BetUS", "Ladbrokes", "LeoVegas", "LiveScore", "LowVig", "Matchbook", "MarathonkBet", "Mr Green", "MyBookie", "Neds", "NordicBet", "PaddyPower", "PointsBet(AU)", "PointsBet(US)", "Pinnacle"}; // ArrayList of String to hold captions for selected checkboxes (class global): private ArrayList<String> bookings = new ArrayList<>(); // String Variable to hold the selected Sport (class global): private String sport; // Constructor: public SportBookingsDemo() { // Create the Dialog Window: createDialog(); // `Submit` button ActionPerformed event: submit.addActionListener((ActionEvent e) -> { bookings.clear(); sport = ""; if (sportCombo.getSelectedItem().equals("Select a sport")) { JOptionPane.showMessageDialog(dia, "You must select a specific Sport!", "No Sport Selected", JOptionPane.WARNING_MESSAGE); sportCombo.requestFocus(); return; } else { sport = sportCombo.getSelectedItem().toString(); } System.out.println("Selected Bookings:"); for (int i = 0; i < chks.length; i++) { if (chks[i].isSelected()) { bookings.add(sportsBookNames[i]); } } if (bookings.isEmpty()) { JOptionPane.showMessageDialog(dia, "You must select at least one Sportsbook!", "No Sportbook Selected", JOptionPane.WARNING_MESSAGE); sportCombo.requestFocus(); return; } // Display dialog results: StringBuilder sb = new StringBuilder(""); sb.append("<html><center>Selected bookings for the Sport of:<br><br>"); sb.append("<font size='5', color=blue><b>").append(sport).append("</b></font><br><br></center>"); for (String bks : bookings) { sb.append("<font size='4', color=green>✓ </font>").append(bks).append("<br>"); } sb.append("<br></html>"); JOptionPane.showMessageDialog(dia, sb.toString(), "Bookings", JOptionPane.INFORMATION_MESSAGE); }); } // Application entry point... public static void main(String[] args) { // App started this way to avoid the need for statics: new SportBookingsDemo().startApp(args); } private void startApp(String[] args) { // Display dialog window: java.awt.EventQueue.invokeLater(() -> { dia.setVisible(true); }); } // Method to create Dialog: private void createDialog() { dia = new JDialog(); // Initialize JDialog. dia.setModal(true); // Set dialog to Modal. dia.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); //Dispose dialog when closed. dia.setTitle("Sports Book Dialog"); // Set dialog title caption. dia.setAlwaysOnTop(true); // Dialog will always be on top of other windows. dia.setSize(280, 390); // Set dialog size. dia.setResizable(false); // Make dialog non-sizeable. dia.setLocationRelativeTo(null); // Open dialog window to center screen. // Decalre and initialize the JDialog's main content panel JPanel mainPanel = new JPanel(); // Set Horizontal and Vertical Gaps for mainPanel's default FlowLayout. FlowLayout layout = new FlowLayout(); /* Set Horiz and Vert layout Gaps. The helps ensure the components in dialog are center aligned as a single column. */ layout.setHgap(140); layout.setVgap(10); mainPanel.setLayout(layout); // Apply Layout. dia.add(mainPanel); // Add mainPanel to dialog Window /* Declare and initialize a JLabel to display the CheckBoxes scrollable list display. */ JLabel label_1 = new JLabel("Sportsbook Selection"); // Set the Font, font style, and font size for the title: label_1.setFont(new Font("Leelawadee UI Semilight", Font.BOLD, 14)); // Set JLabel text Horizontal Alignment to center. label_1.setHorizontalAlignment(JLabel.CENTER); mainPanel.add(label_1); // Add title to mainPanel. /* Declare a JPanel to hold all the checkboxes that will be added to a JScrollPane within the dialog. We also ensure a GridLayout of -1 rows (as many as needed) and 2 columns for the JPanel. */ JPanel chkPanel = new JPanel(new GridLayout(-1, 2)); /* Initialize the chks[] JCheckBox Array to the required length. This length is actually determined by the number of elements within the sportsBookNames[] array which will hereby be considered a `parallel` array to the chks[] array. Every CheckBox element contained within the chks[] array will contain a Caption text from the sportsBookNames[] array at the same index value. */ chks = new JCheckBox[sportsBookNames.length]; /* Iterate through the chks[] array elements and apply JCheckBox objects to them with text captions derived from the sportsBookNames[] array at the current iterated index. Add each chks[] element (JCheckBox) to the `chkPanel` JPanel. */ for (int i = 0; i < chks.length; i++) { chks[i] = new JCheckBox(sportsBookNames[i]); chks[i].setFont(new Font("Calibri", Font.PLAIN, 12)); chkPanel.add(chks[i]); } // Add the now filled chkPanel to a JScrollPane (`chkScroll`). JScrollPane chkScroll = new JScrollPane(chkPanel); // Set the ScrollPane's Preferred Size we want within te dialog. chkScroll.setPreferredSize(new Dimension(250, 200)); // Add the ScrollPane (`chkScroll`) to the Main Panel (`mainPanel`). mainPanel.add(chkScroll); /* Declare a JLabel to be a title for a Sport type JComboBox and provide a title: */ JLabel sportLBL = new JLabel("Sport"); // Set the Font, font style, and font size for the title: sportLBL.setFont(new Font("Leelawadee UI Semilight", Font.BOLD, 14)); // Add the JLabel (`sportLBL`) to the Main Panel (`mainPanel`). mainPanel.add(sportLBL); /* Create a JComboBox (`sportCombo`) and fill it with Sport Types. Sport Type names are contained within the `sports[]` array. */ String[] sports = {"Select A Sport", "NBA", "NFL", "NHL", "ATL", "MLB", "NCAA", "PGA", "CKE"}; sportCombo = new JComboBox<>(sports); sportCombo.setFont(new Font("Leelawadee UI Semilight", Font.PLAIN, 12)); // Set the Preferred Size of the JComboBox (`sportCombo`): sportCombo.setPreferredSize(new Dimension(120, 25)); // Make sure the ComboBox is non-editable. sportCombo.setEditable(false); // Add the JComboBox (`sportCombo`) to the Main Panel (`mainPanel`). mainPanel.add(sportCombo); /* Initialize the `submit` JButton and apply the text caption of "Submit". */ submit = new JButton("Submit"); submit.setFont(new Font("Leelawadee UI Semilight", Font.BOLD, 12)); // Add the JButton (`submit`) to the Main Panel (`mainPanel`). mainPanel.add(submit); } }
How to open a specified window when the user clicks on a button
I'm new to Java and I'd like to event that open a window that I made with windowbuilder when the user click on the button called btnManga, there is the code in the principal.manga(themain window) package biblio; import java.awt.*; import javax.swing.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class principal extends JFrame{ private JFrame frmAcceuil; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); principal window = new principal(); window.frmAcceuil.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public principal() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frmAcceuil = new JFrame(); frmAcceuil.setBackground(new Color(40, 168, 253)); frmAcceuil.setTitle("Acceuil"); frmAcceuil.setResizable(false); frmAcceuil.getContentPane().setBackground(new Color(66, 66, 66)); frmAcceuil.setBounds(100, 100, 518, 571); frmAcceuil.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frmAcceuil.getContentPane().setLayout(null); JLabel lblNewLabel = new JLabel("BIBLIOTHEQUES"); lblNewLabel.setForeground(new Color(40, 168, 253)); lblNewLabel.setBackground(new Color(255, 255, 255)); lblNewLabel.setFont(new Font("Yu Gothic UI Semibold", Font.PLAIN, 30)); lblNewLabel.setBounds(134, 24, 226, 73); frmAcceuil.getContentPane().add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("Que voulez- vous acheter ?"); lblNewLabel_1.setFont(new Font("Yu Gothic UI Semibold", Font.PLAIN, 14)); lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel_1.setForeground(new Color(38, 217, 255)); lblNewLabel_1.setBounds(134, 84, 239, 28); frmAcceuil.getContentPane().add(lblNewLabel_1); JLabel lblNewLabel_1_1 = new JLabel("OU"); lblNewLabel_1_1.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel_1_1.setForeground(new Color(38, 217, 255)); lblNewLabel_1_1.setFont(new Font("Yu Gothic UI Semibold", Font.PLAIN, 14)); lblNewLabel_1_1.setBounds(134, 328, 239, 28); frmAcceuil.getContentPane().add(lblNewLabel_1_1); JButton btnManga = new JButton("MANGA"); btnManga.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { manga mangaWindow = new manga(); mangaWindow.setVisible(true); } }); btnManga.setFont(new Font("Yu Gothic UI Semibold", Font.BOLD, 20)); btnManga.setIcon(null); btnManga.setForeground(new Color(66, 66, 66)); btnManga.setBackground(new Color(40, 168, 253)); btnManga.setBounds(122, 391, 261, 58); frmAcceuil.getContentPane().add(btnManga); JButton btnRoman = new JButton("ROMAN"); btnRoman.setForeground(new Color(66, 66, 66)); btnRoman.setFont(new Font("Yu Gothic UI Semibold", Font.BOLD, 20)); btnRoman.setBackground(new Color(40, 168, 253)); btnRoman.setBounds(122, 234, 261, 58); frmAcceuil.getContentPane().add(btnRoman); } } Here the code in the manga.java that I'd like to display when the button btnManga is clicked package biblio; import java.awt.*; import javax.swing.*; public class manga extends JFrame { private JFrame frmAcheterDesLivres; private JTable mangatable; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { manga mangaWindow = new manga(); mangaWindow.frmAcheterDesLivres.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public manga() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frmAcheterDesLivres = new JFrame(); frmAcheterDesLivres.setTitle("Acheter des Livres"); frmAcheterDesLivres.setResizable(false); frmAcheterDesLivres.getContentPane().setFont(new Font("Yu Gothic Medium", Font.BOLD, 15)); frmAcheterDesLivres.getContentPane().setBackground(new Color(66, 66, 66));; frmAcheterDesLivres.setBounds(100, 100, 758, 601); frmAcheterDesLivres.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frmAcheterDesLivres.getContentPane().setLayout(null); JButton btnNewButton_1_1 = new JButton(""); btnNewButton_1_1.setIcon(new ImageIcon(manga.class.getResource("/resources/mangas/blackClover.jpg"))); btnNewButton_1_1.setBounds(31, 46, 125, 125); frmAcheterDesLivres.getContentPane().add(btnNewButton_1_1); JButton btnNewButton_1 = new JButton(""); btnNewButton_1.setIcon(new ImageIcon(manga.class.getResource("/resources/mangas/chainsawMan.png"))); btnNewButton_1.setBounds(225, 46, 125, 125); frmAcheterDesLivres.getContentPane().add(btnNewButton_1); JButton btnNewButton_1_2 = new JButton(""); btnNewButton_1_2.setIcon(new ImageIcon(manga.class.getResource("/resources/mangas/BokuNoHeroAcademia.jpg"))); btnNewButton_1_2.setBounds(414, 46, 125, 125); frmAcheterDesLivres.getContentPane().add(btnNewButton_1_2); JButton btnNewButton_1_3 = new JButton(""); btnNewButton_1_3.setIcon(new ImageIcon(manga.class.getResource("/resources/mangas/jujutsuKaisen.jpg"))); btnNewButton_1_3.setBounds(590, 46, 125, 125); frmAcheterDesLivres.getContentPane().add(btnNewButton_1_3); JLabel lblNewLabel = new JLabel("Black Clover"); lblNewLabel.setLabelFor(btnNewButton_1_1); lblNewLabel.setForeground(new Color(40, 168, 253)); lblNewLabel.setVerticalAlignment(SwingConstants.TOP); lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel.setFont(new Font("Yu Gothic Medium", Font.ITALIC, 14)); lblNewLabel.setBounds(31, 185, 125, 34); frmAcheterDesLivres.getContentPane().add(lblNewLabel); JLabel lblMga = new JLabel("40 000 MGA"); lblMga.setLabelFor(btnNewButton_1_1); lblMga.setForeground(new Color(40, 168, 253)); lblMga.setVerticalAlignment(SwingConstants.TOP); lblMga.setHorizontalAlignment(SwingConstants.CENTER); lblMga.setFont(new Font("Yu Gothic Medium", Font.ITALIC, 14)); lblMga.setBounds(31, 210, 125, 34); frmAcheterDesLivres.getContentPane().add(lblMga); JLabel lblChainsawMan = new JLabel("Chainsaw Man"); lblChainsawMan.setLabelFor(btnNewButton_1); lblChainsawMan.setForeground(new Color(40, 168, 253)); lblChainsawMan.setVerticalAlignment(SwingConstants.TOP); lblChainsawMan.setHorizontalAlignment(SwingConstants.CENTER); lblChainsawMan.setFont(new Font("Yu Gothic Medium", Font.ITALIC, 14)); lblChainsawMan.setBounds(235, 181, 125, 34); frmAcheterDesLivres.getContentPane().add(lblChainsawMan); JLabel lblMga_1 = new JLabel("60 000 MGA"); lblMga_1.setLabelFor(btnNewButton_1); lblMga_1.setForeground(new Color(40, 168, 253)); lblMga_1.setVerticalAlignment(SwingConstants.TOP); lblMga_1.setHorizontalAlignment(SwingConstants.CENTER); lblMga_1.setFont(new Font("Yu Gothic Medium", Font.ITALIC, 14)); lblMga_1.setBounds(225, 210, 125, 34); frmAcheterDesLivres.getContentPane().add(lblMga_1); JLabel lblBokuNoHero = new JLabel("Boku no Hero Academia"); lblBokuNoHero.setLabelFor(btnNewButton_1_2); lblBokuNoHero.setVerticalAlignment(SwingConstants.TOP); lblBokuNoHero.setHorizontalAlignment(SwingConstants.CENTER); lblBokuNoHero.setForeground(new Color(40, 168, 253)); lblBokuNoHero.setFont(new Font("Yu Gothic Medium", Font.ITALIC, 14)); lblBokuNoHero.setBounds(414, 181, 125, 34); frmAcheterDesLivres.getContentPane().add(lblBokuNoHero); JLabel lblMga_1_1 = new JLabel("35 000 MGA"); lblMga_1_1.setLabelFor(btnNewButton_1_2); lblMga_1_1.setVerticalAlignment(SwingConstants.TOP); lblMga_1_1.setHorizontalAlignment(SwingConstants.CENTER); lblMga_1_1.setForeground(new Color(40, 168, 253)); lblMga_1_1.setFont(new Font("Yu Gothic Medium", Font.ITALIC, 14)); lblMga_1_1.setBounds(414, 213, 125, 34); frmAcheterDesLivres.getContentPane().add(lblMga_1_1); JLabel lblJujutsuKaisen = new JLabel("Jujutsu Kaisen"); lblJujutsuKaisen.setLabelFor(btnNewButton_1_3); lblJujutsuKaisen.setVerticalAlignment(SwingConstants.TOP); lblJujutsuKaisen.setHorizontalAlignment(SwingConstants.CENTER); lblJujutsuKaisen.setForeground(new Color(40, 168, 253)); lblJujutsuKaisen.setFont(new Font("Yu Gothic Medium", Font.ITALIC, 14)); lblJujutsuKaisen.setBounds(590, 181, 125, 34); frmAcheterDesLivres.getContentPane().add(lblJujutsuKaisen); JLabel lblMga_1_1_1 = new JLabel("23 000 MGA"); lblMga_1_1_1.setLabelFor(btnNewButton_1_3); lblMga_1_1_1.setVerticalAlignment(SwingConstants.TOP); lblMga_1_1_1.setHorizontalAlignment(SwingConstants.CENTER); lblMga_1_1_1.setForeground(new Color(40, 168, 253)); lblMga_1_1_1.setFont(new Font("Yu Gothic Medium", Font.ITALIC, 14)); lblMga_1_1_1.setBounds(590, 213, 125, 34); frmAcheterDesLivres.getContentPane().add(lblMga_1_1_1); JLabel lblNewLabel_1 = new JLabel("Votre Panier de Mangas:"); lblNewLabel_1.setFont(new Font("Yu Gothic UI Semibold", Font.PLAIN, 20)); lblNewLabel_1.setForeground(new Color(38, 217, 255)); lblNewLabel_1.setBounds(257, 291, 282, 34); frmAcheterDesLivres.getContentPane().add(lblNewLabel_1); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(61, 335, 641, 158); frmAcheterDesLivres.getContentPane().add(scrollPane); mangatable = new JTable(); scrollPane.setViewportView(mangatable); mangatable.setBackground(new Color(49, 49, 49)); mangatable.setForeground(new Color(38, 217, 255)); } } I've tried to do it myself btnManga.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { manga mangaWindow = new manga(); mangaWindow.setVisible(true); } }); However, this displays an empty window when I click on it.
In your main method in manga you show the window correctly. `mangaWindow.frmAcheterDesLivres.setVisible(true);` In your action listener you call mangaWindow.setVisible(true); but mangaWindow has no components added to it. You need to includ the reference to the JFrame. btnManga.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { manga mangaWindow = new manga(); mangaWindow.frmAcheterDesLivres.setVisible(true); } }); You should delete the extends JFrame to avoid this confusion. You should also use JDialog extra windows.
if you are using netbeans, create a button, then generate actionperformed on that button, then you can follow the following private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { manga mangaWindow = new manga(); mangaWindow.setVisible(true); }
Did you try adding mangaWindow.setSize before setVisible? Edit: also you shouldn't have two different main methods in your application (technically there is nothing bad on it but it can be confusing because only one of them will be used when launching the application)
Why does this program's interface disappear to the right?
I'm working on a group project with my friends for a school assignment. Unfortunately, one of my group members made this program which was much worse before (I tried to fix most of the main issues). Only problem now is, the program doesn't load all of the text on the right, instead leaving ellipses (...). I've tried modifying and removing setBounds, setSize, and contentPane, yet all those proved ineffective. I'm fairly new to Java all round, so please do go easy on my inexperience. public class HomeMenu extends JFrame { private JPanel contentPane; private JTextField textField; public HomeMenu() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(1366, 768); setBounds(100, 100, 954, 712); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); setBackground(new Color(255, 255, 255)); getContentPane().setLayout(null); contentPane.setLayout(null); JPanel panel = new JPanel(); panel.setBackground(Color.BLACK); panel.setForeground(Color.WHITE); panel.setBounds(0, 0, 236, 671); getContentPane().add(panel); panel.setLayout(null); JPanel panel_3 = new JPanel(); panel_3.setBounds(44, 5, 1, 1); panel.add(panel_3); panel_3.setLayout(null); JPanel panel_1 = new JPanel(); panel_1.setBackground(new Color(0, 0, 102)); panel_1.setBounds(233, 240, 705, 431); getContentPane().add(panel_1); panel_1.setLayout(null); JPanel panel_6 = new JPanel(); panel_6.setBackground(SystemColor.controlDkShadow); panel_6.setBounds(307, 235, 285, 164); panel_1.add(panel_6); JLabel lblWED = new JLabel("Weddings\r\n"); panel_6.add(lblWED); lblWED.setForeground(Color.WHITE); lblWED.setFont(new Font("Sitka Subheading", Font.PLAIN, 23)); lblWED.setBackground(Color.WHITE); JLabel lblNewLabel_4 = new JLabel(""); panel_6.add(lblNewLabel_4); lblNewLabel_4.setIcon(new ImageIcon("C:\\Users\\HP\\Downloads\\groupwork_src_index2.jpg")); JPanel panel_4 = new JPanel(); panel_4.setBackground(SystemColor.controlDkShadow); panel_4.setBounds(43, 25, 189, 298); panel_1.add(panel_4); JLabel lblBirthdayPartiesl = new JLabel("Birthday Partiesl"); panel_4.add(lblBirthdayPartiesl); lblBirthdayPartiesl.setFont(new Font("Snap ITC", Font.PLAIN, 18)); lblBirthdayPartiesl.setForeground(Color.WHITE); lblBirthdayPartiesl.setBackground(Color.WHITE); JLabel lblNewLabel_3 = new JLabel(""); panel_4.add(lblNewLabel_3); lblNewLabel_3.setIcon(new ImageIcon("C:\\Users\\HP\\Downloads\\groupwork_src_index.jpg")); JLabel lblWeDoIt = new JLabel("WE DO nothing at ALL !"); lblWeDoIt.setBounds(68, 359, 212, 40); panel_1.add(lblWeDoIt); lblWeDoIt.setForeground(Color.WHITE); lblWeDoIt.setFont(new Font("Tahoma", Font.PLAIN, 28)); lblWeDoIt.setBackground(Color.WHITE); JPanel panel_5 = new JPanel(); panel_5.setBounds(297, 25, 358, 185); panel_1.add(panel_5); panel_5.setBackground(SystemColor.controlDkShadow); JLabel lblMeetings = new JLabel("Meetings"); panel_5.add(lblMeetings); lblMeetings.setForeground(Color.WHITE); lblMeetings.setFont(new Font("Tahoma", Font.PLAIN, 20)); lblMeetings.setBackground(Color.WHITE); JLabel lblNewLabel_6 = new JLabel(""); panel_5.add(lblNewLabel_6); lblNewLabel_6.setIcon(new ImageIcon("C:\\Users\\HP\\Downloads\\groupwork_src_index(1)")); JLabel lblNewLabel_5 = new JLabel(""); panel_5.add(lblNewLabel_5); lblNewLabel_5.setIcon(new ImageIcon("C:\\Users\\18060327\\Desktop\\index 3.jpg")); JPanel panel_2 = new JPanel(); panel_2.setBounds(235, 54, 703, 188); contentPane.add(panel_2); panel_2.setBackground(new Color(204, 204, 204)); panel_2.setLayout(null); JLabel lblNewLabel = new JLabel("ShadEvents"); lblNewLabel.setFont(new Font("Segoe UI Light", Font.PLAIN, 36)); lblNewLabel.setBounds(61, 24, 286, 58); panel_2.add(lblNewLabel); JLabel label = new JLabel("Here to make your life harder"); label.setFont(new Font("Segoe UI Light", Font.PLAIN, 13)); label.setBounds(78, 84, 188, 16); panel_2.add(label); textField = new JTextField(); textField.setBounds(297, 11, 123, 24); contentPane.add(textField); textField.setColumns(10); JLabel lblNewLabel_2 = new JLabel(""); lblNewLabel_2.setIcon(new ImageIcon("C:\\Users\\HP\\Downloads\\icons8-user-24.png")); lblNewLabel_2.setBounds(851, 11, 29, 32); contentPane.add(lblNewLabel_2); JLabel lblUser = new JLabel("Welcome!"); //fix this user shit //unfixable lblUser.setBounds(890, 19, 38, 24); contentPane.add(lblUser); lblUser.setFont(new Font("Segoe UI Light", Font.PLAIN, 15)); JLabel lblNewLabel_1 = new JLabel(); lblNewLabel_1.setBounds(258, 11, 29, 24); contentPane.add(lblNewLabel_1); lblNewLabel_1.setBackground(Color.BLUE); lblNewLabel_1.setIcon(new ImageIcon("C:\\Users\\HP\\Downloads\\groupwork_src_icons8-search-24.png")); } } Instead of "WE DO nothin..." and "Wel..." (at the top right), it should display "WE DO nothing at ALL!" (Also, do let me know if the code doesn't work.)
can not refer a value to a radiobutton
i write a convert temperature code using radiobutton, but when i use actionListener to print the value to a label, it show "Cannot refer to the non-final local variable valueto defined in an enclosing scope" can some one show me the problem and how to fix it? (also sorry for my English) the Radiobutton JRadioButton Cbutton = new JRadioButton("Celsius"); Fbutton.setFont(new Font("Tahoma", Font.PLAIN, 12)); Fbutton.setBounds(10, 40, 109, 23); contentPane.add(Cbutton); JRadioButton Fbutton2 = new JRadioButton("Fahrenheit"); Fbutton2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { label.setText(String.valueOf(valueto)); } }); i use this to convert if(Fbutton.isSelected()&&Cbutton2.isSelected()){ value=F; valueto=(F-32)/1.8; }else if(Fbutton.isSelected()&&Kbutton2.isSelected()){ value=F; valueto=(F+459.67)*5/9; }else if(Cbutton.isSelected()&&Fbutton2.isSelected()){ value=C; valueto=C*1.8+32; }else if(Cbutton.isSelected()&&Kbutton2.isSelected()){ value=C; valueto=C+273.15; }else if(Kbutton.isSelected()&&Cbutton2.isSelected()){ value=K; valueto=K-273.15; }else if(Kbutton.isSelected()&&Fbutton2.isSelected()){ value=K; valueto=K*9.5-459.67; } Edit : i declare valueto at the bottom of the class double value = 0; double valueto = 0; double F = 0, C = 0, K = 0; it ask me to add final to "double valueto", but when i add it the valueto in "if" become error The Whole code import java.awt.EventQueue; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JTextField; import javax.swing.border.Border; import javax.swing.border.EmptyBorder; import javax.swing.border.LineBorder; public class CtoKtoF extends JFrame { private JPanel contentPane; private JTextField textField; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { CtoKtoF frame = new CtoKtoF(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. * #return */ public CtoKtoF() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 303); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); double value = 0 ; double valueto = 0; double F = 0, C = 0, K = 0; Border border = LineBorder.createGrayLineBorder();//make border for Jlabel final JLabel label = new JLabel("New label"); label.setFont(new Font("Tahoma", Font.PLAIN, 12)); label.setBounds(10, 228, 220, 22); label.setBorder(border); contentPane.add(label); JLabel lblNewLabel = new JLabel("Convert from"); lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 12)); lblNewLabel.setBounds(10, 11, 92, 22); contentPane.add(lblNewLabel); JRadioButton Fbutton = new JRadioButton("Fahrenheit"); Fbutton.setFont(new Font("Tahoma", Font.PLAIN, 12)); Fbutton.setBounds(10, 40, 109, 23); //Fbutton.setActionCommand("F"); contentPane.add(Fbutton); JRadioButton Cbutton = new JRadioButton("Celcius"); Cbutton.setFont(new Font("Tahoma", Font.PLAIN, 12)); Cbutton.setBounds(121, 40, 109, 23); //Cbutton.setActionCommand("C"); contentPane.add(Cbutton); JRadioButton Kbutton = new JRadioButton("Kelvin"); Kbutton.setFont(new Font("Tahoma", Font.PLAIN, 12)); Kbutton.setBounds(232, 40, 109, 23); //Kbutton.setActionCommand("K"); contentPane.add(Kbutton); JLabel lblEnterNumericTemperature = new JLabel("Enter Numeric Temperature"); lblEnterNumericTemperature.setFont(new Font("Tahoma", Font.PLAIN, 12)); lblEnterNumericTemperature.setBounds(10, 70, 238, 22); contentPane.add(lblEnterNumericTemperature); textField = new JTextField(); textField.setFont(new Font("Tahoma", Font.PLAIN, 12)); textField.setBounds(10, 103, 220, 22); textField.setText(String.valueOf(value)); contentPane.add(textField); textField.setColumns(10); JRadioButton Fbutton2 = new JRadioButton("Fahrenheit"); Fbutton2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { label.setText(String.valueOf(valueto)); } }); Fbutton2.setFont(new Font("Tahoma", Font.PLAIN, 12)); Fbutton2.setBounds(10, 165, 109, 23); //Fbutton2.setActionCommand("F2"); contentPane.add(Fbutton2); JRadioButton Cbutton2 = new JRadioButton("Celcius"); Cbutton2.setFont(new Font("Tahoma", Font.PLAIN, 12)); Cbutton2.setBounds(121, 165, 109, 23); //Cbutton2.setActionCommand("C2"); contentPane.add(Cbutton2); JRadioButton Kbutton2 = new JRadioButton("Kelvin"); Kbutton2.setFont(new Font("Tahoma", Font.PLAIN, 12)); Kbutton2.setBounds(232, 165, 109, 23); //Kbutton2.setActionCommand("K2"); contentPane.add(Kbutton2); ButtonGroup group1 = new ButtonGroup(); group1.add(Fbutton); group1.add(Kbutton); group1.add(Cbutton); ButtonGroup group2 = new ButtonGroup(); group2.add(Fbutton2); group2.add(Cbutton2); group2.add(Kbutton2); /*Fbutton.addActionListener(this); Cbutton.addActionListener(this); Kbutton.addActionListener(this); Fbutton2.addActionListener(this); Cbutton2.addActionListener(this); Kbutton2.addActionListener(this);*/ //boolean checked = Fbutton.getState(); JLabel lblConvertTo = new JLabel("Convert to"); lblConvertTo.setFont(new Font("Tahoma", Font.PLAIN, 12)); lblConvertTo.setBounds(10, 136, 92, 22); contentPane.add(lblConvertTo); JLabel lblComparableTemperatureIs = new JLabel("Comparable Temperature is"); lblComparableTemperatureIs.setFont(new Font("Tahoma", Font.PLAIN, 12)); lblComparableTemperatureIs.setBounds(10, 195, 238, 22); contentPane.add(lblComparableTemperatureIs); if(Fbutton.isSelected()&&Cbutton2.isSelected()){ value=F; valueto=(F-32)/1.8; }else if(Fbutton.isSelected()&&Kbutton2.isSelected()){ value=F; valueto=(F+459.67)*5/9; }else if(Cbutton.isSelected()&&Fbutton2.isSelected()){ value=C; valueto=C*1.8+32; }else if(Cbutton.isSelected()&&Kbutton2.isSelected()){ value=C; valueto=C+273.15; }else if(Kbutton.isSelected()&&Cbutton2.isSelected()){ value=K; valueto=K-273.15; }else if(Kbutton.isSelected()&&Fbutton2.isSelected()){ value=K; valueto=K*9.5-459.67; } } }
Java Swing GUI Invisible
basically I have a very simple GUI, but for some reason when I run the code the JButtons only appear when I mouse over them, and the JTextField only appear if I click on them. How do I fix this so that they are visible when the program runs? Thanks. Here is my code: //JFrame + settings JFrame frmFormSubmission = new JFrame(); frmFormSubmission.setSize(new Dimension(350, 165)); frmFormSubmission.setTitle("Form Submission - Client"); frmFormSubmission.setLocationRelativeTo(null); frmFormSubmission.setVisible(true); frmFormSubmission.setResizable(false); frmFormSubmission.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //JPanel JPanel panel = new JPanel(); frmFormSubmission.getContentPane().add(panel, BorderLayout.CENTER); panel.setLayout(null); //JLabels //Name JLabel lblName = new JLabel("Name:"); lblName.setFont(new Font("Tahoma", Font.PLAIN, 14)); lblName.setBounds(46, 12, 40, 14); panel.add(lblName); //Address JLabel lblAddress = new JLabel("Address:"); lblAddress.setFont(new Font("Tahoma", Font.PLAIN, 14)); lblAddress.setBounds(33, 37, 53, 14); panel.add(lblAddress); //Phone JLabel lblPhone = new JLabel("Phone #:"); lblPhone.setFont(new Font("Tahoma", Font.PLAIN, 14)); lblPhone.setBounds(28, 62, 58, 14); panel.add(lblPhone); //Email JLabel lblEmail = new JLabel("Email:"); lblEmail.setFont(new Font("Tahoma", Font.PLAIN, 14)); lblEmail.setBounds(46, 87, 36, 14); panel.add(lblEmail); //JTextFields //Name nameField = new JTextField(); nameField.setBounds(93, 11, 137, 20); panel.add(nameField); nameField.setColumns(10); //Address addressField = new JTextField(); addressField.setColumns(10); addressField.setBounds(93, 36, 137, 20); panel.add(addressField); //Phone phoneField = new JTextField(); phoneField.setColumns(10); phoneField.setBounds(93, 61, 137, 20); panel.add(phoneField); //Email emailField = new JTextField(); emailField.setColumns(10); emailField.setBounds(93, 86, 137, 20); panel.add(emailField); //JButtons //Submit JButton btnSubmit = new JButton("Submit"); btnSubmit.setBounds(240, 10, 89, 23); panel.add(btnSubmit); //Cancel JButton btnCancel = new JButton("Cancel"); btnCancel.setBounds(240, 44, 89, 23); panel.add(btnCancel); //Flush JButton btnFlush = new JButton("Flush"); btnFlush.setBounds(240, 76, 89, 23); panel.add(btnFlush); //Checkbox JCheckBox chckbxPromotions = new JCheckBox("Email me with new and promotions!"); chckbxPromotions.setBounds(25, 108, 205, 23); panel.add(chckbxPromotions); Any help is very much appreciated!
Call frmFormSubmission.setVisible(true); as the last thing you do. setVisible will layout your components so they can be properly drawn. If you add components without laying them out, then you'll have graphical issues. For more information on what that means, check out the documentation on the validate method.
Also, try frmFormSubmission.invalidate(); frmFormSubmission.validate(); frmFormSubmission.repaint(); If this still does not work, you can try this: SwingUtilities.updateComponentTreeUI(frmFormSubmission);