how can i remove and replace jlabel with image icon in java? - java

Im trying to create a banking system and to not make it boring we tried to add some image icon into the program in jlabel, when i tried to run the code and first pin mistake, removes the tester label successfully but the problem is when tries are equal to 4, Label pinwrongtester doesn't seem to remove instead it is mixing with the Label that i programmed when they entered pin 4 times, i hope you guys can help me to solve this mistakes
public void Option()
{
//option title
JLabel label = new JLabel("Choose Options");
label.setFont(new Font("Consolas",Font.BOLD,20));
JButton choose = new JButton();
choose.add(label);
choose.setBounds(200,300,200,40);
choose.setBackground(Color.white);
choose.setEnabled(false);
// Buttons Option
JButton Balance = new JButton("Balance");
JButton Withdraw= new JButton("Withdraw");
JButton Send = new JButton("Transfer");
JButton Exit = new JButton("Exit");
//Set Bounds
Balance.setBounds(50,350,200,40);
Withdraw.setBounds(50,450,200,40);
Send.setBounds(350,350,200,40);
Exit.setBounds(350,450,200,40);
//addActionListener
Balance.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
pinbalance();
}
});
Withdraw.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
pinwithdraw();
}
});
Send.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
pintransferfunds();
}
});
//add buttons and labels
p.add(choose);
p.add(Balance);
p.add(Withdraw);
p.add(Send);
p.add(Exit);
}
JPanel enterpinbg;
JFrame enterpinframe;
String account_name;
String balance;
String pindb;
JLabel tester;
JLabel pinwrongtester;
JLabel pinwrongertester;
JLabel balancetester;
JFrame balanceframe;
JPanel balancepanel;
ImageIcon enteryourpin;
ImageIcon pinwrong;
ImageIcon pinwronger;
ImageIcon balanceicon;
private LinkedList<String> picturetrans;
JTextField inputpin;
boolean police = false;
int x =0;
public void pinbalance()
{
//PIN FRAME
enterpinframe = new JFrame("Enter Pin");
enterpinframe.setSize(WIDTH,HEIGHT);
enterpinframe.setVisible(true);
enterpinframe.setResizable(false);
enterpinframe.setLocationRelativeTo(null);
enterpinframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//PIN PANEL
enterpinbg = new JPanel();
enterpinbg.setSize(WIDTH,HEIGHT);
enterpinbg.setBackground(Color.orange);
enterpinbg.setVisible(true);
enterpinbg.setLayout(null);
enterpinframe.add(enterpinbg);
inputpin = new JTextField();
inputpin.setFont(new Font("Consolas",Font.BOLD,20));
inputpin.setForeground(Color.gray);
//Setbounds
inputpin.setBounds(250,600,100,40);
//tester + image
enteryourpin = new ImageIcon(getClass().getResource("Enter yout PIN.png"));
tester = new JLabel(enteryourpin);
tester.setBounds(50,50,500,500);
//enterpin Add
enterpinbg.add(tester);
enterpinbg.add(inputpin);
this.dispose();
inputpin.addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==(KeyEvent.VK_ENTER))
{
String pinx = inputpin.getText();
Connection con= null;
Statement stmt = null;
try
{
Class.forName(JDBC_DRIVER1);
Class.forName(JDBC_DRIVER2);
con = DriverManager.getConnection(URL,USER,PASS);
stmt=con.createStatement();
String sql ="SELECT*FROM TBL_BANK WHERE PIN ='"+pinx+"' ";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next())
{
pindb = rs.getString("PIN");
account_name = rs.getString("ACCOUNT_NAME");
balance = rs.getString("BALANCE");
}
rs.close();
}
catch(Exception f)
{
System.out.println(f);
}
//if Login Success and Failed
if(pinx.equals(pindb))
{
// Balance will show up
}
else if(pinx!=(pindb))
{
enterpinbg.remove(tester);
x++;
System.out.println("x");
inputpin.setText("");
pinwrong = new ImageIcon(getClass().getResource("PIN Wrong (1_2Times).png"));
pinwrongtester = new JLabel(pinwrong);
pinwrongtester.setBounds(50,50,500,500);
enterpinbg.add(pinwrongtester);
enterpinbg.repaint();
if(x>=4 )
{
// this doesnt quite work please help me fix it
enterpinbg.remove(pinwrongtester);
enterpinbg.repaint();
}
}
}
}
#Override
public void keyReleased(KeyEvent e) {
}
});

