How to create InternalFrame per menuOption and then select between them - java

I am building a GUI using internalFrames. I have a menu with different options and I whenever I choose a new option I want it to create that specific frame. However, if that 'option frame' has been created already when I click the option a second time, then I just want that frame to come on top of the other frames (since they are the same size and will lie on top of each other).
This is some of my methods I use:
This method is created when I click a menuOption named: "Home"
protected void createFrameHome()
{
NewFrame frame = new NewFrame();
desktop.add(frame);
try {
frame.setSelected(true);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
}
This Method is created when I click a menuOption named "Travel"
protected void createFrameTravel()
{
NewFrame frame = new NewFrame();
frame.getContentPane().add(table, BorderLayout.NORTH);
desktop.add(frame);
try {
frame.setSelected(true);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
}
Frame create:
class NewFrame extends JInternalFrame {
public NewFrame() {
setSize(700, 500);
setVisible(true);
}
}
Now as you can see I create a new frame every time these methods are being called. I have tried different things to get it to work but none seems to work.. I have for example used a variable so it doesn't create a new frame, but then it doesn't do the frame.setSelected(true); part either.
Does anyone know a way around this? Can I somehow select the frame from before maybe?

Solved it by using a variable which was set to true if frame was created

Related

dispose() does not work in GUI as old JFrame still showing up with new Frame

This is the codding. I m trying moving from closing a JFrame to another (opening) a new JFrame in user interfaces by clicking button. But when I am close the old JFrame the new also being vanish
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try
{
if(e.getSource()==btn )
{
dispose();
customer s =new customer();
s.setVisible(true);
s.pack();
s.setLocationRelativeTo(null);
s.setExtendedState(JFrame.MAXIMIZED_BOTH);
CloseFrame();
}
if(e.getSource()==btn1)
{
dispose();
pet p =new pet();
p.setVisible(true);
p.pack();
p.setLocationRelativeTo(null);
p.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
if(e.getSource()==btn2)
{
dispose();
Transaction t =new Transaction();
t.setVisible(true);
t.pack();
t.setLocationRelativeTo(null);
t.setExtendedState(JFrame.MAXIMIZED_BOTH);
CloseFrame();
}
}
catch(Exception e1)
{}
}
is this the frame of your program? or is it a JDialog? A JDialog should have no problem with the dispose().
You can dispose your main frame this way by using frame.dispose().
But only if this is your only frame. You may never use more than one frame in your GUI. JDialog is the way to go with additional windows.
I also see you try to write an Actionlistener with the getSource() method. I suggest you use a new class explicitly for this. then just create a new MyListener with the button itself.
The first line of the MyListener class would then be:
public class MyListener implements ActionListener. If you want to know about this I suggest reading on abstract classes and methods, Object oriented programming and interfaces.
Does this help you?

Swing Application window not showing changes

I'm making a java proyect using eclipse and the windowbuilder plugin. After finishing the logic I've created an Application Window and execute the project, everything is fine. But now i want to modify the window, so i change the title for example. In the visual view, i can see these changes, but when i execute the application, there are no changes, it's always the same window. This is the code:
public class Application {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Application window = new Application();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Application() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("My Application");
}
}
I've just added one line so I don't know what could be wrong. I also want to know if this is the best way to make a gui application (using Application Window) or if there are better ways.
EDIT: I add some images to show which is the problem
Window when i execute:
Window in windowbuilder:
You can set title using two ways:
1.Set title using JFrame setTitle() method : frame.setTitle("My Application");
2.Set title using JFrame(String Title) constructor: frame = new JFrame("My Application");
According to my opinion both ways work to set title but still if you are struggling with first option, you can try second option. May be this code works for you.
private void initialize() {
frame = new JFrame("My Application");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
After setting changes, go frame.dispose()
and after that You can
frame.repaint();
frame.revalidate();
Please try this:
try {
Application window = new Application();
window.frame.setVisible(true);
window.frame.setTitle("My Application");
} catch (Exception e) {
e.printStackTrace();
And let me know the result.
Edit:
remove setTitle() from initialize method.
Edit:
If you are beginner you can try JavaFX. It is also simple as Java Swing but has some more potential, better design options and stuff. Its something I have tryed with when I was started Java learning.
I think that you should delete your swing and install it again. Because any solution is not working and there is no other reason not to. Just try to reinstall swing.

How do you "X out" of a window in swing after using it?

I have already looked at many other similar questions, but no one has answered to my needs. I need to actually x out of the window itself once an action listener within has been used to open the next window. Below is a portion of my code. the reference to "GUITUI" is a similar section of code in another class. I need to actually exit the window once GUITUI has been called, not just use Frame.dispose() or a similar function to clear the contents of the window.
JPanel Race = new JPanel(new GridLayout(5,6,20,15));
getContentPane().add(Race);
setTitle("Choose a Race.");
setSize(500,200);
setResizable(false);
JButton Race1 = new JButton("Human");
Race.add(Race1);
Race1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
try {
GUITUI a = new GUITUI(1);
a.setVisible(true);
} catch (IOException e) {
e.printStackTrace();
}
}});

