Program Not Updating (Closed) - java

I solved the problem. I was using outdated code later in the program.
I have changed the Title to "Sign Up", but it still says "login" at the top from when I previously had it at that. Also the size of the window is 300,300 not 500,1500. This is just the part of the code with this problem. I use eclipse.
SignUpScreen()
{
super("No Layout Manager");
setLayout(null);
setTitle("Sign Up");
setSize(500,1500);
show();
}

Without seeing all of your code, I can only venture a guess. But from experience, the issue might be the following line:
setLayout(null);
Try not to use null layouts.

I'm assuming that you're working in Swing and trying to extend a JFrame? Here is a simple program that does this:
package com.example;
import javax.swing.JFrame;
public class HelloWorldSwing {
public static void main(String[] args) {
HelloWorldSwing helloWorldSwing = new HelloWorldSwing();
helloWorldSwing.execute();
}
private void execute() {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SignUpScreen();;
}
});
}
private class SignUpScreen extends JFrame {
private static final long serialVersionUID = 1L;
SignUpScreen() {
super("No Layout Manager");
setLayout(null);
// setTitle("Login");
setTitle("Sign Up");
// setSize(300, 300);
setSize(500, 1500);
show();
}
}
}
Changing to the commented-out setTitle and setSize lines changes the title and size of the window for me, on Save. If you paste this code into your environment, and it doesn't change on Save, check Project -> Build Automatically as mentioned in the comments.

Related

Error: Main method not found in class, please define the main method as: public static void main(String[] args)

I tried running this java code, and it came up with the following error:
Main method not found in class p16, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
My Code:
//button events
//on pressing a button color changes
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code = p16 width= 140 height=140>
</applet>
*/
public class p16 extends Applet implements ActionListener {
String msg = "";
Button first, second;
public void init() {
first = new Button("yes");
second = new Button("no");
add(first);
add(second);
first.addActionListener(this);
second.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
String str = ae.getActionCommand();
if (str.equals("yes")) {
first.setBackground(Color.red);
second.setBackground(Color.white);
msg = "pressed yes";
} else {
second.setBackground(Color.blue);
first.setBackground(Color.white);
msg = "pressed no";
}
repaint();
}
public void paint(Graphics g) {
g.drawString(msg, 13, 12);
}
}
Applet cannot be run from command line like common Java applications.
You need to add applet to a webpage, read online about that.
With a little effort, you can turn your applet into an application. You can then continue working there.
Here are the changes:
//button events
//on pressing a button color changes
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// Change the class name so that it complies with Java naming conventions.
// Extend a JPanel instead of an Applet
public class P16 extends JPanel implements ActionListener {
String msg = "";
Button first;
Button second;
// Add a main() method that creates a JFrame
public static void main(String[] args) {
JFrame frame = new JFrame();
// Set a new Instance of your Class as ContentPane
frame.setContentPane(new P16());
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(400, 300));
frame.pack();
frame.setVisible(true);
}
// Make your class constructor out of the Init () method
public P16() {
first = new Button("yes");
second = new Button("no");
add(first);
add(second);
first.addActionListener(this);
second.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
String str = ae.getActionCommand();
if (str.equals("yes")) {
first.setBackground(Color.red);
second.setBackground(Color.white);
msg = "pressed yes";
} else {
second.setBackground(Color.blue);
first.setBackground(Color.white);
msg = "pressed no";
}
repaint();
}
#Override
public void paint(Graphics g) {
g.drawString(msg, 13, 12);
}
}
Maybe that's a help.
On Netbeans IDE: You can right click the class from application folder and choose run file ,But it will give you same error if you pressed run button from toolbars
On Eclipce : it runs either ways
run button from toolbars
right click the class from application folder and run as java applet
This worked for me:
Install NetBeans IDE 8.2
Click on New File
Choose categories: Java, File Types: Applet
Give Class Name for your Applet
Write your code
In project section of IDE select your Applet, right click and choose Run File

Issue with Dialog Modality in Swing in java 1.6

