I am creating a final project for my computer programming course, and have run into a strange problem that I'm hoping someone could possibly help me with. I am attempting to make a blackjack program which will run between two computers, but am having trouble getting the text of my labels to change beyond what they are initially set to. To make this project I am using the eclipse compiler with the window builder addon.
The problem itself lies within the label "lblEnterTextHere", who's text will not change when i try to do so in the rungame method. The random System.out.print messages were to check if the code even reaches the point where this method is run, and it does. "Waiting for connection" is printed.
Note: I'm new to java, and this is merely a grade 12 course, so please explain your answer with clarity. It would help a lot. Also This isn't even a game yet, just the skeleton of what I'm trying to do.
My Code:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.io.*;
import java.net.*;
public class Window {
ServerSocket providerSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
private JFrame frame;
JLabel label_5;
JButton btnNewButton;
JLabel label;
JLabel label_1;
JLabel label_2;
JLabel label_3;
JLabel label_4;
JLabel label_6;
JLabel label_7;
JLabel label_8;
JLabel label_9;
JLabel lblPlayerOneyou;
JLabel lblPlayerother;
JLabel lblNewLabel;
JLabel lblScore;
JLabel lblNewLabel_1;
JLabel lblNewLabel_2;
JButton btnNewButton_1;
JLabel lblStatus;
JLabel lblEnterTextHere;
public static void main(String[] args) {
Window window = new Window();
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Window window2 = new Window();
window2.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
System.out.print("Hello.");
window.runGame();
}
public Window() {
frame = new JFrame();
frame.setBounds(100, 100, 792, 536);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
label_5 = new JLabel("New label");
label_5.setEnabled(false);
label_5.setIcon(new ImageIcon("C:\\Users\\Alexander Wong\\Documents\\Cards\\cardBack.jpg"));
label_5.setBounds(36, 74, 110, 145);
frame.getContentPane().add(label_5);
btnNewButton = new JButton("First Draw");
btnNewButton.setEnabled(false);
btnNewButton.setBounds(661, 125, 89, 94);
frame.getContentPane().add(btnNewButton);
label = new JLabel("New label");
label.setEnabled(false);
label.setIcon(new ImageIcon("C:\\Users\\Alexander Wong\\Documents\\Cards\\cardBack.jpg"));
label.setBounds(156, 74, 110, 145);
frame.getContentPane().add(label);
label_1 = new JLabel("New label");
label_1.setEnabled(false);
label_1.setIcon(new ImageIcon("C:\\Users\\Alexander Wong\\Documents\\Cards\\cardBack.jpg"));
label_1.setBounds(276, 74, 110, 145);
frame.getContentPane().add(label_1);
label_2 = new JLabel("New label");
label_2.setEnabled(false);
label_2.setIcon(new ImageIcon("C:\\Users\\Alexander Wong\\Documents\\Cards\\cardBack.jpg"));
label_2.setBounds(393, 74, 110, 145);
frame.getContentPane().add(label_2);
label_3 = new JLabel("New label");
label_3.setEnabled(false);
label_3.setIcon(new ImageIcon("C:\\Users\\Alexander Wong\\Documents\\Cards\\cardBack.jpg"));
label_3.setBounds(516, 74, 110, 145);
frame.getContentPane().add(label_3);
label_4 = new JLabel("New label");
label_4.setEnabled(false);
label_4.setIcon(new ImageIcon("C:\\Users\\Alexander Wong\\Documents\\Cards\\cardBack.jpg"));
label_4.setBounds(36, 267, 110, 145);
frame.getContentPane().add(label_4);
label_6 = new JLabel("New label");
label_6.setEnabled(false);
label_6.setIcon(new ImageIcon("C:\\Users\\Alexander Wong\\Documents\\Cards\\cardBack.jpg"));
label_6.setBounds(156, 267, 110, 145);
frame.getContentPane().add(label_6);
label_7 = new JLabel("New label");
label_7.setEnabled(false);
label_7.setIcon(new ImageIcon("C:\\Users\\Alexander Wong\\Documents\\Cards\\cardBack.jpg"));
label_7.setBounds(270, 267, 110, 145);
frame.getContentPane().add(label_7);
label_8 = new JLabel("New label");
label_8.setEnabled(false);
label_8.setIcon(new ImageIcon("C:\\Users\\Alexander Wong\\Documents\\Cards\\cardBack.jpg"));
label_8.setBounds(393, 267, 110, 145);
frame.getContentPane().add(label_8);
label_9 = new JLabel("New label");
label_9.setEnabled(false);
label_9.setIcon(new ImageIcon("C:\\Users\\Alexander Wong\\Documents\\Cards\\cardBack.jpg"));
label_9.setBounds(516, 267, 110, 145);
frame.getContentPane().add(label_9);
lblPlayerOneyou = new JLabel("Player one (You)");
lblPlayerOneyou.setBounds(46, 26, 100, 24);
frame.getContentPane().add(lblPlayerOneyou);
lblPlayerother = new JLabel("Player 2 (Other player)");
lblPlayerother.setBounds(49, 241, 123, 14);
frame.getContentPane().add(lblPlayerother);
lblNewLabel = new JLabel("Score:");
lblNewLabel.setBounds(170, 31, 46, 14);
frame.getContentPane().add(lblNewLabel);
lblScore = new JLabel("Score:");
lblScore.setBounds(170, 242, 46, 14);
frame.getContentPane().add(lblScore);
lblNewLabel_1 = new JLabel("0");
lblNewLabel_1.setBounds(220, 31, 46, 14);
frame.getContentPane().add(lblNewLabel_1);
lblNewLabel_2 = new JLabel("??");
lblNewLabel_2.setBounds(220, 241, 46, 14);
frame.getContentPane().add(lblNewLabel_2);
btnNewButton_1 = new JButton("Quit");
btnNewButton_1.setEnabled(false);
btnNewButton_1.setBounds(661, 261, 89, 87);
frame.getContentPane().add(btnNewButton_1);
lblStatus = new JLabel("Status:");
lblStatus.setBounds(23, 458, 46, 14);
frame.getContentPane().add(lblStatus);
lblEnterTextHere = new JLabel("Enter text here");
lblEnterTextHere.setBounds(63, 458, 109, 14);
frame.getContentPane().add(lblEnterTextHere);
}
public void runGame() {
try {
System.out.println("Hello.");
providerSocket = new ServerSocket(5131, 10);
lblEnterTextHere.setText("Waiting for connection");
System.out.println("Waiting for connection");
lblStatus.setText("me");
connection = providerSocket.accept();
lblEnterTextHere.setText("Connection received from " + connection.getInetAddress().getHostName());
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());
out.writeInt(-1);
out.flush();
System.out.println("All systems go.");
try {
in.close();
out.close();
providerSocket.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
} catch (IOException ioException){
ioException.printStackTrace();
}
}
}
You are blocking the EDT by placing ServerSocket functionality in that Thread. Place the functionality found in runGame in a SwingWorker and the UI will be free to update its components.
As you are don't require any return information you can use SwingWorker<Void, Void>
See this related post.
Related
I want to add a dynamic checkbox after selecting an item in combo box.
I am working on eclipse and design my frame on window builder.
final JComboBox<String> comboBox = new JComboBox<String>();
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String typeName = comboBox.getSelectedItem().toString();
for (int i = 0; i < SqlQuery.getCoursesName(typeName).size(); i++) {
JCheckBox c = new JCheckBox(SqlQuery.getCoursesName(typeName).get(i));
c.setVisible(true);
coursePanel.add(c);
frame.repaint();
frame.validate();
System.out.println(c.getText());
}
}
});
comboBox.setBounds(208, 221, 91, 20);
frame.getContentPane().add(comboBox);
edit: thats my full code.
public class registerForm {
private JFrame frame;
private JTextField txtFirstName;
private JTextField txtLastName;
private JTextField txtPassword;
private JTextField txtEmail;
List<Integer> coursesId; // ן¿½ן¿½ן¿½ן¿½ן¿½ ן¿½ן¿½ ן¿½ן¿½ן¿½ן¿½ן¿½ ן¿½ן¿½ ן¿½ן¿½ן¿½ן¿½ן¿½ן¿½
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
registerForm window = new registerForm();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public registerForm() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 442);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("\u05D4\u05E8\u05E9\u05DE\u05D4");
lblNewLabel.setBounds(165, 11, 91, 29);
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 24));
frame.getContentPane().add(lblNewLabel);
JLabel label = new JLabel("\u05E9\u05DD \u05E4\u05E8\u05D8\u05D9:");
label.setBounds(363, 55, 61, 14);
label.setFont(new Font("Tahoma", Font.PLAIN, 14));
frame.getContentPane().add(label);
txtFirstName = new JTextField();
txtFirstName.setBounds(75, 51, 221, 20);
frame.getContentPane().add(txtFirstName);
txtFirstName.setColumns(10);
JLabel label_1 = new JLabel("\u05E9\u05DD \u05DE\u05E9\u05E4\u05D7\u05D4:");
label_1.setBounds(344, 80, 80, 14);
label_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
frame.getContentPane().add(label_1);
txtLastName = new JTextField();
txtLastName.setBounds(75, 82, 221, 20);
txtLastName.setColumns(10);
frame.getContentPane().add(txtLastName);
txtPassword = new JTextField();
txtPassword.setBounds(75, 140, 221, 20);
txtPassword.setColumns(10);
frame.getContentPane().add(txtPassword);
JLabel label_2 = new JLabel("\u05DE\u05D9\u05D9\u05DC:");
label_2.setBounds(392, 110, 32, 14);
label_2.setFont(new Font("Tahoma", Font.PLAIN, 14));
frame.getContentPane().add(label_2);
txtEmail = new JTextField();
txtEmail.setBounds(75, 109, 221, 20);
txtEmail.setColumns(10);
frame.getContentPane().add(txtEmail);
JLabel label_3 = new JLabel("\u05E1\u05D9\u05E1\u05DE\u05D0:");
label_3.setBounds(373, 141, 51, 14);
label_3.setFont(new Font("Tahoma", Font.PLAIN, 14));
frame.getContentPane().add(label_3);
final JDateChooser dateChooser = new JDateChooser();
dateChooser.setBounds(75, 171, 221, 39);
frame.getContentPane().add(dateChooser);
JLabel label_4 = new JLabel("\u05EA\u05D0\u05E8\u05D9\u05DA \u05DC\u05D9\u05D3\u05D4:");
label_4.setBounds(344, 167, 90, 14);
label_4.setFont(new Font("Tahoma", Font.PLAIN, 14));
frame.getContentPane().add(label_4);
JButton btnSend = new JButton("\u05E9\u05DC\u05D7");
btnSend.setBounds(258, 334, 61, 58);
btnSend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// date
Date date = new Date(dateChooser.getDate().getTime());
}
});
frame.getContentPane().add(btnSend);
JButton button = new JButton("\u05E0\u05E7\u05D4");
button.setBounds(175, 334, 61, 58);
frame.getContentPane().add(button);
JLabel label_5 = new JLabel("\u05DE\u05D2\u05DE\u05D4:");
label_5.setFont(new Font("Tahoma", Font.PLAIN, 14));
label_5.setBounds(382, 218, 42, 14);
frame.getContentPane().add(label_5);
final JPanel coursePanel = new JPanel();
coursePanel.setBounds(10, 249, 286, 74);
frame.getContentPane().add(coursePanel);
coursePanel.setLayout(null);
final JComboBox<String> comboBox = new JComboBox<String>();
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String typeName = comboBox.getSelectedItem().toString();
for (int i = 0; i < SqlQuery.getCoursesName(typeName).size(); i++) {
JCheckBox c = new JCheckBox(SqlQuery.getCoursesName(typeName).get(i));
int selectedIndex = comboBox.getSelectedIndex();
boolean isInPanel = c.getParent() == coursePanel;
if (selectedIndex == 1 && !isInPanel) {
coursePanel.add(c);
coursePanel.repaint(); //Repaint the proper panel that has this component.
coursePanel.revalidate();
} else if (isInPanel && selectedIndex != 1) {
coursePanel.remove(c);
coursePanel.repaint(); //Repaint the proper panel that has this component.
coursePanel.revalidate();
}
coursePanel.repaint();
coursePanel.validate();
System.out.println(c.getText());
}
}
});
comboBox.setBounds(208, 221, 91, 20);
frame.getContentPane().add(comboBox);
// fill comboBox
List<String> lst = SqlQuery.getTypes();
for (int i = 0; i < lst.size(); i++)
comboBox.addItem(lst.get(i));
JLabel label_6 = new JLabel("\u05E9\u05DC\u05D9\u05D8\u05D4 \u05D1\u05E7\u05D5\u05E8\u05E1\u05D9\u05DD");
label_6.setFont(new Font("Tahoma", Font.PLAIN, 14));
label_6.setBounds(321, 245, 103, 14);
frame.getContentPane().add(label_6);
}
}
Hope you understand what I wrote. I want to show a list of coursesName after click on the comboBox.
Why the frame does'nt show the checkbox?
Thank you.
First and foremost. The comboBox.setBounds() is a very bad practice. Take some time and see Layout Managers. Also add 'swing' tag in your post since you are referring to Swing library.
The frame doesn't show the component because you repaint the wrong one. You add the checkbox in coursePanel, which means you have to repaint() & revalidate() coursePanel. So coursePanel.repaint() instead of frame.repaint() will help you.
Also checkBox.setVisible(true)is not required, since checkBox is visible by default.
Finally i have added the way of how i would do it.
package test;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test {
private JFrame frame;
private JCheckBox checkBox;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test window = new Test();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Test() {
frame = new JFrame();
frame.setPreferredSize(new Dimension(300, 300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(createPanel());
frame.pack();
}
private JPanel createPanel() {
JPanel p = new JPanel(new FlowLayout());
checkBox = new JCheckBox("I am a checkbox"); //Create the checkbox
JComboBox<String> combo = new JComboBox<>();
combo.addItem("Hello");
combo.addItem("Stack");
combo.addItem("OverFlow");
combo.addActionListener(e -> {
//Its better to handle indices when there is not dynmic data to your combobox
int selectedIndex = combo.getSelectedIndex();
boolean isInPanel = checkBox.getParent() == p;
if (selectedIndex == 1 && !isInPanel) {
p.add(checkBox);
p.repaint(); //Repaint the proper panel that has this component.
p.revalidate();
} else if (isInPanel && selectedIndex != 1) {
p.remove(checkBox);
p.repaint(); //Repaint the proper panel that has this component.
p.revalidate();
}
});
p.add(combo);
return p;
}
}
The answer to your problem (make the checkbox visible) after comments:
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class registerForm {
private JFrame frame;
private JTextField txtFirstName;
private JTextField txtLastName;
private JTextField txtPassword;
private JTextField txtEmail;
private JCheckBox checkBox;
List<Integer> coursesId; // ן¿½ן¿½ן¿½ן¿½ן¿½ ן¿½ן¿½ ן¿½ן¿½ן¿½ן¿½ן¿½ ן¿½ן¿½ ן¿½ן¿½ן¿½ן¿½ן¿½ן¿½
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
registerForm window = new registerForm();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public registerForm() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 442);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("\u05D4\u05E8\u05E9\u05DE\u05D4");
lblNewLabel.setBounds(165, 11, 91, 29);
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 24));
frame.getContentPane().add(lblNewLabel);
JLabel label = new JLabel("\u05E9\u05DD \u05E4\u05E8\u05D8\u05D9:");
label.setBounds(363, 55, 61, 14);
label.setFont(new Font("Tahoma", Font.PLAIN, 14));
frame.getContentPane().add(label);
txtFirstName = new JTextField();
txtFirstName.setBounds(75, 51, 221, 20);
frame.getContentPane().add(txtFirstName);
txtFirstName.setColumns(10);
JLabel label_1 = new JLabel("\u05E9\u05DD \u05DE\u05E9\u05E4\u05D7\u05D4:");
label_1.setBounds(344, 80, 80, 14);
label_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
frame.getContentPane().add(label_1);
txtLastName = new JTextField();
txtLastName.setBounds(75, 82, 221, 20);
txtLastName.setColumns(10);
frame.getContentPane().add(txtLastName);
txtPassword = new JTextField();
txtPassword.setBounds(75, 140, 221, 20);
txtPassword.setColumns(10);
frame.getContentPane().add(txtPassword);
JLabel label_2 = new JLabel("\u05DE\u05D9\u05D9\u05DC:");
label_2.setBounds(392, 110, 32, 14);
label_2.setFont(new Font("Tahoma", Font.PLAIN, 14));
frame.getContentPane().add(label_2);
txtEmail = new JTextField();
txtEmail.setBounds(75, 109, 221, 20);
txtEmail.setColumns(10);
frame.getContentPane().add(txtEmail);
JLabel label_3 = new JLabel("\u05E1\u05D9\u05E1\u05DE\u05D0:");
label_3.setBounds(373, 141, 51, 14);
label_3.setFont(new Font("Tahoma", Font.PLAIN, 14));
frame.getContentPane().add(label_3);
JLabel label_4 = new JLabel("\u05EA\u05D0\u05E8\u05D9\u05DA \u05DC\u05D9\u05D3\u05D4:");
label_4.setBounds(344, 167, 90, 14);
label_4.setFont(new Font("Tahoma", Font.PLAIN, 14));
frame.getContentPane().add(label_4);
JButton button = new JButton("\u05E0\u05E7\u05D4");
button.setBounds(175, 334, 61, 58);
frame.getContentPane().add(button);
JLabel label_5 = new JLabel("\u05DE\u05D2\u05DE\u05D4:");
label_5.setFont(new Font("Tahoma", Font.PLAIN, 14));
label_5.setBounds(382, 218, 42, 14);
frame.getContentPane().add(label_5);
final JPanel coursePanel = new JPanel();
coursePanel.setBounds(10, 249, 286, 74);
frame.getContentPane().add(coursePanel);
coursePanel.setLayout(null);
List<String> lst = new ArrayList<>();
lst.add("Hello");
lst.add("Stack");
lst.add("Over");
lst.add("Flow");
//Prepare the checkBox
checkBox = new JCheckBox("nothing");
final JComboBox<String> comboBox = new JComboBox<String>();
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String typeName = comboBox.getSelectedItem().toString();
for (int i = 0; i < lst.size(); i++) {
checkBox.setText(typeName);
//set the same bounds as comboBox but reduce its X by 80.
//Compare checkBoxbounds and combobox bounds
//Remind: setBounds is baaaaad practice.
checkBox.setBounds(128, 221, 91, 20);
//if you want to add it in coursePanel, change all frame.getContentPane() to coursePanel
int selectedIndex = comboBox.getSelectedIndex();
boolean isInPanel = checkBox.getParent() == frame.getContentPane();
if (selectedIndex == 1 && !isInPanel) {
frame.getContentPane().add(checkBox);
} else if (isInPanel && selectedIndex != 1) {
frame.getContentPane().remove(checkBox);
}
frame.getContentPane().repaint();
frame.getContentPane().validate();
System.out.println(checkBox.getText());
}
}
});
comboBox.setBounds(208, 221, 91, 20);
frame.getContentPane().add(comboBox);
// fill comboBox
for (int i = 0; i < lst.size(); i++)
comboBox.addItem(lst.get(i));
JLabel label_6 = new JLabel("\u05E9\u05DC\u05D9\u05D8\u05D4 \u05D1\u05E7\u05D5\u05E8\u05E1\u05D9\u05DD");
label_6.setFont(new Font("Tahoma", Font.PLAIN, 14));
label_6.setBounds(321, 245, 103, 14);
frame.getContentPane().add(label_6);
}
}
Another option is to create the checkbox as a field outside of the actionlistener (like i did) and set its bounds with window builder and use setVisible(false). Then on your listener, setVisible(true)
public class InterfaceGUI implements ActionListener {
private JFrame frame;
private JPanel panelController;
private JPanel panelJack;
private JPanel panelModbus;
private JPanel panelLog;
private JPanel panelStatus;
private JPanel panelToController;
private JPanel panelToJack;
private JLabel lblIpAddController;
private JLabel lblPortController;
private JLabel lblIpAddJack;
private JLabel lblPortJack;
private JLabel lblStatusToController;
private JLabel lblStatusConnToCont;
private JLabel lblIpAddToController;
private JLabel lblIpAddConnToCont;
private JLabel lblPortToController;
private JLabel lblPortConnToCont;
private JLabel lblStatusToJack;
private JLabel lblStatusConnToJack;
private JLabel lblIpAddToJack;
private JLabel lblIpAddConnToJack;
private JLabel lblPortToJack;
private JLabel lblPortConnToJack;
private JLabel lblSlaveId;
private JLabel lblIcon;
private JButton btnConnController;
private JButton btnDiscController;
private JButton btnConnJack;
private JButton btnDiscJack;
private JButton btnConnModbus;
private JTextArea textAreaConnLog;
private JTextField textFieldIpAddController;
private JTextField textFieldPortController;
private JTextField textFieldIpAddJack;
private JTextField textFieldPortJack;
private JTextField textFieldSlaveId;
private Socket myClient;
public byte[] message = new byte[5];
private static final String modbusConn = "modbusconnect";
private static final String modbusDisc = "modbusdisconnect";
/**
* Launch the application.
*/
public static void main(String[] args){
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
InterfaceGUI window = new InterfaceGUI();
window.frame.setVisible(true);
//window.receiveMessage();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public InterfaceGUI() {
initialize();
}
public void startRunning() throws IOException{
try {
while (true){
System.out.println("receive");
InputStream inFromServer = myClient.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
in.read(message);
System.out.println(Arrays.toString(message));
}
} catch (EOFException eofexception){
System.out.println("error");
}
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
panelController = new JPanel();
panelController.setBorder(new TitledBorder(null, "Connection to Controller", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panelController.setBounds(6, 6, 438, 95);
frame.getContentPane().add(panelController);
panelController.setLayout(null);
lblIpAddController = new JLabel("IP Address :");
lblIpAddController.setBounds(13, 24, 74, 16);
panelController.add(lblIpAddController);
textFieldIpAddController = new JTextField();
textFieldIpAddController.setBounds(92, 18, 174, 28);
panelController.add(textFieldIpAddController);
textFieldIpAddController.setColumns(10);
btnConnController = new JButton("Connect");
btnConnController.setBounds(278, 18, 154, 29);
panelController.add(btnConnController);
btnConnController.addActionListener(this);
lblPortController = new JLabel("Port :");
lblPortController.setBounds(54, 58, 33, 16);
panelController.add(lblPortController);
textFieldPortController = new JTextField();
textFieldPortController.setBounds(92, 52, 174, 28);
panelController.add(textFieldPortController);
textFieldPortController.setColumns(10);
btnDiscController = new JButton("Disconnect");
btnDiscController.setBounds(278, 52, 154, 29);
panelController.add(btnDiscController);
btnDiscController.addActionListener(this);
panelJack = new JPanel();
panelJack.setBorder(new TitledBorder(null, "Connection to Jack", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panelJack.setBounds(6, 113, 289, 95);
frame.getContentPane().add(panelJack);
panelJack.setLayout(null);
lblIpAddJack = new JLabel("IP Address :");
lblIpAddJack.setBounds(14, 24, 74, 16);
panelJack.add(lblIpAddJack);
textFieldIpAddJack = new JTextField();
textFieldIpAddJack.setBounds(93, 18, 106, 28);
textFieldIpAddJack.setText("111.111.1.111");
panelJack.add(textFieldIpAddJack);
textFieldIpAddJack.setColumns(10);
btnConnJack = new JButton("Connect");
btnConnJack.setBounds(200, 20, 86, 29);
btnConnJack.setFont(new Font("Lucida Grande", Font.PLAIN, 11));
panelJack.add(btnConnJack);
lblPortJack = new JLabel("Port :");
lblPortJack.setBounds(55, 58, 33, 16);
panelJack.add(lblPortJack);
textFieldPortJack = new JTextField();
textFieldPortJack.setBounds(93, 52, 106, 28);
panelJack.add(textFieldPortJack);
textFieldPortJack.setColumns(10);
btnDiscJack = new JButton("Disconnect");
btnDiscJack.setBounds(200, 52, 86, 29);
btnDiscJack.setFont(new Font("Lucida Grande", Font.PLAIN, 11));
panelJack.add(btnDiscJack);
panelStatus = new JPanel();
panelStatus.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Connection Status", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panelStatus.setBounds(6, 220, 438, 126);
frame.getContentPane().add(panelStatus);
panelStatus.setLayout(null);
panelToController = new JPanel();
panelToController.setBorder(new TitledBorder(null, "Connection to Controller", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panelToController.setBounds(6, 20, 208, 94);
panelStatus.add(panelToController);
panelToController.setLayout(null);
lblStatusToController = new JLabel("Status");
lblStatusToController.setBounds(41, 19, 39, 16);
panelToController.add(lblStatusToController);
lblStatusConnToCont = new JLabel("DISCONNECTED");
lblStatusConnToCont.setForeground(Color.RED);
lblStatusConnToCont.setBounds(85, 19, 117, 16);
lblStatusConnToCont.setHorizontalAlignment(SwingConstants.CENTER);
panelToController.add(lblStatusConnToCont);
lblIpAddToController = new JLabel("IP Address :");
lblIpAddToController.setBounds(6, 43, 74, 16);
panelToController.add(lblIpAddToController);
lblIpAddConnToCont = new JLabel("New label");
lblIpAddConnToCont.setBounds(113, 43, 61, 16);
lblIpAddConnToCont.setHorizontalAlignment(SwingConstants.CENTER);
panelToController.add(lblIpAddConnToCont);
lblPortToController = new JLabel("Port :");
lblPortToController.setBounds(47, 66, 33, 16);
panelToController.add(lblPortToController);
lblPortConnToCont = new JLabel("New label");
lblPortConnToCont.setBounds(113, 66, 61, 16);
lblPortConnToCont.setHorizontalAlignment(SwingConstants.CENTER);
panelToController.add(lblPortConnToCont);
panelToJack = new JPanel();
panelToJack.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Connection to Jack", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panelToJack.setBounds(218, 20, 214, 94);
panelStatus.add(panelToJack);
panelToJack.setLayout(null);
lblStatusToJack = new JLabel("Status");
lblStatusToJack.setBounds(41, 19, 39, 16);
panelToJack.add(lblStatusToJack);
lblStatusConnToJack = new JLabel("DISCONNECTED");
lblStatusConnToJack.setForeground(Color.RED);
lblStatusConnToJack.setBounds(85, 19, 123, 16);
lblStatusConnToJack.setHorizontalAlignment(SwingConstants.CENTER);
panelToJack.add(lblStatusConnToJack);
lblIpAddToJack = new JLabel("IP Address :");
lblIpAddToJack.setBounds(6, 43, 74, 16);
panelToJack.add(lblIpAddToJack);
lblIpAddConnToJack = new JLabel("New label");
lblIpAddConnToJack.setBounds(116, 43, 61, 16);
lblIpAddConnToJack.setHorizontalAlignment(SwingConstants.CENTER);
panelToJack.add(lblIpAddConnToJack);
lblPortToJack = new JLabel("Port :");
lblPortToJack.setBounds(47, 66, 33, 16);
panelToJack.add(lblPortToJack);
lblPortConnToJack = new JLabel("New label");
lblPortConnToJack.setBounds(116, 66, 61, 16);
lblPortConnToJack.setHorizontalAlignment(SwingConstants.CENTER);
panelToJack.add(lblPortConnToJack);
panelModbus = new JPanel();
panelModbus.setBorder(new TitledBorder(null, "Modbus", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panelModbus.setBounds(296, 113, 148, 95);
frame.getContentPane().add(panelModbus);
panelModbus.setLayout(null);
lblSlaveId = new JLabel("ID :");
lblSlaveId.setBounds(33, 24, 22, 16);
panelModbus.add(lblSlaveId);
textFieldSlaveId = new JTextField();
textFieldSlaveId.setBounds(60, 18, 82, 28);
panelModbus.add(textFieldSlaveId);
textFieldSlaveId.setColumns(10);
lblIcon = new JLabel("icon");
lblIcon.setBounds(17, 52, 32, 35);
panelModbus.add(lblIcon);
Image imgDisconnect = new ImageIcon(this.getClass().getResource("red-unlock-icon.png")).getImage();
lblIcon.setIcon(new ImageIcon(imgDisconnect));
btnConnModbus = new JButton("Connect");
btnConnModbus.setBounds(60, 51, 82, 29);
panelModbus.add(btnConnModbus);
panelLog = new JPanel();
panelLog.setBorder(new TitledBorder(null, "Connection Log", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panelLog.setBounds(6, 358, 438, 214);
frame.getContentPane().add(panelLog);
panelLog.setLayout(null);
textAreaConnLog = new JTextArea();
textAreaConnLog.setBounds(6, 19, 426, 189);
panelLog.add(textAreaConnLog);
}
#Override
public void actionPerformed(ActionEvent ae) {
// TODO Auto-generated method stub
if(ae.getSource() instanceof JButton){
if(ae.getSource() == btnConnController){
//check validity ip dan port number later on
try {
myClient = new Socket("192.168.1.104", 1234);
if(myClient.isConnected() == true){
textAreaConnLog.setText("connected to 192.168.1.104 at port 1234");
lblStatusConnToJack.setText("CONNECTED");
lblStatusConnToJack.setForeground(Color.GREEN);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
I am currently building a client program to receive broadcasted message by the server. Just want to make things clear, is it correct that the method startRunning() will keep looping itself without anyone calling it? Also, the incoming data is in the form of bytes, can i use the method .read(message) to read and store the data into message[]?
Is it correct that the method startRunning() will keep looping itself without anyone calling it?
You have to call the method startRunning() at least once so that it starts executing its code; after that the while block will continue to loop infinitly unless an exception occurs.
Also, the incoming data is in the form of bytes, can i use the method .read(message) to read and store the data into message[]?
Yes, you can. See this link for the specifications
If I might add a tip:
private boolean Continue = true;
public void setContinue(boolean b)
{
Continue = b;
}
public void startRunning() throws IOException
{
DataInputStream in;
try
{
while (Continue)
{
System.out.println("receive");
InputStream inFromServer = myClient.getInputStream();
in = new DataInputStream(inFromServer);
in.read(message);
System.out.println(Arrays.toString(message));
}
}
catch (EOFException eofexception)
{
System.out.println("error");
}
catch(Exception ex)
{
in.close();
throw ex;
}
}
Note the condition of the while-loop. This would make your code more responsive to interrupts, should you need them; streams should not be open infinitly.
Also, you ought to close the stream when an exception does occur.
I am having a problem with opening a new window. If I comment out the if(e.getsource() == btnAddBook) the action performed opens the window. As soon as I add the if statement in and try to open the window, nothing happens.
/* this is the code that adds the button and the action listner
JButton btnAddBook = new JButton("Add Book");
btnAddBook.setFont(new Font("Tahoma", Font.BOLD, 8));
btnAddBook.setBounds(10, 327, 86, 23);
btnAddBook.addActionListener(this);
getContentPane().add(btnAddBook);
*/
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source == btnAddBook){
ChildWindowAdd child = new ChildWindowAdd(this);
this.setVisible(true);
child.setVisible(true);
child.setSize(450,400);
child.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
}
}
class ChildWindowAdd extends JFrame implements ActionListener {
LibraryDatabase parent;
Container c;
JLabel lblTitle, lblaName, lblISBN, lblDate,lbl1,lbl5,lbl10;
JTextField txtTitle, txtaName, txtISBN, txtDate;
JButton btnSave;
JSlider sRating;
JCheckBox chkSci, chkFant, chkRomance, chkAction, chkThriller, chkHorror;
public ChildWindowAdd(LibraryDatabase parent){
this.parent = parent;
getContentPane().setLayout(null);
txtTitle = new JTextField();
txtTitle.setBounds(46, 29, 146, 20);
getContentPane().add(txtTitle);
txtTitle.setColumns(10);
JLabel lblTitle = new JLabel("Book Title");
lblTitle.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblTitle.setBounds(36, 11, 72, 14);
getContentPane().add(lblTitle);
JLabel lblaName = new JLabel("Author Name");
lblaName.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblaName.setBounds(36, 60, 89, 14);
getContentPane().add(lblaName);
txtaName = new JTextField();
txtaName.setBounds(46, 85, 146, 20);
getContentPane().add(txtaName);
txtaName.setColumns(10);
JLabel lblIsbnNumber = new JLabel("ISBN Number");
lblIsbnNumber.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblIsbnNumber.setBounds(36, 116, 86, 14);
getContentPane().add(lblIsbnNumber);
txtISBN = new JTextField();
txtISBN .setBounds(46, 143, 146, 20);
getContentPane().add(txtISBN );
txtISBN .setColumns(10);
JLabel lblDate = new JLabel("Date Added yyyy/mm/dd");
lblDate.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblDate.setBounds(36, 174, 177, 20);
getContentPane().add(lblDate);
txtDate = new JTextField();
txtDate.setBounds(46, 199, 146, 20);
getContentPane().add(txtDate);
txtDate.setColumns(10);
JPanel genreP = new JPanel();
genreP.setBounds(239, 29, 158, 194);
genreP.setBorder(new TitledBorder(null, "Genre's", TitledBorder.LEADING, TitledBorder.TOP, null, null));
getContentPane().add(genreP);
genreP.setLayout(null);
final JCheckBox chkSci = new JCheckBox("Sci - Fi");
chkSci.setBounds(23, 22, 97, 23);
genreP.add(chkSci);
final JCheckBox chkFant = new JCheckBox("Fantasy");
chkFant.setBounds(23, 48, 97, 23);
genreP.add(chkFant);
final JCheckBox chkRomance = new JCheckBox("Romance");
chkRomance.setBounds(23, 74, 97, 23);
genreP.add(chkRomance);
final JCheckBox chkAction = new JCheckBox("Action");
chkAction.setBounds(23, 100, 97, 23);
genreP.add(chkAction);
final JCheckBox chkThriller = new JCheckBox("Thriller");
chkThriller.setBounds(23, 126, 97, 23);
genreP.add(chkThriller);
final JCheckBox chkHorror = new JCheckBox("Horror");
chkHorror.setBounds(23, 152, 97, 23);
genreP.add(chkHorror);
final JSlider sRating = new JSlider();
sRating.setBounds(118, 280, 200, 26);
getContentPane().add(sRating);
JLabel lbl1 = new JLabel("1");
lbl1.setBounds(118, 257, 7, 14);
getContentPane().add(lbl1);
JLabel lbl10 = new JLabel("10");
lbl10.setBounds(301, 257, 17, 14);
getContentPane().add(lbl10);
JLabel lbl5 = new JLabel("5");
lbl5.setBounds(214, 257, 7, 14);
getContentPane().add(lbl5);
JButton btnSave = new JButton("Save Book");
btnSave.addActionListener(this);
btnSave.setBounds(164, 317, 110, 23);
getContentPane().add(btnSave);
JLabel lblRating = new JLabel("Rate the book");
lblRating.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblRating.setBounds(118, 226, 95, 20);
getContentPane().add(lblRating);
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
PrintWriter fileWriter = null;
try {
fileWriter = new PrintWriter(new BufferedWriter(new FileWriter("c:\\temp\\database.dat", true)));
} catch (IOException e) {
e.printStackTrace();
}
fileWriter.println( txtTitle.getText()+" , "+txtaName.getText()+" , "+txtISBN.getText()+" , "+txtDate.getText()+" , "+String.valueOf(chkSci.isSelected())+" , "+String.valueOf(chkFant.isSelected())+" , "+String.valueOf(chkRomance.isSelected())+" , "+String.valueOf(chkAction.isSelected())+" , "+String.valueOf(chkThriller.isSelected())+" , "+String.valueOf(chkHorror.isSelected())+" , "+sRating.getValue());
fileWriter.close();
}
});
}
#Override
public void actionPerformed(ActionEvent arg0) {
parent.setVisible(true);
this.dispose();
}
public void actionPerformed1(ActionEvent e) {
// TODO Auto-generated method stub
}
}
Check on the basis of button text
((JButton)source).getText().equals("Add Book")
here is complete code
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source instanceof JButton && ((JButton) source).getText().equals("Add Book")) {
...
}
}
This worked for me. I had to match the String as the e.getsource was not working.
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
String test = ((JButton)source).getText();
if(test == "Add Book"){
ChildWindowAdd child = new ChildWindowAdd(this);
this.setVisible(true);
child.setVisible(true);
child.setSize(450,400);
child.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
I am having a problem with my Java GUI application. It for some reason says that it cannot find or load the main class. I have checked all the brackets and don't see a problem. I had originally copied this over from another file as I wanted to start over with some things, maybe that is the problem. Here is the code:
public class GradeAverage extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JTextField textField;
int numAverage = 0;
double average;
Container c;
JLabel gradelbl;
class AnswerHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
numAverage++;
gradelbl.setText("" + numAverage);
}
}
public GradeAverage() {
super("Final Grades");
c = getContentPane();
JLabel lblNewLabel = new JLabel("Grade: ");
lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 14));
lblNewLabel.setBounds(30, 50, 46, 14);
textField = new JTextField();
textField.setBounds(81, 47, 86, 20);
textField.setColumns(10);
JLabel gradeLbl = new JLabel("0");
gradeLbl.setFont(new Font("Times New Roman", Font.BOLD, 14));
gradeLbl.setBounds(91, 92, 15, 14);
JLabel gradeTextLbl = new JLabel("Grades Recorded");
gradeTextLbl.setFont(new Font("Times New Roman", Font.BOLD, 14));
gradeTextLbl.setBounds(106, 92, 123, 14);
JButton recordBtn = new JButton("Record");
AnswerHandler ah = new AnswerHandler();
recordBtn.addActionListener(ah);
recordBtn.setBounds(177, 47, 86, 20);
JButton displayAverageBtn = new JButton("New button");
displayAverageBtn.setBounds(30, 117, 233, 23);
JPanel finalGradePanel = new JPanel();
finalGradePanel.setBorder(new TitledBorder(null, "Enter Your Final Grades", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 128)));
finalGradePanel.setBounds(10, 21, 264, 131);
JPanel avgGradePanel = new JPanel();
avgGradePanel.setFont(new Font("Times New Roman", Font.BOLD, 16));
avgGradePanel.setBounds(10, 178, 264, 61);
avgGradePanel.setBorder(new TitledBorder(null, "Average Grade", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 128)));
c.add(avgGradePanel);c.add(finalGradePanel);c.add(displayAverageBtn);c.add(recordBtn);c.add(gradeTextLbl);c.add(gradeLbl);
}
public static void main(String[] args) {
GradeAverage app = new GradeAverage();
app.setSize(300, 300);
app.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
app.setVisible(true);
}
}
I'm having an issue with trying to change the icon of a JLabel. I am using the windowbuilder addon in eclipse to make this program, so just disregard all the bad naming choices of the label and all that, they will be changed later.
The error I'm having is that at the bottom of the my code where I try to change the icon of label_1, I get the following message.
The method seticon(icon) in the type jlabel is not applicable for the
arguments (string)
Does anyone have any idea what this could be?
Here is my code. Sorry that it's rather long, that's window builder for you I suppose:
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.Color;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Game {
public Timer bomb1;
public JLabel label;
public JLabel label_1;
public JLabel lblScore;
public JButton btnQuit;
public JLabel label_2;
public JLabel label_3;
public JLabel label_4;
public JLabel label_5;
public JLabel label_6;
public JLabel label_7;
public JLabel label_8;
public JLabel label_9;
public JLabel label_10;
public JLabel label_11;
public JLabel label_12;
public JLabel label_13;
public JLabel label_14;
public JLabel label_15;
public JLabel label_16;
public JLabel lblDodgeTheBombs;
public JFrame frame;
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Game window = new Game();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Game() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 489, 512);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
frame.setContentPane(contentPane);
contentPane.setLayout(null);
label_1 = new JLabel("");
label_1.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_1.setBackground(Color.WHITE);
label_1.setBounds(59, 44, 80, 80);
contentPane.add(label_1);
label = new JLabel("0");
label.setFont(new Font("Tahoma", Font.PLAIN, 14));
label.setBounds(71, 12, 46, 14);
contentPane.add(label);
lblScore = new JLabel("Score:");
lblScore.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblScore.setBounds(28, 11, 45, 17);
contentPane.add(lblScore);
btnQuit = new JButton("Quit");
btnQuit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
}
});
btnQuit.setBounds(359, 10, 89, 23);
contentPane.add(btnQuit);
label_2 = new JLabel("");
label_2.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_2.setBackground(Color.WHITE);
label_2.setBounds(149, 44, 80, 80);
contentPane.add(label_2);
label_3 = new JLabel("");
label_3.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_3.setBackground(Color.WHITE);
label_3.setBounds(239, 44, 80, 80);
contentPane.add(label_3);
label_4 = new JLabel("");
label_4.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_4.setBackground(Color.WHITE);
label_4.setBounds(329, 44, 80, 80);
contentPane.add(label_4);
label_5 = new JLabel("");
label_5.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_5.setBackground(Color.WHITE);
label_5.setBounds(59, 138, 80, 80);
contentPane.add(label_5);
label_6 = new JLabel("");
label_6.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_6.setBackground(Color.WHITE);
label_6.setBounds(149, 135, 80, 80);
contentPane.add(label_6);
label_7 = new JLabel("");
label_7.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_7.setBackground(Color.WHITE);
label_7.setBounds(239, 135, 80, 80);
contentPane.add(label_7);
label_8 = new JLabel("");
label_8.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_8.setBackground(Color.WHITE);
label_8.setBounds(329, 135, 80, 80);
contentPane.add(label_8);
label_9 = new JLabel("");
label_9.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_9.setBackground(Color.WHITE);
label_9.setBounds(329, 320, 80, 80);
contentPane.add(label_9);
label_10 = new JLabel("");
label_10.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_10.setBackground(Color.WHITE);
label_10.setBounds(59, 229, 80, 80);
contentPane.add(label_10);
label_11 = new JLabel("");
label_11.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_11.setBackground(Color.WHITE);
label_11.setBounds(149, 229, 80, 80);
contentPane.add(label_11);
label_12 = new JLabel("");
label_12.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_12.setBackground(Color.WHITE);
label_12.setBounds(239, 229, 80, 80);
contentPane.add(label_12);
label_13 = new JLabel("");
label_13.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_13.setBackground(Color.WHITE);
label_13.setBounds(329, 229, 80, 80);
contentPane.add(label_13);
label_14 = new JLabel("");
label_14.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_14.setBackground(Color.WHITE);
label_14.setBounds(239, 320, 80, 80);
contentPane.add(label_14);
label_15 = new JLabel();
label_15.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_15.setBackground(Color.WHITE);
label_15.setBounds(149, 320, 80, 80);
contentPane.add(label_15);
label_16 = new JLabel("");
label_16.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\WHITE.png"));
label_16.setBackground(Color.WHITE);
label_16.setBounds(59, 323, 80, 80);
contentPane.add(label_16);
lblDodgeTheBombs = new JLabel("Dodge the bombs!");
lblDodgeTheBombs.setForeground(Color.RED);
lblDodgeTheBombs.setFont(new Font("Tahoma", Font.PLAIN, 25));
lblDodgeTheBombs.setBounds(137, 418, 217, 33);
contentPane.add(lblDodgeTheBombs);
bomb1 = new Timer(1000, new TimerListener());
bomb1.start();
}
private class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
label_1.setIcon("C:\\Users\\Josh\\Desktop\\blank.png");
}
}
}
The method seticon(icon) in the type jlabel is not applicable for the
arguments (string)
yes its because as per the doc found here the setIcon() accepts a Icon as a parrameter not String. So, your code should be...
label_1.setIcon(new ImageIcon("C:\\Users\\Josh\\Desktop\\blank.png"));
I recommend you to go with documentations in the future. Its really a very good practice.
I think the error message is self-explanatory about what went wrong. The "setIcon()" method expects instance of Icon class and not string instance.
jLabelObject.setIcon("string") // Do not pass image path