set title to JFrame - java

I am new to Java. My problem is that I have a class names MyClassExp. I have extended it from JFrame. Inside the class, I initiate an object of another class named TabbedFrame. TabbedFrame also extends a class DemoFrame . In DemoFrame, I have the title of the page set using keyword 'super' like:
super("Some Title");
Now when I run my MyClassExp, even after creating a JFrame as:
new JFrame("New Title");
I'm still getting the same title i.e Some Title. Is there any way to solve this problem? I've tried a lot to solve it but failed :'(

Use the API method public void setTitle(String title).

- Inside the MyClassExp class's constructor use this.setTitle(String title) method.

Just use setTitle("yourTitleName");
for example:
setTitle("Currency Converter");
textField.addKeyListener(this);
combo.addItemListener(this);
label.addMouseListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Related

moving a JPanel from main to simple class

Well I have a Calculator ready in my main class, but I want now to move it in a class but not in main, and when I do it I get some errors. I can create my JPanel with name asd but I can't use any of this:
asd.setTitle("Calculator");
asd.setSize(200, 250);
asd.setResizable(false);
I get an error "package asd does not exist"! I have to make the calculator again or am I doing something wrong?
Seems like you created the Jpanel using command
JPanel asd = new asd();
instead of
JPanel asd = new JPanel();
EDIT: based on the code in the comments, its because you're calling methods globally, you can only instantiate objects globally.
If you want to generate entire panel in the constructor, your code should look like this:
public class kompiouteraki{
// attributes here
public kompiouteraki(){
// your code here
}
}
For more information, about how to set up your class, visit class structure

Java JFrame class cannot be viewed

I created a new JFrame class in java and added GUI elements and provided action functionality
to these buttons.When I open later I cannot view the JFrame class I created, instead it is like a
notepad File.Help me solve this!!!
frames in java usually set to not being visible , you have to change this setting , to do so you have to set in visible in your main function.
should be like this
public static void main (strings[] args) {
frame.setVisible(true);
}

Java JFrame and another class interaction

I've found the similar question, but it's still be unclear for me.
So, I have a main class ProcessorCalculations(), from which I call MainFrame() class. In MainFrame class user should choose the folder. How I can transmit the JFileChooser() object from MainFrame() to ProcessorCalculations()?
I've tried to implement the hint from the link above:
ProcessorCalculation processor = new ProcessorCalculation();
MainFrame mainFrame = new MainFrame(processor);
But I don't know how to call processor methods from mainFrame without creating new objects.
Even I dont't know the correct question I should ask Google.
Help please.
If you're using the code written above, then you're passing the current processor instance into your MainFrame constructor. What are you doing with the reference from within this constructor? Are you settinga a ProcessorCalculation instance to this reference? Please show us your constructor.
Your MainFrame class should look something like...
public class MainFrame extends JFrame {
// your ProcessorCalculation field
private ProcessorCalculation processor;
public MainFrame(ProcessorCalculation processor) {
// set the field with ref passed in parameter
this.processor = processor;
// of course other code goes here
}
public void someMainFrameMethod() {
// use the reference
processor.someProcessorMethod();
}
}
Create an attribute say for example files in the mainframe by which the contents of JFileChooser() are referenced ( you may say contents are stored in this attribute ). If this attribute is private put getter setter methods in the Mainframe for this attribute ( to make it accessible from other classes) now coming back to your ProcessorCalculation class when you write mainFrame.getFiles() ( you have already created object mainFrame object there) it returns the data you wanted in this class.
In case you still face a problem please ask for a coded solution I will do.

Java - Add an instance of an abstract class in another instance

I have this piece of code :
public class Profile extends Container{
public Profile(String value) {
Container profilo_1 =new Container();
Container profilo_2 =new Container();
// (1) THIS ADD A BUTTON TO THE MAIN CLASS
this.createButton().setLabel("Modifica Profilo");
// (2) NOW I NEED TO ADD A BUTTON INTO THE INSTANCE OF profilo_2 BUT IT FAILS
profilo_2.add(this.createButton());
this.add(profilo_1);
this.add(profilo_2);
}
}
the point (2) fails, because it said that im about to adding a child to this container, but it is owner already by a container...
In fact, if i do this :
ILabel label=new ILabel();
profilo_2.add(label);
it said to me that ILabel() is abract and cannot be instantiated!
How can I fix it? Cheers to everybody :)
Guessing wildly, since this depends on your code... Try this (moreless what Piotr said)
profilo_2.add(profilo_2.createButton());
Try changing to
Button button2 = this.createButton();
button2.setLabel("EDIT");
profilo_2.add(button2);
By the way this has nothing to do with abstract classes, from what I see
EDIT: Though you say that #1 "adds a button to the main class", so does that mean that createButton() does this.add(button) ? If so then you should probably change that function so that isn't done every time you create a button.
The problem is probably that when you create a button with "this.createButton", that button has its parent set to "this" (in this context), and when you try to add it to profilo_2, it throws an error. Instead you should createButton on profilo_2 directly, then the parent will be the correct one (and perhaps you won´t have to add() it either?)
Probably, setLabel() returns something which cannot be passed to Container::add(..). Please provide your code for Container

Hava a java panel load another jpanel

I'm using netbeans to create a GUI application. I've made a main form with a panel that I want other jPanels I make to be placed in. It seems like this should be simple to do seeing as the create new context menu allows me to make plain java panels. I've made all the variables public on the new frame also.
EDIT:
I have a separate class extending the jPanel that is public, I'm trying to load it into a panel I have on the main GUI using the following code:
private void qmcatActionPerformed(java.awt.event.ActionEvent evt) {
qmcat jQmcat = new qmcat();
jQmcat.setVisible(true);
jPanel.add(jQmcat);
}
I was using the wrong method, it is .add(name, then component) , this is sad.

Categories