Related

Buttons are not working. Entering Data through TextFields

I want to enter data through textfields and save them. But buttons are not working. There are no errors while I'm compiling and not even in database connection. I guess there is a logical error.
public class Database {
private static JPanel panel;
private static JLabel sname;
private static JLabel saddress;
private static JTextField t;
private static JTextField t1;
private static JButton Delete = new JButton("Delete");
private static JButton update = new JButton("Update");
private static JButton save = new JButton("Save");
private static Container c;
private static Connection con;
private static Statement st;
private static ResultSet rs;
Database() {
connect();
}
public void connect(){
try{
System.out.println("Entered");
System.out.println("Database connection started...");
con=DriverManager.getConnection("jdbc:ucanaccess://D:\\test.mdb");
System.out.println("Hello satarted");
st = con.createStatement();
System.out.println("Connection ok.");
while(rs.next())
{
System.out.println(rs.getString("Student Name"));
System.out.println(rs.getString("Student Address"));
System.out.println();
}
catch (SQLException e){
System.err.println("Exception: " + e.getMessage());
}
}
public static void main(String[] args) {
Database gui = new Database();
JFrame f = new JFrame();
f.setTitle("School Management System");
f.setSize(300, 200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sname = new JLabel("Student Name");
t = new JTextField(10);
saddress = new JLabel("Student Address");
t1 = new JTextField(10);
panel = new JPanel();
panel.add(sname);
panel.add(t);
panel.add(saddress);
panel.add(t1);
// panel.add(insert);
panel.add(Delete);
panel.add(update);
panel.add(save);
f.add(panel);
try {
rs.next();
t.setText(rs.getString("Student Name"));
t1.setText(rs.getString("Student Address"));
} catch (Exception ex) {
}
}
public void btnAction() {
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = t.getText();
String address = t1.getText();
try {
rs.moveToInsertRow();
rs.updateString("Student Name", name);
rs.updateString("Student Address", address);
rs.insertRow();
rs.close();
} catch (Exception ex) {
}
}
});
update.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String sname = t.getText();
String saddress = t1.getText();
try {
rs.updateString("Student name", sname);
rs.updateString("Student address", saddress);
rs.updateRow();
JOptionPane.showMessageDialog(null, "Record Updated!");
} catch (Exception e) {
}
}
});
}
}
when i removed static from each of these variables. I got so many errors
Yes, any time you see a class full of static variables you know the class is designed incorrectly.
Start with the working example from the Swing tutorial on How to Use Text Field. Download the TextDemo code and understand how it works and then make the changes for your logic.
Note how the class extends a JPanel where all the variables and components are defined and there is no need for any static variable. Your code should look something like this.
Once you have a better designed class you can then start to debug your code to figure out why the SQL isn't working.

Jtable cant load data from database