Making custom close and minimize button

I have a decorated JFrame. I need to make close button and minimize button. What should I do?
Here is my code snippet:
public Startup()
{
setTitle("STARTUP");
setSize(800,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setUndecorated(true);
setLocationRelativeTo(null);
setVisible(true);
}
Your approach is very unique and will look quite good. There are many ways to solve your problem. Now, as per your request, you want a CLOSE and a MINIMIZE button. Let us make the following Actions.
private final Action exitAction = new AbstractAction("Exit")
{
#Override
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
};
private final Action minimizeAction = new AbstractAction("Minimize")
{
#Override
public void actionPerformed(ActionEvent e)
{
setState(JFrame.ICONIFIED);
}
};
Now, let us apply the above actions to JButtons:
JButton closeButton = new JButton(exitAction);
JButton miniButton = new JButton(minimizeAction);
There you have it. Now, all you need to do is add your buttons to your JFrame.
Note For Eclipse Users
this code will come in your minimize button when You click On minimize button in Eclipse.
YourFrameName is the Frame name you had set or it is set by default, use that frame name here:
YourFrameName.setState(YourFrameName.ICONIFIED);

Desktop application - how do I dynamically create and destroy forms

I'm creating a small crypto app for the desktop using java.
I'm using JFrames (import javax.swing.JFrame) with Oracle
JDeveloper 11g under Linux.
I want to have a "welcome" form/frame where users can choose
their encryption method, and then on choosing the method,
I want to dynamically create the appropriate form for the
chosen encryption method and also destroy/free/dispose() of
the welcome form. When the user has finished their encrypting,
they should close the frame/form (either by clicking on the
x at the top right - or using the Exit button or by any
method) and the welcome frame should be dynamically recreated
and appear.
I've tried various things - btnEncode_actionPerformed(ActionEvent e)
then this.dispose() - and I've fiddled with this_windowClosed(WindowEvent e)
and dispose(), but nothing seems to work.
Even a workaround using setVisibl(true/false) would be acceptable at
this stage - this has been wrecking my head all day. It's very
easy to do in Delphi!
TIA and rgs,
Paul...
something like this usually does the trick: (Note I haven't tested this)
public class WelcomeMsg extends JFrame
.
.
.
public void btnContinue_actionPerformed(ActionEvent e)
{
this.dispose();
SwingUtilities.invokeLater(new Runnable(){ new JFrameAppropriateWindow(args) });
}
where btnContinue is the Continue button on your welcome form and JFrameAppropriateWindow is the next frame you would like to show depending on the user's choice. Args are any arguments you need to pass.
When you are ready, you can simply dispose the current frame and relaunch an instance of WelcomeMsg
I put together this simple example for creating and displaying a panel depending on a user choice.
public class Window extends JFrame {
public Window() {
this.setLayout(new BorderLayout());
JComboBox encryptionCombobox = new JComboBox();
encryptionCombobox.addItem("foo");
encryptionCombobox.addItem("bar");
//...
encryptionCombobox.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// find choices and the correct panel
JPanel formPanel = new JPanel();
formPanel.setOpaque(true);
formPanel.setBackground(Color.RED);
//...
Window.this.add(formPanel, BorderLayout.CENTER);
Window.this.validate();
Window.this.repaint();
}
});
add(encryptionCombobox, BorderLayout.NORTH);
}
public static void main(String[] args) {
new Window().setVisible(true);
}
}
When I come to think about it, you should probably use a CardLayout instead, which allows you to switch between different panels (cards).

Categories

Resources