Guy Shahar

Greenhorn
+ Follow
since Dec 23, 2005
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Guy Shahar

Thanks Gregg

Yes, I know about the API - my only problem is that when there is something I want to do, it is not always obvious where in the API I need to look to find it....
18 years ago
Thanks Nick

This works and the frame disappears. Does it really free all the resources of the frame?

I ask because when I clicked for the frame to be recreated, it appeared in the same place on the screen as it had been when I clicked for it to be closed. (or maybe this was within the memory of the object??)

Thanks

Guy
18 years ago
Probably a very obvious answer to this one, but I can't find it.

I want to close a JFrame during the running of a program without killing the object that created it. I have looked for a frame.close() or frame.exit() method, but they don't seem to exist.

Is there an easy way of doing this?
18 years ago
I am trying to make an application that delegates some of the JFrame components to other classes, as in the simple example below, but while I would expect four sections to show up, there is only one. Can anybody help?

public class RunTest{

JFrame frame = new JFrame();
Container cont = frame.getContentPane();
ArrayList<Test> tests = new ArrayList<Test>();
Test test1 = new Test();
Test test2 = new Test();
Test test3 = new Test();
Test test4 = new Test();

public static void main(String[] args){

new RunTest().makeGUI();
}

public void makeGUI(){

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);

tests.add(test1);
tests.add(test2);
tests.add(test3);
tests.add(test4);

for (Test t: tests){

t.makePanel();
cont.add(t.getPanel());
}


frame.setVisible(true);
}
}



public class Test{

JPanel panel;
private JLabel label;

public void makePanel(){

panel = new JPanel();

JButton option1 = new JButton("Option 1");
JButton option2 = new JButton("Option 2");
label = new JLabel();

panel.add(option1);
panel.add(option2);
panel.add(label);

option1.addActionListener(new Hello());
option2.addActionListener(new GoodBye());
}

public JPanel getPanel(){

return panel;
}


public class Hello implements ActionListener{

public void actionPerformed(ActionEvent a){

label.setText("Hello");
}
}

public class GoodBye implements ActionListener{

public void actionPerformed(ActionEvent a){

label.setText("Goodbye");
}
}
}
18 years ago
FORGIVE ME - I MADE A STUPID MISTAKE AND FORGOT TO ADD THE TEST OBJECTS TO THE ARRAYLIST. THERE IS STILL A PROBLEM, BUT I WILL START A NEW THREAD TO DISCUSS IT. SORRY!!!
18 years ago
I am trying to make an application that delegates some of the JFrame components to other classes, as in the simple example below, but only an empty JPanel is returned.

public class RunTest{

JFrame frame = new JFrame();
Container cont = frame.getContentPane();
ArrayList<Test> tests = new ArrayList<Test>();
Test test1 = new Test();
Test test2 = new Test();
Test test3 = new Test();
Test test4 = new Test();

public static void main(String[] args){

new RunTest().makeGUI();
}

public void makeGUI(){

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);

for (Test t: tests){

t.makePanel();
cont.add(t.getPanel);
}

frame.setVisible(true);
}
}




public class Test{

JPanel panel;
private JLabel label;

public void makePanel(){

panel = new JPanel();

JButton option1 = new JButton("Option 1");
JButton option2 = new JButton("Option 2");
label = new JLabel();

panel.add(option1);
panel.add(option2);
panel.add(label);
}

public JPanel addPanel(){

return JPanel;
}
}

I have also tried rewriting the addPanel() method as below:

public void addPanel(RunTest r){

r.cont.add(panel);
}

but it makes no difference.

Am I doing something wrong, or is there no way to delegate parts of the GUI to other objects?
18 years ago
Thanks Michael - I hadn't understood last time that this was how it worked and that was the key line. So I just need to update the data model after the Vector is updated. Neat.
18 years ago
A JComboBox has constructors that can take a Vector or a ComboBoxModel.

When I pass a DefaultComboBoxModel to it, and change the contents of the DefaultComboBoxModel, the drop-down list changes as expected. No problem.

However, if I pass a Vector to it and change the contents of the Vector, the drop-down list goes blank. It is still there, because if I click on any point in the blank space, the most recent option is called and the list is then visible.

I have tried calling validate() on the JComboBox, but it makes no difference.

