Can we use a JInternalFame with a button in the main frame? The frame contains a JDesktopPane, of course. The button should open up the JInternalFrame How?
I don't know a way to put a JButton directly on a JDesktopPane, but you can use menu items to create and select a JInternalFrame. In this example, each menu item uses an Action defined in the JInternalFrame to select the corresponding frame.
class MyFrame extends JInternalFrame {
private Action action;
MyFrame(JDesktopPane desktop, String name, int offset) {
…
action = new AbstractAction(name) {
#Override
public void actionPerformed(ActionEvent ae) {
try {
MyFrame.this.setSelected(true);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
}
};
}
public Action getAction() { return action; }
}
Addendum: as #camickr suggests, it is technically possible to put a JButton directly on a JDesktopPane, but it might prove difficult to use in practice.
I don't really understand the question so I will just make some observations.
a) a JInternalFrme is like a frame in that you can add any component to it that you want
b) A JButton works the same whether it is added to an internal frame or a frame
I suggest you start by reading the Swing tutorial for working examples. You might start with the sections on "How to Use Internal Frames" and "How to Use Buttons".
If you still have problems then post your SSCCE that shows what you have tried.
Related
i'm at a standstill developing a java application.
I have a series of JFrames that open each other, and once I open the next, the frame before shall close.
Now, all works fine, I used
setVisible(false);
to "close" the frame, but I have some problems with a specific form I generated:
The form has some panels inside, and the panels each have a button which opens the next form. Now, I wonder how can I apply setVisible(false) to the form that contains those panels.
ChooseForm is the name of the form that contains the panels;
And I have this event handling the button click of each panel's button
private void btn_scegliMouseClicked(java.awt.event.MouseEvent evt) {
Database.getAbitazioneByCodice(label_codice.getText());
MainForm.showFrame();
}
Thanks in advance for the help.
There are many ways to do this.
A Simple way you can do like this:
class YourFrame extends JFrame implements ActionListener {
private YourPanel panel;
public YourFrame() {
//...
panel.addButtonActionListener(this);
}
public void actionPerformed(ActionEvent e) {
//show other Frame and hide this Frame
}
}
class YourPanel extends JPanel {
private JButton yourButton;
public void addButtonActionListener(ActionListener listener) {
yourButton.addActionListener(listener);
}
}
I've read several topics showing how to create a KeyBinding, however, none of them fully worked for me. My JFrame has a JMenuBar and for the items of the menu NetBeans is correctly generating code such as:
mniExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
mniExit.setText(bundle.getString("Menu.File.Exit")); // NOI18N
mniExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mniExitActionPerformed(evt);
}
});
mnuFile.add(mniExit);
However, only this binding is not visible when the menu is hidden. I've tried something like:
getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(mniExit.getAccelerator(), "exit");
getRootPane().getActionMap().put("exit", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
mniExit.doClick();
}
});
But it simply does not work. What am I doing wrong?
Thanks in advance!
You state that
However, only this binding is not visible when the menu is hidden. I've tried something like:
I'm guessing here, but I'm not sure that a button or menu can be clicked if its not visible. To simplify, I would create an ExitAction class, a class that extends from AbstractAction, that is assigned as an Action to any JMenuItems, JButtons, or Key Bindings that need it. If they all share the same ExitAction object, then the Action (and the corresponding menu items and buttons) can be disabled all at once if need be.
If this does not help, again create and post a minimal example program where you create the smallest program that runs, compiles, requires no outside dependencies (images, database) and that demonstrates your problem for us.
I am trying to make a refresh button that will essentially restart the program when ever I click the button. I don't know how I should go about doing this.
I've place the Graphical User Interface i decided to use do complete this action. Any and all help would be greatly appreciated.
package pdfView;
import javax.swing.*;
import java.awt.*;
public class View extends JFrame {
public View() {
super("PDF Viewer");
setLookAndFeel();
setSize(500, 125);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flo = new FlowLayout();
setLayout(flo);
JTextField Search = new JTextField ("Search", 29);
JButton Search1 = new JButton("Search");
//this is where i have the button
JButton ReFresh = new JButton("ReFresh");
add(Search);
add(Search1);
add(ReFresh);
setVisible(true);
}
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(
"com.sun.java.squing.plaf.nimbus.NimbusLookAndFeel"
);
} catch (Exception exc){
}
}
public static void main(String[] args) {
View pdf = new View();
}
}
What do you mean by refresh or restart?
Do you mean:
Let the application be as it is, just update what it's showing?
Really restart the application?
Updating what the application is showing
You first need to decide what actually should cause your application to refresh. You already talked about a Button. The mechanism for activating something like a button is called Action. You can do that stuff manually, using an ActionListener, or you could extend AbstractAction, which is what I recommend. Extending AbstractAction allows you to use the same logical action something in more than one location on the UI. Look at typical applications, they offer Cut/Copy/Paste through menu, toolbar, popupmenu and keyboard shortcuts. The simplest way to achieve this in Java is using Action by extending AbstractAction.
The methods you need to call to update your application are invalidate(), validate() or repaint().
Restarting an application
So you want to run through main() again? That should actually not be required, unless you have an application that supports updating itself. Even then it can sometimes be avoided by smart usage of a ClassLoader.
Some more notes on your code
Usage by extension anti-pattern
I wouldn't extend JFrame just to display a window on the screen. Usage by extension is an anti-pattern. You don't need to extend JFrame to get a JFrame displayed on the screen and do what you want.
Referring static members
I would refer to constants via their original declaration. I.e. I'd refer to EXIT_ON_CLOSE via WindowConstants.EXIT_ON_CLOSE, not JFrame.EXIT_ON_CLOSE.
Typo
You have a typo in your UIManager.setLookAndFeel() code. Search for swing and you will see the typo.
Exception information
You might actually want to print the exception to stderr using exc.printStackTrace() instead of ignoring it completely, because when you have a typo in the LaF class name, as you do, and you don't print the exception, you might actually not come to know what's going wrong.
Sequence of widget construction and UIManager.setLookAndFeel()
The sequence of UIManager.setLookAndFeel() and the effective new JFrame() via super(...) does not guarantee you that the whole UI will be in Nimbus, parts of it might still be in Metal. I recommend to set the LaF before even constructing the first widget, to be on the safe side. As far as I remember, it's not guaranteed that changing the LaF after component construction has an effect, unless you tell the UIManager to update the LaF. See also this quote from the documentation of UIManager:
Once the look and feel has been changed it is imperative to invoke updateUI on all JComponents. The method SwingUtilities.updateComponentTreeUI(java.awt.Component) makes it easy to apply updateUI to a containment hierarchy. Refer to it for details. The exact behavior of not invoking updateUI after changing the look and feel is unspecified. It is very possible to receive unexpected exceptions, painting problems, or worse.
http://docs.oracle.com/javase/8/docs/api/javax/swing/UIManager.html
setSize() vs. pack() with a little help of Insets and Border
Instead of setting the size manually, you might want to play with Insets or Border and JFrame.pack() in order to get a decent layout of your window. Setting the size manually assumes that you know a lot about the screen resolution and the font size of the user.
The pack() method performs automatic size calculation based on the contents. Insets and Border allow you to create some space and borders, even with some designs or labels, around components so they wouldn't be cramped tightly in a window but be nicely spaced.
First you have to assign an actionListener to the ReFresh Jbutton.
You can either implement the interface ActionListener to the class, and override the actionPerformed() method like this
public class View extends JFrame implements ActionListener{
private JButton ReFresh;
public View() {
super("PDF Viewer");
setLookAndFeel();
setSize(500, 125);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flo = new FlowLayout();
setLayout(flo);
JTextField Search = new JTextField ("Search", 29);
JButton Search1 = new JButton("Search");
//this is where i have the button
ReFresh = new JButton("ReFresh");
ReFresh.addActionListener(this);
add(Search);
add(Search1);
add(ReFresh);
setVisible(true);
}
private void setLookAndFeel() { //right way for nimbus: http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/nimbus.html
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void actionPerformed(ActionEvent e)
{
if(e.equals(ReFresh))
{
super.repaint();
}
}}
public static void main(String[] args) {
View pdf = new View();
}
Or you can do inline assignment to addActionListener, like this
ReFresh.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
super.repaint();
}
});
You can try these methods to refresh/reload the JFrame,
invalidate();
validate();
repaint();
you can also use dispose(); and then new View(); to create the new JFrame, but in this sequence it will close the window and create new one.
or you can even try setVisible(false); then setVisible(true);
I recommend the first 3.
I have a JDesktopPane which contains a number of JInternalFrames. The first time I press one button to visible jinternalframe1 and second button to visible jinternalframe2, it appear above the main window without problems. However, if I press one of the buttons to Reopen jinternalframe1 or jinternalframe2, they are not brought in front of the main window... EDIT: actually, i can't do anything with jinternalframe on a button click...i can only click once on the button and then no operation can be perform on the jinternalframe through the button..why it doesn't work!!
this is the coding of button1...
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
jinternalframe1 frame1 = new jinternalframe1();
try {
if(Allow.flag == false) {
desktopPane.add(frame1);
frame1.setVisible(true);
Allow.flag = true;
} else if(Allow.flag == true) {
frame1.setSelected(true);
}
} catch(PropertyVetoException e) {
System.out.println(e);
}
}
Allow.java
public class Allow {
static boolean flag = false;
}
Every time you click on the button you create a new JInternalFrame object, but you only ever add the first internal frame that you create to the desktop pane.
Don't keep creating new internal frame objects. I would guess you should only create the internal frame if your "frame1" variable is null.
If you need more help then post a proper SSCCE that demonstrates the problem.
I am make a project on cars. How can I make distributor frame popup and cars frame not visible and close automatic? Kindly send any solution in simple and effective way.
I have done coding this way:-
{
Cars frm1=new Cars();
Distributor frm2=new Distributor();
frm2.setVisible(true);
frm1.setVisible(false);
frm1.setDefaultCloseOperation(frm1.DISPOSE_ON_CLOSE);
}
".Please help me to how I can make distributor frame popup and cars frame is not visible and close automatic."
Ok so in Netbeans GUI Builder, you may want to do the following (this is assuming you have created two separate JFrame form files
In the frame that is the launching program (we'll call it MyFrame1) add a button to it (we'll call it jButton1)
Add a listener to the button, then the following code should be auto-generated
public void jButton1ActionPerforemd(javax.swing.ActionEvent evt) {
}
In that actionPerformed, just instantiate the second frame (we'll call it MyFrame2) and setVisible(false) to MyFrame1. MyFrame2 should already be visible upon instantiation, so you wouldn't have to setVisisble(true) on it
public void jButton1ActionPerforemd(javax.swing.ActionEvent evt) {
MyFrame2 frame2 = new MyFrame2();
MyFrame1.this.setVisible(false);
// You can also use MyFrame1.this.dispose(); dependind if you ever need to use that frame again
}
I think this should work
you need to setVisible Jframe2 as true...so it can apear on output sceen
public void jButton1ActionPerforemd(javax.swing.ActionEvent evt)
{
myFrame2 frame2=new myframe2();
myframe1.this.setVisible(false);
frame2.setVisible(true);
}
create action event for the button such that when when you click will take
you
to the next page for my case next page is nextjFrame
private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {
setVisible(false);
nextjFrame ob=new nextjFrame();
ob.setVisible(true);
}
private void BTNConvertActionPerformed(java.awt.event.ActionEvent evt) {
/*
This is the action performed event for my Button "BTNConvert"
*/
java.awt.EventQueue.invokeLater
(new Runnable()
{
public void run()
{
new JFrame2().setVisible(true);
}
});
/*
This will set the second Frame Visible.
*/
JFrame1.this.setVisible(false);
/*
This should set the first frame invisible or whatever. Any other code should be
written before the curly brace below.
*/
}
//You're Welcome.