This is kinda a silly question, but I'm beggining with java swing and have been doing kinda great, but I really can't make a JTable appear in my form, so far a blank square appears but no columns at all, here's a snippet:
DefaultTableModel modelo = new DefaultTableModel();
modelo.setColumnCount(1);
tblTrans = new JTable(modelo);
modelo.addColumn("Prueba");
tblTrans.revalidate();
tblTrans.setBounds(316, 47, 248, 243);
contentPane.add(tblTrans);
Here's the complete code in case you need it:
package mx.adk.grafos;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ListSelectionModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JList;
import javax.swing.JTable;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Set;
import java.util.TreeSet;
public class GUI extends JFrame implements ActionListener {
private JPanel contentPane;
private JTextField txtEstados;
private JTextField txtLenguaje;
private JTextField txtFinal;
private JTable tblTrans;
private JList lstEstados;
private JList lstLenguaje;
DefaultListModel listaEstados;
TreeSet<String> setEstados = new TreeSet<String>();
DefaultListModel listaLenguaje;
TreeSet<Character> setLenguaje = new TreeSet<Character>();
/*
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUI frame = new GUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
*/
public void actionPerformed(ActionEvent e) {
String comando = e.getActionCommand();
if(comando.equalsIgnoreCase("btnAEstados")){
String tmp = txtEstados.getText();
if(setEstados.add(tmp)){
listaEstados.addElement(tmp);
}
return;
}
if(comando.equalsIgnoreCase("btneestados")){
String tmp = (String) lstEstados.getSelectedValue();
setEstados.remove(tmp);
listaEstados.removeElement(tmp);
}
if(comando.equalsIgnoreCase("btnALenguaje")){
char tmp = txtLenguaje.getText().charAt(0);
if(setLenguaje.add(tmp)){
listaLenguaje.addElement(tmp);
}
return;
}
if(comando.equalsIgnoreCase("btnelenguaje")){
char tmp = (char) lstLenguaje.getSelectedValue().toString().charAt(0);
setLenguaje.remove(tmp);
listaLenguaje.removeElement(tmp);
}
}
public GUI() {
setTitle("Automata finito no deterministico");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 599, 368);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
txtEstados = new JTextField();
txtEstados.setBounds(26, 82, 72, 28);
contentPane.add(txtEstados);
txtEstados.setColumns(10);
JButton btnAEstados = new JButton(">>");
btnAEstados.setBounds(103, 69, 51, 29);
contentPane.add(btnAEstados);
btnAEstados.addActionListener(this);
btnAEstados.setActionCommand("btnAEstados");
listaEstados = new DefaultListModel();
lstEstados = new JList(listaEstados);
lstEstados.setBounds(154, 46, 105, 109);
contentPane.add(lstEstados);
lstEstados.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JLabel lblEstados = new JLabel("Estados");
lblEstados.setBounds(83, 19, 59, 16);
contentPane.add(lblEstados);
JButton btnEEstados = new JButton("<<");
btnEEstados.setBounds(103, 96, 51, 29);
contentPane.add(btnEEstados);
btnEEstados.addActionListener(this);
btnEEstados.setActionCommand("btnEEstados");
JButton btnELenguaje = new JButton("<<");
btnELenguaje.setBounds(103, 231, 51, 29);
contentPane.add(btnELenguaje);
btnELenguaje.addActionListener(this);
btnELenguaje.setActionCommand("btnELenguaje");
JButton btnALenguaje = new JButton(">>");
btnALenguaje.setBounds(103, 204, 51, 29);
contentPane.add(btnALenguaje);
btnALenguaje.addActionListener(this);
btnALenguaje.setActionCommand("btnALenguaje");
txtLenguaje = new JTextField();
txtLenguaje.setColumns(10);
txtLenguaje.setBounds(26, 217, 72, 28);
contentPane.add(txtLenguaje);
listaLenguaje = new DefaultListModel();
lstLenguaje = new JList(listaLenguaje);
lstLenguaje.setBounds(154, 181, 105, 109);
contentPane.add(lstLenguaje);
lstLenguaje.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JLabel lblLenguaje = new JLabel("Lenguaje");
lblLenguaje.setBounds(95, 161, 59, 16);
contentPane.add(lblLenguaje);
txtFinal = new JTextField();
txtFinal.setText("QFinal");
txtFinal.setColumns(10);
txtFinal.setBounds(26, 298, 72, 28);
contentPane.add(txtFinal);
DefaultTableModel modelo = new DefaultTableModel();
modelo.setColumnCount(1);
tblTrans = new JTable(modelo);
modelo.addColumn("Prueba");
tblTrans.revalidate();
tblTrans.setBounds(316, 47, 248, 243);
contentPane.add(tblTrans);
JLabel lblTransiciones = new JLabel("Transiciones");
lblTransiciones.setBounds(394, 19, 89, 16);
contentPane.add(lblTransiciones);
JButton btnAceptar = new JButton("Aceptar");
btnAceptar.setBounds(476, 311, 117, 29);
contentPane.add(btnAceptar);
btnAceptar.addActionListener(this);
btnAceptar.setActionCommand("btnAceptar");
}
}
What I want to accomplish is to add a column for each "Estado" given, the program is about non deterministic automata, and a row for each "Lenguaje" given so I can make the transition matrix, I do know how to make this each time the buttons are pressed, but it appears like the column is being added but it's not visible at all, just a blank square.
Corrections suggested:
DefaultTableModel modelo = new DefaultTableModel();
modelo.setColumnCount(1);
tblTrans = new JTable(modelo);
panel.add(tblTrans);
panel.setBounds(316, 47, 248, 243);
modelo.addColumn("Test");
tblTrans.revalidate();
contentPane.add(panel);
I was right about the scroll pane. This is working for me:
DefaultTableModel modelo = new DefaultTableModel();
modelo.setColumnCount(1);
modelo.addColumn("Prueba");
tblTrans = new JTable(modelo);
JScrollPane sPane = new JScrollPane(tblTrans);
sPane.setBounds(316, 47, 248, 243);
contentPane.add(sPane);
Related
I'm new in Java programming so that's why I'm here seeking your kind help stackoverflowers.
I have this problem which I could not find the solution on internet, I hope someone could inform me where is my mistake and how can I fix it.
My problem is that when I try to insert the data, everything goes fine, the window opens, I can insert the data, button looks just fine.
But it gives me this message
java.sql.SQLIntegrityConstraintViolationException: Column 'prenom' cannot be null
Which I assume was a problem with php mySQL, then I changed all variables to NULL, activating the button "Null" for all of them.
It then WORKED, but all informations were saved as "null" in my database, which didn't really solve my problem.
My goal is to insert data (name, lastname, phone, email) into a data base (php mySQL) through eclipse. I did a program which I'm obliged to use window builder to insert that data, so graphical user interface MUST be used
What I expect to happen is to save data normally. Like, name = Mike, lastname = Tyson, phone = +55555, email = mtyson#gmail.com
And it shows in my database, not null message.
Thank you everyone
package interfaceGraphique;
import accesBaseDeDonnees.ClientsBDD;
import entites.Clients;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Color;
public class InterfaceClients extends JFrame {
static InterfaceClients window;
private JPanel contentPane;
private JTextField txtPrenom;
private JTextField txtNom;
private JTextField txtTelephone;
private JTextField txtEmail;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
InterfaceClients frame = new InterfaceClients();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public InterfaceClients() {
setBackground(new Color(0, 0, 0));
setOpacity(1.0f);
setTitle("Ajouter un client");
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(null);
JButton btnNewButton = new JButton("Enregistrer");
btnNewButton.addActionListener(new ActionListener() {
private AbstractButton labelResult;
#Override
public void actionPerformed(ActionEvent e) {
String prenom = txtPrenom.getText();
String nom = txtNom.getText();
String telephone = txtTelephone.getText();
String email = txtEmail.getText();
Clients Clients = new Clients(prenom, nom, telephone, email);
ClientsBDD clientsBDD = new ClientsBDD();
Connection cn = clientsBDD.makeConnection();
clientsBDD.insert(Clients, cn);
clientsBDD.closeConnection(cn);
//labelResult = null;
//labelResult.setText("Client ajoute avec succes");
}
});
btnNewButton.setBounds(45, 227, 112, 23);
contentPane.add(btnNewButton);
txtPrenom = new JTextField();
txtPrenom.setBounds(124, 39, 258, 20);
contentPane.add(txtPrenom);
txtPrenom.setColumns(10);
txtNom = new JTextField();
txtNom.setColumns(10);
txtNom.setBounds(124, 82, 258, 20);
contentPane.add(txtNom);
txtTelephone = new JTextField();
txtTelephone.setColumns(10);
txtTelephone.setBounds(124, 125, 258, 20);
contentPane.add(txtTelephone);
txtEmail = new JTextField();
txtEmail.setColumns(10);
txtEmail.setBounds(124, 166, 258, 20);
contentPane.add(txtEmail);
JLabel lblNewLabel = new JLabel("Prenom");
lblNewLabel.setBounds(45, 42, 67, 14);
contentPane.add(lblNewLabel);
JLabel lblMarque = new JLabel("Nom");
lblMarque.setBounds(45, 85, 46, 14);
contentPane.add(lblMarque);
JLabel lblNewLabel_1_1 = new JLabel("Telephone");
lblNewLabel_1_1.setBounds(45, 128, 67, 14);
contentPane.add(lblNewLabel_1_1);
JLabel lblNewLabel_1_1_1 = new JLabel("Email");
lblNewLabel_1_1_1.setBounds(45, 169, 89, 14);
contentPane.add(lblNewLabel_1_1_1);
JLabel labelResult = new JLabel("");
labelResult.setBounds(194, 229, 227, 16);
contentPane.add(labelResult);
}
}
I've been having trouble with exporting my GUI project from Eclipse Oxygen. At first I tried exporting my project by going to export>Runnable Jar File>Main Class, but when I try and open my program nothing shows up. Then I tried opening the GUI inside its own class by specifying a Main method in there, and I keep getting the error that I put in the title. Any help would be appreciated. I'll post both of my classes here.
Also I used WindowBuilder plugin to build my GUI.
Main class
package me.iran.cryptotracker;
import java.util.ArrayList;
import lombok.Getter;
import me.iran.cryptotracker.window.Window;
public class CryptoTracker {
private static SaveFile saveFile = new SaveFile();
private static ReadFile readFile = new ReadFile();
#Getter
public static ArrayList<Crypto> allCrypto = new ArrayList<Crypto>();
public static void main(String[] args) {
readFile.openFile();
readFile.readFile();
readFile.closeFile();
saveFile.openFile();
saveFile.updateFile();
saveFile.closeFile();
Window.open();
}
}
Window1 Class
package me.iran.cryptotracker.window;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.SpringLayout;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import lombok.Getter;
import me.iran.cryptotracker.Crypto;
import me.iran.cryptotracker.CryptoTracker;
import me.iran.cryptotracker.ReadFile;
import me.iran.cryptotracker.SaveFile;
public class Window {
static #Getter
private JFrame frame;
#Getter
private static JTable table;
/**
* Launch the application.
*/
public static void open() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Window window = new Window();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Window() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBackground(Color.DARK_GRAY);
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setBounds(100, 100, 571, 622);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
table = new JTable();
table.setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
table.setBackground(new Color(169, 169, 169));
Object columnName[] = {"Name", "Date", "Initial Price", "Stock", "URL"};
DefaultTableModel model = new DefaultTableModel(columnName, 0);
model.addRow(columnName);
for(Crypto crypto : CryptoTracker.allCrypto) {
String name = crypto.getName();
String date = crypto.getDate();
double initial = crypto.getInitialPrice();
double amount = crypto.getAmount();
String url = crypto.getUrl();
Object[] data = {name, date, initial + "", amount + "", url};
model.addRow(data);
}
SpringLayout springLayout = new SpringLayout();
springLayout.putConstraint(SpringLayout.NORTH, table, 37, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.WEST, table, 30, SpringLayout.WEST, frame.getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, table, 478, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, table, 464, SpringLayout.WEST, frame.getContentPane());
frame.getContentPane().setLayout(springLayout);
table.setModel(model);
frame.getContentPane().add(table);
JButton btnAdd = new JButton("Add Currency");
springLayout.putConstraint(SpringLayout.NORTH, btnAdd, -48, SpringLayout.SOUTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.WEST, btnAdd, 0, SpringLayout.WEST, table);
springLayout.putConstraint(SpringLayout.SOUTH, btnAdd, -25, SpringLayout.SOUTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, btnAdd, 150, SpringLayout.WEST, frame.getContentPane());
frame.getContentPane().add(btnAdd);
btnAdd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
AddWindow.open();
}
});
}
public static void updateTable(JTable table) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setRowCount(0);
Object columnName[] = {"Name", "Date", "Initial Price", "Stock", "URL"};
model.addRow(columnName);
for(Crypto crypto : CryptoTracker.allCrypto) {
String name = crypto.getName();
String date = crypto.getDate();
double initial = crypto.getInitialPrice();
double amount = crypto.getAmount();
String url = crypto.getUrl();
Object[] data = {name, date, initial + "", amount + "", url};
model.addRow(data);
}
table.setModel(model);
}
}
Window 2 Class
package me.iran.cryptotracker.window;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import me.iran.cryptotracker.Crypto;
import me.iran.cryptotracker.CryptoTracker;
import me.iran.cryptotracker.SaveFile;
public class AddWindow {
private JFrame frame;
private JTextField txtName;
private JTextField txtDate;
private JTextField txtCost;
private JTextField txtAmount;
private JTextField txtURL;
private SaveFile saveFile = new SaveFile();
public static void open() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AddWindow window = new AddWindow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public AddWindow() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(Color.LIGHT_GRAY);
frame.getContentPane().setLayout(null);
txtName = new JTextField();
txtName.setFont(new Font("Cambria Math", Font.PLAIN, 14));
txtName.setBounds(107, 11, 138, 20);
frame.getContentPane().add(txtName);
txtName.setColumns(10);
txtDate = new JTextField();
txtDate.setFont(new Font("Cambria Math", Font.PLAIN, 14));
txtDate.setColumns(10);
txtDate.setBounds(107, 44, 138, 20);
frame.getContentPane().add(txtDate);
txtCost = new JTextField();
txtCost.setFont(new Font("Cambria Math", Font.PLAIN, 14));
txtCost.setColumns(10);
txtCost.setBounds(107, 75, 138, 20);
frame.getContentPane().add(txtCost);
txtAmount = new JTextField();
txtAmount.setFont(new Font("Cambria Math", Font.PLAIN, 14));
txtAmount.setColumns(10);
txtAmount.setBounds(107, 106, 138, 20);
frame.getContentPane().add(txtAmount);
txtURL = new JTextField();
txtURL.setFont(new Font("Cambria Math", Font.PLAIN, 14));
txtURL.setColumns(10);
txtURL.setBounds(107, 137, 138, 20);
frame.getContentPane().add(txtURL);
JLabel lblName = new JLabel("Name");
lblName.setFont(new Font("Cambria Math", Font.PLAIN, 14));
lblName.setBounds(22, 11, 46, 14);
frame.getContentPane().add(lblName);
JLabel lblDate = new JLabel("Date");
lblDate.setFont(new Font("Cambria Math", Font.PLAIN, 14));
lblDate.setBounds(22, 44, 46, 14);
frame.getContentPane().add(lblDate);
JLabel lblInitialCost = new JLabel("Initial Cost");
lblInitialCost.setFont(new Font("Cambria Math", Font.PLAIN, 14));
lblInitialCost.setBounds(22, 75, 75, 14);
frame.getContentPane().add(lblInitialCost);
JLabel lblAmount = new JLabel("Amount");
lblAmount.setFont(new Font("Cambria Math", Font.PLAIN, 14));
lblAmount.setBounds(22, 106, 63, 14);
frame.getContentPane().add(lblAmount);
JLabel lblUrl = new JLabel("URL");
lblUrl.setFont(new Font("Cambria Math", Font.PLAIN, 14));
lblUrl.setBounds(22, 137, 46, 14);
frame.getContentPane().add(lblUrl);
final JLabel lblError = new JLabel("");
lblError.setFont(new Font("Cambria Math", Font.PLAIN, 14));
lblError.setBounds(278, 59, 284, 102);
frame.getContentPane().add(lblError);
JButton btnAddCurrency = new JButton("Add Currency");
btnAddCurrency.setBounds(355, 11, 138, 38);
frame.getContentPane().add(btnAddCurrency);
frame.setBackground(Color.LIGHT_GRAY);
frame.setBounds(100, 100, 588, 211);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnAddCurrency.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(!txtName.getText().isEmpty() && !txtDate.getText().isEmpty() && !txtURL.getText().isEmpty() && !txtCost.getText().isEmpty() && !txtAmount.getText().isEmpty()) {
String name = txtName.getText();
String date = txtDate.getText();
String url = txtURL.getText();
try {
double initial = Double.parseDouble(txtCost.getText());
double amount = Double.parseDouble(txtAmount.getText());
CryptoTracker.allCrypto.add(new Crypto(name, date, url, initial, amount));
saveFile.openFile();
saveFile.updateFile();
saveFile.closeFile();
frame.setVisible(false);
Window.getFrame().setVisible(true);
Window.updateTable(Window.getTable());
} catch(Exception exc) {
lblError.setText("You have not entered a number in the Initial Cost and/or Amount field");
}
}
}
});
}
}
Turns out that winrar does not like runnable jar files. I had tried Right Clicking the file and running it as a java application SE binary, but it would not work. So after #Adam Horvath mentioned that it worked for him I set the exported jar to default java jar and it opened up just fine.
This question already has answers here:
how to change UI depending on combo box selection
(1 answer)
Positioning component inside card layout
(1 answer)
Closed 5 years ago.
I am trying to change between card panels using the Jcombobox "Expenses". Can someone tell me what I'm doing wrong? I get the panels to appear properly, but when I see the Expenses j tabbed pane, the first card is shown. Once I try to switch between them, the console gets filled with errors. I noticed that all cards are showing at the same time, but they are overlapping.
1)How do I get only one card (panels) showing at once?
2)How do I get the JComboBox to switch between cards(panels)?
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTabbedPane;
import javax.swing.ListSelectionModel;
import javax.swing.JList;
import java.awt.Color;
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
public class LanaFrame extends JFrame implements ItemListener{
private JPanel contentPane, cards;
private JList driverlist;
private JComboBox expenses;
final static String FUEL= "Fuel";
final static String TOLL= "Toll";
final static String REPAIR="Repair";
private static String[] comboboxitems= {FUEL,TOLL,REPAIR};
private JTextField textFieldFirstName;
private JTextField textFieldLastName;
private JTextField textFieldTruckNumber;
private JTextField textFieldTrailerNumber;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LanaFrame frame = new LanaFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public LanaFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 500);
setResizable(false);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setBounds(5, 5, 775, 480);
contentPane.add(tabbedPane);
//Drivers Panel
JPanel Drivers = new JPanel();
tabbedPane.addTab("Drivers", null, Drivers, null);
Drivers.setLayout(null);
driverlist= new JList(comboboxitems);
driverlist.setBackground(Color.WHITE);
driverlist.setVisibleRowCount(4);
driverlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
Drivers.add(driverlist);
driverlist.setBounds(482,6,266,211);
JButton btnAddDriver = new JButton("Add Driver");
btnAddDriver.setBounds(5, 285, 117, 29);
Drivers.add(btnAddDriver);
JTextArea driverArea = new JTextArea();
driverArea.setBackground(Color.LIGHT_GRAY);
driverArea.setBounds(482, 217, 266, 211);
Drivers.add(driverArea);
driverArea.setEditable(false);
JTextPane txtpnFirstName = new JTextPane();
txtpnFirstName.setText("First name");
txtpnFirstName.setBounds(5, 20, 140, 15);
Drivers.add(txtpnFirstName);
txtpnFirstName.setEditable(false);
textFieldFirstName = new JTextField();
textFieldFirstName.setBounds(5, 40, 140, 30);
Drivers.add(textFieldFirstName);
textFieldFirstName.setColumns(10);
JTextPane txtpnLastName = new JTextPane();
txtpnLastName.setText("Last Name");
txtpnLastName.setBounds(5, 75, 140, 15);
Drivers.add(txtpnLastName);
txtpnLastName.setEditable(false);
textFieldLastName = new JTextField();
textFieldLastName.setBounds(5, 95, 140, 30);
Drivers.add(textFieldLastName);
textFieldLastName.setColumns(10);
JTextPane txtpnTruckNumber = new JTextPane();
txtpnTruckNumber.setText("Truck Number");
txtpnTruckNumber.setBounds(5, 130, 140, 15);
Drivers.add(txtpnTruckNumber);
txtpnTruckNumber.setEditable(false);
textFieldTruckNumber = new JTextField();
textFieldTruckNumber.setBounds(5, 150, 140, 30);
Drivers.add(textFieldTruckNumber);
textFieldTruckNumber.setColumns(10);
JTextPane txtpnTrailerNumber = new JTextPane();
txtpnTrailerNumber.setText("Trailer Number");
txtpnTrailerNumber.setBounds(5, 185, 140, 15);
Drivers.add(txtpnTrailerNumber);
txtpnTrailerNumber.setEditable(false);
textFieldTrailerNumber = new JTextField();
textFieldTrailerNumber.setBounds(5, 205, 140, 30);
Drivers.add(textFieldTrailerNumber);
textFieldTrailerNumber.setColumns(10);
JButton btnDeleteDriver = new JButton("Delete Driver");
btnDeleteDriver.setBounds(359, 185, 117, 29);
Drivers.add(btnDeleteDriver);
//New Expenses Panel
JPanel NewExpense = new JPanel();
tabbedPane.addTab("New Expense", null, NewExpense, null);
//adds types of expenses to jcombo
expenses = new JComboBox(comboboxitems);
expenses.setBounds(257, 5, 236, 30);
expenses.setEditable(false);
expenses.addItemListener(this);
NewExpense.setLayout(null);
NewExpense.add(expenses);
//create panels for each combo option
JPanel fuel = new JPanel();
fuel.setBounds(0, 0, 743, 399);
fuel.setBackground(Color.BLUE);
JPanel toll = new JPanel();
toll.setBounds(0, 0, 743, 399);
toll.setBackground(Color.RED);
JPanel repair = new JPanel();
repair.setBounds(0, 0, 743, 399);
repair.setBackground(Color.BLACK);
//Assigns Panels to combo options
cards = new JPanel(new CardLayout());
cards.setBounds(5, 29, 743, 399);
cards.setLayout(null);
cards.add(fuel, FUEL);
cards.add(toll, TOLL);
cards.add(repair, REPAIR);
NewExpense.add(cards);
JPanel Income = new JPanel();
tabbedPane.addTab("Income", null, Income, null);
Income.setLayout(null);
JPanel Results = new JPanel();
tabbedPane.addTab("Result", null, Results, null);
Results.setLayout(null);
}
#Override
public void itemStateChanged(ItemEvent e) {
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, (String)e.getItem());
}
}
cards = new JPanel(new CardLayout());
cards.setBounds(5, 29, 743, 399);
cards.setLayout(null); // ???!!!
Seriously? You're setting the cards layout to CardLayout, and then immediatly setting it again to null. And then you get a NullPointerException when you try to use the cards "layout" -- no surprise!
Hi I'm trying to display an ArrayList of Objects on a Jlist. While displaying the ArrayList, I'm trying to be able to add or delete objects to the ArrayList through the GUI.
1) How can I display the ArrayList objects on the Jlist? 2)How can I delete an object by selecting it on the JList?
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTabbedPane;
import javax.swing.ListSelectionModel;
import javax.swing.JList;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
public class DriverFrame extends JFrame implements ItemListener{
private JPanel contentPane;
private JList driverlist;
private ArrayList drivers= new ArrayList<Driver>();
JTextField textFieldFirstName;
JTextField textFieldLastName;
JTextField textFieldTruckNumber;
JTextField textFieldTrailerNumber;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JTextField textField_6;
private JTextField textField_7;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DriverFrame frame = new DriverFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public DriverFrame() {
Driver guy1= new Driver("Jeff","Blank","4125","1234",1400);
Driver guy2= new Driver("Marcus","Geralds","0093","1203",1800);
Driver guy3= new Driver("Steve","Wemmings","2010","2046",2100);
Driver guy4= new Driver("Kyle","Patricks","8427","5625",900);
Driver guy5= new Driver("Ficel","Metter","9124","4536",5500);
setBackground(Color.WHITE);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 500);
setResizable(false);
contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setBackground(Color.WHITE);
tabbedPane.setBounds(5, 5, 495, 480);
contentPane.add(tabbedPane);
//Drivers TabbedPan ********
JPanel Drivers = new JPanel();
Drivers.setBackground(Color.WHITE);
tabbedPane.addTab("Drivers", null, Drivers, null);
Drivers.setLayout(null);
//List to display drivers
JList<Object> driverlist = new JList<>(drivers.toArray(new String[0]));
driverlist.setBackground(Color.WHITE);
driverlist.setVisibleRowCount(8);
driverlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
Drivers.add(driverlist);
driverlist.setBounds(221,6,247,234);
JTextPane txtpnFirstName = new JTextPane();
txtpnFirstName.setText("First name");
txtpnFirstName.setBounds(5, 20, 140, 15);
Drivers.add(txtpnFirstName);
txtpnFirstName.setEditable(false);
textFieldFirstName = new JTextField();
textFieldFirstName.setBounds(5, 40, 140, 30);
Drivers.add(textFieldFirstName);
textFieldFirstName.setColumns(10);
JTextPane txtpnLastName = new JTextPane();
txtpnLastName.setText("Last Name");
txtpnLastName.setBounds(5, 75, 140, 15);
Drivers.add(txtpnLastName);
txtpnLastName.setEditable(false);
textFieldLastName = new JTextField();
textFieldLastName.setBounds(5, 95, 140, 30);
Drivers.add(textFieldLastName);
textFieldLastName.setColumns(10);
JTextPane txtpnTruckNumber = new JTextPane();
txtpnTruckNumber.setText("Truck Number");
txtpnTruckNumber.setBounds(5, 130, 140, 15);
Drivers.add(txtpnTruckNumber);
txtpnTruckNumber.setEditable(false);
textFieldTruckNumber = new JTextField();
textFieldTruckNumber.setBounds(5, 150, 140, 30);
Drivers.add(textFieldTruckNumber);
textFieldTruckNumber.setColumns(10);
JTextPane txtpnTrailerNumber = new JTextPane();
txtpnTrailerNumber.setText("Trailer Number");
txtpnTrailerNumber.setBounds(5, 185, 140, 15);
Drivers.add(txtpnTrailerNumber);
txtpnTrailerNumber.setEditable(false);
textFieldTrailerNumber = new JTextField();
textFieldTrailerNumber.setBounds(5, 205, 140, 30);
Drivers.add(textFieldTrailerNumber);
textFieldTrailerNumber.setColumns(10);
JButton btnAddDriver = new JButton("Add Driver");
btnAddDriver.setBounds(5, 285, 117, 29);
Drivers.add(btnAddDriver);
JButton btnNewButton = new JButton("Delete Driver");
btnNewButton.setBounds(302, 285, 117, 29);
Drivers.add(btnNewButton);
btnAddDriver.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Driver blah = new Driver();
blah.setFirst(textFieldFirstName.getText());
blah.setLast(textFieldLastName.getText());
blah.setTnum(textFieldTruckNumber.getText());
blah.setTrailer(textFieldTrailerNumber.getText());
drivers.add(blah);
System.out.println(blah.printDriver());
System.out.println(drivers.size());
//drivers.remove(example);
}
});
//End of DriversPane******
//New Expenses Tab*****
JPanel NewExpense = new JPanel();
tabbedPane.addTab("New Expense", null, NewExpense, null);
JButton buttontest = new JButton();
NewExpense.add(buttontest);
//End of New Expense Tab****
//Income Tab****
JPanel Income = new JPanel();
Income.setBackground(Color.WHITE);
tabbedPane.addTab("Income", null, Income, null);
Income.setLayout(null);
JComboBox comboBox_3 = new JComboBox();
comboBox_3.setBounds(6, 70, 138, 27);
Income.add(comboBox_3);
JTextPane txtpnDriver_3 = new JTextPane();
txtpnDriver_3.setText("Driver:");
txtpnDriver_3.setBounds(6, 42, 42, 16);
Income.add(txtpnDriver_3);
JTextPane txtpnLoadLocationstate = new JTextPane();
txtpnLoadLocationstate.setText("Load Location(State): ");
txtpnLoadLocationstate.setBounds(6, 161, 138, 16);
Income.add(txtpnLoadLocationstate);
textField_6 = new JTextField();
textField_6.setBounds(6, 205, 130, 26);
Income.add(textField_6);
textField_6.setColumns(10);
JTextPane txtpnLoadAmount = new JTextPane();
txtpnLoadAmount.setText("Load Amount $$:");
txtpnLoadAmount.setBounds(6, 299, 108, 16);
Income.add(txtpnLoadAmount);
textField_7 = new JTextField();
textField_7.setBounds(6, 339, 130, 26);
Income.add(textField_7);
textField_7.setColumns(10);
//End of Income Tab****
//Results Tab***
JPanel Results = new JPanel();
Results.setBackground(Color.WHITE);
tabbedPane.addTab("Result", null, Results, null);
Results.setLayout(null);
JComboBox comboBox_4 = new JComboBox();
comboBox_4.setBounds(6, 69, 140, 27);
Results.add(comboBox_4);
JTextPane txtpnDriver_4 = new JTextPane();
txtpnDriver_4.setText("Driver:");
txtpnDriver_4.setBounds(6, 42, 140, 16);
Results.add(txtpnDriver_4);
JTextArea txtrHowMuchTo = new JTextArea();
txtrHowMuchTo.setText("How much to pay driver. How much are the expenses of the driver");
txtrHowMuchTo.setBounds(6, 124, 234, 304);
Results.add(txtrHowMuchTo);
JTextPane txtpnDriverInformation = new JTextPane();
txtpnDriverInformation.setText("Driver Information");
txtpnDriverInformation.setBounds(16, 108, 140, 16);
Results.add(txtpnDriverInformation);
JTextPane txtpnCompanyInfo = new JTextPane();
txtpnCompanyInfo.setText("Company Info:");
txtpnCompanyInfo.setBounds(414, 80, 178, 16);
Results.add(txtpnCompanyInfo);
JTextArea txtrIncome = new JTextArea();
txtrIncome.setText("Income Expenses");
txtrIncome.setBounds(390, 124, 294, 304);
Results.add(txtrIncome);
//End of Results Tab****
}
#Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
}
}
I am trying to open a new JDialog when the "Register" Button is clicked..
But I am unable to make the JDialog modal..
I have tried setModal(true) and also setModalityType(ModalityType.DOCUMENT_MODAL);
But no luck..(I am using Eclipse's WindowBuilder plugin)
P.S: I am new to JAVA..
Here is my code...
import java.awt.BorderLayout;
import java.awt.Dialog.ModalityType;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Frame;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.xml.ws.handler.MessageContext;
public class loginpage extends JFrame {
private JPanel contentPane;
private JTextField userid;
private JPasswordField password;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
loginpage frame = new loginpage();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public loginpage() {
super("User Login");
setResizable(false);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(400, 200, 550, 370);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
JLabel label = new JLabel("Please enter your login credentials...");
label.setFont(new Font("Times New Roman", Font.PLAIN, 25));
label.setForeground(Color.BLUE);
label.setBounds(79, 11, 400, 61);
contentPane.add(label);
JLabel lblLoginId = new JLabel("User ID :");
lblLoginId.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblLoginId.setBounds(159, 142, 71, 17);
contentPane.add(lblLoginId);
JLabel lblUserType = new JLabel("User Type :");
lblUserType.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblUserType.setBounds(159, 102, 85, 17);
contentPane.add(lblUserType);
JLabel lblPassword = new JLabel("Password :");
lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblPassword.setBounds(159, 188, 96, 14);
contentPane.add(lblPassword);
userid = new JTextField();
userid.setBounds(254, 141, 114, 20);
contentPane.add(userid);
userid.setColumns(10);
password = new JPasswordField();
password.setBounds(254, 186, 114, 20);
contentPane.add(password);
JComboBox usertype = new JComboBox();
usertype.setBounds(254, 101, 89, 20);
contentPane.add(usertype);
usertype.addItem("Admin");
usertype.addItem("Employee");
JButton btnLogin = new JButton("Login");
btnLogin.setBounds(254, 228, 71, 23);
contentPane.add(btnLogin);
JLabel lblNewEmployeeRegister = new JLabel("New employee? ");
lblNewEmployeeRegister.setForeground(Color.RED);
lblNewEmployeeRegister.setBounds(296, 304, 114, 14);
contentPane.add(lblNewEmployeeRegister);
JButton btnRegister = new JButton("Register Here");
btnRegister.setBounds(398, 301, 126, 20);
contentPane.add(btnRegister);
ImageIcon myicon = new ImageIcon("src\\logo.gif");
JLabel iconlabel = new JLabel(myicon);
iconlabel.setBounds(0, 233, 103, 99 );
iconlabel.setIcon(myicon);
contentPane.add(iconlabel);
//Adding Listeners to buttons
//Login Button Listener
btnLogin.addActionListener(new ActionListener()
{
//Making the Connection and registering the Drivers
#Override
public void actionPerformed(ActionEvent arg0) {
}//actionPerformed ends here
});//Login Button Listener ends here
//Register Button Listener
btnRegister.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JDialog jd=new JDialog(loginpage.this,"User Registration");
jd.setLayout(null);
jd.setVisible(true);
jd.setBounds(400,300, 479, 329);
jd.setResizable(false);
setLocationRelativeTo(loginpage.this);
jd.setModal(true);
JLabel lblFillUpThe = new JLabel("Fill up the form to register");
lblFillUpThe.setForeground(Color.BLUE);
lblFillUpThe.setFont(new Font("Times New Roman", Font.PLAIN, 25));
lblFillUpThe.setBounds(93, 11, 400, 61);
jd.add(lblFillUpThe);
JLabel lblUserId = new JLabel("Type in a User ID :");
lblUserId.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblUserId.setBounds(77, 96, 104, 14);
jd.add(lblUserId);
JTextField newid = new JTextField();
newid.setBounds(236, 94, 122, 20);
jd.add(newid);
newid.setColumns(10);
JLabel lblTypeA = new JLabel("Type in a new Password :");
lblTypeA.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblTypeA.setBounds(77, 142, 142, 14);
jd.add(lblTypeA);
JPasswordField newpass = new JPasswordField();
newpass.setBounds(236, 140, 122, 20);
jd.add(newpass);
JLabel lblConfirmThePassword = new JLabel("Confirm the Password :");
lblConfirmThePassword.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblConfirmThePassword.setBounds(77, 189, 130, 14);
jd.add(lblConfirmThePassword);
JPasswordField confpass = new JPasswordField();
confpass.setBounds(236, 187, 122, 20);
jd.add(confpass);
JButton reg = new JButton("Register");
reg.setBounds(239, 237, 89, 23);
jd.add(reg);
reg.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(getParent(), "User Registered Successfully..!!", "Successful",JOptionPane.INFORMATION_MESSAGE);
}
});
}
});//Register Button Listener ends here
}
}
Try making the dialog modal BEFORE you try and make it visible. You can't change the modal state once the dialog is made visible...
JDialog jd=new JDialog(loginpage.this,"User Registration");
jd.setModal(true);
jd.setLayout(null); // THIS IS A BAD IDEA //
jd.setLocationRelativeTo(null);
// This is somewhat pointless, you've set relative location, but know overridden it...
// You should also be relying on the layout manager and pack to determine the size...
jd.setBounds(400,300, 479, 329);
jd.setResizable(false);
setLocationRelativeTo(loginpage.this);
// Add you other components
jd.setVisible(true);