This question already has an answer here:
Getting runtime errors in Java program
(1 answer)
Closed 8 years ago.
Okay, so I did javac dicebot.java, it worked fine, but when I did java dicebot.java I got this:
I really do not know how much better I can explain it considering I have no clue what these errors that I got mean. So any help would be great, Thank you!
It is different than that question that you stated there because it has nothing to do with it. This is a completely different question and it's errors are unique...
java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1086)
at java.awt.Container.add(Container.java:966)
at dicebot.<init>(dicebot.java:67)
at dicebot$1.run(dicebot.java:26)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Here is my coding that I made and tried to run:
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class dicebot extends JFrame implements ActionListener {
static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
public static JComboBox combo;
public static JButton btnConfirm;
public static JTextField txtUserName;
public static JTextField txtStartBid;
public static JTextField txtMultiplier;
public static JTextField txtMinRemaining;
public static JTextField txtPassword;
public static JTextField txtOdds;
public static JTextField txtMaxBet;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
dicebot frame = new dicebot();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public dicebot() {
setTitle("Dice Bot");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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.WEST);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0};
gbl_panel.rowHeights = new int[]{0, 0};
gbl_panel.columnWeights = new double[]{0.0, 1.0};
gbl_panel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
//Every new Label however needs every part that says "user" or on the Password: "pass" changed to something unique.
JLabel userTag = new JLabel("Username:");
GridBagConstraints gbc_userTag = new GridBagConstraints();
gbc_userTag.insets = new Insets(0, 0, 0, 5);
gbc_userTag.anchor = GridBagConstraints.EAST;
gbc_userTag.gridx = 0;//Here are your x + y coords
gbc_userTag.gridy = 0;//Adding to x moves left, adding to y moves down
panel.add(userTag, gbc_userTag);
//Every new textfield needs only the * part to change for it to be valid. (gbc_* =)
txtUserName = new JTextField();
GridBagConstraints txtUserName = new GridBagConstraints();
txtUserName.fill = GridBagConstraints.HORIZONTAL;
txtUserName.gridx = 1;
txtUserName.gridy = 0;
panel.add(textField,txtUserName);
textField.setColumns(10);
JLabel startTag = new JLabel("Starting Bid:");
GridBagConstraints gbc_startTag = new GridBagConstraints();
gbc_startTag.insets = new Insets(0, 0, 0, 5);
gbc_startTag.anchor = GridBagConstraints.EAST;
gbc_startTag.gridx = 0;
gbc_startTag.gridy = 2;
panel.add(startTag, gbc_startTag);
txtStartBid = new JTextField();
GridBagConstraints txtStartBid = new GridBagConstraints();
txtStartBid.fill = GridBagConstraints.HORIZONTAL;
txtStartBid.gridx = 1;
txtStartBid.gridy = 2;
panel.add(textField, txtStartBid);
textField.setColumns(10);
JLabel multTag = new JLabel("Multiplier:");
GridBagConstraints gbc_multTag = new GridBagConstraints();
gbc_multTag.insets = new Insets(0, 0, 0, 5);
gbc_multTag.anchor = GridBagConstraints.EAST;
gbc_multTag.gridx = 0;
gbc_multTag.gridy = 3;
panel.add(multTag, gbc_multTag);
txtMultiplier = new JTextField();
GridBagConstraints txtMultiplier = new GridBagConstraints();
txtMultiplier.fill = GridBagConstraints.HORIZONTAL;
txtMultiplier.gridx = 1;
txtMultiplier.gridy = 3;
panel.add(textField, txtMultiplier);
textField.setColumns(10);
JLabel minTag = new JLabel("Min Remaining:");
GridBagConstraints gbc_minTag = new GridBagConstraints();
gbc_minTag.insets = new Insets(0, 0, 0, 5);
gbc_minTag.anchor = GridBagConstraints.EAST;
gbc_minTag.gridx = 0;
gbc_minTag.gridy = 4;
panel.add(minTag, gbc_minTag);
txtMinRemaining = new JTextField();
GridBagConstraints txtMinRemaining = new GridBagConstraints();
txtMinRemaining.fill = GridBagConstraints.HORIZONTAL;
txtMinRemaining.gridx = 1;
txtMinRemaining.gridy = 4;
panel.add(textField, txtMinRemaining);
textField.setColumns(10);
txtPassword = new JTextField();
GridBagConstraints txtPassword = new GridBagConstraints();
txtPassword.fill = GridBagConstraints.HORIZONTAL;
txtPassword.gridx = 1;
txtPassword.gridy = 1;
panel.add(textField, txtPassword);
textField.setColumns(10);
JLabel passTag = new JLabel("Password:");
GridBagConstraints gbc_passTag = new GridBagConstraints();
gbc_passTag.insets = new Insets(0, 0, 0, 5);
gbc_passTag.anchor = GridBagConstraints.EAST;
gbc_passTag.gridx = 0;
gbc_passTag.gridy = 1;
panel.add(passTag, gbc_passTag);
txtOdds = new JTextField();
GridBagConstraints txtOdds = new GridBagConstraints();
txtOdds.fill = GridBagConstraints.HORIZONTAL;
txtOdds.gridx = 1;
txtOdds.gridy = 5;
panel.add(textField, txtOdds);
textField.setColumns(10);
JLabel oddsTag = new JLabel("Odds %:");
GridBagConstraints gbc_oddsTag = new GridBagConstraints();
gbc_oddsTag.insets = new Insets(0, 0, 0, 5);
gbc_oddsTag.anchor = GridBagConstraints.EAST;
gbc_oddsTag.gridx = 0;
gbc_oddsTag.gridy = 5;
panel.add(oddsTag, gbc_oddsTag);
txtMaxBet = new JTextField();
GridBagConstraints txtMaxBet = new GridBagConstraints();
txtMaxBet.fill = GridBagConstraints.HORIZONTAL;
txtMaxBet.gridx = 1;
txtMaxBet.gridy = 6;
panel.add(textField, txtMaxBet);
textField.setColumns(10);
//This is the Combo Box
combo = new JComboBox<String>(new String[]{"BTC","LTC","PPC","NMC","XPM","FTC","ANC","DOGE","NXT"});
combo.addActionListener(this);
GridBagConstraints gbc_list = new GridBagConstraints();
gbc_list.fill = GridBagConstraints.HORIZONTAL;
gbc_list.gridx = 1;
gbc_list.gridy = 7;
panel.add(combo, gbc_list);
JLabel maxTag = new JLabel("MaxBet:");
GridBagConstraints gbc_maxTag = new GridBagConstraints();
gbc_maxTag.insets = new Insets(0, 0, 0, 5);
gbc_maxTag.anchor = GridBagConstraints.EAST;
gbc_maxTag.gridx = 0;
gbc_maxTag.gridy = 6;
panel.add(maxTag, gbc_maxTag);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.SOUTH);
panel_1.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
btnConfirm = new JButton("Turn Up");
btnConfirm.addActionListener(this);
panel_1.add(btnConfirm);
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
JTextArea textArea = new JTextArea("Current Balance");
textArea.setColumns(1);
scrollPane.setViewportView(textArea);
JScrollPane scrollPanel1 = new JScrollPane();//This will hold the information the bot sends over such as win/loose or error
contentPane.add(scrollPane, BorderLayout.CENTER);
JTextArea textAreal = new JTextArea("Input bot information here...");
textArea.setColumns(20);
scrollPane.setViewportView(textArea);
pack();
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == combo) {
System.out.println(combo.getSelectedIndex()+1);
}
}
}
A NullPointerException means that something is trying to call a method or access a variable of an object which is null.
According to your comment the exception is thrown at
panel.add(textField,txtUserName);
Now, txtUserName is initialized few lines before so it shouldn't be the cause, what about textField variable?
As Jack pointed out, textField is not initialized.
Related
I am currently trying to include a JTable to my JPanel. I was not having any problems with my code previously until I started to code the table. Sometimes when I run my program everything will appear, sometimes just the panels appear, and sometimes/majority of the time nothing will appear. I just don't understand why it is not consistent. There are no errors in the console. I have included below my ViewPage.java, Page.java, and Main.java.
`
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ViewPage extends Page implements ActionListener
{
JPanel panel1, panel2, panel3;
JLabel searchLabel, repairsLabel, dateLabel;
JButton goButton, recentButton, oldestButton, homeButton;
JComboBox dropDown;
JTextField textField;
JTable tabel;
JScrollPane scrollpane;
public ViewPage()
{
panel1 = new JPanel();
panel1.setBackground(Color.blue);
panel1.setBounds(0,0,1280,120);
panel2 = new JPanel();
panel2.setBackground(Color.green);
panel2.setBounds(0,120,1280,480);
panel3 = new JPanel();
panel3.setBackground(Color.red);
panel3.setBounds(0,600,1280,120);
//Panel 1 components
repairsLabel = new JLabel("Repairs");
repairsLabel.setFont(new Font("Serif", Font.BOLD, 50));
searchLabel = new JLabel("Search :");
searchLabel.setFont(new Font("Serif", Font.PLAIN, 25));
goButton = new JButton("GO");
goButton.setFocusable(false);
goButton.addActionListener(this);
textField = new JTextField();
String[] filters = {" ", "customerID", "First_Name", "Last_Name"};
dropDown = new JComboBox(filters);
dropDown.addActionListener(this);
panel1.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(1, 10, 1, 0);
gbc.weightx = 5.5;
gbc.gridx = 0;
gbc.gridy = 0;
panel1.add(repairsLabel, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 0.5;
gbc.gridx = 1;
gbc.gridy = 0;
panel1.add(searchLabel, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(1, 10, 1, 50);
gbc.weightx = 3;
gbc.gridx = 2;
gbc.gridy = 0;
panel1.add(dropDown, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(1, 10, 1, 50);
gbc.gridx = 3;
gbc.gridy = 0;
gbc.ipadx = 100;
panel1.add(textField, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(1, 10, 1, 100);
gbc.gridx = 4;
gbc.gridy = 0;
gbc.ipadx = 1;
panel1.add(goButton, gbc);
//Panel 2 components
panel2.setLayout(new GridBagLayout());
String[][] data = new String[50][8];
String[] headings = {"customer_ID", "Date", "First Name", "Last Name", "Phone #", "OS", "PC Type", "Problem"};
JTable table = new JTable(data, headings);
table.setEnabled(false);
table.setPreferredScrollableViewportSize(new Dimension(1000,400));
table.setFillsViewportHeight(true);
scrollpane = new JScrollPane(table);
panel2.add(scrollpane);
//Panel 3 componets
panel3.setLayout(new GridBagLayout());
GridBagConstraints gbc2 = new GridBagConstraints();
dateLabel = new JLabel("Date Filter: ");
dateLabel.setFont(new Font("Serif", Font.PLAIN, 25));
recentButton = new JButton("Most Recent");
oldestButton = new JButton("Oldest");
homeButton = new JButton("Home");
gbc2.fill = GridBagConstraints.HORIZONTAL;
gbc2.insets = new Insets(1, 10, 1, 0);
gbc2.weightx = 5.5;
gbc2.gridx = 0;
gbc2.gridy = 0;
panel3.add(dateLabel, gbc);
frame.add(panel1);
frame.add(panel2);
frame.add(panel3);
}
#Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == dropDown)
{
System.out.println(dropDown.getSelectedItem());
}
}
}
`
`
import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
public abstract class Page
{
private final static int pageWidth = 1280;
private final static int pageHeight = 720;
protected JFrame frame;
public Page()
{
frame = new JFrame();
frame.setTitle("Computer Repair Store");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(pageWidth, pageHeight);
frame.setLayout(null);
frame.setVisible(true);
}
public int getpageWidth()
{
return pageWidth;
}
public int getpageHeight()
{
return pageHeight;
}
}
`
`
public class Main {
public static void main(String[] args) {
ViewPage viewPage = new ViewPage();
}
}
`
Ouput:
enter image description here
This is what I am expecting to output which has only appeared 2-3 times out of the 50 times I have tried running the program.
enter image description here
I've a problem with a JPanel.
It's a CardLayout and two sub-panel that change.
I need to do a button which switch from subpanel 1 to 2 and display selected Product.
ISSUE:
When I select a product card, JPanel changes but it's blank.
If I step over the window with the mouse , the buttons appear, but not the JLabels.
And If I resize the windows, it's empty again.
What is the problem?
EDIT:
Here the code that return same error.
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.*;
import javax.swing.*;
public class MainFrame extends JFrame {
private static final long serialVersionUID = 1L;
// private String title;
// private Dimension d;
public MainFrame(String title, Dimension d) {
// LoginPanel template = new LoginPanel(this);
// RegisterPanel template = new RegisterPanel(this);
CustomerPanel template = new CustomerPanel(this);
this.setTitle(title);
this.setSize(d);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.getContentPane().add(template);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new MainFrame("Fubars", new Dimension(800, 650));
});
}
}
#SuppressWarnings("serial")
class CustomerPanel extends JPanel {
MainFrame mf;
JPanel one, two;
JPanel panel;
public CustomerPanel(MainFrame mf) {
this.mf = mf;
mf.getContentPane().setLayout(new CardLayout(0, 0));
JPanel container = new JPanel();
mf.getContentPane().add(container, "name_36743208542992");
container.setLayout(new BorderLayout(0, 0));
JPanel back = new JPanel();
container.add(back, BorderLayout.NORTH);
back.setLayout(new FlowLayout(FlowLayout.LEFT));
JButton btnControl = new JButton("<");
back.add(btnControl);
panel = new JPanel();
container.add(panel, BorderLayout.CENTER);
CardLayout cl = new CardLayout(0, 0);
panel.setLayout(cl);
one = new OnePanel(this.mf);
two = new JPanel();
panel.add(one, "1");
panel.add(two, "2");
btnControl.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
two = new LoginPanel(mf);
cl.next(panel);
}
});
}
}
#SuppressWarnings("serial")
class LoginPanel extends JPanel {
// private MainFrame mf;
private JTextField textField;
private JPasswordField passwordField;
public LoginPanel(MainFrame mf) {
// this.mf = mf;
mf.getContentPane().setLayout(new BorderLayout(0, 0));
JPanel panel = new JPanel();
mf.getContentPane().add(panel, BorderLayout.CENTER);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[] { 100, 55, 72, 171, 0 };
gbl_panel.rowHeights = new int[] { 69, 22, 22, 0 };
gbl_panel.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
gbl_panel.rowWeights = new double[] { 0.0, 0.0, 0.0, Double.MIN_VALUE };
panel.setLayout(gbl_panel);
JLabel lblNewLabel = new JLabel("Email");
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.fill = GridBagConstraints.HORIZONTAL;
gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
gbc_lblNewLabel.gridx = 1;
gbc_lblNewLabel.gridy = 1;
panel.add(lblNewLabel, gbc_lblNewLabel);
textField = new JTextField();
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.anchor = GridBagConstraints.NORTH;
gbc_textField.insets = new Insets(0, 0, 5, 0);
gbc_textField.gridx = 3;
gbc_textField.gridy = 1;
panel.add(textField, gbc_textField);
textField.setColumns(15);
JLabel lblNewLabel_1 = new JLabel("Password");
GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
gbc_lblNewLabel_1.anchor = GridBagConstraints.WEST;
gbc_lblNewLabel_1.insets = new Insets(0, 0, 0, 5);
gbc_lblNewLabel_1.gridx = 1;
gbc_lblNewLabel_1.gridy = 2;
panel.add(lblNewLabel_1, gbc_lblNewLabel_1);
passwordField = new JPasswordField();
passwordField.setColumns(15);
GridBagConstraints gbc_passwordField = new GridBagConstraints();
gbc_passwordField.anchor = GridBagConstraints.NORTH;
gbc_passwordField.gridx = 3;
gbc_passwordField.gridy = 2;
panel.add(passwordField, gbc_passwordField);
JPanel panel_1 = new JPanel();
mf.getContentPane().add(panel_1, BorderLayout.SOUTH);
panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 30));
JButton btnNewButton = new JButton("LOGIN");
panel_1.add(btnNewButton);
btnNewButton.setActionCommand("login");
JButton btnRegistration = new JButton("REGISTER");
panel_1.add(btnRegistration);
btnRegistration.setActionCommand("registration");
}
}
#SuppressWarnings("serial")
class OnePanel extends JPanel {
// private MainFrame mf;
public OnePanel(MainFrame mf) {
// this.mf = mf;
mf.getContentPane().setLayout(new CardLayout(0, 0));
JPanel container = new JPanel();
mf.getContentPane().add(container, "name_36743208542992");
container.setLayout(new BorderLayout(0, 0));
JPanel image = new JPanel();
container.add(image, BorderLayout.CENTER);
JButton btnImageBack = new JButton("<");
image.add(btnImageBack);
JLabel imageContainer = new JLabel("Images");
image.add(imageContainer);
imageContainer.setBounds(new Rectangle(100, 100, 100, 100));
imageContainer.setHorizontalTextPosition(SwingConstants.CENTER);
imageContainer.setHorizontalAlignment(SwingConstants.CENTER);
imageContainer.setAlignmentX(Component.CENTER_ALIGNMENT);
imageContainer.setIconTextGap(3);
imageContainer.setIcon(null);
JButton btnImageForward = new JButton(">");
image.add(btnImageForward);
btnImageForward.setAlignmentY(Component.BOTTOM_ALIGNMENT);
btnImageForward.setAlignmentX(Component.CENTER_ALIGNMENT);
JPanel info = new JPanel();
container.add(info, BorderLayout.EAST);
GridBagLayout gbl_info = new GridBagLayout();
gbl_info.columnWidths = new int[] { 0, 0, 63, 0, 0, 0 };
gbl_info.rowHeights = new int[] { 0, 0, 25, 0, 0, 0, 0 };
gbl_info.columnWeights = new double[] { 1.0, 0.0, 1.0, 0.0, 1.0, Double.MIN_VALUE };
gbl_info.rowWeights = new double[] { 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE };
info.setLayout(gbl_info);
JLabel lblTitle = new JLabel("Title");
GridBagConstraints gbc_lblTitle = new GridBagConstraints();
gbc_lblTitle.anchor = GridBagConstraints.NORTHWEST;
gbc_lblTitle.insets = new Insets(0, 0, 5, 5);
gbc_lblTitle.gridx = 2;
gbc_lblTitle.gridy = 1;
info.add(lblTitle, gbc_lblTitle);
JLabel lblDescription = new JLabel("Description");
GridBagConstraints gbc_lblDescription = new GridBagConstraints();
gbc_lblDescription.anchor = GridBagConstraints.WEST;
gbc_lblDescription.insets = new Insets(0, 0, 5, 5);
gbc_lblDescription.gridx = 2;
gbc_lblDescription.gridy = 2;
info.add(lblDescription, gbc_lblDescription);
JLabel lblPrice = new JLabel("Price");
GridBagConstraints gbc_lblPrice = new GridBagConstraints();
gbc_lblPrice.anchor = GridBagConstraints.WEST;
gbc_lblPrice.insets = new Insets(0, 0, 5, 5);
gbc_lblPrice.gridx = 2;
gbc_lblPrice.gridy = 3;
info.add(lblPrice, gbc_lblPrice);
lblPrice.setToolTipText("Price");
JButton btnAddCart = new JButton("Add to Cart");
GridBagConstraints gbc_btnAddCart = new GridBagConstraints();
gbc_btnAddCart.insets = new Insets(0, 0, 5, 5);
gbc_btnAddCart.anchor = GridBagConstraints.WEST;
gbc_btnAddCart.gridx = 2;
gbc_btnAddCart.gridy = 4;
info.add(btnAddCart, gbc_btnAddCart);
}
}
I have a fucntion in where I am implemeting the rmi server interface (SchoolInterfaceImpl) I want to check the jtextfield (textField_6) is empty or not if empty load a Jdialog box (FindStudent) how I do this
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.Date;
public class SchoolInterfaceImpl implements SchoolInterface {
#Override
public void find(String str) throws RemoteException, SQLException {
// TODO Auto-generated method stub
}
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import com.toedter.calendar.JDateChooser;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class StudentProfile extends JFrame {
String noOfClasses;
private JPanel contentPane;
private JTextField textField;
private JLabel lblFatherguardianName;
private JTextField textField_1;
private JLabel lblGender;
private JComboBox comboBox;
private JLabel lblDateOfBirth;
private JDateChooser dateChooser;
private JLabel lblCurrentAddress;
private JLabel lblPermenantAddress;
private JLabel lblPhoneNo;
private JLabel lblMobileNo;
private JLabel lblClass;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JComboBox comboBox_1;
private JLabel lblRegistrationNo;
private JTextField textField_6;
private JButton btnFind;
private JButton btnSave;
private JButton btnEdit;
private JButton btnDelete;
private JButton btnCancel;
private JLabel label;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
StudentProfile frame = new StudentProfile();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public StudentProfile() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setTitle("Student Profile");
setBounds(100, 100, 525, 375);
setLocation(100, 200);
setResizable(false);
setAlwaysOnTop(true);
setVisible(true);
contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
String str = "Female";
lblRegistrationNo = new JLabel("Registration No.");
GridBagConstraints gbc_lblRegistrationNo = new GridBagConstraints();
gbc_lblRegistrationNo.anchor = GridBagConstraints.WEST;
gbc_lblRegistrationNo.insets = new Insets(0, 0, 5, 5);
gbc_lblRegistrationNo.gridx = 1;
gbc_lblRegistrationNo.gridy = 2;
contentPane.add(lblRegistrationNo, gbc_lblRegistrationNo);
textField_6 = new JTextField();
GridBagConstraints gbc_textField_6 = new GridBagConstraints();
gbc_textField_6.fill = GridBagConstraints.HORIZONTAL;
gbc_textField_6.insets = new Insets(0, 0, 5, 5);
gbc_textField_6.gridx = 3;
gbc_textField_6.gridy = 2;
contentPane.add(textField_6, gbc_textField_6);
textField_6.setColumns(10);
btnFind = new JButton("Find");
btnFind.setBackground(Color.WHITE);
btnFind.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
FindStudent fs = new FindStudent();
fs.setModal(true);
fs.setVisible(true);
}
});
GridBagConstraints gbc_btnFind = new GridBagConstraints();
gbc_btnFind.anchor = GridBagConstraints.WEST;
gbc_btnFind.insets = new Insets(0, 0, 5, 5);
gbc_btnFind.gridx = 4;
gbc_btnFind.gridy = 2;
contentPane.add(btnFind, gbc_btnFind);
label = new JLabel("Picture");
GridBagConstraints gbc_label = new GridBagConstraints();
gbc_label.gridheight = 5;
gbc_label.gridwidth = 3;
gbc_label.insets = new Insets(0, 0, 5, 5);
gbc_label.gridx = 5;
gbc_label.gridy = 2;
contentPane.add(label, gbc_label);
JLabel lblStudentName = new JLabel("Student Name");
GridBagConstraints gbc_lblStudentName = new GridBagConstraints();
gbc_lblStudentName.anchor = GridBagConstraints.WEST;
gbc_lblStudentName.gridwidth = 2;
gbc_lblStudentName.insets = new Insets(0, 0, 5, 5);
gbc_lblStudentName.gridx = 1;
gbc_lblStudentName.gridy = 3;
contentPane.add(lblStudentName, gbc_lblStudentName);
textField = new JTextField();
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.insets = new Insets(0, 0, 5, 5);
gbc_textField.gridx = 3;
gbc_textField.gridy = 3;
contentPane.add(textField, gbc_textField);
textField.setColumns(20);
lblFatherguardianName = new JLabel("Father/Guardian Name");
GridBagConstraints gbc_lblFatherguardianName = new GridBagConstraints();
gbc_lblFatherguardianName.anchor = GridBagConstraints.WEST;
gbc_lblFatherguardianName.insets = new Insets(0, 0, 5, 5);
gbc_lblFatherguardianName.gridx = 1;
gbc_lblFatherguardianName.gridy = 4;
contentPane.add(lblFatherguardianName, gbc_lblFatherguardianName);
textField_1 = new JTextField();
GridBagConstraints gbc_textField_1 = new GridBagConstraints();
gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
gbc_textField_1.insets = new Insets(0, 0, 5, 5);
gbc_textField_1.gridx = 3;
gbc_textField_1.gridy = 4;
contentPane.add(textField_1, gbc_textField_1);
textField_1.setColumns(20);
lblGender = new JLabel("Gender");
GridBagConstraints gbc_lblGender = new GridBagConstraints();
gbc_lblGender.anchor = GridBagConstraints.WEST;
gbc_lblGender.insets = new Insets(0, 0, 5, 5);
gbc_lblGender.gridx = 1;
gbc_lblGender.gridy = 5;
contentPane.add(lblGender, gbc_lblGender);
comboBox = new JComboBox();
comboBox.setBackground(Color.WHITE);
GridBagConstraints gbc_comboBox = new GridBagConstraints();
gbc_comboBox.anchor = GridBagConstraints.WEST;
comboBox.setPrototypeDisplayValue(str);
comboBox.setMaximumRowCount(2);
gbc_comboBox.insets = new Insets(0, 0, 5, 5);
gbc_comboBox.gridx = 3;
gbc_comboBox.gridy = 5;
contentPane.add(comboBox, gbc_comboBox);
lblDateOfBirth = new JLabel("Date of Birth");
GridBagConstraints gbc_lblDateOfBirth = new GridBagConstraints();
gbc_lblDateOfBirth.anchor = GridBagConstraints.WEST;
gbc_lblDateOfBirth.insets = new Insets(0, 0, 5, 5);
gbc_lblDateOfBirth.gridx = 1;
gbc_lblDateOfBirth.gridy = 6;
contentPane.add(lblDateOfBirth, gbc_lblDateOfBirth);
dateChooser = new JDateChooser();
dateChooser.getCalendarButton().setBackground(Color.WHITE);
GridBagConstraints gbc_dateChooser = new GridBagConstraints();
gbc_dateChooser.fill = GridBagConstraints.HORIZONTAL;
gbc_dateChooser.anchor = GridBagConstraints.NORTH;
gbc_dateChooser.insets = new Insets(0, 0, 5, 5);
gbc_dateChooser.gridx = 3;
gbc_dateChooser.gridy = 6;
contentPane.add(dateChooser, gbc_dateChooser);
lblCurrentAddress = new JLabel("Current Address");
GridBagConstraints gbc_lblCurrentAddress = new GridBagConstraints();
gbc_lblCurrentAddress.anchor = GridBagConstraints.WEST;
gbc_lblCurrentAddress.insets = new Insets(0, 0, 5, 5);
gbc_lblCurrentAddress.gridx = 1;
gbc_lblCurrentAddress.gridy = 7;
contentPane.add(lblCurrentAddress, gbc_lblCurrentAddress);
String str1 = "Computer Sciences";
textField_2 = new JTextField();
GridBagConstraints gbc_textField_2 = new GridBagConstraints();
gbc_textField_2.fill = GridBagConstraints.HORIZONTAL;
gbc_textField_2.insets = new Insets(0, 0, 5, 5);
gbc_textField_2.gridx = 3;
gbc_textField_2.gridy = 7;
contentPane.add(textField_2, gbc_textField_2);
textField_2.setColumns(20);
lblPermenantAddress = new JLabel("Permenant Address");
GridBagConstraints gbc_lblPermenantAddress = new GridBagConstraints();
gbc_lblPermenantAddress.anchor = GridBagConstraints.WEST;
gbc_lblPermenantAddress.insets = new Insets(0, 0, 5, 5);
gbc_lblPermenantAddress.gridx = 1;
gbc_lblPermenantAddress.gridy = 8;
contentPane.add(lblPermenantAddress, gbc_lblPermenantAddress);
textField_3 = new JTextField();
GridBagConstraints gbc_textField_3 = new GridBagConstraints();
gbc_textField_3.fill = GridBagConstraints.HORIZONTAL;
gbc_textField_3.insets = new Insets(0, 0, 5, 5);
gbc_textField_3.gridx = 3;
gbc_textField_3.gridy = 8;
contentPane.add(textField_3, gbc_textField_3);
textField_3.setColumns(20);
lblPhoneNo = new JLabel("Phone No.");
GridBagConstraints gbc_lblPhoneNo = new GridBagConstraints();
gbc_lblPhoneNo.anchor = GridBagConstraints.WEST;
gbc_lblPhoneNo.insets = new Insets(0, 0, 5, 5);
gbc_lblPhoneNo.gridx = 1;
gbc_lblPhoneNo.gridy = 9;
contentPane.add(lblPhoneNo, gbc_lblPhoneNo);
textField_4 = new JTextField();
GridBagConstraints gbc_textField_4 = new GridBagConstraints();
gbc_textField_4.fill = GridBagConstraints.HORIZONTAL;
gbc_textField_4.insets = new Insets(0, 0, 5, 5);
gbc_textField_4.gridx = 3;
gbc_textField_4.gridy = 9;
contentPane.add(textField_4, gbc_textField_4);
textField_4.setColumns(20);
lblMobileNo = new JLabel("Mobile No.");
GridBagConstraints gbc_lblMobileNo = new GridBagConstraints();
gbc_lblMobileNo.anchor = GridBagConstraints.WEST;
gbc_lblMobileNo.insets = new Insets(0, 0, 5, 5);
gbc_lblMobileNo.gridx = 1;
gbc_lblMobileNo.gridy = 10;
contentPane.add(lblMobileNo, gbc_lblMobileNo);
textField_5 = new JTextField();
GridBagConstraints gbc_textField_5 = new GridBagConstraints();
gbc_textField_5.fill = GridBagConstraints.HORIZONTAL;
gbc_textField_5.insets = new Insets(0, 0, 5, 5);
gbc_textField_5.gridx = 3;
gbc_textField_5.gridy = 10;
contentPane.add(textField_5, gbc_textField_5);
textField_5.setColumns(20);
lblClass = new JLabel("Class");
GridBagConstraints gbc_lblClass = new GridBagConstraints();
gbc_lblClass.anchor = GridBagConstraints.WEST;
gbc_lblClass.insets = new Insets(0, 0, 5, 5);
gbc_lblClass.gridx = 1;
gbc_lblClass.gridy = 11;
contentPane.add(lblClass, gbc_lblClass);
comboBox_1 = new JComboBox();
comboBox_1.setBackground(Color.WHITE);
GridBagConstraints gbc_comboBox_1 = new GridBagConstraints();
comboBox_1.setPrototypeDisplayValue(str);
comboBox_1.setMaximumRowCount(10);
gbc_comboBox_1.anchor = GridBagConstraints.WEST;
gbc_comboBox_1.insets = new Insets(0, 0, 5, 5);
gbc_comboBox_1.gridx = 3;
gbc_comboBox_1.gridy = 11;
contentPane.add(comboBox_1, gbc_comboBox_1);
btnSave = new JButton("Save");
btnSave.setBackground(Color.WHITE);
GridBagConstraints gbc_btnSave = new GridBagConstraints();
gbc_btnSave.anchor = GridBagConstraints.EAST;
gbc_btnSave.insets = new Insets(0, 0, 5, 5);
gbc_btnSave.gridx = 3;
gbc_btnSave.gridy = 13;
contentPane.add(btnSave, gbc_btnSave);
btnEdit = new JButton("Edit");
btnEdit.setBackground(Color.WHITE);
GridBagConstraints gbc_btnEdit = new GridBagConstraints();
gbc_btnEdit.insets = new Insets(0, 0, 5, 5);
gbc_btnEdit.gridx = 4;
gbc_btnEdit.gridy = 13;
contentPane.add(btnEdit, gbc_btnEdit);
btnDelete = new JButton("Delete");
btnDelete.setBackground(Color.WHITE);
GridBagConstraints gbc_btnDelete = new GridBagConstraints();
gbc_btnDelete.insets = new Insets(0, 0, 5, 5);
gbc_btnDelete.gridx = 5;
gbc_btnDelete.gridy = 13;
contentPane.add(btnDelete, gbc_btnDelete);
btnCancel = new JButton("Cancel");
btnCancel.setBackground(Color.WHITE);
GridBagConstraints gbc_btnCancel = new GridBagConstraints();
gbc_btnCancel.insets = new Insets(0, 0, 5, 5);
gbc_btnCancel.gridx = 6;
gbc_btnCancel.gridy = 13;
contentPane.add(btnCancel, gbc_btnCancel);
}
}
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.GridBagLayout;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JTable;
import java.util.Vector;
import javax.swing.JLabel;
import java.awt.Color;
public class FindStudent extends JDialog {
private final JPanel contentPanel = new JPanel();
private JTextField textField;
private JTextField textField_1;
private JTable table;
Connection conn = null;
Statement stmt = null;
static Vector<Vector<String>> data = new Vector<Vector<String>>();
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
FindStudent dialog = new FindStudent();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public FindStudent() {
Vector<Vector<String>> data = new Vector<Vector<String>>();
Vector<String> columnNames = new Vector<String>();
columnNames.add("RegNo");
columnNames.add("StudentName");
columnNames.add("FatherName");
columnNames.add("Class");
String query = "Select RegNo, StudentName, FatherName, Class from SchoolDB.dbo.StudentProfile";
try{
conn = DriverManager.getConnection("jdbc:sqlserver:" + "//" +
"localhost;1433" + "Database=SchooDB"+";integratedSecurity=true;");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
stmt = conn.createStatement();
//data.clear();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
Vector<String> vstring = new Vector<String>();
vstring.add(rs.getString("RegNo"));
vstring.add(rs.getString("StudentName"));
vstring.add(rs.getString("FatherName"));
vstring.add(rs.getString("Class"));
data.add(vstring);
}
}
catch (Exception e){
e.printStackTrace();
}
/*catch (SQLException e) {
e.printStackTrace();
}*/
finally{
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
setBounds(100, 100, 430, 350);
this.setAlwaysOnTop(true);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBackground(Color.WHITE);
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
GridBagLayout gbl_contentPanel = new GridBagLayout();
gbl_contentPanel.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_contentPanel.rowHeights = new int[]{0, 0, 35, 0};
gbl_contentPanel.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
gbl_contentPanel.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
contentPanel.setLayout(gbl_contentPanel);
{
textField = new JTextField();
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.anchor = GridBagConstraints.NORTH;
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridwidth = 25;
gbc_textField.insets = new Insets(0, 0, 10, 5);
gbc_textField.gridx = 1;
gbc_textField.gridy = 0;
contentPanel.add(textField, gbc_textField);
textField.setColumns(10);
}
{
textField_1 = new JTextField();
GridBagConstraints gbc_textField_1 = new GridBagConstraints();
gbc_textField_1.gridwidth = 25;
gbc_textField_1.insets = new Insets(0, 0, 10, 5);
gbc_textField_1.anchor = GridBagConstraints.NORTH;
gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
gbc_textField_1.gridx = 1;
gbc_textField_1.gridy = 1;
contentPanel.add(textField_1, gbc_textField_1);
textField_1.setColumns(10);
}
{
table = new JTable();
GridBagConstraints gbc_table = new GridBagConstraints();
gbc_table.insets = new Insets(0, 0, 0, 5);
gbc_table.anchor = GridBagConstraints.NORTH;
gbc_table.gridwidth = 3;
gbc_table.gridx = 1;
gbc_table.gridy = 2;
DefaultTableModel model = new DefaultTableModel(data, columnNames);
final JTable table = new JTable(model){/**
*
*/
// private static final long serialVersionUID = 1L;
public boolean isCellEditable(int row, int column) {
return false;
};
};
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setPreferredScrollableViewportSize(new Dimension(425, 200));
table.setFillsViewportHeight(true);
table.getColumnModel().getColumn(0).setPreferredWidth(90);
table.getColumnModel().getColumn(1).setPreferredWidth(120);
table.getColumnModel().getColumn(2).setPreferredWidth(120);
table.getColumnModel().getColumn(3).setPreferredWidth(40);
JScrollPane jsp = new JScrollPane(table);
GridBagConstraints jsp_scroll = new GridBagConstraints();
jsp_scroll.insets = new Insets(0, 0, 0, 5);
jsp_scroll.fill = GridBagConstraints.BOTH;
jsp_scroll.gridwidth = 25;
jsp_scroll.gridx = 1;
jsp_scroll.gridy = 3;
contentPanel.add(jsp, jsp_scroll);
}
{
JPanel buttonPane = new JPanel();
buttonPane.setBackground(Color.WHITE);
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("OK");
okButton.setBackground(Color.WHITE);
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.setBackground(Color.WHITE);
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}
}
}
I suppose, instead of checking the emptiness of textField_6 inside SchoolInterfaceImpl, you need to do it when the "Find" button is clicked as follows:
btnFind.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String strTextField_6 = textField_6.getText();
if ("".equals(strTextField_6)) {
FindStudent fs = new FindStudent();
fs.setModal(true);
fs.setVisible(true);
}
SchoolInterfaceImpl sii = new SchoolInterfaceImpl();
sii.find(strTextField_6);
}
});
I am not sure what actually you mean because I am not home now, but you can try this:
if (textField_6.getText().equals("")) {
Jdialog findStudent = new Jdialog(with the arguments needed);
findStudent.setVisible(true);
}
Okay, so I have made a GUI with some input boxes and a combo box. I am wondering how I would go about having a listener know when the button is pressed then relay all the information in the imput boxes and combo box into different parts of the script...
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class dogedice extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
private JComboBox combo;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
dogedice frame = new dogedice();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public dogedice() {
setTitle("DogeDice Bot");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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.WEST);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0};
gbl_panel.rowHeights = new int[]{0, 0};
gbl_panel.columnWeights = new double[]{0.0, 1.0};
gbl_panel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
//Every new Label however needs every part that says "user" or on the Password: "pass" changed to something unique.
JLabel userTag = new JLabel("Username:");
GridBagConstraints gbc_userTag = new GridBagConstraints();
gbc_userTag.insets = new Insets(0, 0, 0, 5);
gbc_userTag.anchor = GridBagConstraints.EAST;
gbc_userTag.gridx = 0;//Here are your x + y coords
gbc_userTag.gridy = 0;//Adding to x moves left, adding to y moves down
panel.add(userTag, gbc_userTag);
//Every new textfield needs only the * part to change for it to be valid. (gbc_* =)
textField = new JTextField();
GridBagConstraints gbc_Username = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 0;
panel.add(textField, gbc_textField);
textField.setColumns(10);
JLabel startTag = new JLabel("Starting Bid:");
GridBagConstraints gbc_startTag = new GridBagConstraints();
gbc_startTag.insets = new Insets(0, 0, 0, 5);
gbc_startTag.anchor = GridBagConstraints.EAST;
gbc_startTag.gridx = 0;
gbc_startTag.gridy = 2;
panel.add(startTag, gbc_startTag);
textField = new JTextField();
GridBagConstraints gbc_StartBid = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 2;
panel.add(textField, gbc_textField);
textField.setColumns(10);
JLabel multTag = new JLabel("Multiplier:");
GridBagConstraints gbc_multTag = new GridBagConstraints();
gbc_multTag.insets = new Insets(0, 0, 0, 5);
gbc_multTag.anchor = GridBagConstraints.EAST;
gbc_multTag.gridx = 0;
gbc_multTag.gridy = 3;
panel.add(multTag, gbc_multTag);
textField = new JTextField();
GridBagConstraints gbc_Multiplier = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 3;
panel.add(textField, gbc_textField);
textField.setColumns(10);
JLabel minTag = new JLabel("Min Remaining:");
GridBagConstraints gbc_minTag = new GridBagConstraints();
gbc_minTag.insets = new Insets(0, 0, 0, 5);
gbc_minTag.anchor = GridBagConstraints.EAST;
gbc_minTag.gridx = 0;
gbc_minTag.gridy = 4;
panel.add(minTag, gbc_minTag);
textField = new JTextField();
GridBagConstraints gbc_MinRemaining = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 4;
panel.add(textField, gbc_textField);
textField.setColumns(10);
textField = new JTextField();
GridBagConstraints gbc_Password = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 1;
panel.add(textField, gbc_textField);
textField.setColumns(10);
JLabel passTag = new JLabel("Password:");
GridBagConstraints gbc_passTag = new GridBagConstraints();
gbc_passTag.insets = new Insets(0, 0, 0, 5);
gbc_passTag.anchor = GridBagConstraints.EAST;
gbc_passTag.gridx = 0;
gbc_passTag.gridy = 1;
panel.add(passTag, gbc_passTag);
textField = new JTextField();
GridBagConstraints gbc_Odds = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 5;
panel.add(textField, gbc_textField);
textField.setColumns(10);
JLabel oddsTag = new JLabel("Odds %:");
GridBagConstraints gbc_oddsTag = new GridBagConstraints();
gbc_oddsTag.insets = new Insets(0, 0, 0, 5);
gbc_oddsTag.anchor = GridBagConstraints.EAST;
gbc_oddsTag.gridx = 0;
gbc_oddsTag.gridy = 5;
panel.add(oddsTag, gbc_oddsTag);
textField = new JTextField();
GridBagConstraints gbc_ComboBox = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 6;
panel.add(textField, gbc_textField);
textField.setColumns(10);
//This is the Combo Box
combo = new JComboBox<String>(new String[]{"BTC","LTC","PPC","NMC","XPM","FTC","ANC","DOGE","NXT"});
combo.addActionListener(this);
GridBagConstraints gbc_list = new GridBagConstraints();
gbc_list.fill = GridBagConstraints.HORIZONTAL;
gbc_list.gridx = 1;
gbc_list.gridy = 7;
panel.add(combo, gbc_list);
JLabel maxTag = new JLabel("MaxBet:");
GridBagConstraints gbc_maxTag = new GridBagConstraints();
gbc_maxTag.insets = new Insets(0, 0, 0, 5);
gbc_maxTag.anchor = GridBagConstraints.EAST;
gbc_maxTag.gridx = 0;
gbc_maxTag.gridy = 6;
panel.add(maxTag, gbc_maxTag);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.SOUTH);
panel_1.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
JButton btnConfirm = new JButton("Turn Up");
panel_1.add(btnConfirm);
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
JTextArea textArea = new JTextArea("Current Balance");
textArea.setColumns(1);
scrollPane.setViewportView(textArea);
JScrollPane scrollPanel = new JScrollPane();//This will hold the information the bot sends over such as win/loose or error
contentPane.add(scrollPane, BorderLayout.CENTER);
JTextArea textAreal = new JTextArea("Input bot information here...");
textArea.setColumns(20);
scrollPane.setViewportView(textArea);
pack();
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == combo) {
System.out.println(combo.getSelectedIndex()+1);
}
}
}
It's quite straightforward to add an ActionListener to a JButton.
// ...
btnConfirm = new JButton("Turn Up");
btnConfirm.addActionListener(this);
panel_1.add(btnConfirm);
// ...
Because you'll want to know when the button is triggering the callback, like so:
private JButton btnConfirm;
This way, you'll be able to tell when it's that button that triggered the actionListener() method:
if (event.getSource() == btnConfirm) {
// Handle "Turn Up" button press here
}
Start by taking a look at How to Use Buttons, Check Boxes, and Radio Buttons and How to Write an Action Listener
Essentially you need to register an ActionListener with your button...
There are a number of ways you might be able to achieve this...
You Could
Use the more traditional, purpose class...
public class ActionHandler implements ActionListener {
public void actionPerformed(ActionEvent evt) {
if ("Turn Up".evt.getActionCommand()) {
// Handle Turn Up...
}
}
}
In this context, you'll be required to provide a reference to the component you want to modify so that the ActionHandler can interact with it, personally, this is best done via an interface, but that's just me...
public class ActionHandler implements ActionListener {
private dogedice dice;
public ActionHandler(dogedice dice) {
this.dice = dice;
}
public void actionPerformed(ActionEvent evt) {
//...
}
}
The benefit of this is you can plug an play the action handler, changing what the action does depending on your needs. If you use an interface instead of an implementation reference, you further decouple the action from the program
You Could
Use use an anonymous class
btnConfirm = new JButton("Turn Up");
btnConfirm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// handle turn up action...
}
});
The benefit to this is, you don't end up with "another" class, the ActionListener can reference all the internal fields of the parent class and, because you've attached it directly, you can assume that the only thing that is going to generate the ActionEvent is the btnConfirm button.
The downside is you lose the flexibility to change how the action is handled without physically modifying the code
You Could
Take advantage of the Actions API, this is a little like the first option, in that you (should normally) create another class specifically designed to handle the "Turn Up" event, but these are self contained entities. That is, they carry all the information required to configure the UI element and perform the required action when triggered.
This is really good where the action may be repeated in the UI via menus, buttons or key strokes
I've read the documentation for GridBagLayout and I can't make sense of it. I basically want to accomplish something like this:
I made some example code to help me figure this out. How can I modify this code to accomplish this?
import java.awt.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JLabel label = new JLabel("label");
JTextField field = new JTextField();
JLabel label2 = new JLabel("label2");
JTextField field2 = new JTextField();
JPanel jp = new JPanel();
jp.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
//gbc.weightx = ??
jp.add(label, gbc);
gbc.gridx = 1;
gbc.gridwidth = 2;
//gbc.weightx = ??
jp.add(field, gbc);
JPanel jp2 = new JPanel();
jp2.setLayout(new GridBagLayout());
GridBagConstraints gbc2 = new GridBagConstraints();
gbc2.fill = GridBagConstraints.BOTH;
gbc2.gridx = 0;
gbc2.gridy = 0;
gbc2.gridwidth = 1;
//gbc2.weightx = ??
jp2.add(label2, gbc2);
gbc2.gridx = 1;
gbc2.gridwidth = 2;
//gbc2.weightx = ??
jp2.add(field2, gbc2);
JPanel jpContainer = new JPanel();
jpContainer.setLayout(new BoxLayout(jpContainer, BoxLayout.Y_AXIS));
jpContainer.add(jp);
jpContainer.add(jp2);
JFrame f = new JFrame();
f.setSize(300, 100);
f.setContentPane(jpContainer);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
EDIT: Changed JTextArea to JTextField
Using GridBagLayout columnWidths you can manually adjust the widths and then set the GridBagConstraints fill to GridBagConstraints.HORIZONTAL :
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;
public class Example extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Example frame = new Example();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Example() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[] {100, 0};
gbl_contentPane.rowHeights = new int[]{0, 0, 0};
gbl_contentPane.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
JLabel lblNewLabel = new JLabel("jlabel");
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
gbc_lblNewLabel.anchor = GridBagConstraints.WEST;
gbc_lblNewLabel.gridx = 0;
gbc_lblNewLabel.gridy = 0;
contentPane.add(lblNewLabel, gbc_lblNewLabel);
textField = new JTextField();
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.insets = new Insets(0, 0, 5, 0);
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 0;
contentPane.add(textField, gbc_textField);
textField.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("jlabel2");
GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
gbc_lblNewLabel_1.anchor = GridBagConstraints.WEST;
gbc_lblNewLabel_1.insets = new Insets(0, 0, 0, 5);
gbc_lblNewLabel_1.gridx = 0;
gbc_lblNewLabel_1.gridy = 1;
contentPane.add(lblNewLabel_1, gbc_lblNewLabel_1);
textField_1 = new JTextField();
GridBagConstraints gbc_textField_1 = new GridBagConstraints();
gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
gbc_textField_1.gridx = 1;
gbc_textField_1.gridy = 1;
contentPane.add(textField_1, gbc_textField_1);
textField_1.setColumns(10);
}
}
Of course, if you want to maintain the 1/3 Label and 2/3 JTextField, you might consider using a MigLayout as such:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class MigLayoutExample extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MigLayoutExample frame = new MigLayoutExample();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MigLayoutExample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new MigLayout("", "[grow 33][grow 66]", "[][]"));
JLabel lblNewLabel = new JLabel("New label");
contentPane.add(lblNewLabel, "cell 0 0");
textField = new JTextField();
contentPane.add(textField, "cell 1 0,growx");
textField.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("New label");
contentPane.add(lblNewLabel_1, "cell 0 1");
textField_1 = new JTextField();
contentPane.add(textField_1, "cell 1 1,growx");
textField_1.setColumns(10);
}
}
You only have two components on each row so you can only have two columns.
If you want the JTextArea to occupy more space then define the JTextArea like:
JTextArea textArea = new JTextArea(3, 30);
to control the size of the text area by specifying the row/columns of the text area.
I'm not sure why you are using a JTextArea. It seems like a JTextField would be more appropriate. You can also specify the columns when you create a JTextField. Check out the JTextField API.