Getting InvalidateStateException when using GroupLayout - java

I am getting the message Baseline must be used along vertical axis. I've tried using all the enums for alignment, to no avail and made sure that all components have been added to both horizontal and vertical axes.
Here is the exception chain
at javax.swing.GroupLayout$BaselineGroup.checkAxis(GroupLayout.java:2926)
at javax.swing.GroupLayout$BaselineGroup.calculateSize(GroupLayout.java:2706)
at javax.swing.GroupLayout$Group.calculatePreferredSize(GroupLayout.java:1602)
at javax.swing.GroupLayout$Spring.getPreferredSize(GroupLayout.java:1346)
at javax.swing.GroupLayout$Group.getSpringSize(GroupLayout.java:1638)
at javax.swing.GroupLayout$Group.calculateSize(GroupLayout.java:1624)
at javax.swing.GroupLayout$Group.calculatePreferredSize(GroupLayout.java:1602)
at javax.swing.GroupLayout$Spring.getPreferredSize(GroupLayout.java:1346)
at javax.swing.GroupLayout$Group.getSpringSize(GroupLayout.java:1638)
at javax.swing.GroupLayout$Group.calculateSize(GroupLayout.java:1625)
at javax.swing.GroupLayout$Group.calculatePreferredSize(GroupLayout.java:1602)
at javax.swing.GroupLayout$Spring.getPreferredSize(GroupLayout.java:1346)
at javax.swing.GroupLayout$SequentialGroup.setValidSize(GroupLayout.java:2017)
at javax.swing.GroupLayout$Group.setSize(GroupLayout.java:1587)
at javax.swing.GroupLayout.calculateAutopadding(GroupLayout.java:1079)
at javax.swing.GroupLayout.layoutContainer(GroupLayout.java:918)
at java.awt.Container.layout(Container.java:1513)
at java.awt.Container.doLayout(Container.java:1502)
at java.awt.Container.validateTree(Container.java:1698)
at java.awt.Container.validateTree(Container.java:1707)
at java.awt.Container.validateTree(Container.java:1707)
at java.awt.Container.validateTree(Container.java:1707)
at java.awt.Container.validateTree(Container.java:1707)
at java.awt.Container.validate(Container.java:1633)
at java.awt.Container.validateUnconditionally(Container.java:1670)
at java.awt.Window.pack(Window.java:818)
at resumebuilder.ResumeBuilder.(ResumeBuilder.java:32)
at resumebuilder.program.main(program.java:15)
Here is the code
private void initComponents() {
GroupLayout groupLayout = new GroupLayout(this);
groupLayout.setAutoCreateGaps(true);
groupLayout.setAutoCreateContainerGaps(true);
jLabel1 = new JLabel("Name");
jLabel1.setHorizontalAlignment(SwingConstants.RIGHT);
firstName = new JTextField(50);
firstName.setToolTipText("First Name");
lastName = new JTextField(50);
lastName.setToolTipText("Last Name");
jLabel2 = new JLabel("Address");
jLabel2.setHorizontalAlignment(SwingConstants.RIGHT);
address1 = new JTextField(50);
address1.setToolTipText("Address 1");
address2 = new JTextField(50);
address2.setToolTipText("Address 2");
add(address2);
toggleAddress3 = new javax.swing.JCheckBox();
toggleAddress3.setText("Show third address");
toggleAddress3.addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent e) {
setToggle();
}
#Override
public void mousePressed(MouseEvent e) {
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
});
address3 = new JTextField(50);
address3.setToolTipText("Address 3");
address3.setVisible(false);
city = new JTextField(50);
city.setToolTipText("City");
state = new JTextField(10);
state.setToolTipText("State");
postalCode = new JTextField(25);
postalCode.setToolTipText("Postal Code");
jLabel3 = new JLabel("Gender");
jLabel3.setHorizontalAlignment(SwingConstants.RIGHT);
add(jLabel3);
String[] items = {"", "Male", "Female"};
gender = new JComboBox<String>(items);
gender.setToolTipText("Gender");
jLabel4 = new JLabel("Date of Birth");
jLabel4.setHorizontalAlignment(SwingConstants.RIGHT);
dateOfBirth = new JSpinner();
groupLayout.setHorizontalGroup(groupLayout.createParallelGroup()
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(firstName)
.addComponent(lastName))
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(address1))
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(toggleAddress3)
.addComponent(address2))
.addComponent(address3)
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(city)
.addComponent(state)
.addComponent(postalCode))
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(gender))
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(dateOfBirth))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(firstName, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lastName, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(groupLayout.createParallelGroup(
GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(address1)
)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel3)
.addComponent(jLabel4))
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(address2, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(toggleAddress3))
.addComponent(address3, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(city, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(state, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(postalCode, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(gender))
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(dateOfBirth))
);
setLayout(groupLayout);
}

Related

JSplitPane setOneTouchExpandable(true) is not shown

