I am Currently building a GUI for a tic tak toe game and i have finished it all, however when I tried to run it failed so I checked the code to find the problem and the only one i can currently find is that initComponents is red lined and I am Having a hard time finding out why.
I am currently new to Java and have only started coding recently so any assistance in this will be very appreciated.
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import java.awt.Component;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author matt
*/
public class TicTacToe extends javax.swing.JFrame {
/**
* Creates new form TicTacToe
*/
public TicTacToe() {
initComponents();
addAction();
}
public void winEffect(JButton b1, JButton b2, JButton b3)
{
b1.setBackground(Color.black);
b2.setBackground(Color.black);
b3.setBackground(Color.BLACK);
b1.setForeground(Color.WHITE);
b2.setForeground(Color.WHITE);
b3.setForeground(Color.WHITE);
String msg = b1.getText()+ " Is the winner";
jLabelMSG.setText(msg);
}
boolean win=false;
public void getthewinner(){
if(jButton1.getText().equals("")&& jButton1.getText().equals(jButton2.getText())&& jButton1.getText().equals(jButton3.getText())){
winEffect(jButton1, jButton2, jButton3);
win=true;
}
if(jButton4.getText().equals("")&& jButton4.getText().equals(jButton5.getText())&& jButton4.getText().equals(jButton6.getText())){
winEffect(jButton4, jButton5, jButton6);
win=true;
}
if(jButton7.getText().equals("")&& jButton7.getText().equals(jButton8.getText())&& jButton7.getText().equals(jButton9.getText())){
winEffect(jButton7, jButton8, jButton9);
win=true;
}
if(jButton1.getText().equals("")&& jButton1.getText().equals(jButton4.getText())&& jButton1.getText().equals(jButton7.getText())){
winEffect(jButton1, jButton4, jButton7);
win=true;
}
if(jButton2.getText().equals("")&& jButton2.getText().equals(jButton5.getText())&& jButton2.getText().equals(jButton9.getText())){
winEffect(jButton2, jButton5, jButton8);
win=true;
}
if(jButton3.getText().equals("")&& jButton3.getText().equals(jButton6.getText())&& jButton3.getText().equals(jButton9.getText())){
winEffect(jButton3, jButton6, jButton9);
win=true;
}
if(jButton1.getText().equals("")&& jButton1.getText().equals(jButton5.getText())&& jButton1.getText().equals(jButton9.getText())){
winEffect(jButton1, jButton5, jButton9);
win=true;
}
if(jButton3.getText().equals("")&& jButton3.getText().equals(jButton5.getText())&& jButton3.getText().equals(jButton7.getText())){
winEffect(jButton3, jButton5, jButton7);
win=true;
}
else if(allButtonTextLegnth()==9 && win==false){
jLabelMSG.setText("no one wins");
}
}
public int allButtonTextLegnth(){
String txt ="";
Component[]comps= jPanel3.getComponents();
for(Component comp :comps){
if(comp instanceof JButton){
JButton button = (JButton)comp;
txt = txt +button.getText();
}
}
return txt.length();
}
int x_or_o =0;
public ActionListener createAction(JButton button){
ActionListener al = new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
if(button.getText().equals("")){
if((x_or_o %2)==0){
button.setText("x");
button.setForeground(Color.BLUE);
jLabelMSG.setText("o turn now");
getthewinner();
}else{
button.setText("o");
button.setForeground(Color.red);
jLabelMSG.setText("x turn now");
getthewinner();
}
x_or_o ++;
}
}
};
return al;
}
//function to add action to the buttons
public void addAction(){
Component[]comps= jPanel3.getComponents();
for(Component comp :comps){
if(comp instanceof JButton){
JButton button = (JButton)comp;
button.addActionListener(createAction(button));
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jPanel3 = new javax.swing.JPanel();
jLabelMSG = new javax.swing.JLabel();
jButtonReplay = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton4.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton5.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton6.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton7.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton8.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton9.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jPanel3.setBackground(new java.awt.Color(255, 51, 51));
jLabelMSG.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabelMSG.setForeground(new java.awt.Color(255, 255, 255));
jLabelMSG.setText("jLabel1");
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addGap(94, 94, 94)
.addComponent(jLabelMSG)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(jLabelMSG)
.addContainerGap(19, Short.MAX_VALUE))
);
jButtonReplay.setText("replay");
jButtonReplay.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonReplayActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(0, 80, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jButtonReplay, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton7, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
.addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
.addComponent(jButton8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(94, 94, 94))))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
.addComponent(jButtonReplay)
.addGap(24, 24, 24))
);
pack();
}// </editor-fold>
}
private void jButtonReplayActionPerformed(java.awt.event.ActionEvent evt) {
win =false;
Component[]comps= jPanel3.getComponents();
for(Component comp :comps){
if(comp instanceof JButton){
JButton button = (JButton)comp;
button.setText("");
button.setBackground(Color.WHITE);
jLabelMSG.setText("play");
}
}
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
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) {
java.util.logging.Logger.getLogger(TicTacToe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
`enter code here`java.util.logging.Logger.getLogger(TicTacToe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
`enter code here`java.util.logging.Logger.getLogger(TicTacToe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) { `enter code here`java.util.logging.Logger.getLogger(TicTacToe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TicTacToe().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JButton jButtonReplay;
private javax.swing.JLabel jLabelMSG;
private javax.swing.JPanel jPanel3;
// End of variables declaration
}
`
After your initComponents method, you have 2 closing } (after pack()).remove 1 of the brackets and add it after your addAction() method. this will solve your issue
Code with replaced class name:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import java.awt.Component;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author matt
*/
public class Test extends javax.swing.JFrame {
/**
* Creates new form TicTacToe
*/
public Test() {
initComponents();
addAction();
}
public void winEffect(JButton b1, JButton b2, JButton b3) {
b1.setBackground(Color.black);
b2.setBackground(Color.black);
b3.setBackground(Color.BLACK);
b1.setForeground(Color.WHITE);
b2.setForeground(Color.WHITE);
b3.setForeground(Color.WHITE);
String msg = b1.getText() + " Is the winner";
jLabelMSG.setText(msg);
}
boolean win = false;
public void getthewinner() {
if (jButton1.getText().equals("") && jButton1.getText().equals(jButton2.getText())
&& jButton1.getText().equals(jButton3.getText())) {
winEffect(jButton1, jButton2, jButton3);
win = true;
}
if (jButton4.getText().equals("") && jButton4.getText().equals(jButton5.getText())
&& jButton4.getText().equals(jButton6.getText())) {
winEffect(jButton4, jButton5, jButton6);
win = true;
}
if (jButton7.getText().equals("") && jButton7.getText().equals(jButton8.getText())
&& jButton7.getText().equals(jButton9.getText())) {
winEffect(jButton7, jButton8, jButton9);
win = true;
}
if (jButton1.getText().equals("") && jButton1.getText().equals(jButton4.getText())
&& jButton1.getText().equals(jButton7.getText())) {
winEffect(jButton1, jButton4, jButton7);
win = true;
}
if (jButton2.getText().equals("") && jButton2.getText().equals(jButton5.getText())
&& jButton2.getText().equals(jButton9.getText())) {
winEffect(jButton2, jButton5, jButton8);
win = true;
}
if (jButton3.getText().equals("") && jButton3.getText().equals(jButton6.getText())
&& jButton3.getText().equals(jButton9.getText())) {
winEffect(jButton3, jButton6, jButton9);
win = true;
}
if (jButton1.getText().equals("") && jButton1.getText().equals(jButton5.getText())
&& jButton1.getText().equals(jButton9.getText())) {
winEffect(jButton1, jButton5, jButton9);
win = true;
}
if (jButton3.getText().equals("") && jButton3.getText().equals(jButton5.getText())
&& jButton3.getText().equals(jButton7.getText())) {
winEffect(jButton3, jButton5, jButton7);
win = true;
}
else if (allButtonTextLegnth() == 9 && win == false) {
jLabelMSG.setText("no one wins");
}
}
public int allButtonTextLegnth() {
String txt = "";
Component[] comps = jPanel3.getComponents();
for (Component comp : comps) {
if (comp instanceof JButton) {
JButton button = (JButton) comp;
txt = txt + button.getText();
}
}
return txt.length();
}
int x_or_o = 0;
public ActionListener createAction(JButton button) {
ActionListener al = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (button.getText().equals("")) {
if ((x_or_o % 2) == 0) {
button.setText("x");
button.setForeground(Color.BLUE);
jLabelMSG.setText("o turn now");
getthewinner();
} else {
button.setText("o");
button.setForeground(Color.red);
jLabelMSG.setText("x turn now");
getthewinner();
}
x_or_o++;
}
}
};
return al;
}
// function to add action to the buttons
public void addAction() {
Component[] comps = jPanel3.getComponents();
for (Component comp : comps) {
if (comp instanceof JButton) {
JButton button = (JButton) comp;
button.addActionListener(createAction(button));
}
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jPanel3 = new javax.swing.JPanel();
jLabelMSG = new javax.swing.JLabel();
jButtonReplay = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton4.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton5.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton6.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton7.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton8.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton9.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jPanel3.setBackground(new java.awt.Color(255, 51, 51));
jLabelMSG.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabelMSG.setForeground(new java.awt.Color(255, 255, 255));
jLabelMSG.setText("jLabel1");
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup().addGap(94, 94, 94).addComponent(jLabelMSG)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup().addGap(19, 19, 19).addComponent(jLabelMSG)
.addContainerGap(19, Short.MAX_VALUE)));
jButtonReplay.setText("replay");
jButtonReplay.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonReplayActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout
.createSequentialGroup().addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout
.createSequentialGroup().addGap(0, 80, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jButtonReplay, javax.swing.GroupLayout.PREFERRED_SIZE, 250,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())))
.addGroup(layout.createSequentialGroup().addGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(jButton7, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
.addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 78,
Short.MAX_VALUE)
.addComponent(jButton8, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 74,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup().addGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING,
false)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE,
74, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE,
74, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(94, 94, 94)))))));
layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 74,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 74,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 74,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 63,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 63,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 63,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 64,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 64,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 64,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
.addComponent(jButtonReplay).addGap(24, 24, 24)));
pack();
}
private void jButtonReplayActionPerformed(java.awt.event.ActionEvent evt) {
win = false;
Component[] comps = jPanel3.getComponents();
for (Component comp : comps) {
if (comp instanceof JButton) {
JButton button = (JButton) comp;
button.setText("");
button.setBackground(Color.WHITE);
jLabelMSG.setText("play");
}
}
}
/**
* #param args
* the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
// <editor-fold defaultstate="collapsed" desc=" Look and feel setting code
// (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the default
* look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
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) {
java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
// </editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Test().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JButton jButtonReplay;
private javax.swing.JLabel jLabelMSG;
private javax.swing.JPanel jPanel3;
// End of variables declaration
}
Related
This question already exists:
JTextarea gives out weird text [closed]
Closed 29 days ago.
Hey I’m working on a Selling class of a simple Java Swing GUI supermarket project. When I add the first item to the billingtxt, it lists them normally as if there is nothing wrong! But if I add more items, it will bring out weird text as shown in the image.
First item works just fine but second will bring weird text
This is the text Im talking about:
My code is shared underneath. Any help would be greatly appreciated! Thanks in advance:)
package supermarket1;
import java.awt.Color;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import net.proteanit.sql.DbUtils;
/**
*
* #author patrickschnepf
*/
public class Selling1 extends javax.swing.JFrame {
/**
* Creates new form Selling1
*/
public Selling1() {
initComponents();
SelectSeller();
}
Connection Con = null;
Statement St = null;
ResultSet Rs = null;
public void SelectSeller()
{
try {
Con = DriverManager.getConnection("jdbc:derby://localhost:1527/SuperMarketdb1","User1","1234");
St = Con.createStatement();
Rs = St.executeQuery("Select * from User1.PRODUCTTBL");
ProductTable.setModel(DbUtils.resultSetToTableModel(Rs));
} catch (Exception e) {
e.printStackTrace();
}
}
int PrId, newQty;
public void update()
{
newQty = AvailQty - Integer.valueOf(ProdQty.getText());
try{
Con = DriverManager.getConnection("jdbc:derby://localhost:1527/SuperMarketdb1","User1","1234");
String Query ="Update User1.PRODUCTTBL set PRODQUANTITY="+newQty+""+" where PRODID="+PrId;
Statement Add = Con.createStatement();
Add.executeUpdate(Query);
SelectSeller();
}catch(SQLException e){
e.printStackTrace();
}
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel8 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
ProductTable = new javax.swing.JTable();
jLabel9 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jScrollPane2 = new javax.swing.JScrollPane();
BillTxt = new javax.swing.JTextArea();
AddBtn = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
ProdName = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
ProdQty = new javax.swing.JTextField();
ClearBtn = new javax.swing.JButton();
EditBtn = new javax.swing.JButton();
SummeLbl = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(0, 153, 0));
jLabel8.setFont(new java.awt.Font("Arial Black", 1, 20)); // NOI18N
jLabel8.setForeground(new java.awt.Color(255, 255, 255));
jLabel8.setText("X");
jLabel8.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jLabel8MouseClicked(evt);
}
public void mouseEntered(java.awt.event.MouseEvent evt) {
jLabel8MouseEntered(evt);
}
});
jPanel2.setBackground(new java.awt.Color(255, 255, 255));
ProductTable.setAutoCreateRowSorter(true);
ProductTable.setFont(new java.awt.Font("Arial Black", 1, 12)); // NOI18N
ProductTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"ProdID", "Name", "Menge", "Preis", "Kategorie"
}
));
ProductTable.setRowHeight(25);
ProductTable.setSelectionBackground(new java.awt.Color(255, 102, 0));
ProductTable.setSelectionForeground(new java.awt.Color(255, 255, 255));
ProductTable.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
ProductTableMouseClicked(evt);
}
});
jScrollPane1.setViewportView(ProductTable);
jLabel9.setFont(new java.awt.Font("Arial Black", 1, 14)); // NOI18N
jLabel9.setForeground(new java.awt.Color(255, 102, 0));
jLabel9.setText("Wählen sie die zu Verkaufende Produkte aus:");
jLabel3.setFont(new java.awt.Font("Arial Black", 1, 20)); // NOI18N
jLabel3.setForeground(new java.awt.Color(255, 102, 0));
jLabel3.setText("Produkt Liste");
BillTxt.setColumns(20);
BillTxt.setFont(new java.awt.Font("Arial Black", 0, 14)); // NOI18N
BillTxt.setRows(5);
jScrollPane2.setViewportView(BillTxt);
AddBtn.setFont(new java.awt.Font("Arial Black", 1, 12)); // NOI18N
AddBtn.setForeground(new java.awt.Color(255, 102, 0));
AddBtn.setText("Zur Rechnung hinzufügen");
AddBtn.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
AddBtnMouseClicked(evt);
}
});
jLabel4.setFont(new java.awt.Font("Arial Black", 1, 14)); // NOI18N
jLabel4.setForeground(new java.awt.Color(255, 102, 0));
jLabel4.setText("Name:");
ProdName.setFont(new java.awt.Font("Arial Black", 1, 12)); // NOI18N
ProdName.setForeground(new java.awt.Color(255, 102, 0));
jLabel5.setFont(new java.awt.Font("Arial Black", 1, 14)); // NOI18N
jLabel5.setForeground(new java.awt.Color(255, 102, 0));
jLabel5.setText("Menge:");
ProdQty.setFont(new java.awt.Font("Arial Black", 1, 12)); // NOI18N
ProdQty.setForeground(new java.awt.Color(255, 102, 0));
ClearBtn.setFont(new java.awt.Font("Arial Black", 1, 12)); // NOI18N
ClearBtn.setForeground(new java.awt.Color(255, 102, 0));
ClearBtn.setText("Zurücksetzen");
ClearBtn.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
ClearBtnMouseClicked(evt);
}
});
ClearBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ClearBtnActionPerformed(evt);
}
});
EditBtn.setFont(new java.awt.Font("Arial Black", 1, 12)); // NOI18N
EditBtn.setForeground(new java.awt.Color(255, 102, 0));
EditBtn.setText("Drucken");
EditBtn.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
EditBtnMouseClicked(evt);
}
});
SummeLbl.setFont(new java.awt.Font("Arial Black", 1, 14)); // NOI18N
SummeLbl.setForeground(new java.awt.Color(255, 102, 0));
SummeLbl.setText("Summe: ");
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(244, 244, 244)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel5)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(ProdQty, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(ProdName, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(150, 150, 150)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 396, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 38, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addComponent(jLabel9)
.addGap(68, 68, 68))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addComponent(AddBtn)
.addGap(150, 150, 150)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(151, 151, 151))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 396, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addComponent(SummeLbl)
.addGap(159, 159, 159)))
.addGap(38, 38, 38))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
.addGap(85, 85, 85)
.addComponent(EditBtn)
.addGap(63, 63, 63)
.addComponent(ClearBtn)
.addContainerGap())))))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(jLabel3)
.addGap(18, 18, 18)
.addComponent(jLabel9)
.addGap(15, 15, 15)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(ProdName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4))
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(ProdQty, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5))))
.addGap(17, 17, 17)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(AddBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(SummeLbl)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(EditBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(ClearBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)))
);
jLabel7.setFont(new java.awt.Font("Arial Black", 1, 20)); // NOI18N
jLabel7.setForeground(new java.awt.Color(255, 255, 255));
jLabel7.setText("Logout");
jLabel7.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jLabel7MouseClicked(evt);
}
});
jLabel1.setFont(new java.awt.Font("Arial Black", 1, 20)); // NOI18N
jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.setText("Kasse");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1)
.addGap(342, 342, 342)
.addComponent(jLabel8)
.addContainerGap())
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(26, 26, 26)
.addComponent(jLabel7)
.addGap(18, 18, 18)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel7)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel8)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(22, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
Double Uprice, ProdTot=0.0,GrdTot=0.0;
int AvailQty;
private void ProductTableMouseClicked(java.awt.event.MouseEvent evt) {
DefaultTableModel model = (DefaultTableModel)ProductTable.getModel();
int MyIndex = ProductTable.getSelectedRow();
PrId = Integer.valueOf(model.getValueAt(MyIndex, 0).toString());
AvailQty = Integer.valueOf(model.getValueAt(MyIndex, 2).toString());
ProdName.setText(model.getValueAt(MyIndex, 1).toString());
Uprice = Double.valueOf(model.getValueAt(MyIndex, 3).toString());
}
int i = 0;
private void AddBtnMouseClicked(java.awt.event.MouseEvent evt) {
if(ProdQty.getText().isEmpty() || ProdName.getText().isEmpty() )
{
JOptionPane.showMessageDialog(this, "Fehlende Informationen");
}
else if(AvailQty <= Integer.valueOf(ProdQty.getText()))
{
JOptionPane.showMessageDialog(this, "Nicht genug auf Lager");
}
else{
i++;
ProdTot = Uprice * Double.valueOf(ProdQty.getText());
GrdTot = GrdTot+ProdTot;
if( i == 1)
{
BillTxt.setText(BillTxt.getText()+"============Tante Emma Laden============\n"+"Nummer Produkt Preis Menge Summe\n"+i+" "+ProdName.getText()+" "+Uprice+" "+ProdQty.getText()+" "+ProdTot+"\n\t");
}else
{
BillTxt.setText(BillTxt.getText()+" "+i+" "+ProdName+" "+Uprice+" "+ProdQty.getText()+" "+ProdTot+"\n");
}
SummeLbl.setText("Zu zahlen: "+GrdTot + " €");
update();
}
}
private void ClearBtnMouseClicked(java.awt.event.MouseEvent evt) {
ProdName.setText("");
BillTxt.setText("");
ProdQty.setText("");
SummeLbl.setText("Summe: ");
}
private void ClearBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void EditBtnMouseClicked(java.awt.event.MouseEvent evt) {
try{
BillTxt.print();
}catch (Exception e)
{
}
}
private void jLabel8MouseClicked(java.awt.event.MouseEvent evt) {
System.exit(0);
}
private void jLabel8MouseEntered(java.awt.event.MouseEvent evt) {
jLabel8.setForeground(Color.red);
}
private void jLabel7MouseClicked(java.awt.event.MouseEvent evt) {
new Login().setVisible(true);
this.dispose();
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
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) {
java.util.logging.Logger.getLogger(Selling1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Selling1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Selling1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Selling1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Selling1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton AddBtn;
private javax.swing.JTextArea BillTxt;
private javax.swing.JButton ClearBtn;
private javax.swing.JButton EditBtn;
private javax.swing.JTextField ProdName;
private javax.swing.JTextField ProdQty;
private javax.swing.JTable ProductTable;
private javax.swing.JLabel SummeLbl;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
// End of variables declaration
}
Ive tried looking for errors but there were non and
You have an object named ProdName that is lying; it's not a name at all, it's a JTextField object. It isn't text, it's a field that can contain text. That field should be named prodNameField for some example (in java we writeVariableNamesLikeThis and TypesLikeThis. You're signing yourself up for a life of annoyance if you don't adjust your style, all java code is written like that).
In your code you have this line:
BillTxt.setText(BillTxt.getText()+" "+i+" "+ProdName+" "+Uprice+" "+ProdQty.getText()+" "+ProdTot+"\n");
This calls toString() on ProdName and concatenates that to the rest of the strings in this statement. The toString of a JTextField is the 'strange text' you see. You want to [A] make that ProdName.getText(), and [B] you want to adjust all your variable names because the problem with them is that they are confusing, and this question proves that it has confused you. prodQtyField, prodNameField, etcetera.
Im using the Netbeans Swing Gui builder.
I create a simple menu that I want to appear/disappear when i press a hotkey.
Here is the code for the menu:
package overlay;
import java.util.Properties;
import javax.swing.JOptionPane;
import static overlay.Overlay.flags;
public class Menu extends javax.swing.JFrame {
static boolean isVisible = true;
/**
* Creates new form Menu
*/
public Menu() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jSeparator4 = new javax.swing.JSeparator();
jPanel1 = new javax.swing.JPanel();
jLabel3 = new javax.swing.JLabel();
jSeparator1 = new javax.swing.JSeparator();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jSeparator2 = new javax.swing.JSeparator();
jSeparator3 = new javax.swing.JSeparator();
jSeparator5 = new javax.swing.JSeparator();
jCheckBox1 = new javax.swing.JCheckBox();
jCheckBox2 = new javax.swing.JCheckBox();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setUndecorated(true);
setOpacity(0.9F);
setResizable(false);
setType(java.awt.Window.Type.POPUP);
jLabel3.setFont(new java.awt.Font("Arial", 1, 24)); // NOI18N
jLabel3.setText("Foo");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGap(0, 11, Short.MAX_VALUE)
.addComponent(jLabel3))
);
jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);
jLabel1.setFont(new java.awt.Font("Arial", 1, 24)); // NOI18N
jLabel1.setText("Foo");
jLabel2.setFont(new java.awt.Font("Arial", 1, 24)); // NOI18N
jLabel2.setText("Foo");
jSeparator3.setOrientation(javax.swing.SwingConstants.VERTICAL);
jCheckBox1.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
jCheckBox1.setText("Foo");
jCheckBox1.setActionCommand("JFoo");
jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jCheckBox1ActionPerformed(evt);
}
});
jCheckBox2.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
jCheckBox2.setText("Foo");
jCheckBox2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jCheckBox2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jCheckBox2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 280, Short.MAX_VALUE)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jCheckBox1))
.addGap(261, 261, 261))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jSeparator2)
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jSeparator5)
.addContainerGap())))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSeparator5, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(2, 2, 2)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jCheckBox1)
.addComponent(jCheckBox2))
.addGap(0, 104, Short.MAX_VALUE))
.addComponent(jSeparator1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
jCheckBox1.getAccessibleContext().setAccessibleName("JFooB");
pack();
}// </editor-fold>
private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here: Foo
Overlay.Foo = !Overlay.Foo;
}
private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here: //Foo1
Overlay.Foo1 = !Overlay.Foo1;
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
Properties props = new Properties();
props.put("windowDecoration", "off");
props.put("textAntiAliasing", "on");
props.put("backgroundPattern", "off");
props.put("backgroundColor", "60 63 65");
props.put("windowBorderColor", "255 255 255");
// props.put("selectionForegroundColor", "255 255 255");
// props.put("selectedTexture", "255 255 255");
props.put("focusColor", "60 63 65");
com.jtattoo.plaf.hifi.HiFiLookAndFeel.setCurrentTheme(props);
javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.hifi.HiFiLookAndFeel");
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Menu().setVisible(true);
}
});
}
public void toggleMenu() {
isVisible = !isVisible;
if (isVisible) {
setVisible(true);
//JOptionPane.showMessageDialog(null, String.valueOf(isVisible), String.valueOf(isVisible), JOptionPane.INFORMATION_MESSAGE);
} else {
//JOptionPane.showMessageDialog(null, String.valueOf(isVisible), String.valueOf(isVisible), JOptionPane.INFORMATION_MESSAGE);
setVisible(false);
dispose();
}
}
// Variables declaration - do not modify
private javax.swing.JCheckBox jCheckBox1;
private javax.swing.JCheckBox jCheckBox2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JSeparator jSeparator3;
private javax.swing.JSeparator jSeparator4;
private javax.swing.JSeparator jSeparator5;
// End of variables declaration
}
What I'm doing is calling the toggleMenu() method from my Main class(named Overlay) upon a key press, and it flips the isVisible boolean. Thing is, when I press the button, it doesnt do anything. Then when I press it again, it creates a new Window with a different look and feel(not the one i specified initially).
I also tried to use the hide(); and show(); methods, but without success.
So what am I doing wrong? I am not really familiar with how Netbeans handles gui creation, although i think this should work.
Fixed, in the run method, change this:
public void run() {
new Menu().setVisible(true);
}
to this:
public void run() {
m = new Menu();
m.setVisible(true);
}
(Create a static Menu object called m, and then in the toggleMenu() method, use
m.setVisible(true);
or false. Closed.
I want to create an empty .ser file to store an arraylist of objects if not created yet, to use later in program .I will add if not exist check later after figuring out my current problem.I am having a nullpointer exception on my File = new File(etc etc) row.Here is a better explanation with the screenshot.
edit:Added source codes
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package dark.rusty;
import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
/**
*
* #author UoD
*/
public class Gui extends javax.swing.JFrame {
/**
* Creates new form Gui
*/
public Gui() {
initComponents();
setLocationRelativeTo(null);
setTitle("CMS - Dark&Rusty");
this.getContentPane().setBackground(new Color(190, 190, 190));
MenuItem a = new MenuItem("temp", 0, 0);
ArrayList<MenuItem> tempAList = new ArrayList<MenuItem>();
createFileIFN();
}
private void createFileIFN() {
try {
File myFile = new File((this.getClass().getResource("/text/menu.ser").toString());
if (myFile.createNewFile()) {
System.out.println("File is created!");
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
private ArrayList<MenuItem> getArrList(File tempFile) {
ArrayList<MenuItem> items = new ArrayList<MenuItem>();
MenuItem tempItem;
try {
FileInputStream fileIn = new FileInputStream(tempFile);
ObjectInputStream in = new ObjectInputStream(fileIn);
tempItem = (MenuItem) in.readObject();
in.close();
fileIn.close();
} catch (IOException i) {
i.printStackTrace();
} catch (ClassNotFoundException c) {
System.out.println("Employee class not found");
c.printStackTrace();
}
return items;
}
private void writeItemtoFile(MenuItem a, File tempFile) {
try {
FileOutputStream fos = new FileOutputStream(tempFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
// write object to file
oos.writeObject(a);
// closing resources
oos.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();
jScrollPane2 = new javax.swing.JScrollPane();
jList2 = new javax.swing.JList<>();
jButton6 = new javax.swing.JButton();
jPanel3 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jButton9 = new javax.swing.JButton();
jButton10 = new javax.swing.JButton();
jScrollPane3 = new javax.swing.JScrollPane();
jList3 = new javax.swing.JList<>();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(190, 190, 190));
setResizable(false);
setSize(new java.awt.Dimension(800, 600));
jPanel1.setBackground(new java.awt.Color(190, 190, 190));
jPanel1.setPreferredSize(new java.awt.Dimension(800, 50));
jButton7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/if_Sidebar_Left_Interface_Menu_Window_1654369.png"))); // NOI18N
jButton7.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton7.setMargin(new java.awt.Insets(2, 20, 2, 20));
jButton7.setPreferredSize(new java.awt.Dimension(32, 32));
jPanel1.add(jButton7);
jButton8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/if_TaskStroke_1737368.png"))); // NOI18N
jButton8.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton8.setMargin(new java.awt.Insets(2, 20, 2, 20));
jButton8.setPreferredSize(new java.awt.Dimension(32, 32));
jPanel1.add(jButton8);
jPanel2.setBackground(new java.awt.Color(190, 190, 190));
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 800, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
jScrollPane2.setPreferredSize(new java.awt.Dimension(300, 120));
jList2.setBackground(new java.awt.Color(190, 190, 190));
jList2.setBorder(javax.swing.BorderFactory.createMatteBorder(2, 2, 2, 2, new java.awt.Color(240, 240, 240)));
jList2.setFont(new java.awt.Font("Rockwell", 1, 14)); // NOI18N
jList2.setModel(new javax.swing.AbstractListModel<String>() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
public int getSize() { return strings.length; }
public String getElementAt(int i) { return strings[i]; }
});
jList2.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
jList2.setPreferredSize(new java.awt.Dimension(290, 80));
jScrollPane2.setViewportView(jList2);
jButton6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/if_Tick_1737369 (1).png"))); // NOI18N
jButton6.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton6.setPreferredSize(new java.awt.Dimension(32, 32));
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});
jPanel3.setBackground(new java.awt.Color(190, 190, 190));
jPanel3.setPreferredSize(new java.awt.Dimension(290, 136));
jLabel1.setFont(new java.awt.Font("Poiret One", 1, 24)); // NOI18N
jLabel1.setForeground(new java.awt.Color(51, 102, 0));
jLabel1.setText("Kalan:");
jLabel1.setPreferredSize(new java.awt.Dimension(120, 30));
jLabel2.setFont(new java.awt.Font("Poiret One", 1, 24)); // NOI18N
jLabel2.setForeground(new java.awt.Color(204, 0, 51));
jLabel2.setText("Odenen:");
jLabel2.setPreferredSize(new java.awt.Dimension(120, 30));
jLabel3.setFont(new java.awt.Font("Poiret One", 1, 24)); // NOI18N
jLabel3.setForeground(new java.awt.Color(0, 153, 255));
jLabel3.setText("0");
jLabel3.setToolTipText("");
jLabel3.setPreferredSize(new java.awt.Dimension(120, 30));
jLabel4.setFont(new java.awt.Font("Poiret One", 1, 24)); // NOI18N
jLabel4.setForeground(new java.awt.Color(51, 102, 0));
jLabel4.setText("5.00");
jLabel4.setPreferredSize(new java.awt.Dimension(120, 30));
jLabel5.setFont(new java.awt.Font("Poiret One", 1, 24)); // NOI18N
jLabel5.setForeground(new java.awt.Color(204, 0, 51));
jLabel5.setText("13.00");
jLabel5.setPreferredSize(new java.awt.Dimension(120, 30));
jLabel6.setFont(new java.awt.Font("Poiret One", 1, 24)); // NOI18N
jLabel6.setForeground(new java.awt.Color(0, 153, 255));
jLabel6.setText("İptal:");
jLabel6.setPreferredSize(new java.awt.Dimension(120, 30));
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel3Layout.createSequentialGroup()
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
jButton9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/if_Dollar_1737376(1).png"))); // NOI18N
jButton9.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton9.setPreferredSize(new java.awt.Dimension(32, 32));
jButton10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/if_Wrong_1737371.png"))); // NOI18N
jButton10.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton10.setPreferredSize(new java.awt.Dimension(32, 32));
jScrollPane3.setPreferredSize(new java.awt.Dimension(300, 120));
jList3.setBackground(new java.awt.Color(190, 190, 190));
jList3.setBorder(javax.swing.BorderFactory.createMatteBorder(2, 2, 2, 2, new java.awt.Color(240, 240, 240)));
jList3.setFont(new java.awt.Font("Rockwell", 1, 14)); // NOI18N
jList3.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
jList3.setPreferredSize(new java.awt.Dimension(290, 80));
jScrollPane3.setViewportView(jList3);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGap(6, 6, 6)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(10, 10, 10)))
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 182, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
);
pack();
}// </editor-fold>
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
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) {
java.util.logging.Logger.getLogger(Gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Gui().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JList<String> jList2;
private javax.swing.JList<String> jList3;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
// End of variables declaration
}
this.getClass().getResource("/text/menu.ser")
will return null if "/text/menu.ser" does not exists. Consequently,
this.getClass().getResource("/text/menu.ser").toString()
will result in a NullPointerException.
Moreover, getResource returns an URL instance and an URL's string reprensentation (e.g. file:/home/user3524633/text/menu.ser) is not the same as a local file system path (which is what you want to use with the File constructor).
Why are you using getResource in the first place? Are you trying to determine a path relative to your application's classes?
This is what the textfield looks like before clicking the check button:
This is what happens after clicking check. This only happens with a string that is longer than the textfield.
This is the code from netbeans:
package filevalidator;
import java.awt.Color;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
/**
*
* #author Kosar
*/
public class Validator extends javax.swing.JFrame {
static String path1 = null;
static String path2 = null;
/**
* Creates new form Validator
*/
public Validator() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
file1path = new javax.swing.JTextField();
select1 = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
file2path = new javax.swing.JTextField();
select2 = new javax.swing.JButton();
hash1 = new javax.swing.JLabel();
hash2 = new javax.swing.JLabel();
result = new javax.swing.JLabel();
checkBtn = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
jLabel1.setFont(new java.awt.Font("Trebuchet MS", 0, 18)); // NOI18N
jLabel1.setText("Original File:");
file1path.setFont(new java.awt.Font("Trebuchet MS", 0, 14)); // NOI18N
file1path.setAutoscrolls(false);
select1.setText("Select");
select1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
select1ActionPerformed(evt);
}
});
jLabel2.setFont(new java.awt.Font("Trebuchet MS", 0, 18)); // NOI18N
jLabel2.setText("Second File:");
file2path.setFont(new java.awt.Font("Trebuchet MS", 0, 14)); // NOI18N
file2path.setAutoscrolls(false);
file2path.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
select2.setText("Select");
select2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
select2ActionPerformed(evt);
}
});
hash1.setFont(new java.awt.Font("Trebuchet MS", 1, 20)); // NOI18N
hash1.setForeground(java.awt.Color.blue);
hash1.setText(" ");
hash2.setFont(new java.awt.Font("Trebuchet MS", 1, 20)); // NOI18N
hash2.setForeground(java.awt.Color.blue);
hash2.setText(" ");
result.setFont(new java.awt.Font("Trebuchet MS", 1, 20)); // NOI18N
result.setText(" ");
checkBtn.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
checkBtn.setText("Check");
checkBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
checkBtnActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addGap(32, 32, 32)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(result, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(151, 151, 151)
.addComponent(hash2, javax.swing.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE))
.addComponent(hash1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(30, 30, 30))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(file1path, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(file2path))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(select1, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(select2, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(checkBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(331, 331, 331))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(59, 59, 59)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(file1path, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(select1))
.addGap(27, 27, 27)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(file2path, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(select2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
.addComponent(hash1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(hash2)
.addGap(13, 13, 13)
.addComponent(result)
.addGap(5, 5, 5)
.addComponent(checkBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(27, 27, 27)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(27, 27, 27))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(34, 34, 34)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>
private void select1ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Select file");
if (chooser.showOpenDialog(select1) == JFileChooser.APPROVE_OPTION) {
path1 = chooser.getSelectedFile().getAbsolutePath();
}
file1path.setText(path1);
file1path.setBackground(Color.white);
}
private void select2ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Select file");
if (chooser.showOpenDialog(select2) == JFileChooser.APPROVE_OPTION) {
path2 = chooser.getSelectedFile().getAbsolutePath();
}
file2path.setText(path2);
file2path.setBackground(Color.white);
}
private void checkBtnActionPerformed(java.awt.event.ActionEvent evt) {
try {
HashMD5 hasher = new HashMD5();
try {
hash1.setText(hasher.hash(path1));
hash2.setText(hasher.hash(path2));
} catch (NoSuchAlgorithmException | IOException ex) {
}
if ( hash1.getText().equals(hash2.getText())) {
result.setForeground(Color.GREEN);
result.setText("Hashes are the same. File has not been modified.");
}
else {
result.setForeground(Color.RED);
result.setText("Hashes are not the same. File has been modified.");
}
} catch (NullPointerException e) {
if (file1path.getText() == null) {
file1path.setBackground(Color.red);
}
else if (file2path.getText() == null) {
file2path.setBackground(Color.red);
}
else {
file1path.setBackground(Color.red);
file2path.setBackground(Color.red);
}
}
}
/**
* #param args the command line arguments
*/
// Variables declaration - do not modify
private javax.swing.JButton checkBtn;
private javax.swing.JTextField file1path;
private javax.swing.JTextField file2path;
private javax.swing.JLabel hash1;
private javax.swing.JLabel hash2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JLabel result;
private javax.swing.JButton select1;
private javax.swing.JButton select2;
// End of variables declaration
}
You can handle it by using TextField and its method to set columns i.e. "setColumns(int numOfCols)"
import java.awt.FlowLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class TextFieldExample extends JFrame {
JFrame frame = new JFrame();
public TextFieldExample()
{
frame = new JFrame("Test");
Container container = frame.getContentPane();
JTextField fld1 = new JTextField("Java C++ C");
JTextField fld2 = new JTextField("");
JTextField fld3 = new JTextField("");
fld1.setColumns(5); //Now, it will not be resized.
fld2.setColumns(7); //Now, it will not be resized.
fld3.setColumns(9); //Now, it will not be resized.
container.setLayout(new FlowLayout());
container.add(fld1);
container.add(fld2);
container.add(fld3);
frame.setVisible(true);
frame.setSize(150,150);
}
public static void main(String[] args) {
TextFieldExample obj = new TextFieldExample();
}
}
This is not a complete program but im wondering why it freezez when i press the "JButton". I am Currently working on a dice game and so far its going fine but i cant understand why it wont run at the moment. Thanks for the help in beforehand :)
PS. The problem started after i added the while loop in the method diceLogic.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package dicegame_leonardasp;
/**
*
* #author LeoAsp
*/
import java.awt.Dimension;
import java.awt.Image;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.util.Random;
import javafx.scene.control.Cell;
public class DiceGameGui extends javax.swing.JFrame {
//Instansvariabel
//Skapa instans av Randomklassen
Random rand = new Random();
public DiceGameGui() {
initComponents();
}
public String doMessage() {
String message = "";
return message;
}
public String doReDice() {
String image = "";
int randNum = rand.nextInt(6 - 1) + 1;
if (randNum == 1) {
image = "one.gif";
} else if (randNum == 2) {
image = "two.gif";
} else if (randNum == 3) {
image = "three.gif";
} else if (randNum == 4) {
image = "four.gif";
} else if (randNum == 5) {
image = "five.gif";
} else if (randNum == 6) {
image = "six.gif";
} else {
dispose();
}
return image;
}
public void diceLogic(int players, int dice) {
jDiceResult.removeAll();
jDiceResult.setLayout(new java.awt.GridLayout(6, 5));
JLabel playersArray[][] = new JLabel[players][dice];
int j;
int i;
for (i = 0; i <= players; i++) {
for (j = 0; j < dice; j++) {
playersArray[i][j] = new JLabel();
playersArray[i][j].setIcon(new ImageIcon(doReDice()));
playersArray[i][j].setPreferredSize(new Dimension(60, 50));
// playersArray[i][j].setPreferredSize(new Dimension(20, 20));
//playersArray[i][j].setMinimumSize(new Dimension(20, 20));
//playersArray[i][j].setMaximumSize(new Dimension(20, 20));
jDiceResult.add(playersArray[i][j]);
jDiceResult.revalidate();
jDiceResult.repaint();
}
while (j <= 6) {
jDiceResult.add(javax.swing.Box.createGlue());
}
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#
SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jBoxPlayers = new javax.swing.JComboBox();
jBoxDice = new javax.swing.JComboBox();
jBtnRoll = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton1 = new javax.swing.JButton();
jDiceResult = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Menlo", 0, 18)); // NOI18N
jLabel1.setText("Players:");
jLabel2.setFont(new java.awt.Font("Menlo", 0, 18)); // NOI18N
jLabel2.setText("Dice:");
jLabel3.setFont(new java.awt.Font("Menlo", 0, 18)); // NOI18N
jBoxPlayers.setFont(new java.awt.Font("Menlo", 1, 14)); // NOI18N
jBoxPlayers.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
"1", "2", "3", "4", "5"
}));
jBoxPlayers.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBoxPlayersActionPerformed(evt);
}
});
jBoxDice.setFont(new java.awt.Font("Menlo", 1, 14)); // NOI18N
jBoxDice.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
"1", "2", "3", "4", "5", "6"
}));
jBoxDice.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBoxDiceActionPerformed(evt);
}
});
jBtnRoll.setFont(new java.awt.Font("Menlo", 0, 18)); // NOI18N
jBtnRoll.setText("Roll the Dice");
jBtnRoll.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBtnRollActionPerformed(evt);
}
});
jTextArea1.setEditable(false);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jButton1.setFont(new java.awt.Font("Menlo", 0, 14)); // NOI18N
jButton1.setText("Show Highscore");
jDiceResult.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
jDiceResult.setPreferredSize(new java.awt.Dimension(250, 300));
javax.swing.GroupLayout jDiceResultLayout = new javax.swing.GroupLayout(jDiceResult);
jDiceResult.setLayout(jDiceResultLayout);
jDiceResultLayout.setHorizontalGroup(
jDiceResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 246, Short.MAX_VALUE)
);
jDiceResultLayout.setVerticalGroup(
jDiceResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 296, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jBoxPlayers, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(91, 91, 91)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel3))
.addComponent(jBoxDice, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jBtnRoll, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jDiceResult, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jBoxPlayers, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jBoxDice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jBtnRoll, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 347, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jDiceResult, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
} // </editor-fold>
private void jBoxPlayersActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jBoxDiceActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jBtnRollActionPerformed(java.awt.event.ActionEvent evt) {
int dice = Integer.parseInt(jBoxDice.getSelectedItem().toString());
int players = Integer.parseInt(jBoxPlayers.getSelectedItem().toString());
diceLogic(players, dice);
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
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) {
java.util.logging.Logger.getLogger(DiceGameGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(DiceGameGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(DiceGameGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(DiceGameGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DiceGameGui().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JComboBox jBoxDice;
private javax.swing.JComboBox jBoxPlayers;
private javax.swing.JButton jBtnRoll;
private javax.swing.JButton jButton1;
private javax.swing.JPanel jDiceResult;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
}
This is the problem:
while (j <= 6) {
jDiceResult.add(javax.swing.Box.createGlue());
}
With this line, you create a infinite loop, because you check whether j<=6 but don't change the value of j, so j<=6 is always true.
I guess you want to change while to if.
I guess:
while (j <= 6) {
jDiceResult.add(javax.swing.Box.createGlue());
}
I think may be it should be:
if (j <= 6) {
jDiceResult.add(javax.swing.Box.createGlue());
}