Resize Jframe using external frame - java

What I have done is created a form that has a table displaying inventory. When an item is right clicked, a popup menu is shown where the user can click edit inventory edit (JFrame will resize to (988, 736)). A Panel will be loaded from an external class with buttons and labels. What I am trying to achieve here is when the user clicks save, a hidden "Close" button becomes visible. Once the close button is clicked then the panel will be set invisible and what I also need it to do is set the JFrame back to its original size (988, 430). How do I go about achieving this (Setting the JFrame back to (988, 430) from the External Class)?
Here is the code I have for it so far:
On this event button below i would like to insert the coding to resize the JFrame. This coding is in the class (SV2.java)
private void btn_closeActionPerformed(java.awt.event.ActionEvent evt)
{
SV1 objc = new SV1();
Panel.setVisible(false);
objc.getContentPane().setPreferredSize(new Dimension(988,430));
objc.getContentPane().setSize(new Dimension(988, 430));
// coding to resize JFrame in class SV1.java, from setting it within this class
}

In SV2.java:
//Add instance variable for parent
private JFrame parent;
//Add JFrame to the constructor
public SV2(SomeType someParameter, OtherType foo, JFrame parent) {
this.parent = parent;
//rest of constructor code...
}
//Use JFrame to resize
private void btn_closeActionPerformed(ActionEvent evt) {
parent.setSize(988, 430);
//rest of code triggered by close button...
}
In the class that calls the panel:
//Pass the frame when instantiating the SV2 object
new SV2(yourParameter1, yourOtherParameter, frame);

Related

How to call setVisible from another class?

What I have:
Two Classes that instantiate two JFrames.
What I am trying to achieve:
One with a button and the other that will become invisible when a action is fired on the button.
Problems:
I do not know how to pursue this. How should I go about coding this?
Class 1
public class test1{
public static void main(String[] args){
JFrame frame = new JFrame("Blinkly Frame");
frame.setSize(100, 100);
frame.setVisible(true);
}
}
Class 2
public class test2{
public static void main(String[] args){
JButton button = new JButton();
//when i will click this button i want to make invisible frame
}
}
Solution:
Create an instance of the class that has a Jframe or extends a JFrame.
First we need the JFrame that will be disappearing.
public class BClass extends JFrame{
// Disappearing frame
public BClass()
{
this.setSize(300,300);//sets frame properties
this.setLocationRelativeTo(null);
}
}
Next we need the Frame that will be holding the buttons. (Documentation added)
public class ACLass {
public static void main(String[] args) {
JFrame frame = new JFrame("Magician"); // instantiates
BClass b = new BClass(); // instantiates class that extends JFrame
b.setVisible(true);//
frame.setSize(300,300);//
frame.setVisible(true);//
frame.setLocationRelativeTo(null);//
JButton disappearButton = new JButton("Disappear"); //Adds button
disappearButton.addActionListener(new ActionListener() { // Adds action -When button is "clicked"
public void actionPerformed(ActionEvent e) { // method called when action fired
b.setVisible(false); //visibility changed
}
});
disappearButton.setBounds(0,0,300,150);
JButton appearButton = new JButton("appear"); //Adds button
appearButton.addActionListener(new ActionListener() { // Adds action -When button is "clicked"
public void actionPerformed(ActionEvent e) { // method called when action fired
b.setVisible(true); //visibility changed
}
});
disappearButton.setBounds(0,100,300,150);
frame.add(disappearButton, BorderLayout.PAGE_START); //adds button to frame
frame.add(appearButton, BorderLayout.PAGE_END); //adds button to frame
//I used border layout however use the a layout manager that works with your components/frame
}
}
Exotic Explanation
I'm going to explain this in terms of a magic show as it provides better understanding.
So first, we have one magician(JFrame) and his wand(JButton) and then we have the helper(the second JFrame) that will disappear and a stage that has been set(all properties defined etc.)
First the magician adds some magic power to his wand(actionListener that handles the button being pushed) that will react when the magician waves it(action fired a.k.a button being pushed).
Next we show the audience the helper(instantiating JFrame to disappear).
As we have shown the audience the helper, we can now show him/her disappearing (Now we can call setVisible through the instance variable of the class).
Finally, the magician waves his wand(firing an action), and the helper gets the signal(actionPerformed method) to disappear. The helper then can call the
b.setVisible(false); //making the frame invisible
General Explanation
https://docs.oracle.com/javase/tutorial/java/javaOO/usingobject.html
Basically, by instantiating an object in another class you can also call the objects methods in that class i.e. setVisible(boolean b).
Other less preferable solutions:
If your disappearing class has does not extend a JFrame but instantiates one..
You would need to create an instance variable
private JFrame j;
Then use getters/setters to access the object which will then allow you to call its methods.
secondClass.getFrame().setVisible(true);//getFrame returns the JFrame
Then add that to the actionPerformed method.
You can also use a static instance variable and statically reference it in the actionPerformed method... (not recommended)
secondClass.frame.setVisible(true);

