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