Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
My JDialog throws a NullPointerException when I access it through Jframe but works fine when I run the JDialog class itself.
I have a jMenuItem called "modify" in my jFrame that accesses jDialog.
private void modifyActionPerformed(java.awt.event.ActionEvent evt){
Modify mod = new Modify(this,true); //"Modify" is my jDialog class name.
mod.setVisible(true);
}
Modify throws a NullPointerException when I query the database when I access the dialog from jframe but I can query the database successfully from Modify when I run the class itself.
please Check your code somthing else in your code .
check Modify Class.
This Simple example shows JDialog opening.
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
class DiagonalLineDemo extends JFrame implements ActionListener {
public DiagonalLineDemo()
{
setVisible(true);
setSize(100,100);
JMenuBar s=new JMenuBar();
JMenu m=new JMenu("Open ");
JMenuItem s1=new JMenuItem("Dialog");
m.add(s1);
s.add(m);
setJMenuBar(s);
s1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
JDialog j=new JDialog(this,true);
j.setVisible(true);
}
public static void main(String args[]) {
DiagonalLineDemo f=new DiagonalLineDemo();
}
}
Open Frame click Menu open and MenuItem Dialog.
After Click Dialog The DialogBox is shows.
Fixed it. Turns out I just needed to set my jDialog database connection to non-static so I can initialize the class from my main interface. Thanks anyway.
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
Im stuck today with a java error on an eclipse 3.8 platform.
I try to generate a simple MenuBar on a JFrame. The JMenuBar contains the JMenu("Help") and the JMenu contains a JMenuItem("Quit"). The class extends a JFrame. With the function (this.)setJMenuBar(MenuBar); I try to set my MenuBar to my JFrame, which should work fine as long I just have one MenuBar.
GUI-class code:
import javax.swing.*;
import java.awt.*;
public class Taschenrechner extends JFrame{
//Instanzvariablen im Zusamenhang mit dem Menu
private JMenuBar MenuBar;
private JMenu MenuHelp;
private JMenuItem MenuHelpQuit;
public Taschenrechner() {
super();
//Flaeche festlegen
this.setSize(300,300);
//Menukomponenten definieren
MenuHelp= new JMenu("Help");
MenuHelpQuit=new JMenuItem("Quit");
//Menukomponenten zusammensetzen
MenuHelp.add(MenuHelpQuit);
MenuBar.add(MenuHelp);
this.setJMenuBar(MenuBar);
}
}
my main-class code (declare objekt and setVisible):
public class Taschenrechnerstart {
public static void main(String[] args) {
Taschenrechner taschenrechner1 = new Taschenrechner();
taschenrechner1.setVisible(true);
}
}
Now on starting the code I get a NullPointerException-error in the GUI-class on line:
MenuBar.add(MenuHelp);
and a NullPointerException-error in the main-class on the line:
Taschenrechner taschenrechner1 = new Taschenrechner();
Does someone have an idea why my code isn't working?
MenuBar.add(MenuHelp);
You are accessing the MenuBar variable without initializing it.
You are missing a
MenuBar = new JMenuBar (..);
BTW, your code would be more readable if you use Java naming conventions (variables should start with a lower case letter).
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
How do I? Can I?
public class Jbutton {
public static void main (String[] args){
JButton button = new JButton("I'm colorful!");
button.
What do I do here ^ to set the button color?
}
}
You have 2 options either use setBackground or setIcon E.g.
button.setBackground(myColor);
button.setIcon(new javax.swing.ImageIcon(pathToIcon));
Using your example:
public class Jbutton {
public static void main (String[] args){
//You're going to need some context for this like a JFrame
JButton button = new JButton("I'm colorful!");
button.setBackground(Color.RED);
}
}
Though I think for a more enlightening section of code, you might explore This Question to see how some parts of swing and AWT work.
I believe its like so
{
JButton btn = new
JButton(String.valueOf(i));
btn.setBackground(Color.BLACK);
btn.setForeground(Color.GRAY);
p3.add(btn);
}
Using my phone so trouble editing it to post code, that's what I used on a previous project though if that helps
I just have a very basic question about how to use textfields in Java. It's very simple, but the tutorials and other questions I've been looking for haven't been helpful, and I'm hoping that someone can explain things a little more clearly for me.
Right now I have the following code that I just sort of slapped together for the sake of example:
import javax.swing*;
public class testText {
public static void main(String[] args){
JFrame frame = new JFrame();
JTextField text = new JTextField();
frame.add(text);
frame.setVisible(true);
System.out.println(text.getText());
}
}
All I'm trying to do, is print what the user types into the text field in the console. But nothing happens when I type into the text field.
Now,based on the research I've done, I think the problem is that I'm not using an actionListener. The thing is, I really don't understand how those work, and I'm hoping someone can clarify for me.
I've been using this tutorial to try and figure things out, and particularly the TextDemo example they have near the top. I'm still kind of at a loss though, and I can't seem to find any way to use the actionlistener interface without breaking the program. If someone could either just explain simply and directly how to use the actionlistener to pull a string from a text field and then use it, or else point me to somewhere else where I can FIND a simple straightforward explanation, I would immensely appreciate it. I've been beating my head against this for five hours now with absolutely nothing to show for it, so I apologize for asking such a basic question but I'm at a loss.
An action listener will be called when an enter key is pressed while typing in the field. From the JTextfield Javadoc :
How the text field consumes VK_ENTER events depends on whether the
text field has any action listeners. If so, then VK_ENTER results in
the listeners getting an ActionEvent, and the VK_ENTER event is
consumed.
Here is your example modified to work with an action listener :
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class testText {
public static void main(String[] args){
JFrame frame = new JFrame();
final JTextField text = new JTextField();
frame.add(text);
frame.setVisible(true);
text.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println(text.getText());
}
});
}
}
And here is an object oriented complete example not relying only on a static main method.
It's Giving me an error saying that "The method setContentPane(Container) in the type JFrame is not applicable for the arguments (GamePanel)"
Here is my Code:
package main;
import javax.swing.JFrame;
public class Game {
public static void main(String[] args){
JFrame window = new JFrame("Dragon Tales");
window.setContentPane(new GamePanel());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
}
}
I am following a tutorial exactly and his screen shows no errors at all.
Your GamePanel class does not extend any Swing GUI component such as Container or one of its children. Probably it should extend JPanel.
i.e.,
import javax.swing.JPanel;
public class GamePanel extends JPanel {
// .... etc
}
Please don't add the urgent or "help as soon as possible" bit. Yes your question is very important, but it is no more important than anyone else's.
Edit: Mad's link is worth putting in the answer: The Oracle Swing Tutorial.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I've been using StackOverflow for a while now but this is my first question, so please exchuse me if something comes or seems not up to par.
I'd like to create a JPanel object (here referred to as 'panel') in a different class (ClassFrom) and then have it show in another JFrame object (here referred to as 'frame') in another class (ClassTo) but there seems to be something off with what I have so far since the JPanel 'panel' won't show in the JFrame 'frame' when the JLabel is clicked.
Could anyone out there please look at my code and please help me out where possible. I would like Will really appreciate.
Heres my code.
import javax.swing.JFrame;
// The Class with the JFame that gets the components from ClassFrom
public class ClassTo {
JFrame frame = new JFrame("The JFrame");
ClassFrom classFrom = new ClassFrom();
public static void main (String[] args)
{
// This is where there seems to be a problem
frame.add(classFrom.contentMethod());
}
}
import javax.swing.JLabel;
import javax.swing.JPanel;
// The Class with the components to be added to the JFrame in ClassTo
public class ClassFrom {
public static void contentMethod() {
JPanel panel = new JPanel();
JLabel label = new JLabel("Try Label");
panel.add(label);
}
}
The problem is with a static method trying to access instance variables. Declare frame and classFrom as static or in your main method to solve the compilation error.
Edit: As Ingo pointed out change the return type of contentMethod to JPanel and return that panel so it can be added to the frame variable.