Jpanel does not display what it supposes to - java

I have created a class that extends JPanel using eclipse, now i added this class to netbeans through creating a new JFrame and dragging this class to the Jframe using the Netbeans designer. now at run time, the Camera stream do not show on the jpanel despite the same code was runnning in eclipse.
is there any attribute i ahve to modify its value in the netbeans designer to get the jpanel working?
Code:
private static class CamThreadRun implements Runnable {
#Override
public void run() {
camStream();
}
}
private static void camStream() {
while(vidCap.grab()) {
grappedMat = new Mat();
vidCap.retrieve(grappedMat);
if (!grappedMat.empty()) {
BufferedImage buffImg = mat2BufferedImage(grappedMat);
if (buffImg != null) {
facePanel.setFace(buffImg);
facePanel.repaint();
}
}
}
}

Can't remember exactly but Netbeans creates some kind of .form file that doesn't get recognised in eclipse. It's best to design it yourself with the code as it will work on both IDEs instead of using Netbeans GUI designer which works best for Netbeans.

Related

jMenuItem doesn't appear

I've just starting using Java Swing and I have a issue.
I tried to do a simple menuBar and a menuItem 'Exit', but before linking the button to the action the menuItem appeared, now that I've linked the button to a System.exit(0) action it disappeared. Help?
The code is the following:
in MainPanel (the autogenerated code from swing is excluded):
public void init() {
initComponents();
initActions();
setLocationRelativeTo(null);
pack();
setVisible(true);
}
private void initActions() {
this.menuItemExit.setAction(Application.getInstance().getPanelControl().getActionExit());
}
In PanelControl:
public class PanelControl {
private Action actionExit;
public Action getActionExit() {
return actionExit;
}
public class ActionExit extends AbstractAction{
public ActionExit(){
putValue(Action.NAME, "Exit");
putValue(Action.SHORT_DESCRIPTION, "Exit from the application");
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl e"));
putValue(Action.MNEMONIC_KEY, KeyEvent.VK_E);
}
#Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
In Application:
private void init() {
viewMainPanel = new MainPanel();
controlPanel = new ControlPanel();
viewMainPanel.init();
}
i think the problem is somewhere in here but i can't figure out where. any help?
(there's other code but i just put the more relevant part, also i translated the code from italian so i'm sorry if there are any problems or a few names dont match up)
private Action actionExit;
public Action getActionExit() {
return actionExit;
}
Your actionExit variable is null.
Nowhere in your code do you create an instance of your ActionExit class.
Somewhere you need:
actionExit = new ActionExit();
Your design seems a bit complicated, I have no idea why you have a panel just to create an instance of the ActionExit class.
I would suggest you just create the ActionExit instance in your main class and get rid of the PanelControl class.
Instead of using an IDE to generate confusing code you should consider learning how to write the code yourself so you can better structure your classes. Read the section from the Swing tutorial on How to Use Menus for a working example to get you started.
A menu item has to be added to a Native Java Swing component. You have to add it to a JFrame. You can't add a MenuItem to a Panel. The Parent 'root' container in any Java Swing application is 'native' and a JFrame. Everything else in that container is 'drawn' into the rectangle using the look and feel of your choosing.
Then you CREATE a MenuItem using your TAbstractAction item. That object CAN be used to create a JButton, JMenuItem or ToolBar button. Keeping a reference to your TAbstractAction in your code, you can enable/disable the object and it implements an 'observable' pattern where it will enable/disable ALL UI controls you used to build with it. I actually wrote a Java Swing framework for doing Java Applications. It used to be on the Sun Open Source web site. If you wish I can put it up on GitLab for you to play with. Java Swing is nice but JavaFX should be the long term goal for UI on a JVM.
In your JFrame object you need to do this:
_menuBar = new JMenuBar();
// add controls to the frame
setJMenuBar(_menuBar);
Then you need to add your 'exitMenuItem' to your _MenuBar control.
Cheers

Embed PApplet intp JApplet

How can I embed a PApplet into a JApplet ?
I wanted to add it to a JPanel inside the applet, but I couldn't.
If any of you know how I can do this. ??
As of Processing 3, you can no longer do this. PApplet no longer extends Applet, so it can't be treated as a component.
For 95% of users, this is okay. Applets are dead anyway, so you really shouldn't be using them. If at all possible, you should try deploying with Processing.js.
If you need to execute a Processing sketch from Java code, then you should use the PApplet.main("YourSketchNameHere"); function to launch it. Processing will take care of the window for you.
If you really need to treat a PApplet as a component, then you're going to have to go through its PSurface. The PSurface class contains a getNative() function that returns an object that can be treated as a component. But that's overkill for most Processing users.
Prior to Processing 3, this code should have worked for you as #Kevin has explained. So, if your question is directed towards understanding a legacy code here is what you will need to know:
import javax.swing.JFrame;
import javax.swing.JPanel;
class MyPApplet extends PApplet implements ActionListener{
#Override
public void setup() {
super.setup();
// setup
}
#Override
public void draw() {
// my draw code
}
}
public class PAppletDemo {
public static void main(String[] args) {
final JFrame frame = new JFrame("PApplet in Java Application");
JPanel panel = new JPanel();
//create an instance of your processing applet
final MyPApplet applet = new MyPApplet();
applet.init();
panel.add(applet); // From processing 3, this will give you error that applet is not a Component
frame.add(panel);
frame.setSize(applet.getSize().width, applet.getSize().height +200);
frame.setVisible(true);
}
}
To circumvent this, you will need to use PSurface getNative() function. Please refer to the example and discussion given on this link.

Why my program works on JFrame but not on JApplet?

I've got a project in Java (developped with Eclipse), in which I use some Swing components. At the start, I worked with JFrame : I created a new class herited from JPanel (PanelTr), added in it my JLabels, JTextField and JButton with an ActionListener on my button, and added an object PanelTr to my JFrame-herited class. Here's a piece of my code :
package gui;
public class PanelTr extends JPanel implements ActionListener {
// Here I instantiate my JTextField and JButton used by ActionListener
private JTextField textCap = new JTextField(20);
private JButton submitButton = new JButton("Submit");
public PanelTr() {
// Here I add my JLabels and my layout manager
submitButton.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
if (evt.getSource()==submitButton)
{
JOptionPane.showMessageDialog(null,"DEBUG"); // Just to see if it is displayed
String err = Analyzer.process(textCap.getText().toString()); // An analyzing process I use in my program
if (err=="")
{
JOptionPane.showMessageDialog(null,"OK");
}
else
{
JOptionPane.showMessageDialog(null,err,"Error",JOptionPane.ERROR_MESSAGE);
}
}
}
}
And my JFrame :
package gui;
public class FrameTr extends JFrame
{
public FrameTr()
{
PanelFils ctn = new PanelFils(); //Conteneur
setContentPane(ctn);
ctn.setBackground(new Color(0,255,255));
setTitle("Project");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String args[])
{
new FrameTr();
}
}
In my Analyzer class, I use several Excel files located in the root folder of my project.
So with this code, my program runs well, on Eclipse and when I export it into a runnable jar (I place my Excel files in the same folder of my .jar file).
But now I need to make an Applet which allows the program to run on a web browser. So I created a new JApplet-based class with just this code :
package gui;
public class TestApplet extends JApplet{
public void init()
{
PanelTr ctn = new PanelTr();
setContentPane(ctn);
}
}
When I run this class on Eclipse, my debug JOptionPane is displayed, but in the analyzing process I get a FileNotFoundException on my first Excel File. I tried to move it to the src folder, to the gui folder, even to the analyse folder (package of my Analyzer class), but still this FileNotFoundException...
I also tried to run my program in a webpage, so I exported as a runnable jar (Note : I couldn't choose TestApplet in my launch configuration since it doesn't have a main(), so I choosed FrameTr). I used the tag :
<applet archive="app.jar" width="700" height="100" code="gui/TestApplet.class"/>
My pane is displayed, but when I click on my button, the debug JOptionPane doesn't pop out !
So first, how can I solve the FileNotFound problem ? And then, why my JOptionPane isn't displayed on my web browser ?
Applets are restricted and can't access File System (with exception for signed applets).
If the files inside your jar you can use them as resource. Instead of using e.g. FileInputStream use this.getClass().getResourceAsStream("/Countries.xlsx") (the Countries.xlsx should be in the root)
The Java Applet is executed client side - it runs on the computer of the person using the website. It does not run on the web server.
If you have files in the server then the client will not be able to access them unless you serve them up from your web server and access them via HTTP(S).
If the value in the files are constants then you should put the files inside your JAR and distribute them as part of the Applet.

Method called in constructor is not working after built in java netbeans

Here while I run java project in netbeans all things are working okay. But after they are built there is not any item added in combobox as it works during netbeans run. The sample code is given below.
First Login JFrame
public class Login_Frame extends javax.swing.JFrame {
welcome w = new welcome();
public Login_Frame() {
initComponents();
}
//button action perform event for dispose this window and open new welcome window
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
.
.
w.setVisible(true);
this.dispose();
.
.
}
}
Second JFrame
public final class welcome extends javax.swing.JFrame {
// comboitem is class in which method for adding item in combobox from sqlite
db is declared
comboitem c = new comboitem();
// textclass is class in which method for changing lowercase text entered in
text to uppercase is declared
textclass tc = new textclass();
public welcome() {
// while I try to run the project using netbeans run project option
// logincall() method initialized and work fine.
Problem
After project built when I try to run the jar file from the cmd. it runs without any
error but logincall() method doesn't work or may be not initialized.
initComponents();
logincall();
.
.
}
public void logincall(){
//Remarks
//tc.uppercase() method is working fine after built. But other c.but_stn() like
// doen't.while during running project through netbeans all thing working fine.
c.bus_stn();
c.bus_trl();
c.inq_stn();
c.editframe();
c.userlist();
c.editTrainStation();
c.editFlightStation();
c.flightFlight();
c.pickupstand();
tc.uppercase();
}
I didn't know what is wrong with it. I searched on google but didn't find any proper answer.
There also any error showing up in netbeans. Please fill free to ask any questions if more information is needed. I appreciate all your replies.
This is my Welcome Main class's main method.
public static void main(String args[]) {
...
look and feel auto generated code
....
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new welcome().setVisible(true);
}
});
}
A couple of things that might be an issue here:
1) Are you running your GUI on the EventDispatchThread? this is mandatory for java swing GUI to be able to work properly. The main reason for this is concurrency. Details here.
2) Are you re-rendering your combobox? it is important that you do this because changes to GUI elements may not be immediately shown.
3) What Database are you using? in order to determine if the fault lies in the code or the DB you could write a test using static data, if it loads in the IDE and outside of it chances are that your code is correct but the DB isn't

