Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I've seen similar questions with answers that didn't solve my problem.
In the following code, I can create a BookCard object and call its getAuthorField method in other classes, but my IDE doesn't seem to be finding it from the particular class that needs it.
package com.company;
import com.SourceTypeCards.BookCard;
import com.SourceTypeCards.FilmCard;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class EditSourceDialog extends JDialog
{
private JPanel contentPane;
private JButton buttonOK;
private JButton buttonCancel;
private JComboBox<Enum> typeOfSourceComboBox;
private JPanel cards;
JTextField authorNameField = new JTextField(20);
JPanel bookCard = new BookCard();
JPanel filmCard = new FilmCard();
...
private void onOK()
{
System.out.println(bookCard.getAuthorField()); //CANNOT RESOLVE METHOD
dispose();
}
}
Why is my IDE/compiler not finding this method in this particular EditSourceDialog class?
BookCard.java
package com.SourceTypeCards;
import javax.swing.*;
/**
* Created by Trevor on 14/07/2015.
*/
public class BookCard extends MainCard
{
JLabel authorLabel;
JLabel editorsLabel;
JLabel yearPublishedLabel;
JLabel titleLabel;
CardTextField authorField;
JCheckBox editorsCheckBox;
CardTextField yearPublishedField;
CardTextField titleField;
...
public String getAuthorField()
{
return authorField.getText();
}
}
Change
JPanel bookCard = new BookCard();
to
BookCard bookCard = new BookCard();
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I'm beginner of java and I study about JFrame now but I have an issue. I made a constructor with parameters of JPanel but when I invoke it with arguments, an error is happened. Could you help me to find any solutions??
import javax.swing.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.*;
public class JFlexiblePanel extends JFrame{
private Color col1;
private Color col2;
private Font font1;
private String str;
private JPanel panel1;
private JLabel label1;
public JFlexiblePanel(Color col1, Color col2, Font font1, String str) {
this.col1 = col1;
this.col2 = col2;
this.font1 = font1;
this.str = str;
panel1.setBackground(this.col1);
panel1.setForeground(this.col2);
label1.setFont(this.font1);
label1.setText(this.str);
panel1.add(label1);
}
}
In different class to invoke this constructor
JFlexiblePanel p1 = new JFlexiblePanel(Color.BLUE, Color.RED, new Font("Arial",Font.BOLD,12), "America");
Your class name implies that it's a JPanel class.
So it can't extends a JFrame, but have to extend JPanel. I think you are misunderstanding how they both work.
To be short : JFrame is the top Level of a "window" and is composed by JPanel.
Moreover, your Jpanel1 and JLabel1 are never instanciated, then the error might be here :
panel1.setBackground(this.col1);
panel1.setForeground(this.col2);
panel1.add(label1);
But if you want more help you have to give more information like the type of the error. Also consider this tutorial : link to tutorial
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I made this code in eclipse=
Strart CLASS(i made a mistace)
import javax.swing.JFrame;
public class strart {
public static void main(String args[])
{
Window object = new Window();
object.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
object.setSize(300,250);
object.setVisible(true);
}
}
Window CLASS
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.TextField;
import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.Event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Window extends JFrame {
TextField kimeno = new TextField(25);
TextField dkimeno = new TextField(25);
TextField n1 = kimeno;
TextField n2 = dkimeno;
private JButton plus;
public Window()
{
super("Math engine");
setLayout(new FlowLayout());
plus = new JButton("+");
plus.setFocusable(false);
plus.addActionListener(new EnableButton());
add(plus);
add(kimeno);
add(dkimeno);
}
public class EnableButton implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println(n1+n2);
}
}
}
But i am still getting an error in line 32 of the Window class
the error is "The operator + is undefined for the argument type(s) java.awt.TextField, java.awt.TextField"
The + operator makes no sense for TextField objects. You want
System.out.println(n1.getText() + n2.getText());
What would it mean to add two TextField Objects? The + operator in Java can only be used with operands that are numbers or strings. (Well, that's simplifying it a bit you can look up the exact rules in the Java language specification.)
If you want to calculate the sum of two numeric strings stored in the TextFields (I assume integers) you can use something like this:
Integer.parseInt(n1.getText()) + Integer.parseInt(n2.getText())
Of course, you should add some error handling.
Have you tried
System.out.println(n1.getText() + n2.getText());
The '+' opperator when used with Strings will concatenate them together.
In this case you are not dealing with Strings, but TextField's, which have the .getText() function attached to get the String representation of their value.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
private JPanel textField = new JPanel();
public class Moves extends JFrame implements Observer {
public Moves(final CLASS1 class1){
textField.setLayout(new BoxLayout(textField, BoxLayout.LINE_AXIS));
textField.add(new JTextField("Moved: " + hm.getMoveCount()));
}
public void update(Observable o, Object arg){
textField.setText("Update: " + hm.updateMoves());
}
}
below the update method, .setText(String) is undefined for the type JPanel. How would I fix this?
My bad. I was wrong. I wish I can delete this. I put JPanel instead of JTextField.
Thank you.
Solution for the people:
private JTextField textField = new JTextField();
public class Moves extends JFrame implements Observer {
public Moves(final CLASS1 class1){
textField.setLayout(new BoxLayout(textField, BoxLayout.LINE_AXIS));
textField.add(new JTextField("Moved: " + hm.getMoveCount()));
}
public void update(Observable o, Object arg){
textField.setText("Update: " + hm.updateMoves());
}
}
JPanel doesn't have a method setText(String).
You might be looking for setTitle(String) in JFrame.
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.
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.