How to access components in a JFrame from an Internal frame

I've created java Swing app where it consist of a jDesktoppane, inside it I'm loading/calling some jinternal frames from toggle buttons in the main frame (JFrame). And I've used jButton group to all the toggle buttons so only one frame will when a button is pressed.
Since I've used toggle button, even though I dispose a JInternalFrame the relevant toggle button will be in pressed mode (Selected). I've tried many ways and couldn't change the toggle-button's state from selected to UnSelected.
first i've created a method inside my Main JFrame.
public void buttongroup_off(){
buttonGroup 1.setSelected(null,false);
}
Then I created an object inside the exit button of the JInternalFrame and through that I called the buttongroup_off() method.
private void jButton 7 ActionPerformed(java.awt.event.ActionEvent evt) {
Main m1= new Main();
m1.buttongroup_off();
this.dispose();
}
but it does not work!!, Can someone help me on this?
im kind a new to programming.
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
Main m1= new Main();
m1.buttongroup_off();
this.dispose();
}
In this code you are creating a new JFrame Main (which is invisible after creation) and disable its buttongroup. That is not what you want. You have to call buttongroup_off method using a reference to an existing Main instance. You may pass the reference via custom constructor for custom class that extends JInternalFrame, or you may add a static method to the Main class that will return reference to the Main instance. Like this:
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
Main m1 = Main.getInstance();
m1.buttongroup_off();
this.dispose();
}
You may also look at this question answers: managing parent frame from child frame on java swing
You can get the JFrame by using code like:
Component source = (Component)event.getSource();
Main frame = (Main)SwingUtilities.windowForComponent( source );
Now that you have a reference to the frame you can invoke any method from your custom frame class.

How should I create a "panel" outside JFrame?