I cant seems to load data into table despite being able to load column name dynamically. When I try to load data nothing appear despite the fact that Java Icon still showed on the taskbar. It doesn't look like there is anything wrong with GUI itself so I was wondering if opening a connection from constructor will cause logical error.I printed out the locals variables in the second for loop of populateTable method to check if data was passed into local variables and they did, so I don't know exactly what was causing this error.
here is the code for GUI:
public class WeatherFrame extends JFrame {
private JPanel contentPane;
private JTable table;
HealthData health = new HealthData();
private DefaultTableModel model;
String[] columnNames = {"zipcode", "county", "city", "state", "year", "month","ageGroup",
"numOfVisits", "MonthlyMax", "MonthlyMin", "MonthlyNor"};
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
WeatherFrame frame = new WeatherFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public WeatherFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 300);
contentPane = new JPanel();
contentPane.setBounds(100, 100,750, 200);
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(6, 25, 788, 180);
contentPane.add(scrollPane);
model = new DefaultTableModel();
populateTable();
table = new JTable(model);
scrollPane.setViewportView(table);
JButton btnInsert = new JButton("insert");
btnInsert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnInsert.setBounds(315, 217, 117, 29);
contentPane.add(btnInsert);
JButton btnDelete = new JButton("delete");
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnDelete.setBounds(198, 243, 117, 29);
contentPane.add(btnDelete);
JButton btnSearch = new JButton("search");
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnSearch.setBounds(81, 243, 117, 29);
contentPane.add(btnSearch);
JLabel lblWeatherTable = new JLabel("Weather Table");
lblWeatherTable.setBounds(149, 6, 107, 16);
contentPane.add(lblWeatherTable);
JButton btnNext = new JButton("update");
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnNext.setBounds(198, 217, 117, 29);
contentPane.add(btnNext);
JButton btnRefresh = new JButton("refresh");
btnRefresh.setBounds(81, 217, 117, 29);
contentPane.add(btnRefresh);
}
public void populateTable() {
for(String name: columnNames)
model.addColumn(name);
ArrayList<Health> healthdata = new ArrayList<Health>();
healthdata = health.showAllData();
model.addRow(healthdata.toArray());
}
}
this is the constructor for the HealthData class
public HealthData(){
try {
/* Connect to database */
connection = DriverManager.getConnection(jdbcURL, MySQLConfig.user, MySQLConfig.password);
statement = connection.createStatement();
data = new ArrayList<Health>();
} catch (Exception e)
{
e.printStackTrace();
}
}
this is the code for showAlldata method
public ArrayList<Health> showAllData(){
ArrayList<Health> list = new ArrayList<Health>();
try {
connection = DriverManager.getConnection(jdbcURL, MySQLConfig.user, MySQLConfig.password);
Statement stmt = connection.createStatement();
ResultSet result = stmt.executeQuery(SELECT_ALL_QUERY);
/* Get and print out data from health table : zipcode, county, city, state, year, month, ageGroup, numberOfVisits, MMax, MMin, MNor */
while(result.next()){
Health data = new Health();
int zipcode = result.getInt(1);
data.setZipCode(zipcode);
String county = result.getString(2);
data.setCounty(county);
String city = result.getString(3);
data.setCity(city);
String state = result.getString(4);
data.setState(state);
int year = result.getInt(5);
data.setYear(year);
int month = result.getInt(6);
data.setMonth(month);
String ageGroup = result.getString(7);
data.setAgeGroup(ageGroup);
int numOfVisits = result.getInt(8);
data.setNumOfVisits(numOfVisits);
float MMax = result.getFloat(9);
data.setMMax(MMax);
float MMin = result.getFloat(10);
data.setMMin(MMin);
float MNor = result.getFloat(11);
data.setMNor(MNor);
list.add(data);
connection.close()
stmt.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
Can someone suggest a solution for solving this problem. Thank you in advance for helping. I already posted a question about this but this question is up to date with required codes.
I edit the code to make only one call to showAlldata and also close connection and statement, however, the same error still persist
public void populateTable() {
model = new DefaultTableModel(){
#Override
public boolean isCellEditable(int row, int column) {
//all cells false
return false;
}
};
for(String name: columnNames)
model.addColumn(name);
ArrayList<Health> temp = new ArrayList<Health>();
temp = health.showAllData();
for(int i = 0; i< temp.size(); i++) {
Object[] data = {temp.get(i).getZipCode(), temp.get(i).getCounty(), temp.get(i).getCounty(), temp.get(i).getState(),temp.get(i).getYear(),
temp.get(i).getMonth(), temp.get(i).getAgeGroup(), temp.get(i).getNumOfVisits(), temp.get(i).getMMax(), temp.get(i).getMMin(), temp.get(i).getMNor()};
model.addRow(data);
}
}
thank you very much for helping, I solved this question by modifying my populateTable Method as above.

Java :How to connect to database using Jbutton taking user input data

In gui user enter username and password then click ok button.But the problem is i don't know how to connect with my Connector Class with ok button. And Give restriction to that button when user enter correct fields then connect to database.
Main Method
public class Main {
public static void main(String[] args) {
Gui obj = new Gui();
Connector conn = new Connector(obj.Setproperty());
}
}
Gui Class
public class Gui extends JFrame implements ActionListener{
private JLabel label1;
private JLabel label2;
private JTextField t1;
private JTextField t2;
private JButton ok;
private JPanel p1,p2;
Properties property;
public Gui(){
super("GUI");
p1 = new JPanel();
p1.setLayout(new GridLayout(0,2));
p2=new JPanel();
label1 = new JLabel("User Name");
t1 = new JTextField();
label2 = new JLabel("Password");
t2 = new JTextField();
ok= new JButton("OK");
ok.addActionListener(this);
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.SOUTH);
p1.add(label1);
p1.add(t1);
p1.add(label2);
p1.add(t2);
p2.add(ok);
setSize(300,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
property = new Properties();
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==ok){
}
}
public Properties Setproperty(){
property.setProperty("UNAME",t1.getText());
property.setProperty("PWD",t2.getText());
return property;
}
}
Connector Class
public class Connector {
Connection conn;
Statement state;
String username;
String pass;
String url;
public Connector(Properties p) {
username = p.getProperty("UNAME");
pass = p.getProperty("PWD");
url= "jdbc:mysql://localhost/world";
}
public boolean open() {
try {
conn = DriverManager.getConnection(url, username, pass);
state = conn.createStatement();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
e.printStackTrace();
}
if(conn==null){
System.out.println("Connection is NULL or server not connected");
return false;
}
System.out.println("Connection Successfull");
return true;
}
}
Try changing the actionPerformed method to this:
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==ok){
Properties prop = Setproperty();
Connector c = new Connector(prop);
c.open();
}
}

