JFrames/Objects access between two different projects - java

I have two separate projects, A and B, added the jar file of B in A's Libraries using netbeans. A has two JFrame (BasicFrame1 and BasicFrame2) and B has JFrame (Newframe). When i tries to make visible BsicFrame2 and unvisible BasicFrame1 it all works coz they are in the same project. But whenever i tries to make visible the project B's JFrame (Newframe) by clicking on the button in Project A's BasicFrame1 it does not show anything.
Newframe connenct=new Newframe();
this.BasicFrame1.setVisible(false);
Newframe.setVisible(true);
Please help me, why this not working and the same case when i try to access the Newframe's jPanel (butPanel).
Newframe connect=new Newframe();
connect.butPanel.setVisible(true);
This code also does not works but when i do this between the project A's Frames it works. What's the mistake i have done? Please help.

Related

How do I add a java program to a weebly site?

I'm experimenting with Weebly, and I'm currently trying to add an arbitrary swing program to the Weebly editor. I have tried two approaches thus far, as shown here:
1: (Note: - replaces < and >)
-embed height=400 width=400 src="siteName/uploads/someNumbers/testapplet.class"--/embed-
2: (Same substitution as above)
-applet codebase="siteName/uploads/someNumbers" code="testapplet.ckass" width=400 height=400--/applet-
Upon publishing and viewing the page, the first one says I need a plugin to display the content, and the second one says my security preferences won't let me run java on the site, whereas I can run java pretty much everywhere else just fine.
What should I do to make this work? This could include some of the following:
Modifying my program (i.e. the java code itself)
Modifying how I upload the program (i.e. .class vs .jar)
Modifying how I display the program (i.e. the actual -applet- or -embed-)
For reference, here is the java code- just a basic JButton and JLabel, with the JLabel's value increasing upon each click of the button:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class TestApplet extends JApplet
{
static int x = 0;
static JLabel l = new JLabel(x+"");
public void init()
{
setLayout(new FlowLayout());
JButton b = new JButton("Button");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
x++;
l.setText(x+"");
}
});
add(b);
add(l);
}
}
Sign in to weebly and click on the edit button next to your site name
On the "Elements" tab on the top, go to "Multimedia" and drag a file element onto your page.
Follow directions to upload your .class file. Name should be lowercase.
Hit the publish button and go to your published page.
Remember your .class file? Right-click on it and copy the link address or link location.
Go back to editing your weebly page.
On the "Elements" tab, go down to "More" and drag in the "Custom HTML" element.
Click to edit the custom HTML element- this is where we are going to put our applet code.
If my link location to my .class file was http://www.johndoe.com/uploads/3/3/2/6/3326331/countme.class, my applet code would read:
<applet codebase="http://www.johndoe.com/uploads/3/3/2/6/3326331" code="countme.class" width=something height=something></applet>
The idea is that the codebase tells the browser where to look for a .class file and the code itself tells it whick .class it is.
10. Publish again, and your applet should appear.
I haven't tried it personally, but I had a chat with a support support staff and they affirmed this method.
All the best!

Access Panel from Main Form from a Panel Form

I am trying to access a panel from my Main JForm which I use as a dynamic panel that repaints different Panel Forms.
this is the hierarchy of the forms.
Package_Main-Main_Form(JForm)-Dynamic_Panel(Panel)-DashBoard(Panel_Form)
Package_Panels-Panel_A-Panel_B
What I did was Display JForm first and repaint its JPanel with DashBoard.
now heres the problem.
I have a button inside my dashboard and when I tried to import Package_Panels.PanelA, but it doesn't work properly.
heres the first code I tried:
This code was added inside DashBoard Button.
PanelA x = new PanelA ();
this.removeAll();
this.add(x);
this.revalidate();
this.repaint();
x.setVisible(true);
I had doubts about this one since I knew if i use this.function, it points out the DashBoard Panel.
anyone knows a way to access the JForm's panel?
UPDATE: I found a way around this by using this.getParent() but other ideas are welcome. I don't to be stuck doing this over and over again.
heres what I did:
Panel_A x = new Panel_A();
this.setVisible(false);
this.getParent().add(x);
this.getParent().revalidate();
this.getParent().repaint();
x.setVisible(true);
//this.getParent().remove(this); - Does this really work? I dont want to keep this instance open.

Open an url in a new tab from a Jpanel inside a JApplet

