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