JSplitPane setOneTouchExpandable(true) is not shown - java

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);
});
}
}

Related

JFrames load blank

I'm trying to make a program that uses Swing for the GUI, but the form that opens up when I run the code below is blank. I'm still able to get information regarding the objects that aren't seen, however (tested with getting btnLogin.getText()). Am I missing something, or am I doing something wrong?
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class loginForm extends JFrame implements ActionListener{
public static void main(String[] args){
new loginForm();
}
public loginForm() {
System.out.println("Starting...");
pack();
initComponents();
System.out.println("Opened");
this.setVisible(true);
}
private void initComponents() {
loginForm = new JFrame();
lblUserID = new JLabel();
existingUserIDTextField = new JTextField();
lblPassword = new JLabel();
existingUserPasswordField = new JPasswordField();
btnLogin = new JButton();
btnLogin.addActionListener(this);
lblWelcome = new JLabel();
//======== loginForm ========
{
loginForm.setResizable(false);
loginForm.setTitle("Library Reservation System");
Container loginFormContentPane = loginForm.getContentPane();
//---- lblUserID ----
lblUserID.setText("User ID:");
lblUserID.setFont(new Font("Tahoma", Font.PLAIN, 16));
//---- lblPassword ----
lblPassword.setText("Password:");
lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 16));
//---- btnLogin ----
btnLogin.setText("Login");
//---- lblWelcome ----
lblWelcome.setText("Welcome to our Library Reservation System");
lblWelcome.setFont(new Font("Tahoma", Font.PLAIN, 16));
GroupLayout loginFormContentPaneLayout = new GroupLayout(loginFormContentPane);
loginFormContentPane.setLayout(loginFormContentPaneLayout);
loginFormContentPaneLayout.setHorizontalGroup(
loginFormContentPaneLayout.createParallelGroup()
.addGroup(loginFormContentPaneLayout.createSequentialGroup()
.addGroup(loginFormContentPaneLayout.createParallelGroup()
.addGroup(loginFormContentPaneLayout.createSequentialGroup()
.addGap(199, 199, 199)
.addComponent(btnLogin, GroupLayout.PREFERRED_SIZE, 145, GroupLayout.PREFERRED_SIZE))
.addGroup(loginFormContentPaneLayout.createSequentialGroup()
.addGap(72, 72, 72)
.addGroup(loginFormContentPaneLayout.createParallelGroup()
.addComponent(lblUserID, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
.addComponent(lblPassword))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(loginFormContentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
.addComponent(existingUserIDTextField, GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE)
.addComponent(existingUserPasswordField, GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE)))
.addGroup(GroupLayout.Alignment.TRAILING, loginFormContentPaneLayout.createSequentialGroup()
.addContainerGap()
.addComponent(lblWelcome, GroupLayout.PREFERRED_SIZE, 318, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(118, Short.MAX_VALUE))
);
loginFormContentPaneLayout.setVerticalGroup(
loginFormContentPaneLayout.createParallelGroup()
.addGroup(loginFormContentPaneLayout.createSequentialGroup()
.addContainerGap()
.addComponent(lblWelcome, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
.addGap(11, 11, 11)
.addGroup(loginFormContentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(lblUserID)
.addComponent(existingUserIDTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(loginFormContentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(lblPassword)
.addComponent(existingUserPasswordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(btnLogin)
.addContainerGap(12, Short.MAX_VALUE))
);
loginForm.pack();
loginForm.setLocationRelativeTo(loginForm.getOwner());
}
}
private JFrame loginForm;
private JLabel lblUserID;
private JTextField existingUserIDTextField;
private JLabel lblPassword;
private JPasswordField existingUserPasswordField;
private JButton btnLogin;
private JLabel lblWelcome;
}
The frame containing the controls is never displayed.
loginForm.setVisible(true);
Instead of having an outer redundant frame class you could do
public class LoginFormApp extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
LoginFormApp loginFormApp = new LoginFormApp();
loginFormApp.initComponents();
}
});
.....
}
where initComponents makes the loginForm visible as previously stated.

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)

Passing text field value into a constructor

Ok, I have a program I am doing for class. I have to take the input form the text field, ad throw it into an overloaded constructor that takes a string as an argument. There are a few other things I have to do with constructors as well, so in my code you'll see some stuff that is outside of this question. also, I am not posting my methods because it's a big part of the homework, and those all work fine, (prior to my attempt at passing info to the constructor). What I would like to know is i there is a way to pass the text field info into the constructor, because I have been going at this for a couple hours now, but it's becoming plainly obvious that I don't understand this. If you just want to direct me to somewhere that I can learn how to do this I would appreciate it.
Code:
package lab4;
import java.awt.*;
import java.awt.List;
import javax.swing.*;
import java.awt.event.*;
import java.lang.Iterable.*;
import java.util.*;
public class Lab4 extends JFrame{
private JTextField jTextField1;
private JButton doSome;
private JLabel ansTxt;
private JLabel asciSum;
private JLabel jLabel1;
private JLabel jLabel3;
private JLabel jLabel4;
private JLabel jLabel5;
private JLabel jLabel6;
private JLabel lowCnt;
private JLabel numDigit;
private JLabel upCnt;
private JLabel vowelCnt;
private String userInput = jTextField1.getText();
public Lab4()
{
initComponents();
}
public Lab4(String x)
{
this.userInput = x;
}
public Lab4(char[] x)
{
}
public Lab4(byte[] x)
{
}
private void initComponents() {
jTextField1 = new JTextField();
jLabel1 = new JLabel();
doSome = new JButton();
ansTxt = new JLabel();
jLabel3 = new JLabel();
jLabel4 = new JLabel();
jLabel5 = new JLabel();
jLabel6 = new JLabel();
asciSum = new JLabel();
vowelCnt = new JLabel();
numDigit = new JLabel();
upCnt = new JLabel();
lowCnt = new JLabel();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setAutoRequestFocus(false);
setFont(new java.awt.Font("Comic Sans MS", 1, 12)); // NOI18N
jLabel1.setText("Enter a string Here and I'll show you some info about it!");
doSome.setToolTipText("This Will Do Something");
doSome.setInheritsPopupMenu(true);
doSome.setLabel("Click Me When Your Done!");
doSome.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
doSomeActionPerformed(evt);
}
});
ansTxt.setText("ASCII SUM:");
jLabel3.setText("Number of Vowels:");
jLabel4.setText("Number of Digits:");
jLabel5.setText("Number Of Uppercase:");
jLabel6.setText("Number Of Lowercase:");
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
.addComponent(ansTxt)
.addComponent(jLabel5, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel4, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel6, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(84, 84, 84)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(lowCnt)
.addComponent(vowelCnt)
.addComponent(asciSum)
.addComponent(numDigit)
.addComponent(upCnt)))
.addGroup(layout.createSequentialGroup()
.addGap(95, 95, 95)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField1)
.addComponent(jLabel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(doSome, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap(239, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(34, 34, 34)
.addComponent(jLabel1)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(doSome)
.addGap(34, 34, 34)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(ansTxt)
.addComponent(asciSum))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(vowelCnt))
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(numDigit))
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(upCnt))
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(lowCnt))
.addContainerGap(157, Short.MAX_VALUE))
);
pack();
}
private void doSomeActionPerformed(ActionEvent evt)
{
String takeTfIn = this.jTextField1.getText();
this.asciSum.setText(getAsciiSum(takeTfIn));
this.vowelCnt.setText(getNumVowels(takeTfIn));
this.numDigit.setText(getNumDigits(takeTfIn));
this.upCnt.setText(getNumUpperCase(takeTfIn));
this.lowCnt.setText(getNumLowerCase(takeTfIn));
}
// Excluded methods go here...
public static void main(String[] args)
{
Lab4 test = new Lab4();
test.setVisible(true);
}
}
You'll need to create new instance of lab4 after you fill tex field value and click on submit. on click of submit you can do like this
this.setVisible(false);//Hide current instance
Lab4 test = new Lab4(this.jTextField1.getText()); // create new instance
test.setVisible(true); // set new instance visible
Don't forget to add following constructor:
public Lab4 (String value)
{
this.jTextField1.setText(value); // if you like to set value back to textbox
}

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))
);

jFormDesigner design it won't display?

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.

Categories

Resources