Class with JFrame [duplicate] - java

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

Related

JRadioButton: How to overload an abstract method [duplicate]

This question already has answers here:
Java throwing error "<Class Name> is not abstract and does not override abstract method in the <Interface>"
(3 answers)
Closed 6 years ago.
I'm trying to implement ActionListener so that my GenderIsStored method of my GenderGUI class can record the number of times a user selects each JRadioButton that represents a gender, with each iteration of the loop in the driver class of my program. However, I cannot get my GenderGUI class to compile properly, due to the fact that I cannot figure out how to override the abstract method. The GenderCount class is implementing the ActionListener interface, which is abstract. Originally, I thought that in order to compile correctly, I would have to implement all the methods inside the parent interface (which is ActionListener), but the same problem occurred. Does anyone know how I can solve this problem?
public GenderGUI()
{
GenderChoice = new JLabel ("Select your gender below: ");
Male = new JRadioButton ("Male");
Female = new JRadioButton ("Female");
ButtonGroup GenderGroup = new ButtonGroup();
GenderGroup.add (Male);
GenderGroup.add (Female);
GenderCount listener = new GenderCount ();
Male.addActionListener (listener);
Female.addActionListener (listener);
primary = new JPanel();
primary.add (GenderChoice);
primary.add (Male);
primary.add (Female);
}
public JPanel getGenderPanel()
{
return primary;
}
private class GenderCount implements ActionListener
{
public void GenderIsStored (ActionEvent event)
{
Object source = event.getSource();
if (Male.isSelected())
{
MaleCount++;
}
else
FemaleCount++;
}
}
}
Your GenderCount clearly doesn't implement ActionListener.
You need anactionPerformed(). Actually looks like you have one, but you have given it the wrong name: GenderIsStored instead of actionPerformed

Nothing happens when I try to apply an ActionListener to a JButton to change the content of the JLabel

I need a way to create an ActionListener that when a JButton is pressed, it updates the content of 7 different JLabels to display the information in the form of text.
The data is retrieved from methods called from an external JAR file. The methods return ArrayList. I attempted to convert the ArrayList into a String, and tried to change the JLabel content with setText().
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import api.anAPI.THEAPINAME;
public class Controller implements ActionListener {
private MainGUI maingui;
private SubPanel subpanel;
private static THEAPINAME anAPI =new THEAPINAME("XyP0D75oRCGrLE78","x47ka5jmOGaJ2zvw");
static ArrayList<String> nameList =new ArrayList<String>();
private String names;
public Controller(MainGUI maingui,SubPanel subpanel){
this.maingui = maingui;
this.SubPanel = subpanel;
MainGUI.getSearchBtn().addActionListener(this);
nameList.addAll(anAPI.getNames());
for (String s: nameList){
names+= s+"\t";
}
}
public void actionPerformed(ActionEvent e) {
SubPanel.label1.setText(names);
//6 more Labels.
}
}
An additional, because I have 7 JLabels, would I need to do 7 getLabel methods? Or is there a way to get them all with just 1 method.
I am not entirely sure what I am doing incorrectly, it could be that the getMethods I used returned the wrong widget in question as the code for the GUI was not done by me but by a teammate and he had done a really poor job of making it clear for us.
UPDATE:
Fixed up the GUI to make it clearer, so I think that is no longer the problem. Now I think the problem might be that I did not convert the contents of the ArrayList into a String in the way I thought.
The desired function of the code is when the JButton is clicked on, the JLabels in question are all updated to their relevant data.
addController method
public void addController(Controller controller){
control = controller;
jb1.addActionListener(control);
}
You didn't really describe what the problem is of your current code.
You can add a method say getLabels() in SubPanel class to return all of its labels, or you can add a method setLabelText(String text) to set text for all of its labels by extending or directly modifying SubPanel class.
UPDATE
You have several very confusing parts in your code.
In your constructor, it should be this.subpanel = subpanel and then it should be maingui.getSearchBtn().addActionListener(this), also in method actionPerformed it should be subpanel.label1.setText(names). These might not be your problems though since you didn't say it's the code you're actually running.
Looks like that you haven't created any instance of class Controller thus the code in it never gets executed.
You need to have some code outside of you Controller class like this:
MainGUI maingui;
SubPanel subpanel;
// they're somehow initialized
Controller controller = new Controller(maingui, subpanel);

Java is not finding my class's method [closed]

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();

java null pointer exception JMenuBar [duplicate]

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).

How to add an object from another class [closed]

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.

Categories