I want to experiment with generics, and cannot do so with DefaultComboBoxModel. Can anybody tell me a simple way to use Vectors in Swing without weird results???
18 years ago
Thanks Michael. I have messed around a bit and found a way to make it work (not sure how).
18 years ago
I am trying to make a drop-down JComboBox with its options taken from a Vector. The problem is that when I add a new String to the Vector, the JComboBox becomes empty rather than displaying the updated options.

I had the same problem with a JList, but for this I was able to substitute a DefaultListModel for a Vector and it worked fine. Unfortunately, this option does not seem to be available for a JComboBox.

Can anybody help???
19 years ago
Thanks to the excellent Head First Java, the Sun Certified Programmer and Developer book and my housemate (who gave me 3 large-scale projects to practise what I was learning), I passed certification this morning with 88%, despite not knowing what Java was in September....

However, I don't know whether I should write this on a CV, as I don't know how this compares with others. The exam seemed to be much easier than any simulator I have tried. I know several people who have scored in the eighties and nineties, and nobody who has scored below that. Should I put my 88% on my CV, or would it just be drawing attention to an average grade?
I am preparing to take the Java Certification (Programmer) exam next month, and as part of my prep, my java-hot friend has suggested some applications for me to make to see how the java concepts work in practice.

One of them is a simple simulation of a supermarket ordering and selling procedure. Unfortunately, my friend is now far too busy at work to look at my code and give me feedback on it, so I am appealing here for some kind volunteer to look over it for me.

As far as I can tell, it does what it is supposed to do and I haven't found any bugs in it. What I would really like is feedback on the structure. I have tried to make the application as flexible as possible, but even so, I cannot think how to structure it to make it possible to add or delete more lines of stock without some rewriting of the code.

There are about 30 classes, but most of them are very small, and about half are subclasses of StockItem, with the same few lines of code. It would be greatly appreciated.

My email address is [email protected]

Thank you.
19 years ago
The main method is like any other method, except that it is the one that will run automatically when you call your program.

Each method can take "arguments" which are listed in brackets after the name of the method. These arguments can provide information for the method to use.
For example - public void printSomething(String s){System.out.print(s);}
When you call this method, you may type in:
printSomething("Hello");
and the method with print the word "Hello" on the screen.

The main method takes as an argument an array of Strings, which is traditionally given the name args, though it could be called anything (for example, "main(String[] potatoCakes)" would be valid aswell). The array comes from anything that the user types AFTER the name of the class at the commandline. Normally, to start an application, you would type:
java MyApp
And anything you type after that is an item in the String array. So if I type:
java MyApp Potatoes Cakes PotatoCakes Mmmmm
Then the String[] args array would contain 4 elements:
args[0] = "Potatoes"
args[1] = "Cakes"
args[2] = "PotatoCakes"
args[3] = "Mmmmm"
and these can be used within the main method by referring to their reference in the array. e.g. System.out.print(args[1]);
19 years ago
Yes, it would be better to use style = "display: none;" (and then "display: block;" to show them) rather than style = "visibility: hidden;". The only problem is then that if BOTH of the elements are displayed or BOTH are not displayed, then it could affect the positioning of everything else. An alternative would be to use CSS absolute positioning to place the elements in the same position as each other, and then you would have the flexibility to use the "visibility" option, and the whole thing would be independent of any other element on the page. It would mean moving away from using tables though......
Thanks Maximillian

Yes the scripting sort of grew to quite a large size (though you should see the size of the HTML.....)

Anyway, you inspired me to do some more investigating, and I found that the problem was with the line:

document.getElementById('css').href = css + 'css.css';

(which simply refers to the HTML line
link id = "css" rel = "stylesheet" type = "text/css" href = "mainshortcss.css" / )

Now, the site has 4 stylesheets: maincss.css, mainshortcss.css, largecss.css, largeshortcss.css. Here is some strange information about how things seem to be working:

1) With Mozilla and Netscape browsers there are no problems at all and everything is working fine.

2) With Internet Explorer, the stylesheets maincss.css and largecss.css are working fine.

3) In IE, if maincss.css or largecss.css are called using the javascript line above, they result in the browser freezing, BUT....

4) The problem cannot be with the stylesheets themselves because if maincss.css or largecss.css are called through the HTML link tag, everything works fine, even in IE.

What is going on....?