I'm using nested JSplitPane...
I want to use OneTouchExpandable, but running the code is not shown...
But in preview mode I can see it.
Here complete code:
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.Dimension;
import java.util.logging.Level;
import java.util.logging.Logger;
package splitpane;
public class JFr_SplitPane extends JFrame {
private JButton jbLeftRight;
private JButton jbRightLeft;
private JButton jbRightRight;
private JLabel jlLeftLeft;
private JPanel jpLeftLeft;
private JPanel jpLeftRight;
private JPanel jpMain;
private JPanel jpRightLeft;
private JPanel jpRightRight;
private JSplitPane jspLeft;
private JSplitPane jspMain;
private JSplitPane jspRight;
private JTextField jtfLeftLeft;
private JTextField jtfLeftRight;
private JTextField jtfRightLeft;
private JTextField jtfRightRight;
public JFr_SplitPane() {
initComponents();
}
private void initComponents() {
jpMain = new JPanel();
jspMain = new JSplitPane();
jspLeft = new JSplitPane();
jpLeftLeft = new JPanel();
jlLeftLeft = new JLabel();
jtfLeftLeft = new JTextField();
jpLeftRight = new JPanel();
jbLeftRight = new JButton();
jtfLeftRight = new JTextField();
jspRight = new JSplitPane();
jpRightLeft = new JPanel();
jbRightLeft = new JButton();
jtfRightLeft = new JTextField();
jpRightRight = new JPanel();
jbRightRight = new JButton();
jtfRightRight = new JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jpMain.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jpMain.setPreferredSize(new Dimension(692, 36));
jspMain.setDividerLocation(344);
jspMain.setMinimumSize(new Dimension(276, 28));
jspMain.setOneTouchExpandable(true);
jspMain.setPreferredSize(new Dimension(688, 34));
jspLeft.setDividerLocation(169);
jspLeft.setMinimumSize(new Dimension(132, 25));
jspLeft.setOneTouchExpandable(true);
jspLeft.setPreferredSize(new Dimension(338, 30));
jpLeftLeft.setMinimumSize(new Dimension(60, 25));
jpLeftLeft.setPreferredSize(new Dimension(320, 25));
jlLeftLeft.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jlLeftLeft.setPreferredSize(new Dimension(24, 18));
jtfLeftLeft.setPreferredSize(new Dimension(6, 25));
GroupLayout jpLeftLeftLayout = new GroupLayout(jpLeftLeft);
jpLeftLeft.setLayout(jpLeftLeftLayout);
jpLeftLeftLayout.setHorizontalGroup(
jpLeftLeftLayout.createParallelGroup(Alignment.LEADING)
.addGroup(jpLeftLeftLayout.createSequentialGroup()
.addGap(4, 4, 4)
.addComponent(jlLeftLeft, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(2, 2, 2)
.addComponent(jtfLeftLeft, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
jpLeftLeftLayout.setVerticalGroup(
jpLeftLeftLayout.createParallelGroup(Alignment.LEADING)
.addGroup(jpLeftLeftLayout.createSequentialGroup()
.addGap(3, 3, 3)
.addComponent(jlLeftLeft, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(jtfLeftLeft, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
);
jspLeft.setLeftComponent(jpLeftLeft);
jpLeftRight.setMinimumSize(new Dimension(60, 25));
jpLeftRight.setPreferredSize(new Dimension(320, 25));
jbLeftRight.setPreferredSize(new Dimension(30, 21));
jtfLeftRight.setPreferredSize(new Dimension(6, 25));
GroupLayout jpLeftRightLayout = new GroupLayout(jpLeftRight);
jpLeftRight.setLayout(jpLeftRightLayout);
jpLeftRightLayout.setHorizontalGroup(
jpLeftRightLayout.createParallelGroup(Alignment.LEADING)
.addGroup(jpLeftRightLayout.createSequentialGroup()
.addGap(4, 4, 4)
.addComponent(jbLeftRight, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(2, 2, 2)
.addComponent(jtfLeftRight, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
jpLeftRightLayout.setVerticalGroup(
jpLeftRightLayout.createParallelGroup(Alignment.LEADING)
.addGroup(jpLeftRightLayout.createParallelGroup(Alignment.CENTER)
.addComponent(jbLeftRight, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(jtfLeftRight, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
);
jspLeft.setRightComponent(jpLeftRight);
jspMain.setLeftComponent(jspLeft);
jspRight.setDividerLocation(169);
jspRight.setMinimumSize(new Dimension(132, 25));
jspRight.setOneTouchExpandable(true);
jspRight.setPreferredSize(new Dimension(338, 30));
jpRightLeft.setMinimumSize(new Dimension(60, 25));
jpRightLeft.setPreferredSize(new Dimension(320, 25));
jbRightLeft.setPreferredSize(new Dimension(30, 21));
jtfRightLeft.setPreferredSize(new Dimension(6, 25));
GroupLayout jpRightLeftLayout = new GroupLayout(jpRightLeft);
jpRightLeft.setLayout(jpRightLeftLayout);
jpRightLeftLayout.setHorizontalGroup(
jpRightLeftLayout.createParallelGroup(Alignment.LEADING)
.addGroup(jpRightLeftLayout.createSequentialGroup()
.addGap(4, 4, 4)
.addComponent(jbRightLeft, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(2, 2, 2)
.addComponent(jtfRightLeft, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
jpRightLeftLayout.setVerticalGroup(
jpRightLeftLayout.createParallelGroup(Alignment.LEADING)
.addGroup(jpRightLeftLayout.createSequentialGroup()
.addGap(2, 2, 2)
.addComponent(jbRightLeft, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(jtfRightLeft, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
);
jspRight.setLeftComponent(jpRightLeft);
jpRightRight.setMinimumSize(new Dimension(60, 25));
jpRightRight.setPreferredSize(new Dimension(320, 25));
jbRightRight.setPreferredSize(new Dimension(30, 21));
jtfRightRight.setPreferredSize(new Dimension(6, 25));
GroupLayout jpRightRightLayout = new GroupLayout(jpRightRight);
jpRightRight.setLayout(jpRightRightLayout);
jpRightRightLayout.setHorizontalGroup(
jpRightRightLayout.createParallelGroup(Alignment.LEADING)
.addGroup(jpRightRightLayout.createSequentialGroup()
.addGap(4, 4, 4)
.addComponent(jbRightRight, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(2, 2, 2)
.addComponent(jtfRightRight, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
jpRightRightLayout.setVerticalGroup(
jpRightRightLayout.createParallelGroup(Alignment.LEADING)
.addGroup(jpRightRightLayout.createSequentialGroup()
.addGap(2, 2, 2)
.addComponent(jbRightRight, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jpRightRightLayout.createSequentialGroup()
.addComponent(jtfRightRight, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
jspRight.setRightComponent(jpRightRight);
jspMain.setRightComponent(jspRight);
GroupLayout jpMainLayout = new GroupLayout(jpMain);
jpMain.setLayout(jpMainLayout);
jpMainLayout.setHorizontalGroup(
jpMainLayout.createParallelGroup(Alignment.LEADING)
.addComponent(jspMain, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
jpMainLayout.setVerticalGroup(
jpMainLayout.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, jpMainLayout.createSequentialGroup()
.addGap(0, 0, 0)
.addComponent(jspMain, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
);
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(Alignment.LEADING)
.addGap(0, 702, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, 0)
.addComponent(jpMain, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap()))
);
layout.setVerticalGroup(
layout.createParallelGroup(Alignment.LEADING)
.addGap(0, 54, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, 0)
.addComponent(jpMain, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
);
pack();
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
Logger.getLogger(JFr_SplitPane.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(JFr_SplitPane.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(JFr_SplitPane.class.getName()).log(Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
Logger.getLogger(JFr_SplitPane.class.getName()).log(Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JFr_SplitPane().setVisible(true);
}
});
}
}
In preview mode:
Running is not shown...
What's the problem?
How can I to solve it?
Testing Look And Feel
Windows
Nimbus
Windows Classic
CDE/Motif
Metal
Probably you are looking for SplitPane.oneTouchButtonOffset:
import java.awt.*;
import javax.swing.*;
public class OneTouchButtonOffsetTest {
public JComponent makeUI() {
System.out.println(UIManager.getInt("SplitPane.oneTouchButtonOffset"));
UIManager.put("SplitPane.oneTouchButtonOffset", 0);
UIDefaults d = new UIDefaults();
d.put("SplitPane:SplitPaneDivider[Enabled+Vertical].foregroundPainter",
new Painter<JComponent>() {
#Override public void paint(Graphics2D g, JComponent c, int w, int h) {
/* empty */
}
});
JSplitPane splitPane = new JSplitPane();
splitPane.putClientProperty("Nimbus.Overrides", d);
splitPane.setLeftComponent(makePanel());
splitPane.setRightComponent(makePanel());
splitPane.setOneTouchExpandable(true);
JPanel p = new JPanel(new BorderLayout());
p.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
p.add(splitPane, BorderLayout.NORTH);
return p;
}
private static JComponent makePanel() {
JPanel p = new JPanel(new BorderLayout());
p.add(new JButton(), BorderLayout.WEST);
p.add(new JTextField("aaaaaaaa"));
return p;
}
public static void main(String... args) {
EventQueue.invokeLater(() -> {
try {
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(laf.getName())) {
UIManager.setLookAndFeel(laf.getClassName());
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new OneTouchButtonOffsetTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}

I cant figure out how to get my JCombox's to insert their data into my database

please help me i cant figure out to get my State ComboBox data to insert into my database here is my code. If anyone can help me id really appreciate it because it is driving me crazy that i cant figure it out.
import java.awt.*;
import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Component;
import java.sql.Connection;
import javax.swing.Box;
import com.mysql.jdbc.PreparedStatement;
import java.sql.*;
import java.text.SimpleDateFormat;
import com.toedter.calendar.JDateChooser;
public class Form1 extends JInternalFrame {
private JButton btnSubmit;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private Cancel form3;
private JTextField textField_5;
private JTextField textField_6;
private JTextField textField_7;
private JTextField textField_8;
private JTextField textField_9;
private JComboBox statesComboBox;
private JComboBox comboBox;
private JDateChooser Date;
Connection con = null;
/*public void fillComboBox(){
try{
String query= "insert into Oilform2 (State,OilType) values(?,?)";
java.sql.PreparedStatement pst = con.prepareStatement(query);
ResultSet rs= pst.executeQuery();
while (rs.next()){
statesComboBox.addItem(rs.getString("State"));
comboBox.addItem(rs.getString("OilType"));
}
}catch (Exception e){
e.printStackTrace();
}
}
*/
/**
* Create the frame.
*/
public Form1() {
con = database.dbConnector();
setTitle("New Customer");
setIconifiable(true);
setClosable(true);
setBounds(-10, -10, 600, 450);
JPanel panel = new JPanel();
JPanel panel_1 = new JPanel();
GroupLayout groupLayout = new GroupLayout(getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
.addContainerGap()
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
.addComponent(panel_1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 505, Short.MAX_VALUE)
.addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 505, Short.MAX_VALUE))
.addContainerGap())
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 235, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE)
.addContainerGap(14, Short.MAX_VALUE))
);
btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
String query = "insert into Oilform2 (FirstName, LastName, table1col, StreetAddress, City, Make, Model, CarYear, PhoneNumber, Mileage, State, OilType) values (?,?,?,?,?,?,?,?,?,?,?,?)";
java.sql.PreparedStatement pst= con.prepareStatement(query);
pst.setString(1, textField.getText());
pst.setString(2, textField_1.getText());
pst.setString(3, textField_2.getText());
pst.setString(4, textField_3.getText());
pst.setString(5, textField_4.getText());
//Combo Boxes States
//String value=statesComboBox.getSelectedItem().toString();
//pst.setString(6, value);
//pst.setString(6, (String)statesComboBox.getSelectedItem());
pst.setString(6, textField_5.getText());
pst.setString(7, textField_6.getText());
pst.setString(8, textField_7.getText());
pst.setString(9, textField_8.getText());
pst.setString(10,textField_9.getText());
//pst.setString(13, (String)comboBox.getSelectedItem());
//Combo Boxes Oil Types
//String value1=comboBox.getSelectedItem().toString();
//pst.setString(13, value1);
JOptionPane.showMessageDialog(null, "Data Saved");
pst.execute();
}catch(Exception i) {
System.err.println("Exception: " + i.getMessage());
}
}
});
// Quit
JButton btnQuit = new JButton("Quit");
btnQuit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
GroupLayout gl_panel_1 = new GroupLayout(panel_1);
gl_panel_1.setHorizontalGroup(
gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(148)
.addComponent(btnSubmit)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(btnQuit)
.addContainerGap(147, Short.MAX_VALUE))
);
gl_panel_1.setVerticalGroup(
gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, gl_panel_1.createSequentialGroup()
.addContainerGap(21, Short.MAX_VALUE)
.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
.addComponent(btnSubmit)
.addComponent(btnQuit))
.addContainerGap())
);
panel_1.setLayout(gl_panel_1);
//----------------------------------------------------
JLabel lblLastName = new JLabel("Last Name:");
textField = new JTextField();
textField.setColumns(10);
//----------------------------------------------------
JLabel lblMi = new JLabel("MI:");
textField_1 = new JTextField();
textField_1.setColumns(10);
//----------------------------------------------------
JLabel lblFirstName = new JLabel("First Name:");
textField_2 = new JTextField();
textField_2.setColumns(10);
//----------------------------------------------------
JLabel lblStreetAddress = new JLabel("Street Address");
textField_3 = new JTextField();
textField_3.setColumns(10);
//----------------------------------------------------
String[] statesStrings = { "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA",
"HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME","MD", "MA", "MI", "MN",
"MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK",
"PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"};
JComboBox statesComboBox = new JComboBox(statesStrings);
statesComboBox.setSelectedIndex(0);
//states.addActionListener(this);
//----------------------------------------------------
JLabel lblState = new JLabel("State");
//----------------------------------------------------
JLabel lblCity = new JLabel("City");
textField_4 = new JTextField();
textField_4.setColumns(10);
//----------------------------------------------------
JLabel lblMake = new JLabel("Make");
textField_5 = new JTextField();
textField_5.setColumns(10);
//----------------------------------------------------
JLabel lblModel = new JLabel("Model");
textField_6 = new JTextField();
textField_6.setColumns(10);
//----------------------------------------------------
JLabel lblYear = new JLabel("Year");
textField_7 = new JTextField();
textField_7.setColumns(10);
//----------------------------------------------------
JLabel lblNumber = new JLabel("Phone Number");
textField_8 = new JTextField();
textField_8.setColumns(10);
//----------------------------------------------------
JLabel lblMileage = new JLabel("Mileage");
textField_9 = new JTextField();
textField_9.setColumns(10);
//----------------------------------------------------
JLabel lblOilType = new JLabel("Oil Type");
String[] oilTypes = { "Oil Type 1", "Oil Type 2", "Oil Type 3", "Oil Type 4"};
JComboBox comboBox = new JComboBox(oilTypes);
comboBox.setSelectedIndex(0);
JLabel lblDate = new JLabel("Date:");
JDateChooser Date = new JDateChooser();
Date.setDateFormatString("yyyy-MM-dd");
//----------------------------------------------------
GroupLayout gl_panel = new GroupLayout(panel);
gl_panel.setHorizontalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addContainerGap()
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addComponent(lblLastName, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)
.addComponent(lblStreetAddress))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_panel.createSequentialGroup()
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 141, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(lblMi)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(lblFirstName, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textField_2, GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE))
.addGroup(gl_panel.createSequentialGroup()
.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_panel.createSequentialGroup()
.addComponent(textField_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(lblCity)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textField_4, GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
.addGap(18))
.addGroup(gl_panel.createSequentialGroup()
.addComponent(lblYear)
.addPreferredGap(ComponentPlacement.RELATED)))
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addComponent(textField_7, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)
.addGroup(gl_panel.createSequentialGroup()
.addComponent(lblState)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(statesComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))))
.addGroup(gl_panel.createSequentialGroup()
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addComponent(lblMake)
.addComponent(lblNumber))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addComponent(textField_8, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGroup(gl_panel.createSequentialGroup()
.addComponent(textField_5, GroupLayout.PREFERRED_SIZE, 81, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(lblModel))))
.addGroup(gl_panel.createSequentialGroup()
.addComponent(lblOilType)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addComponent(textField_6, GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE)
.addGroup(gl_panel.createSequentialGroup()
.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
.addComponent(lblDate)
.addComponent(lblMileage))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
.addComponent(Date, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(textField_9))))
.addGap(70)))
.addContainerGap())
);
gl_panel.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addContainerGap()
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblLastName)
.addComponent(lblMi)
.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblFirstName))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(lblStreetAddress)
.addComponent(textField_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(statesComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblState)
.addComponent(lblCity)
.addComponent(textField_4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(textField_5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblMake)
.addComponent(textField_7, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblYear)
.addComponent(textField_6, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblModel))
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNumber)
.addComponent(textField_8, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblMileage)
.addComponent(textField_9, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(lblOilType)
.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblDate))
.addComponent(Date, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addContainerGap(53, Short.MAX_VALUE))
);
panel.setLayout(gl_panel);
getContentPane().setLayout(groupLayout);
}
}
You're shadowing you variables...
You declare statesComboBox as an instance field in your class...
private JComboBox statesComboBox;
But when you initalise it within your constructor, you declare it as a local variable...
JComboBox statesComboBox = new JComboBox(statesStrings);
This means you ActionListener is trying to use a null references (the instance field)

MySQL Java Use a JComboBox to set a second JComboBox

I am using MySQL server 5.6 to host a database that I am using to manage part number records. I have a Java client front end. My connections between the client and database are all good. In my panel to create parts, I have a JComboBox-typeComboBox that is populated by querying the database for all possible part types and displaying their type numbers in the drop down menu. Depending on the selection from that JComboBox the tdescripTextField and seqTextField are filled with the the appropriate information but I can not get the JComboBox-matComboBox to be populated by the appropriate results that are queried using the selected PartType number.
I have used a number of System.out.println(); to debug the part of the code where the issue is happening, and from what I can tell the String[] mats does contain the correct information, but it just wont assign it to the matComboBox. I notifced if I don't initialize matComboBox = new JComboBox(); right after the typeComboBox I get a compilation error saying that matComboBox is null.
Thanks in advance for any help.
p.s. all of my query methods return JSONArrays from the database.
class CreatePanel extends JPanel{
//JButtons
private JButton saveButton;
private JButton backButton;
//JComboBox
private JComboBox<?> typeComboBox;
private JComboBox<?> matComboBox;
//JTextField
private JTextField tdescripTextField;
private JTextField mdescripTextField;
private JTextField descripTextField;
private JTextField seqTextField;
private JTextField bpartTextField;
private JTextField cpartTextField;
private JTextField spartTextField;
//JLabel
private JLabel lblSeq;
private JLabel lblDescription;
private JLabel lblMatterialDescription;
private JLabel lblTypeDescription;
private JLabel lblType;
private JLabel lblMatterial;
private JLabel lblBosalPartNumber;
private JLabel lblCustomerPartNumber;
private JLabel lblSupplierPartNumber;
private JLabel lblCreateAPart;
private JLabel Bosal;
JPanel contentPane;
//StringPanel
public CreatePanel(final JPanel create)
{
//TextFields
tdescripTextField = new JTextField();
tdescripTextField.setEditable(false);
mdescripTextField = new JTextField();
mdescripTextField.setEditable(false);
descripTextField = new JTextField();
seqTextField = new JTextField();
seqTextField.setEditable(false);
bpartTextField = new JTextField();
bpartTextField.setEditable(false);
cpartTextField = new JTextField();
spartTextField = new JTextField();
//ComboBoxes
typeComboBox = new JComboBox<Object>();
matComboBox = new JComboBox<Object>();
JSONArray temp1 = new JSONArray();
String[] types = null;
try{
temp1 = con.queryReturnAllTypes();
types = new String[temp1.length()];
for(int i = 0; i < temp1.length(); i++){
types[i] = temp1.getJSONObject(i).get("PartType").toString();
}
}catch(Exception ex){/*ignore*/}
typeComboBox = new JComboBox<Object>(types);
typeComboBox.setEditable(true);
typeComboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
int partType = Integer.valueOf((String) typeComboBox.getSelectedItem());
/*Debug*/System.out.println(partType);
JSONArray temp1 = new JSONArray();
JSONArray temp2 = new JSONArray();
String typeDescrip = null;
String seqNum = null;
String[] mats = null;
try{
temp1 = con.queryPartType(partType);
/*Debug*/System.out.println(temp1);
temp2 = con.queryMaterialPartType(partType);
/*Debug*/System.out.println(temp2);
mats = new String[temp2.length()];
/*Debug*/System.out.println(temp2.length());
typeDescrip = temp1.getJSONObject(0).get("TypeDescription").toString();
tdescripTextField.setText(typeDescrip);
seqNum = temp1.getJSONObject(0).get("SeqNumber").toString();
seqTextField.setText(seqNum);
for(int i = 0; i < temp2.length(); i++){
/*Debug*/System.out.println(i);
/*Debug*/System.out.println(temp2.getJSONObject(i).get("Material").toString());
mats[i] = temp2.getJSONObject(i).get("Material").toString();
/*Debug*/System.out.println(mats[i]);
}
for(int i = 0; i<mats.length; i++){
/*Debug*/System.out.println(mats[i]);
}
}catch(Exception ex){/*ignore*/}
for(int i = 0; i<mats.length; i++){
/*Debug*/System.out.println(mats[i]);
}
matComboBox = new JComboBox<Object>(mats);
matComboBox.setEditable(true);
}
});
//Labels
lblType = new JLabel("Type");
lblMatterial = new JLabel("Material");
lblTypeDescription = new JLabel("Type Description");
lblMatterialDescription = new JLabel("Material Description");
lblSeq = new JLabel("Seq");
lblDescription = new JLabel("Description");
lblBosalPartNumber = new JLabel("Bosal Part Number");
lblCustomerPartNumber = new JLabel("Customer Part Number");
lblSupplierPartNumber = new JLabel("Supplier Part Number");
lblCreateAPart = new JLabel("Create a Part Number");
ImageIcon bosal = new ImageIcon(getClass().getResource("/Images/bosal.jpg"));
Bosal = new JLabel(bosal);
setBackground(Color.DARK_GRAY);
//Buttons
ImageIcon save = new ImageIcon(getClass().getResource("/images/save1.jpg"));
saveButton = new JButton(save);
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == saveButton){
int n = JOptionPane.showConfirmDialog(
frame,
"Are you sure you want to save part data?",
"Save:",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
}}});
add(saveButton);
ImageIcon back = new ImageIcon(getClass().getResource("/images/back1.jpg"));
backButton = new JButton(back);
backButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == backButton)
{
setVisible(false);
frame.setLocation(550,220);
frame.setSize(700, 580);
main.setVisible(true);
}}});
add(backButton);
setupPanel();
};
private void setupPanel()
{
//Label Fonts
lblType.setFont(new Font("Tahoma", Font.BOLD, 14));
lblType.setForeground(Color.BLACK);
lblMatterial.setFont(new Font("Tahoma", Font.BOLD, 14));
lblMatterial.setForeground(Color.BLACK);
lblTypeDescription.setFont(new Font("Tahoma", Font.BOLD, 14));
lblTypeDescription.setForeground(Color.BLACK);
lblMatterialDescription.setFont(new Font("Tahoma", Font.BOLD, 14));
lblMatterialDescription.setForeground(Color.BLACK);
lblDescription.setFont(new Font("Tahoma", Font.BOLD, 14));
lblDescription.setForeground(Color.BLACK);
lblSeq.setFont(new Font("Tahoma", Font.BOLD, 14));
lblSeq.setForeground(Color.BLACK);
lblSupplierPartNumber.setFont(new Font("Tahoma", Font.BOLD, 14));
lblSupplierPartNumber.setForeground(Color.BLACK);
lblCreateAPart.setFont(new Font("EucrosiaUPC", Font.BOLD, 64));
lblCreateAPart.setForeground(Color.BLACK);
lblCustomerPartNumber.setFont(new Font("Tahoma", Font.BOLD, 14));
lblCustomerPartNumber.setForeground(Color.BLACK);
lblBosalPartNumber.setFont(new Font("Tahoma", Font.BOLD, 14));
lblBosalPartNumber.setForeground(Color.BLACK);
//Group Layout
GroupLayout groupLayout = new GroupLayout(this);
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(24)
.addComponent(Bosal, GroupLayout.PREFERRED_SIZE, 199, GroupLayout.PREFERRED_SIZE)
.addGap(6)
.addComponent(lblCreateAPart, GroupLayout.PREFERRED_SIZE, 434, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addGap(50)
.addComponent(lblType)
.addGap(121)
.addComponent(lblTypeDescription)
.addGap(163)
.addComponent(lblBosalPartNumber))
.addGroup(groupLayout.createSequentialGroup()
.addGap(50)
.addComponent(typeComboBox, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
.addGap(76)
.addComponent(tdescripTextField, GroupLayout.PREFERRED_SIZE, 211, GroupLayout.PREFERRED_SIZE)
.addGap(67)
.addComponent(bpartTextField, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addGap(50)
.addComponent(lblMatterial)
.addGap(101)
.addComponent(lblMatterialDescription)
.addGap(143)
.addComponent(lblCustomerPartNumber))
.addGroup(groupLayout.createSequentialGroup()
.addGap(50)
.addComponent(matComboBox, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
.addGap(76)
.addComponent(mdescripTextField, GroupLayout.PREFERRED_SIZE, 211, GroupLayout.PREFERRED_SIZE)
.addGap(67)
.addComponent(cpartTextField, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addGap(50)
.addComponent(lblSeq)
.addGap(129)
.addComponent(lblDescription)
.addGap(201)
.addComponent(lblSupplierPartNumber))
.addGroup(groupLayout.createSequentialGroup()
.addGap(331)
.addComponent(backButton, GroupLayout.PREFERRED_SIZE, 157, GroupLayout.PREFERRED_SIZE)
.addGap(41)
.addComponent(saveButton, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addGap(50)
.addComponent(seqTextField, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
.addGap(97)
.addComponent(descripTextField, GroupLayout.PREFERRED_SIZE, 211, GroupLayout.PREFERRED_SIZE)
.addGap(67)
.addComponent(spartTextField, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(36, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(32)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(Bosal, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE)
.addComponent(lblCreateAPart, GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE))
.addGap(6)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(lblType)
.addComponent(lblTypeDescription)
.addComponent(lblBosalPartNumber))
.addGap(6)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(typeComboBox, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
.addGroup(groupLayout.createSequentialGroup()
.addGap(3)
.addComponent(tdescripTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addGap(3)
.addComponent(bpartTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addGap(6)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(lblMatterial)
.addComponent(lblMatterialDescription)
.addComponent(lblCustomerPartNumber))
.addGap(6)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(matComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(mdescripTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(cpartTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(9)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(lblSeq)
.addComponent(lblDescription)
.addComponent(lblSupplierPartNumber))
.addGap(6)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(seqTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(descripTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(spartTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(37)
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
.addComponent(saveButton, 0, 0, Short.MAX_VALUE)
.addComponent(backButton, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE))
.addContainerGap(135, Short.MAX_VALUE))
);
setLayout(groupLayout);
}}
You already have a combo box, which is already displayed in the panel. What you want is to popule this already existing combo box.
But your code does
matComboBox = new JComboBox<Object>(mats);
So it creates another JComboBox, populated with the mats that you retrieved. That won't popukate your existing, displayed combo box. What you need to do is to change the data inside the combo box:
matComboBox.setModel(new DefaultComboBoxModel(mats));

Java UI Generator causing massive lag?

I am awful at Java GUI and used a generator to make most of the GUI for me. I made it in Netbeans and then copied it over to Eclipse. It lags out my pretty powerful computer for about 5 seconds on start and although I've tried timing it and stuff, I can't figure out why. I don't like to blame the generator but it is 100% coming from that UI code.
I would really appreciate someone helping me figure out what is causing it, and just in general improving the ugly UI code. The other code is unfinished, and this is just the interface part.
note that you can remove all of my code, even the gridLayout, and it will still lag, and if i compile it to a .jar and compile it with a -classpath argument it has the same lag
package ...
imports ...
public class x extends javax.swing.JFrame {
public x() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Point centrePoint = new Point(screenSize.width / 2, screenSize.height / 2);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
schMon = new javax.swing.JLabel();
schTue = new javax.swing.JLabel();
schWed = new javax.swing.JLabel();
schThu = new javax.swing.JLabel();
schFri = new javax.swing.JLabel();
schSat = new javax.swing.JLabel();
schSun = new javax.swing.JLabel();
timeLabel = new javax.swing.JLabel();
schedulePanel = new javax.swing.JPanel();
holdingPanel = new javax.swing.JPanel();
isEnabled = new javax.swing.JCheckBox();
dropboxPath = new javax.swing.JTextField();
fileButton = new javax.swing.JButton();
saveButton = new javax.swing.JButton();
resetButton = new javax.swing.JButton();
sep2 = new javax.swing.JSeparator();
sep = new javax.swing.JSeparator();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
schMon.setText("Monday");
schTue.setText("Tuesday");
schWed.setText("Wednesday ");
schThu.setText("Thursday");
schFri.setText("Friday");
schSat.setText("Saturday");
schSun.setText("Sunday");
Font font = new Font("Calibri", Font.BOLD, 13);
timeLabel.setFont(font);
timeLabel.setText("");
schedulePanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
schedulePanel.setLayout(new GridLayout(7, 24, -1, -1));
schedulePanel.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent mouseEvent) {
//JPanel clickedPanel = (JPanel) getComponentAt(mouseEvent.getPoint());
// System.out.println(clickedPanel.getName().toString());
}
});
ArrayList<ArrayList<JPanel>> times = new ArrayList<ArrayList<JPanel>>();
for (int day = 0; day < 7; day++) {
times.add(new ArrayList<JPanel>());
for (int hour = 0; hour < 24; hour++) {
times.get(day).add(new JPanel());
JPanel current = times.get(day).get(hour);
current.setSize(42, 42);
current.setBorder(BorderFactory.createLineBorder(Color.black));
current.setMaximumSize(current.getSize());
current.setBackground(Color.LIGHT_GRAY);
schedulePanel.add(current);
}
}
holdingPanel.setPreferredSize(new java.awt.Dimension(2, 100));
isEnabled.setText("Enable Scheduler");
dropboxPath.setText("Path to your Dropbox.exe");
dropboxPath.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
fileButton.setText("Find");
fileButton.setPreferredSize(new java.awt.Dimension(60, 22));
saveButton.setText("Save");
resetButton.setText("Reset");
resetButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
sep2.setOrientation(javax.swing.SwingConstants.VERTICAL);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(
holdingPanel);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(isEnabled)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(dropboxPath, GroupLayout.PREFERRED_SIZE, 480, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(fileButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(sep2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(58)
.addComponent(saveButton)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(resetButton)
.addGap(2))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(Alignment.TRAILING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(Alignment.BASELINE)
.addComponent(isEnabled, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(sep2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(dropboxPath, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(fileButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(saveButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(resetButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
holdingPanel.setLayout(jPanel2Layout);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addContainerGap()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(sep)
.addComponent(
holdingPanel,
javax.swing.GroupLayout.DEFAULT_SIZE,
630, Short.MAX_VALUE)
.addGroup(
layout.createSequentialGroup()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(
schSat)
.addComponent(
schSun)
.addComponent(
timeLabel)
.addComponent(
schMon)
.addComponent(
schTue)
.addComponent(
schWed)
.addComponent(
schThu)
.addComponent(
schFri))
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(
schedulePanel,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)))
.addContainerGap()));
layout.setVerticalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addContainerGap()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addGap(10)
.addComponent(
schMon)
.addGap(30)
.addComponent(
schTue)
.addGap(30)
.addComponent(
schWed)
.addGap(30)
.addComponent(
schThu)
.addGap(30)
.addComponent(
schFri)
.addGap(30)
.addComponent(
schSat)
.addGap(30)
.addComponent(
schSun)
.addGap(12)
.addComponent(
timeLabel)
.addGap(0,
0,
Short.MAX_VALUE))
.addComponent(
schedulePanel,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
.addGap(10, 10, 10)
.addComponent(sep,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(1, 1, 1)
.addComponent(holdingPanel,
javax.swing.GroupLayout.PREFERRED_SIZE,
45,
javax.swing.GroupLayout.PREFERRED_SIZE)));
pack();
Point newLocation = new Point(centrePoint.x - (this.getWidth() / 2), centrePoint.y - (this.getHeight() / 2));
setLocation(newLocation);
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
}
public static void main(String args[]) {
try {
javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager
.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(x.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(x.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(x.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(x.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new x().setVisible(true);
}
});
}
private javax.swing.JButton fileButton;
private javax.swing.JButton saveButton;
private javax.swing.JButton resetButton;
private javax.swing.JCheckBox isEnabled;
private javax.swing.JLabel schMon;
private javax.swing.JLabel schTue;
private javax.swing.JLabel schWed;
private javax.swing.JLabel schThu;
private javax.swing.JLabel schFri;
private javax.swing.JLabel schSat;
private javax.swing.JLabel schSun;
private javax.swing.JLabel timeLabel;
private javax.swing.JPanel holdingPanel;
private javax.swing.JSeparator sep;
private javax.swing.JSeparator sep2;
private javax.swing.JTextField dropboxPath;
private javax.swing.JPanel schedulePanel;
}
If the lag is during WindowBuilder's opening of the design page (parsing), the lag is normal.

GroupLayout Alignment Issue

I am having problems in setting the GroupLayout as desired in Java.
My code is given below. The desired placement for components is:
++++++++++++++++++++++++++++++++++++++++++++++++++++
+LABEL_A caseStudyComboBox LABEL_B +
+LABEL_C TextfieldE BtnD +
++++++++++++++++++++++++++++++++++++++++++++++++++++
Instead of the above, the output is:
+++++++++++++++++++++++++++++++++++++++++++++++++++
+LABEL_A +
+ caseStudyCombBox +
+ LABEL_B +
+ LABEL_C IS LONGER THAN A: +
+ TextfieldE +
+ BtnD +
+++++++++++++++++++++++++++++++++++++++++++++++++++
Please suggest some remedy.
Thanks.
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.*;
import static javax.swing.GroupLayout.Alignment.*;
public class EXP1 extends JFrame {
String [] caseStudyList = {
"",
"Case A",
"Case B"
};
//
public EXP1() {
JLabel Label_A = new JLabel("LABEL A ");
JComboBox caseStudyComboBox = new JComboBox(caseStudyList);
JLabel Label_B = new JLabel("LABEL B");
JLabel Label_C = new JLabel("LABEL C IS LONGER THAN A: ");
JButton BtnD = new JButton("BUTTON D");
JTextField TextFieldE = new JTextField();
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(TRAILING))
.addComponent(Label_A, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Label_C, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(LEADING))
.addComponent(caseStudyComboBox, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(TextFieldE, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(LEADING))
.addComponent(Label_B, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(BtnD, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.linkSize(SwingConstants.HORIZONTAL, Label_B, BtnD);
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(BASELINE))
.addComponent(Label_A)
.addComponent(caseStudyComboBox)
.addComponent(Label_B)
.addGroup(layout.createParallelGroup(LEADING))
.addComponent(Label_C)
.addComponent(TextFieldE)
.addComponent(BtnD)
);
setTitle("EXPERIMENT");
this.pack();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//BPAOntoEIAUI mainUI = new BPAOntoEIAUI();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(
"javax.swing.plaf.metal.MetalLookAndFeel");
// "com.sun.java.swing.plaf.motif.MotifLookAndFeel");
//UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception ex) {
ex.printStackTrace();
}
new EXP1().setVisible(true);
}
});
}
}
Looks like there is a bit of confusion with brackets when you set up horizontal and vertical groups. The controls fall out of the intended groups that you create.
Instead of
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup())
.addComponent(Label_A)
.addComponent(Label_C)
It should actually be:
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup()
.addComponent(Label_A)
.addComponent(Label_C))
In first case you're adding labels to the sequential group instead of the parallel group like in the second case.
Here is the result with the following slightly modified code:
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(TRAILING)
.addComponent(Label_A, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Label_C, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(caseStudyComboBox, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(TextFieldE, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(Label_B, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(BtnD, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.linkSize(SwingConstants.HORIZONTAL, Label_B, BtnD);
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(BASELINE)
.addComponent(Label_A)
.addComponent(caseStudyComboBox)
.addComponent(Label_B))
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(Label_C)
.addComponent(TextFieldE)
.addComponent(BtnD))
);

Categories

Resources