JFrame help

S

StillillWill

So my question is if I build a JFrame can I use a button in that JFrame
to open a .java file in the same folder?
 
A

Andrew Thompson

(e-mail address removed) wrote:

Sub: JFrame Help

Note that 'Help' is redundant in titles, and 'JFrame'
does not tell us much, a better title might be
'load file from same folder, possible?'
So my question is if I build a JFrame can I use a button in that JFrame
to open a .java file in the same folder?

Yes.

Andrew Thompson
 
S

StillillWill

Thanks Andrew,


public class GUIMain extends javax.swing.JFrame {


public GUIMain() {
initComponents();
}


private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();

getContentPane().setLayout(null);

setTitle("Main Menu");
setName("Main Menu");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});

jLabel1.setText("Main Menu:");
getContentPane().add(jLabel1);
jLabel1.setBounds(150, 10, 64, 20);

jButton1.setText("Customer");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});

getContentPane().add(jButton1);
jButton1.setBounds(10, 40, 90, 26);

jButton2.setText("Vehicle");
jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton2MouseClicked(evt);
}
});

getContentPane().add(jButton2);
jButton2.setBounds(110, 40, 76, 26);

jButton3.setText("Rental");
jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton3MouseClicked(evt);
}
});

getContentPane().add(jButton3);
jButton3.setBounds(200, 40, 70, 26);

jButton4.setText("Close");
jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton4MouseClicked(evt);
}
});

getContentPane().add(jButton4);
jButton4.setBounds(280, 40, 66, 26);

pack();
java.awt.Dimension screenSize =
java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setSize(new java.awt.Dimension(365, 120));

setLocation((screenSize.width-365)/2,(screenSize.height-120)/2);
}

private void jButton4MouseClicked(java.awt.event.MouseEvent evt) {
// Add your handling code here:close
System.exit(0);
}

private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {
// Add your handling code here:rental
}

private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {
// Add your handling code here:vehicle
}

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
// Add your handling code here:customer
Process p = rt.exec("GUICustomer.class") ;
}

private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new GUIMain().show();
}


private javax.swing.JLabel jLabel1;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;

}

That's my code and I'm trying to make jButton1open a .java file but
cant figure out the code to do it, any idea?

Thanks again.
 
A

Andrew Thompson

Andrew said:
(e-mail address removed) wrote:

Sub: JFrame Help

Note that 'Help' is redundant in titles, and 'JFrame'
does not tell us much, a better title might be
'load file from same folder, possible?'


Yes.

I saw a later post from you that had code, and
went to reply to it, did you cancel it?

In any case, since you were apparently OK
to the point of acting on button clicks, here
is a simple (and kludgy) way to do what you
were trying to achieve.

<sscce>
import java.io.File;
import javax.swing.*;
import java.awt.Dimension;

public class LoadMe {

public static void main(String args[]) throws Exception {
File source = new File(".","LoadMe.java");
JEditorPane jep = new JEditorPane(
source.toURI().toURL() );
JScrollPane sp = new JScrollPane(jep);
sp.setPreferredSize(new Dimension(400,200));
JOptionPane.showMessageDialog(null,sp);
}
}
</sscce>

Comments on the code I wrote.
This is probably not the way I would go about
'loading a Java file from the current directory',
but it would depend on what I intended doing
with the Java file, e.g.
- whether I ever might want to load a Java file
from *another* directory,
- handle mutliple Java files at the same time, or
- later potentially want to save an altered copy
back to disk...

Comments on the code I saw (that you posted)..
- The code would initially not compile, because of the
reference to a Process which was not declared,
please ensure posted code is compilable.
- .show() was deprecated in Java 1.2 - consult
the JavaDocs for replacements.
- .setBounds() is the sign of a very fragile GUI,
for reliable GUI's, see layouts in the Java Tutorial,
to arrange GUI elements in a robust way.
- When posting the code, it really only needed
1 button (as opposed to the four it had) and no labels,
the SSCCE document discusses why these extra bits
should best be trimmed while concentrating on the
problem. Please read it.

Andrew T.
 
S

StillillWill

Thanks again Andrew you've been more help then you know.

Actually as you said there are many pages to the GUI, four to be exact
and I was working on trying to get them to open either in the same
window or in other windows. Each different window is a .java file and
was unsure about how to load them. So I'm going to try to get your idea
to work but like I said thanks very much for your help.

On a seperate note I tried to use a layout but couldn't find one that
caters for that sort of form do you know one?
 
L

Lew

Each different window is a .java file and
was unsure about how to load them.

What exactly do you mean by "each different window is a .java file"?

Do you mean "each different window displays the contents of a .java file"?

- Lew
 
S

StillillWill

Sorry,

I mean that I have written 4 different .java files and they are all
part of a GUI, different windows if you like.

Each of these windows has buttons on them. I am trying to make the
buttons, when clicked, open one of the other .java files.

Does that make more sense?

Will
 
L

Lew

I mean that I have written 4 different .java files and they are all
part of a GUI, different windows if you like.