I am trying to create a GUI with a main window for input, and a separate box to show output below the main window. However, the width of the output box will be different from the main window and I don't want the difference in width to be part of the frame (meaning that the difference should be invisible and user can click through the empty space to the back of the application).
But I also want to output box to be shown as 1 window with the main window. I can set the position of the output box with a listener to make it stick to the main window, but I don't want it to be shown as a separate window in the Windows(Microsoft) task bar. I also don't want the output box to be selectable
as a result of being a separate window.
Therefore is there any method to create a JPanel outside JFrame, or is there a way I can do this with JFrame and make it work with my constraints? Any other methods?
Show the separate JPanel in a window as a non-modal JDialog. You can make it undecorated if you don't want the menu buttons on the top right.
I'm not sure what you mean by:
(meaning that the difference should be invisible and user can click through the empty space to the back of the application).
Regarding
I also don't want the output box to be selectable as a result of being a separate window.
Make sure that you have no focusable components in the dialog, or if focusable, have the focusable property set to false.
Edit
To prevent the dialog window from being focused, add a line:
dialog.setFocusableWindowState(false);
For example:
import java.awt.Dimension;
import javax.swing.*;
public class DialogNoFocus {
public DialogNoFocus() {
// TODO Auto-generated constructor stub
}
private static void createAndShowGui() {
DialogNoFocus mainPanel = new DialogNoFocus();
JFrame frame = new JFrame("JFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(Box.createRigidArea(new Dimension(400, 300)));
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
JDialog dialog = new JDialog(frame, "JDialog", false);
dialog.getContentPane().add(Box.createRigidArea(new Dimension(500, 100)));
int x = frame.getLocation().x;
int y = frame.getLocation().y + frame.getHeight();
dialog.setLocation(x, y);
dialog.pack();
dialog.setFocusableWindowState(false);
dialog.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
JDialog
How about to put 2 internal Frames(one is decorated, another one - undecorated und not selectable) or 1 internal Frame and panel in one big translucent undecorated Frame?
At first you will get only one icon in the Windows(Microsoft) task bar. Internal frames wont show up in the task bar.
The main frame should be translucent : how to set JFrame background transparent but JPanel or JLabel Background opaque?

How to add external JPanel in JFrame?

I'm working on large scale program. As you can see I have one main JFrame and about 20 menu items on that. Each menu item must pop up a new window. At the beginning I have created a JLayeredPanel and then I assigned each menu item to one JPanel which is inside JFrame.Then I put 25 panel in JLayeredPanel... Default all the panels are set to invisible like:
panel1.setVisible(false);
panel2.setVisible(false);
so on
When user click on one menu item, its JPanel will be visible and rest are invisible. It looks messy and I have 5000 lines code. I used InternalFrame and TabbedPane but I'm not happy with them. I want to split my code in different JPanel classes and assign them to the main JFrame. I mean when user clicked on each menu item it will call the external JPanel and render it on the JPanel on the main JFrame. I am using design mode in netbeans and it does everything for me but the simpled structure is like this and it is not working:
public class NewJPanel extends JPanel{
//I have added buttons and etc on this panel
......
}
public class frame extends JFrame(){
JPanel panel = new JPanel();
.....
Public frame(){
frame.add(panel);
}
......
//When use click on the any button on the panel
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//this is not working
NewJPanel fi = new NewJPanel ();
panel1.add(fi);
//or I tested this way separately but it did not work
panel1.remove();
panel1 = new NewJPanel();
add(panel);
invalidate();
}
}
please give me any suggestion how I can control this program in splited classes in professional way.
remove JPanel from JFrame.getContentPane.remove(myPanel)
add a new JPanel with constants, everyhing depends of used LayoutManager and its methods implemented in API
call JFrame.(re)validate() and JFrame.repaint() as last code lines, if everything is done, these notifiers correctly repaint available area
again to use CardLayout, there isn't signoficant performance or memory issue
Please give me any suggestion how I can control this program in splited classes in proressional way.
Ok.
You should put all of your JPanels in a JTabbedPane. The JTabbedPane would be added to the JFrame.
The JFrame, JTabbedPane, and each JPanel would be constructed in a separate class.
You use Swing components, rather than extending them. The only reason you extend a Swing component is if you override one of the component methods.
You should also create model classes for each of the JPanels, as well as a model class for the application.
Read this article to see how to put a Swing GUI together.
make's code better
public class NewJPanel extends JPanel{
//I have added buttons and etc on this panel
......
}
public class frame extends JFrame(){
JPanel panel = new JPanel();
.....
Public frame(){
//frame.add(panel); you dont need call frame because extends JFrame in frame class
add(panel);
......
//When use click on the any button on the panel
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//this is not working
NewJPanel fi = new NewJPanel();
add(fi);
//or I tested this way separately but it did not work
/*panel1.remove();
panel1 = new NewJPanel();
add(panel);
invalidate();you must define panel1 before use it,like :JPanel panel1 = new JPanel();*/
}
}

Linking two frames in java

I am creating a java application which consists of two frames(JFrame1 and JFrame2)
JFrame1 has a grid 6x6 button; and JFrame2 has 6 radio buttons representing colours. How can I link the two frames so that when a button in JFrame1 is clicked, JFrame2 pops up and when a colour is chosen from that the JFrame2 closes and the clicked button gets the respective colour?
It is better to have one JFrame for every application. Use one for the 6x6 JButtons and create a modal JDialog for your color JRadioButtons.
The color selection JDialog should have a public getSelectedColor() method in order to return the selected color to the caller class.
Instantiate the ColorDialog in main and do not set it visible.
The ActionListener of each JButton should make the modal JDialog visible.
The RadioButton ActionPerformed should set the selected color and make the JDialog invisible.
Call getSelectedColor() and apply the returned color to your JButton.
In your frame1's button action listner, you can do something like this
public void actionPerformed(ActionEvent e) {
Frame2 frame = new Frame2(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
where "this" refers to the frame1 object. This is you can access its jTEXTFIELD and jBUTTONs from the second frame. So naturally you have store it in Frame1 object declared in your second class.
Suppose you have a clickable color field in frame2 object, once you click on it, you should trigger a function that get the input field from frame1 (using your locale object reference) and store it in it. something like this:
public void actionPerformed(ActionEvent e) {
frame1.getMyTextField().setText(WHAT_THE_CLICKED_ON);
this.close();
}
Sorry if I made any syntax errors, it's been a long time I didnt work with java :)
Just create another class, let us say FrameMananger, then use the singleton pattern to manage them.
Then in any class, you can use FrameManager.getFrame1() to get the frame1, same as the frame2. You can add logic judgement inside, like dynamically dispose some frame or only create them when needed.
This issue is fairly common concept when you create a game and try to navigate between your every view(like the show score panel from everywhere).
public class FrameManager
{
Frame1 frame1;
Frame1 frame2;
public static Frame1 getFrame1()
{
if(frame1 == null)
frame1 = new Frame1();
return frame1;
}
public static Frame1 getFrame2()
{
if(frame2 == null)
frame2 = new Frame1();
return frame2;
}
public class Frame1 extends JFrame
{
}
public class Frame2 extends JFrame
{
}
}

Categories

Resources