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
Related
I am working in eclipse making a temperature converter app. I made a JButton btnConvert. However, when I go to make the event listener it tells me btnConvert cannot be resolved.
I noticed it isn't showing up where the other controls are listed on the outline either and I clearly have it in my code:
JButton btnConvert = new JButton("Convert");
Can anyone help? I have everything imported that needs to be imported and didn't run into any issues until this.
If the button was only created in a method, then it will only be visible within the scope of that method, and if you're trying to access it outside of the method it was created in, it will say it can't find the button. Instead, you may want to use an instance variable, it would look something like this:
public class WhateverYourClassNameIs
{
public JButton btnConvert;
public static void main(String[] args)
{
//define the instance variable, this doesn't have to be in main
// it can be wherever you are initializing the code you're working on
btnConvert = new JButton("Convert");
}
}
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);
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.
Although I've used Swing before I've never used a GUI designer and I'm having trouble accessing components I've dropped onto my panel from within my source code.
I created a new project and chose to create a GUI form. I then created the main method using the 'generate' option and now I have this code in my 'helloWorld.java' file.
public class helloWorld {
private JPanel myForm;
private JLabel text;
public static void main(String[] args) {
JFrame frame = new JFrame("helloWorld");
frame.setContentPane(new helloWorld().myForm);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(800, 600));
frame.pack();
frame.setVisible(true);
}
}
I then added a JLabel in the designer with the field name title which added an attribute to the head of my helloWorld class. I now want to set the text on the field name after the program has run.
If I create the JLabel instance with a new string as an argument and add it to my JFrame then the program crashes with a null pointer exception.
If I create a JLabel with no arguments and call setText on it and then repaint on the JFrame, nothing happens.
I guess to some up my problem in a single line: How do you access components that I have created using the GUI designer?
First, IntelliJ is a bit special in that it hides a lot of the boilerplate code for you, so your source code looks actually simpler than what is really going on under the hood.
Basically, when you use the IntelliJ GUI builder, you end up with a source code corresponding to your form that looks like this:
public class DialogEditView {
private JPanel mainPanel;
private JLabel labelDescription;
private JLabel labelExample;
private JComboBox comboboxDEJC;
}
To be able to access these, you can simply add getters to that source file:
public class DialogEditView {
private JPanel mainPanel;
private JLabel labelDescription;
private JLabel labelExample;
private JComboBox comboboxDEJC;
public JPanel getMainPanel() {
return mainPanel;
}
// etc.
}
Once again, IntelliJ either modifies the source code or modifies the class files automagically for you (you can go in the Settings / GUI Builder to test the two options and see what they do).
How do you access components that I have created using the GUI
designer?
You can go to the source code file corresponding to your GUI and add getters. Make sure to name your components...
The automatically generated initialization code in your binding class looks like:
private void $$$setupUI$$$() {}
For more informations about IntelliJ's Initialization Code see this Jetbrains documentation: Creating Form Initialization Code
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.