Each of these windows has buttons on them. I am trying to make the
buttons, when clicked, open one of the other .java files.

Does that make more sense?

Still not 100% clear what you mean by "open one of the other .java files". A
..java file is a text file containing program source lines, usually. The usual
act upon opening a text file is to present it for viewing or editing.

If you are referring to the classes you wrote, presumably compiled from these
text files, it would make sense to describe the behavior of the program you
wish to evince, which is not to "open" a class at all. Instead it is, perhaps
but I cannot tell from your post, to open a window (displaying what?), or to
invoke a business-logic action like a database change.

Without a clear description of what you intend to do it is well-nigh
impossible to provide meaningful advice. Provide the action description in
terms of the user experience; I guarantee you that the program user does not
experience a .java file that's behind any of your classes. I also guarantee
you that a .java file is not "part of a GUI". And if you have different
windows, then you have them whether I like or not. A window is an objective
phenomenon.

- Lew
 
S

StillillWill

Gosh I'm really sorry to make this so long for you, and thanks for your
help.

Yeah I mean to open a class that I have compiled from a .java text file
that does pretty much the same thing as the original class, open new
classes with buttons and save text fields to a file.

I am making a GUI for a program that has navigation buttons. I've
built each of the GUI frames or 'windows' into different class files.
So I want the buttons in each frame to access another class file, how
do I do that?

If it would help I could mail you the code.

Thanks for your help,
Will
 
L

Lew

Gosh I'm really sorry to make this so long for you, and thanks for your
help.

No worries - just asking the questions I need to ask to understand what you're
asking.
Yeah I mean to open a class that I have compiled from a .java text file
that does pretty much the same thing as the original class, open new
classes with buttons and save text fields to a file.

Just to narrow down the issue, what is the class declaration from one of these
other window classes?

I am asking for the part that looks a little like

public class SomeClassName {

I'm guessing that inside this class you have a line something like

JFrame frame = ...;

or

JInternalFrame frame = ...;

and probably another line that looks like

JButton someButton = ...;
I am making a GUI for a program that has navigation buttons. I've
built each of the GUI frames or 'windows' into different class files.
So I want the buttons in each frame to access another class file, how
do I do that?

I am guessing that you have a button handler in your "main" window that you
want to cause some action in another window, or else that you have a button
handler inside your other window that you want to cause an action in the main
window. Is that what you mean so far?

"Access" is such a vague word. The answer really depends on the kind of access
you intend, in other words, the action and reaction sequence you mean to happen.

Guessing very deeply on the basis of what I infer through wild speculation, I
wonder if you might have structured your classes to be a bit too autonomous,
almost as if each one is too separate. Without a behavioral model of your
application it is impossible to really say, but multiwindow-multiclass systems
usually communicate through the usual listeners and by forwarded methods from
the custom class to a contained Swing component.

- Lew
 
A

Andrew Thompson

On a seperate note I tried to use a layout but couldn't find one that
caters for that sort of form do you know one?

A nested layout, with a FlowLayout in the NORTH
of a BorderLayout for the JLabel, and a GridLayout
in the CENTER for the buttons.

Andrew T.
 
M

Mark Space

Lew said:
"Access" is such a vague word. The answer really depends on the kind of
access you intend, in other words, the action and reaction sequence you
mean to happen.

I agree with Lew, it's darn hard to guess what you actually intend to do
here.
multiwindow-multiclass systems usually communicate through the usual
listeners and by forwarded methods from the custom class to a contained
Swing component.

Lew assumes that you want to load a file and execute it. Run some code.
I assume that too.

I can see a couple of different ways of doing this. Using listeners is
correct and the usual way of doing it.

If you don't know what code is going to be run, however, you might need
a ClassLoader to find and load the class up. If you have bits of code
that are supplied by two different groups, this is one way to join them
up. One bit of code loads the other with a class loader.

The other possibility is to use Reflection. This is what programs like
Tomcat, Netbeans, and Eclipse use to "peak inside" other bits of code
and understand it's internal structure.

Let us know if any of these ideas are close to the mark.
 
I

Ian Wilson

Gosh I'm really sorry to make this so long for you, and thanks for your
help.

Yeah I mean to open a class that I have compiled from a .java text file
that does pretty much the same thing as the original class, open new
classes with buttons and save text fields to a file.

I am making a GUI for a program that has navigation buttons. I've
built each of the GUI frames or 'windows' into different class files.
So I want the buttons in each frame to access another class file, how
do I do that?

Is the following right?

Your application has four windows.
When first started one of those windows is displayed.
When a button on that window is pressed, one of the other four windows
is displayed.
What happens to the original window - remains open or is closed?

Each window is like
+-----------------+
| A |
| some content |
| [a] [c] [d] |
+-----------------+
Where the buttons "a" through "d" cause the corresponding window to be
opened (or maybe brought to the front)
Presumably button "a" is omitted from Window A, b from B etc.

Presumably you already rejected CardPanel as a design?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top