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
Related
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
Just a quick and simple question. I have a program with several classes that read information off of a .properties file. Is it better practice to pass the file from class to class as an argument in the constructor, or open the file directly in each class?
If you're going to do this by hand, I would recommend you create a configuration class, that takes the file via the constructor, and reads the property values into member variables. Then every other class that needs configuration takes a Configuration class via it's constructor. However, almost no one does this, and instead uses a framework like spring, which handles property injection for you.
In spring, it would look something like this:
<!-- application context xml file -->
<context:property-placeholder location="file:///some/path/to/file" />
Then in your java classes:
public class SomeClass {
#Value("${some.property}")
private String someProp;
#Value("${some.other.prop}")
private Integer someOtherProp;
// ...
}
At application startup the properties get injected into your class.
My suggestion is to have a Util class which loads the properties file and get values from that Util to the required classes.
Note: I dont think you have any issues on loading the properties file.
I would suggest that you create an immutable class that takes in the file as a constructor argument and sets all the instance variables. I'd call it PropertyConfiguration. Then since the class is immutable, you won't have to worry about passing it to everyone. You could even have a class that holds it.
For example, the code below would set you up to have a nice set up to have several things available project wide. I would just ensure that anything that's shared be immutable to ensure thread safety.
public class ClientUtils {
private static ClientContext _clientContext = null;
public static void setClientContext(ClientContext cc) {
_clientContext = cc;
}
public static ClientContext getContext() {
return _clientContext;
}
}
public class ClientContext {
private final Configuration _configuration;
public ClientContext(Configuration config){
_configuration = config;
}
public Configuration getClientContext() {
return _configuration;
}
}
If your program contains data which need not be a part of compilation and can vary from deployment to deployment, you must add it to the properties file : ( Things like database connection string, email addresses ).
Just in case you need this, I'm adding the code for accessing the properties files.
Drop the file in build directory.
Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("credentials.properties"));
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.