Trying to run Program in Ecplise as a class × 259989

I am a newbie in the Java world and this is among my first programs (Hello World, obviously!). The problem is when I try to run this program as a class from the menu with a play icon on it, a blank window shows up with no "Hello World" on it (just white colour filling the window). At the bottom border of this window there is a black thick line. But when I run this program as an applet from the same menu everything is okay and the "Hello World" shows at the right position and everything is fine. But how can I make the program run regularly as a class???
This is my code...
import acm.graphics.*;
import acm.program.*;
import java.awt.*;
public class HelloProgram extends GraphicsProgram {
private static final long serialVersionUID = 1L;
public void run() {
GLabel label = new GLabel("hello, world", 100, 75);
label.setFont("SansSerif-36");
label.setColor(Color.RED);
add(label);
}
}
I have no idea about acm, but looked in this page: http://jtf.acm.org/tutorial/Introduction.html
As you see there GraphicsProgram is a subclass of JApplet and because of that it's supposed to be run as applet, not desktop application.
Try this. It will be a separate class, but you will be able to run your program from this one. Please let me know if you are confused.
public class Runner{
public static void main(String[] args){
HelloProgram p = new HelloProgram();
p.run();
}
}
I agree with publ1c_stat1c, your program is missing the "main" method for it to be considered a standalone application.
Create an instance of your application
HelloProgram hello = new HelloProgram();
call the run method of the instance
hello.run();
the main method doesn't have to be in a different class, try adding below codes in-between "run method" and "private static final long serialVersionUID = 1L;"
public static void main(String[] args){
HelloProgram hello = new HelloProgram();
hello.run();
}

Categories