How to add an object from another class [closed] - java

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.

Related

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

How do I set a JButton's fill color? [closed]

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

The method setText(String) is undefined for the type JPanel [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 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.

JDIalog throws NullPointerException when accessed from JFrame [closed]

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.

Categories