update/refresh combobox with glazedlists

how could i ahhm.."auto-update" my comboBox..?im using glazedlists autoComplete and im bit lost on how to do it..ive read some like use eventlists and basiclistbut i couldnt get the idea on how to make it work..
pls help :(
heres my sample code..but i dunno whats next to it..ive tried using eventlists but couldnt make it update on its own..
abc = AutoCompleteSupport.install(comboSearch,GlazedLists.eventListOf(auto));
abc.setStrict(false);
public void count(){
try{
String sql2 = "select count(*) from daily_input";
stmt = conn.prepareStatement(sql2);
rs=stmt.executeQuery();
while(rs.next()){
String x = rs.getString("count(*)");
z = Integer.parseInt(x);
}
auto = new String[z];
idNum = new int[z];
}
catch(SQLException | NumberFormatException e){
}
}
public void cB(){
count();
i=0;
try{
String sql = "Select concat(first_name, ' ',last_name) as full_name from daily_input";
stmt = conn.prepareStatement(sql);
rs=stmt.executeQuery();
while(rs.next()){
String name = rs.getString("full_name");
auto[i] = name;
i++;
}
comboSearch.isEditable();
}
You need to connect your combo-box with event list and then add elements to event list.
public class ComboBoxTest {
private final EventList<String> values;
public ComboBoxTest() {
this.values = GlazedLists.eventListOf("A", "B", "C", "D");
}
public Component createControl() {
JPanel panel = new JPanel(new BorderLayout());
panel.add(this.createComboBox(), BorderLayout.NORTH);
panel.add(new JButton(new AbstractAction("Add Elements") {
#Override
public void actionPerformed(ActionEvent e) {
ComboBoxTest.this.addElements();
}
}), BorderLayout.SOUTH);
return panel;
}
public void addElements() {
List<String> toAdd = new ArrayList<>(2);
toAdd.add("E");
toAdd.add("F");
this.values.addAll(toAdd);
}
private Component createComboBox() {
JComboBox<String> box = new JComboBox<>();
box.setEditable(true);
AutoCompleteSupport.install(box, this.values);
return box;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
ComboBoxTest testApp = new ComboBoxTest();
JFrame frame = new JFrame("ComboBox Test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(testApp.createControl());
frame.setSize(600, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}

Can not fill an array in thread

This gui should draw moving images on frame panel called "system". But first of all i need to make those objects. Here i'm trying to add them to an array and use that array in other classes. But array keeps being empty!
I guess the problem is in declaring(bottom of the code). There is no exception thrown because I let other classes to execute only when this array(Planetarium) is not empty(it should work like that, because other moves depends on planets created(those objects)). But if it is empty that means nothing is declared...
What should i do if I want to fill an array in the thread executed in event listener?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GUI extends Frame implements WindowListener,ActionListener {
cpWindow window1 = new cpWindow();
cmWindow window2 = new cmWindow();
Delete window3 = new Delete();
SolarSystem system = new SolarSystem(300,300);
Planet[] Planetarium = null; // using this to make an array
Moon[] Moonarium = null;
/**
* Frame for general window.
*/
public void createFrame0(){
JFrame f0 = new JFrame("Choose what you want to do");
f0.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f0.addWindowListener(this);
f0.setLayout(new GridLayout(3,1));
JButton cP = new JButton("Create a planet");
JButton cM = new JButton("Create a moon");
JButton Delete = new JButton("Annihilate a planet or moon");
f0.add(cP);
f0.add(cM);
f0.add(Delete);
cP.addActionListener(this);
cM.addActionListener(this);
Delete.addActionListener(this);
cP.setActionCommand("1");
cM.setActionCommand("2");
Delete.setActionCommand("3");
f0.pack();
f0.setVisible(true);
}
/**
* Frame for planet adding window.
*/
class cpWindow implements ActionListener,WindowListener{
JLabel name1 = new JLabel("Name");
JLabel color1 = new JLabel("Color");
JLabel diam1 = new JLabel("Diameter");
JLabel dist1 = new JLabel("Distance");
JLabel speed1 = new JLabel("Speed");
JTextField name2 = new JTextField();
JTextField color2 = new JTextField();
JTextField diam2 = new JTextField();
JTextField dist2 = new JTextField();
JTextField speed2 = new JTextField();
double distance;
int Speed;
double diameter;
public void createFrame1() {
JFrame f1 = new JFrame("Add planet");
f1.addWindowListener(this);
f1.setLayout(new GridLayout(6,2,5,5));
JButton mygt = new JButton("Create planet");
mygt.addActionListener(this);
name2.setText("belekoks");color2.setText("RED");diam2.setText("30");dist2.setText("60");spe ed2.setText("2");
f1.add(name1);f1.add(name2);f1.add(color1);f1.add(color2);f1.add(diam1);
f1.add(diam2);f1.add(dist1);f1.add(dist2);f1.add(speed1);f1.add(speed2);
f1.add(mygt);
f1.pack();
f1.setVisible(true);
}
public void createVariables(){
try {
distance = Double.parseDouble(dist2.getText());
Speed = Integer.parseInt(speed2.getText());
diameter = Double.parseDouble(diam2.getText());
}
catch(NumberFormatException i) {
}
Main.diametras = diameter;
Main.distancija = distance;
Main.greitis = Speed;
Main.vardas = name2.getText();
Main.spalva = color2.getText();
}
public void actionPerformed(ActionEvent e) {
createVariables();
new NewThread().start();
list.display();
}
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
public void windowClosed(WindowEvent e) {}
public void windowActivated(WindowEvent arg0) {}
public void windowDeactivated(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0) {}
public void windowOpened(WindowEvent arg0) {}
}
/**
* Frame for moon adding window
*/
CheckboxGroup planets = new CheckboxGroup();
String which;
class cmWindow implements ActionListener,WindowListener, ItemListener{
JLabel name1 = new JLabel("Name");
JLabel color1 = new JLabel("Color");
JLabel diam1 = new JLabel("Diameter");
JLabel speed1 = new JLabel("Speed");
JTextField name2 = new JTextField();
JTextField color2 = new JTextField();
JTextField diam2 = new JTextField();
JTextField speed2 = new JTextField();
JLabel info = new JLabel("Which planet's moon it will be?");
JLabel corDist1 = new JLabel("Distance from centre of rotation");
JLabel corSpeed1 = new JLabel("Speed which moon centres");
JTextField corDist2 = new JTextField();
JTextField corSpeed2 = new JTextField();
int cordistance;
int corspeed;
public void createFrame1() {
JFrame f1 = new JFrame("Add moon");
f1.addWindowListener(this);
f1.setLayout(new GridLayout(8,2,5,5));
JButton mygt = new JButton("Create moon");
mygt.addActionListener(this);
for(int i=0;i<Planetarium.length;i++){
add(new Checkbox(Planetarium[i].nam,planets,false));
}
corDist2.setText("15");corSpeed2.setText("3");
f1.add(name1);f1.add(name2);f1.add(color1);f1.add(color2);
f1.add(diam1);f1.add(diam2);f1.add(speed1);f1.add(speed2);
f1.add(corDist1);f1.add(corDist2);f1.add(corSpeed1);f1.add(corSpeed2);
f1.add(mygt);
f1.pack();
f1.setVisible(true);
}
int Speed;
double diameter;
public void createVariables(){
try {
Speed = Integer.parseInt(speed2.getText());
diameter = Double.parseDouble(diam2.getText());
cordistance = Integer.parseInt(corDist2.getText());
corspeed = Integer.parseInt(corSpeed2.getText());
}
catch(NumberFormatException i) {}
Main.diametras = diameter;
Main.greitis = Speed;
Main.vardas = name2.getText();
Main.spalva = color2.getText();
Main.centGrt = corspeed;
Main.centAts = cordistance;
}
public void actionPerformed(ActionEvent e) {
createVariables();
new NewThread().start();
}
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
public void windowClosed(WindowEvent e) {}
public void windowActivated(WindowEvent arg0) {}
public void windowDeactivated(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0) {}
public void windowOpened(WindowEvent arg0) {}
#Override
public void itemStateChanged(ItemEvent e) {
which = planets.getSelectedCheckbox().getLabel();
}
}
/**
* Deleting window
*/
class Delete implements ActionListener,WindowListener{
Checkbox[] checkB = new Checkbox[100];
public void createFrame2(){
JFrame f2 = new JFrame("Death Start");
f2.addWindowListener(this);
f2.setLayout(new GridLayout(6,2,5,5));
JButton Del = new JButton("Shoot it!");
Del.addActionListener(this);
JLabel[] planetName = new JLabel[100];
for(int i=0;i<Planetarium.length;i++){
planetName[i].setText(Planetarium[i].nam);
checkB[i].setLabel("This");
checkB[i].setState(false);
f2.add(planetName[i]);f2.add(checkB[i]);
}
}
public void actionPerformed(ActionEvent e) {
for(int i=0;i<Planetarium.length;i++){
if(checkB[i].getState()){
Planetarium[i] = null;
}
}
}
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
public void windowClosed(WindowEvent e) {}
public void windowActivated(WindowEvent arg0) {}
public void windowDeactivated(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0) {}
public void windowOpened(WindowEvent arg0) {}
}
////////////////////////////////////////////////////////
public GUI() {
createFrame0();
}
public void actionPerformed(ActionEvent e) {
if ("1".equals(e.getActionCommand())) {this.window1.createFrame1();}
else if ("2".equals(e.getActionCommand()) & Planetarium != null) {this.window2.createFrame1();}
else if ("3".equals(e.getActionCommand()) & Planetarium != null) {this.window3.createFrame2();}
}
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
public void windowClosed(WindowEvent e) {}
public void windowActivated(WindowEvent arg0) {}
public void windowDeactivated(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0) {}
public void windowOpened(WindowEvent arg0) {}
LinkedList list = new LinkedList();
class NewThread extends Thread {
Thread t;
NewThread() {
t = new Thread(this);
t.start();
}
public void run() {
Moon moon = null;
Planet planet = new Planet(Main.vardas,Main.distancija,Main.diametras,Main.spalva,Main.greitis);
if(Planetarium != null){
for(int i=0;i<Planetarium.length;i++){
if (which == Planetarium[i].nam){
moon = new Moon(Main.vardas,Planetarium[i].dist,Main.diametras,Main.spalva,Main.greitis,Main.centGrt,Main.centAts);
}
}}
int a=0,b=0;
int i = 0;
if (Main.centAts == 0){
Planetarium[i] = planet; //i guess problem is here
a++;
list.add(planet);
for(i=0; i <= i+1 0; i++) {
planet.move();
planet.drawOn(system);
system.finishedDrawing();
if (i==360){i=0;}
}
}
else{
Moonarium[i] = moon;
b++;
if(i==Main.greitis){
for(int l = 0; l <= l+1; l++) {
moon.move();
moon.drawOn(system);
system.finishedDrawing();
}}
}
}
}
}
EDIT: add linkedlist(still nothing after display) and moved declaration before infinite loop
You don't appear to have assigned these arrays anything so they should be null rather than empty.
The way you are using them a List might be better.
final List<Planet> planetarium = new ArrayList<Planet>();
planetarium.add(new Planet( .... ));
Planet p = planetarium.get(i);
for(Planet p: planetarium){
// something for each planet.
}
Note: you have to create an array before using it and you have know its length which is fixed. A list can have any length, but you still need to create it first.
Look at the loop before:
Planetarium[i] = planet;
It looks like it will loop infinitely
for(i=0; i >= 0; i++)
i will alway be >= 0.

Categories