So my Applet work like that :
The main .class is extended from JApplet, therefore it can be use as an applet.
This main class load other .class file to display new window, that are extented from JPanel.
This current set up work fine as an applet, however, in one of my JPanel class I have a button that open an URL. I use the Desktop API and it works fine on the browser, the problem is : It opens the URL in the same tab as the applet.
I would like the URL to open in a new tab from my JPanel. I know I can use something like :
AppletContext a = getAppletContext();
URL url = new URL(link);
a.showDocument(url,"_blank");
but the method getAppletContext() only work from a class that has extented JApplet, not a JPanel.
I have tried to change my Jpanel to a JApplet but that seems to create some kind of mess.
Any idea how I could achieve that ?
Thank you !
I haven't tried it, but two possibilities come to mind:
Pass the AppletContext to your JPanel as a constructor parameter.
Set the target attribute in the URL, as shown here.

Working with two projects-

I have developed a basic desktop-app using Netbeans IDE, on the app I have a button called Ok. Now I have created another project, its a CRUD-app. Is there a way to make the ok-button in my 1st project to run the CRUD-app in another project- or how do I link tha ok-button to open/run the crud-app? In a different project..Project names are pro1 and CruD-test-
Below is a sample-code for my button:
OK.addActionListener(new ActionListener(){
//please add a link to the CRUD-app contained in project Crud-test
#Max: I wanna run pro1 on the IDE and run the CruD-test as jar...how do I link mu OK-button to the CruD-test?
Basically it is going to be code as :
OK.addActionListener(new ActionListener(){
MyCRUDApplication crudApp = new MyCRUDApplication(); //This is only an example, keep a gloabal reference as needed
//assuming MyCRUDApplication constructor would create and show its main window.
//Otherwise you will have to provide the relevant class that really shows the project.
}
For this, you should add MyCRUDApplication application as a referenced project in Netbeans. It is explained here

Why my Swing based GUI application is not responding?

I am trying to create my first GUI application using (Java + Eclipse + Swing). This is my code:
import java.awt.*;
import javax.swing.*;
public class HelloWorldSwing extends JFrame {
JTextArea m_resultArea = new JTextArea(6, 30);
//====================================================== constructor
public HelloWorldSwing() {
//... Set initial text, scrolling, and border.
m_resultArea.setText("Enter more text to see scrollbars");
JScrollPane scrollingArea = new JScrollPane(m_resultArea);
scrollingArea.setBorder(BorderFactory.createEmptyBorder(10,5,10,5));
// Get the content pane, set layout, add to center
Container content = this.getContentPane();
content.setLayout(new BorderLayout());
content.add(scrollingArea, BorderLayout.CENTER);
this.pack();
}
//============================================================= main
public static void main(String[] args) {
JFrame win = new HelloWorldSwing();
win.setTitle("TextAreaDemo");
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setVisible(true);
}
}
The code was taken from here.
When I run the application from Eclipse the expected window appears (So, it's good. I see what I want to see). However, when I try to close the window or try to write something in the text area the program freezes. The OS writes me that program is not responding (I try it on Ubuntu).
Can anybody help me to find the reason of the problem?
Thank you in advance for any help.
I'm sure this doesn't have to do with the code, as others have found the code runs just fine on their machines - which points to a machine specific issue. From within Eclipse, make sure it is setup to use the expected JDK/JRE. However, before worrying about how Eclipse is handling your situation, I'd run things by hand first - especially since you've got a very simple class.
I would check to ensure that you're using the expected compiler and runtime. On Linux:
which javac
which java
If they're both what you expect, do the following:
javac HelloWorldSwing.java
java HelloWorldSwing
If you get a similar problem, then you know it's not the Eclipse configuration and it's something else. If you're not using the latest JDK, upgrade to the latest. If you're already at the latest, it could be a display driver. Do other JAVA swing programs work on this computer? I'm sure you could find some on the net, download an app already packaged as a jar and try running it.
did you try using the eventdispatcherthread to view the JFrame?
something like:
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
createAndViewJFrame();
}
});
}
public void createAndViewJFrame(){
JFrame win = new HelloWorldSwing();
win.setTitle("TextAreaDemo");
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setVisible(true);
}
then your frame would be shown by the swing dispatcher thread.
hope it helped, although im just guessing...
Update: as commenters pointed you i f**ed up the invokeLater() call. I just edited this post to correct that. Thanx go to yishai & willcodejavaforfood for pointing it out!
frank
You need to catch the exit event and
respond with a System.exit( 0 );
You should be able to find that in
most swing examples online.
wrong stuff... sorry... coffee... argh....

Categories