Accessing components created in GUI designer on IntelliJ - java

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

Related

Open JFrame, only after successfull login verification with database. Using Eclipse?

what I'm trying to do is open up a main application from a login screen, only after the login information has been verified in the connected database.
Using Eclipse, what I have so far:
database.java: connection to MS Access Database using UCanAccess. (Success)
login.java: A login window that extends JFrame. When a username and password is entered, it is verified with the database. (Success)
Home.java: The main application window, that I want to only be accessible with a correct username and password. Does not extend JFrame, but has a JFrame within it.
Now, I have been able to set it up so that if the entered username and password are correct, a window pops up saying "Successful login". However, how do I approach setting it up so that after the successful login, it opens up Home.java?
I have looked at:
Open a new JFrame - I have tried the setVisible with my home but Eclipse returns an error saying to create a setVisible method in Home...I thought this is supposed to be an automatic control? After trying to create the method, more issues just arise.
JFrame Open Another JFrame - which suggests adding actionListener and then setting it visible..which I have done: public void actionPerformed(ActionEvent e) {this.setVisible(false); new Home().setVisible(true); but Eclipse just doesn't open up the login window at all. Initially, I thought it could be because my success message is in the actionListener, however even after removing that it still does not work.
Call Jframe from Java class and Open window after button click - My only conclusion is that this is not working since Home.java does not extend JFrame? However, I read through other sources that it is not good to use "extends JFrame"?
I guess I also don't have an understanding of the difference between "extends JFrame" vs a new JFrame within a class? I have been learning java on my own and I'm new to GUI creation. Maybe I am missing something very obvious, but I just can't find a solution.
Any ideas? Thanks
To give an idea, my Home.java starts like this:
public class Home {
private JFrame frame;
private JTable data;
private JTextField Column1;
private JTextField Column2;
private JTable table;
// Launch the application.
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Areas window = new Areas();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
// Create the application.
public Home() {
initialize();
}
//Initialize the contents of the frame.
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 697, 518);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Start by defining a simple work flow which allows you to understand the logical path you want your code to take.
Break down the areas of responsibility so that your objects only do the work that they absolutely have to, for example, your login component should only collect the credentials from the user, it should not be responsible for validating them, that should be the responsibility of some kind of controller.
Equally, neither the login component or it's controller should be responsible for determine what happens after a successful login, that is the responsibility for another controller
This decouples the code and encourages reuse, so the next time you need to present some "login" view, you don't have to recode the whole thing, simply update the controller, model and/or view as required, or re-use it as it is
The concept of a controller in Swing is a little different then a normal MVC implementation, because Swing is already a form of MVC.
What I tend to do instead, is define a contract between the controller and the view which describes what events the view generates (and the events that the controller can expect), for example attemptLogin. This disconnects the controller from the view's implementation, so the view is free to form the view in what ever way it feels like, so long as when it wants to validate the actual credentials it calls attemptLogin
So, you would start with a "main controller" which is responsible for controlling the login and main application controllers. It defines the work flow between the login and the main application and monitors for appropriate events which the controllers may generate to make decisions about what it should do next
A basic flow of operation might look something like
This concept is demonstrated in Java and GUI - Where do ActionListeners belong according to MVC pattern?
Just create a method in your Home class that sets its JFrame to be visible:
public void setJFrameVisible(boolean visible)
{
frame.setVisible(visible);
}
Then, assuming your instance of your Home class is called "home", all you would have to do is:
home.setJFrameVisible(true);
Let me add a bit more context. When you're extending JFrame, the class inherits all the methods/properties of the JFrame class. That's why when you extend JFrame you can just call obj.setVisible(true), because your class inherited the setVisible method from the JFrame class. What you have is a class that contains a JFrame, so you have to call the setVisible method on the internal JFrame, not the class.

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

Can components created by JavaSwingBuilder reference other Yaml files?

Typically when using JavaBuilder1 you can define a new component thusly:
Yaml myFrame.yaml:
JFrame(name=frame, title=frame.title, size=packed, defaultCloseOperation=exitOnClose):
- JPanel(name=panel1, tabTitle=tab.panel1Name):
-JTextArea(name=textArea1)
- JPanel(name=panel2, tabTitle=tab.panel2Name):
-JTextArea(name=textArea2)
- JPanel(name=panel3, tabTitle=tab.panel3Name):
-JTextArea(name=textArea3)
Java myFrame.java:
public class MyFrame extends JFrame {
private BuildResult result = SwingJavaBuilder.build(this);
}
JavaBuilders knows what to load by the file name. Is there a way to get JavaBuilders to load another set of files called panel1...panel3 depending on the component name? This would be so one part of my application can write the yaml files out and then I can 'Hot Deploy' them when my JFrame is built (with private BuildResult result = SwingJavaBuilder.build(this);)
My intention is to read a directory looking for a file/set of files that might read like so:
my.dir.panel1=available
my.dir.panel1.textArea1=available
my.dir.panel1.textArea1.default=Declarative GUIs!
my.dir.panel1.textArea2=unavailable
my.dir.panel2=available
my.dir.panel2.textArea1=available
my.dir.panel3=available
my.dir.panel3.textArea1=available
my.dir.panel4=unavailable
my.dir.panel4.textArea1=unavailable
And then use that to populate components in Swing. I can write out the Yaml files programmatically with snakeYaml dependant on what is 'available' in the above properties file but if I can avoid rewriting the JFrame section (which may contain a variety of other components that won't change) in the Yaml file that would be ideal.
The only other way I can think of this is to have multiple results created in Java like so
public class MyFrame extends JFrame {
private BuildResult frankenResult = SwingJavaBuilder.build(this);
private BuildResult subReult1 = SwingJavaBuilder.build(new MyPanel());
JFrame myFrame = (JFrame)result.get("myFrame");
JPanel myPanel = (JPanel)result.get("panel1");
myFrame.add(myPanel);
}
But thinking about it I'm not certain I can make this call SwingJavaBuilder.build(new MyPanel());
So how can I create subsets of Components in a separate yaml file and then reference them in a main Yaml file?
1: GitHub Repo

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

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