I have 3 dialog that gets displayed together in my project.
First dialog is modeless with setVisibleOnTop(false).
Second dialog is modeless with setVisibleOnTop(true).
Third dialog is Application Modal with setVisibleOnTop(true).
Now the issue is :
Ideally when there is a dialog "third" opened with APPLICATION_MODAL property then no other JComponent should accept the click. This works fine with java 1.7.
With java 1.6 is I click on dialog "one" then dialog "second" goes at the back of dialog "one". Whereas dialog "third" is still opened.
Now the question is:
Why dialog "one" comes in front when there is an APPLICATION_MODAL dialog (third) opened?
Why second dialog with property setAlwaysOnTop(true) goes at back?
I believe this is a issue with java 1.6. Does anyone know about this?
Is this bug documented somewhere?
Sample Code:
import java.awt.Frame;
import javax.swing.JDialog;
class MyDialog1 extends JDialog {
MyDialog1 ()
{
super();
super.setVisible(false);
setTitle("one");
}
}
class MyDialog2 extends JDialog {
MyDialog2 ()
{
super(null,ModalityType.MODELESS);
setAlwaysOnTop(true);
setTitle("second");
}
}
class MyDialog3 extends JDialog {
MyDialog3 ()
{
super(new Frame(),ModalityType.APPLICATION_MODAL);
setTitle("third");
setAlwaysOnTop(true);
super.setVisible(false);
}
}
public class ModalityIssue {
public static void main(String args[])
{
MyDialog1 d1=new MyDialog1();
d1.setSize(600, 600);
MyDialog2 d2=new MyDialog2();
d2.setSize(500, 500);
MyDialog3 d3=new MyDialog3();
d3.setSize(400, 400);
d1.setVisible(true);
d2.setLocationRelativeTo(d1);
d2.setVisible(true);
d3.setLocationRelativeTo(d2);
d3.setVisible(true);
}
}
dont to use awt components
JFrame ignores alwaysOnTop and modality,
use Initial Thread,
important is code ordering too
you can to use aplication modality (seems like as better, but nobody knows if meets with your requirement/s)
MyDialog1 always flashing (MyDialog2 is painted before MyDialog1, then jumps behing MyDialog2, standard users can't catch that), maybe there aren't any JComponents added to any of Top-Level Containers in the current JVM
for example (Java 1.6.21 / 025 / 031, the same corerctly works in 1.7.04 and 1.8.60 / 66 / win10)
import java.awt.Dialog.ModalityType;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
class MyDialog1 extends JDialog {
public MyDialog1() {
super(null, ModalityType.MODELESS);
setAlwaysOnTop(true);
setTitle("fist");
}
}
class MyDialog2 extends JDialog {
public MyDialog2() {
super(null, ModalityType.MODELESS);
setAlwaysOnTop(true);
setTitle("second");
}
}
public class ModalityIssue {
private JFrame frame = new JFrame();
private MyDialog1 d1 = new MyDialog1();
private MyDialog2 d2 = new MyDialog2();
public ModalityIssue() {
frame.setTitle("third");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setLocation(100, 100);
frame.setVisible(true);
d1.setSize(400, 300);
d1.setLocation(200, 200);
d1.setVisible(true);
d2.setSize(400, 300);
d2.setLocation(300, 300);
d2.setVisible(true);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new ModalityIssue();
}
});
}
}

Java JFrame gui - Why won't the buttons show up?

I've just started learning Swing/JFrame, basically making a GUI.
I've been doing Java for a month now, just using the console, making a sin/true or false games and it is pretty easy for me now.
I decided to take a further step, and I must say it's totally a pain, different logic.
That's what I've done so far:
Main.java:
import java.awt.*;
import javax.swing.*;
import java.io.*;
class Main {
public static void main(String[] args) {
final Gui gui = new Gui();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
gui.createMyGui();
}
});
}
}
gui.java
class Gui {
protected JFrame j = new JFrame("My First window");
protected JPanel p = new JPanel();
protected Container c;
public Gui() {
j.setSize(500, 400);
p.setSize(j.getSize());
this.c = j.getContentPane();
}
public void createMyGui() {
setButtons();
setGuiBackground();
j.setVisible(true);
p.setVisible(true);
this.c.add(p);
}
private void setGuiBackground() {
this.c.setBackground(Color.green);
}
private void setButtons() {
p.add(new JButton("Hey"));
}
}
Problem
I can't really get the button to show up, people are telling me to use setBounds but I am not really sure on how to start as I can't even place a button there. I've tried searching about my problem, but no luck actually.
Basically what happens is a 500x400 green GUI opens, and that's it.
Why won't the button show?
people are telling me to use setBounds
Dont! Layout managers are the correct way to go.
Your problem is you add your buttons to the "p" panel, but you never add it (p panel) to the contentPane

Enabling Swing Menus on OSX with Java7

