I would like to center content of a GroupLayout in Java applet. After long research I still cannot figure out how to do it.
Code below shows my applet. All elements are centered to layout but the layout is not center to applet.
JPanel cp=new JPanel();
String[] s = new String[2];
s[0] = "Price";
s[1] = "Name";
JComboBox c = new JComboBox(s);
JProgressBar pb=new JProgressBar(17, 23);
pb.setValue(20);
pb.setStringPainted(true);
JLabel l=new JLabel("Name of product");
JButton b=new JButton("Send a message");
b.setEnabled(true);
cp.add(c);
cp.add(pb);
cp.add(l);
cp.add(b);
GroupLayout layout = new GroupLayout(cp);
cp.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(c,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(pb,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(l,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(b,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE))
);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addComponent(c,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(pb,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(l,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(b,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
);
I will appriciate any help.
The important lines are:
Container cont = getContentPane();
cont.setLayout(new GridBagLayout());
add(cp);
Screenshot
SSCCE
The 'full' 79 line code is:
//<applet code='TestApplet' width='250' height='250'></applet>
import java.awt.*;
import javax.swing.*;
public class TestApplet extends JApplet {
public void init() {
JPanel cp=new JPanel();
Container cont = getContentPane();
cont.setLayout(new GridBagLayout());
add(cp);
String[] s = new String[2];
s[0] = "Price";
s[1] = "Name";
JComboBox c = new JComboBox(s);
JProgressBar pb=new JProgressBar(17, 23);
pb.setValue(20);
pb.setStringPainted(true);
JLabel l=new JLabel("Name of product");
JButton b=new JButton("Send a message");
b.setEnabled(true);
cp.add(c);
cp.add(pb);
cp.add(l);
cp.add(b);
GroupLayout layout = new GroupLayout(cp);
cp.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(
GroupLayout.Alignment.CENTER)
.addComponent(c,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(pb,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(l,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(b,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE))
);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addComponent(c,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(pb,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(l,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(b,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
);
}
}
You can create vertical and horizontal Glue elements from the Box factory and add these to the sides of your layout. When the layout is set out, the glue expands and forces the rest of the layout into the center.
Related
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);
}
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)
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));
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))
);
import java.awt.*;
import javax.swing.*;
import javax.swing.GroupLayout;
/**
* #author Ene Ion
*/
public class launcher extends JFrame {
public launcher() {
initComponents();
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
// Generated using JFormDesigner Evaluation license - Ene Ion
mbmlauncherfata = new JFrame();
textField1 = new JTextField();
checkBox1 = new JCheckBox();
label1 = new JLabel();
comboBox1 = new JComboBox();
label2 = new JLabel();
comboBox2 = new JComboBox();
button1 = new JButton();
button2 = new JButton();
button3 = new JButton();
progressBar1 = new JProgressBar();
label3 = new JLabel();
label4 = new JLabel();
button4 = new JButton();
button5 = new JButton();
label5 = new JLabel();
button6 = new JButton();
//======== mbmlauncherfata ========
{
mbmlauncherfata.setTitle("MBMLauncher");
mbmlauncherfata.setIconImage(new ImageIcon("E:\\Chituri\\MBMLauncherJava\\src\\resursele\\icon_4_64x64x32.png").getImage());
mbmlauncherfata.setResizable(false);
Container mbmlauncherfataContentPane = mbmlauncherfata.getContentPane();
//---- textField1 ----
textField1.setText("Username");
textField1.setForeground(new Color(153, 0, 0));
//---- checkBox1 ----
checkBox1.setText("Remember Me");
//---- label1 ----
label1.setText("RAM Allocated");
//---- comboBox1 ----
comboBox1.setModel(new DefaultComboBoxModel(new String[] {
"256M",
"512M",
"1024M",
"2G",
"3G",
"4G"
}));
//---- label2 ----
label2.setText("to");
//---- comboBox2 ----
comboBox2.setModel(new DefaultComboBoxModel(new String[] {
"512M",
"1024M",
"2G",
"3G",
"4G",
"5G",
"6G"
}));
//---- button1 ----
button1.setText("Play");
button1.setFont(new Font("Snap ITC", Font.BOLD, 36));
button1.setForeground(new Color(255, 102, 0));
//---- button2 ----
button2.setText("Update Game");
//---- button3 ----
button3.setText("Update Launcher");
//---- label3 ----
label3.setText("Download status:");
//---- label4 ----
label4.setText("Nothing in download");
//---- button4 ----
button4.setText("Texture Packs");
//---- button5 ----
button5.setText(".game");
//---- label5 ----
label5.setText("Drag and drop your texture\npack in the box *.zip files");
label5.setIcon(new ImageIcon("E:\\Chituri\\MBMLauncherJava\\src\\resursele\\dasdasdasd.png"));
//---- button6 ----
button6.setText("Made By Malasuerte94");
GroupLayout mbmlauncherfataContentPaneLayout = new GroupLayout(mbmlauncherfataContentPane);
mbmlauncherfataContentPane.setLayout(mbmlauncherfataContentPaneLayout);
mbmlauncherfataContentPaneLayout.setHorizontalGroup(
mbmlauncherfataContentPaneLayout.createParallelGroup()
.addGroup(mbmlauncherfataContentPaneLayout.createSequentialGroup()
.addContainerGap()
.addGroup(mbmlauncherfataContentPaneLayout.createParallelGroup()
.addComponent(progressBar1, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(button1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(mbmlauncherfataContentPaneLayout.createSequentialGroup()
.addComponent(button2, GroupLayout.PREFERRED_SIZE, 131, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(button3, GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE))
.addGroup(mbmlauncherfataContentPaneLayout.createSequentialGroup()
.addComponent(button4, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(button5, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(mbmlauncherfataContentPaneLayout.createSequentialGroup()
.addComponent(textField1, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(checkBox1))
.addGroup(mbmlauncherfataContentPaneLayout.createSequentialGroup()
.addComponent(label1)
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(comboBox1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(label2)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(comboBox2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(mbmlauncherfataContentPaneLayout.createSequentialGroup()
.addComponent(label3, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(label4))
.addComponent(label5, GroupLayout.PREFERRED_SIZE, 270, GroupLayout.PREFERRED_SIZE)
.addComponent(button6, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
mbmlauncherfataContentPaneLayout.setVerticalGroup(
mbmlauncherfataContentPaneLayout.createParallelGroup()
.addGroup(mbmlauncherfataContentPaneLayout.createSequentialGroup()
.addContainerGap()
.addGroup(mbmlauncherfataContentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(checkBox1)
.addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(mbmlauncherfataContentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label2)
.addComponent(comboBox2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label1)
.addComponent(comboBox1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(button1)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(mbmlauncherfataContentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(button2)
.addComponent(button3))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(progressBar1, GroupLayout.PREFERRED_SIZE, 18, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(mbmlauncherfataContentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label4)
.addComponent(label3))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(mbmlauncherfataContentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(button4)
.addComponent(button5))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(label5)
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(button6, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
.addContainerGap(10, Short.MAX_VALUE))
);
mbmlauncherfata.setSize(305, 395);
mbmlauncherfata.setLocationRelativeTo(null);
}
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
// Generated using JFormDesigner Evaluation license - Ene Ion
private JFrame mbmlauncherfata;
private JTextField textField1;
private JCheckBox checkBox1;
private JLabel label1;
private JComboBox comboBox1;
private JLabel label2;
private JComboBox comboBox2;
private JButton button1;
private JButton button2;
private JButton button3;
private JProgressBar progressBar1;
private JLabel label3;
private JLabel label4;
private JButton button4;
private JButton button5;
private JLabel label5;
private JButton button6;
// JFormDesigner - End of variables declaration //GEN-END:variables
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
launcher GUI = new launcher();
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUI.setVisible(true);
}
});
}
}
Result is a empty java box..
picture
I try to fix this problem but i am beginer in java scripting, where is the problem and how i can fix it ?
Tanks for helping, and recomand me please some good plugins for Eclipse.
That usually happens when the frame is not packed.
Try this:
launcher GUI = new launcher();
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUI.pack(); // <=========== PACK
GUI.setVisible(true);
Here is a nice tutorial that will get you started with Java frames.