I have a swing application in which I need to enable a JMenu from a different thread. I'm doing this on OSX and am using the native screen menu via apple.laf.useScreenMenuBar. Since switching to java 7 the initially disabled menus now never become enabled and I can't figure out why. I've attached a small program which illustrates the problem. Clicking on the Fixed > Change menu item should enable the test menu after a brief pause (a dialog should open and close).
Using java6 it works fine. In java 7 the menu is not enabled. If I don't use the screen menu it works in 6 or 7, and if I use EventQueue.invokeAndWait it works in 6 or 7, but I don't think I should need to do this.
Is this a bug, or am I mis understanding how interactions between swing threads should work?
import java.awt.EventQueue;
import java.awt.event.*;
import javax.swing.*;
public class MainWindow extends JFrame implements ActionListener {
private JMenu testMenu;
public MainWindow () {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Menu Enable Test");
JMenuBar bar = new JMenuBar();
JMenu fixedMenu = new JMenu("Fixed");
JMenuItem change = new JMenuItem("Change");
change.addActionListener(this);
fixedMenu.add(change);
bar.add(fixedMenu);
testMenu = new JMenu("Test");
testMenu.setEnabled(false);
JMenuItem seeMe = new JMenuItem("Can you see me?");
testMenu.add(seeMe);
bar.add(testMenu);
setJMenuBar(bar);
setSize(800,600);
setLocationRelativeTo(null);
setVisible(true);
}
public void makeVisible () {
testMenu.setEnabled(true);
}
public void actionPerformed(ActionEvent ae) {
new RemoteChanger(this);
}
private class RemoteChanger extends JDialog implements Runnable {
private MainWindow window;
public RemoteChanger (MainWindow window) {
super(window);
setSize(200,100);
setLocationRelativeTo(window);
this.window = window;
setVisible(true);
Thread t = new Thread(this);
t.start();
}
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
EventQueue.invokeLater(new Runnable() {
public void run() {
window.makeVisible();
}
});
setVisible(false);
}
}
public static void main (String [] args) {
System.setProperty("apple.laf.useScreenMenuBar", "true");
new MainWindow();
}
}
I think the source of the issue is your setVisible(false) being outside of the Swing thread. Moving it inside of the invokeLater() call seems more correct and gives you the expected behavior.
EventQueue.invokeLater(new Runnable() {
public void run() {
window.makeVisible();
setVisible(false);
}
});

Make a Java application invisible to a user

I'm trying to figure out a way to make a Java application invisible to the user.
Basically just trying to remove this
<- Image
How can this be done?
public class TransparentWindow extends JFrame {
public TransparentWindow() {
initComponents();
}
#SuppressWarnings("unchecked")
private void initComponents() {
setExtendedState(Frame.MAXIMIZED_BOTH);
setResizable(false);
setUndecorated(true);
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
setAlwaysOnTop(true);
System.setProperty("sun.java2d.noddraw", "true");
WindowUtils.setWindowTransparent(this, true);
WindowUtils.setWindowAlpha(this, 0.6f);
}
public static void main(String[] args) {
new TransparentWindow().setVisible(true);
}
}
I just seems to have found the answer, just put the line setVisible(false); into comments and you will see the actual program, UNCOMMENT the line to see no trace is left, as far as I can see, that the Java Program is running somewhere, until you won't add the icon to your system tray, manually. Moreover how to remove your Application from Task Manager that question still remains, though you can remove the said icon, as pointed by you in your question.
import javax.swing.*;
public class TransparentWindow extends JFrame
{
public TransparentWindow()
{
initComponents();
}
#SuppressWarnings("unchecked")
private void initComponents()
{
setExtendedState(JFrame.MAXIMIZED_BOTH);
setResizable(false);
setUndecorated(true);
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
setAlwaysOnTop(true);
setOpacity(0.8f);
setSize(200, 200);
//System.setProperty("sun.java2d.noddraw", "true");
//WindowUtils.setWindowTransparent(this, true);
//WindowUtils.setWindowAlpha(this, 0.6f);
setVisible(true);
setVisible(false);
JOptionPane.showMessageDialog(this, "It is working!", "Guess : ", JOptionPane.INFORMATION_MESSAGE);
}
public static void main(String[] args)
{
TransparentWindow tw = new TransparentWindow();
}
}
Here is a snapshot of my desktop on running this program, see the taskbar
Extend from JWindow insted of JFrame. (I did not test this on Windows 7 as I don't sit in front of a Windows box right now. It worked for XP and works for Unity, which surprised me.)
As far as I know, there is no way to remove the task bar icon.

Categories

Resources