Access Panel from Main Form from a Panel Form - java

I am trying to access a panel from my Main JForm which I use as a dynamic panel that repaints different Panel Forms.
this is the hierarchy of the forms.
Package_Main-Main_Form(JForm)-Dynamic_Panel(Panel)-DashBoard(Panel_Form)
Package_Panels-Panel_A-Panel_B
What I did was Display JForm first and repaint its JPanel with DashBoard.
now heres the problem.
I have a button inside my dashboard and when I tried to import Package_Panels.PanelA, but it doesn't work properly.
heres the first code I tried:
This code was added inside DashBoard Button.
PanelA x = new PanelA ();
this.removeAll();
this.add(x);
this.revalidate();
this.repaint();
x.setVisible(true);
I had doubts about this one since I knew if i use this.function, it points out the DashBoard Panel.
anyone knows a way to access the JForm's panel?
UPDATE: I found a way around this by using this.getParent() but other ideas are welcome. I don't to be stuck doing this over and over again.
heres what I did:
Panel_A x = new Panel_A();
this.setVisible(false);
this.getParent().add(x);
this.getParent().revalidate();
this.getParent().repaint();
x.setVisible(true);
//this.getParent().remove(this); - Does this really work? I dont want to keep this instance open.

Related

How to use another classes' functions in Java

First time using the stackoverflow. So, please don't be too harsh :)
I have a little Java project. We are learning how to built GUI with swing. I am stuck with methodology.
I have a JFrame for GUI and I am creating 4 JPanels for:
Menu (Menu Class)
Buttons (Buttons Class)
ActionArea (ActionArea Class)
Statusbar (StatusBar Class)
I have been asked to have same options in the Menu and Buttons. i.e. New File or Open File options will be available in the Menu and Buttons sections.
I don't want to duplicate the code and copy into Menu Class and Buttons Class. I believe there is a way to use only one function from both classes.
Could somebody help me to achieve this?
Here is main code:
import java.awt.BorderLayout;
import javax.swing.JFrame;
public class GUI extends JFrame {
private Menu menu;
private Buttons buttons;
private ActionArea actionArea;
private StatusBar statusBar;
public GUI() {
super("New GUI");
this.setSize(800, 600);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout (new BorderLayout());
this.menu = new Menu();
this.buttons = new Buttons();
this.actionArea = new ActionArea();
this.statusBar = new StatusBar();
this.setJMenuBar(this.menu);
add(this.toolbar, BorderLayout.NORTH);
add(this.actionArea, BorderLayout.CENTER);
add(this.statusBar, BorderLayout.SOUTH);
}
}
lets say the function name is newFile();
I don't want to write this function one for menu class and one for buttons class.
Thanks in advance to all
EDIT:
this.menu = new Menu(); creates menuitems with required actions in Menu class
this.buttons = new Buttons(); creates jbuttons with required actions in Buttons class
actions are exactly same. Code is doubled. This is the problem.
Use a Swing Action. See How to use Actions for details on using them in buttons and menu items.
you can use Action class which is an extension of ActionListener....there are several properties you can set in objects of these class...you can check out the line:
Tutorial for Using Action classes
and for API: Action classes API
it may help you..
You can make your Button class to take the object of Menu class as parameter in its constructor. Now, with this object, you can get access to the Action classes of your Menu class, and then, you can use them for your button.
If you still can't understand, please provide the code of your Button and Menu classes.

Simple Java GUI, cards not appearing

import javax.swing.*;
public class SlideShow {
JFrame slide = new JFrame("Slide Show");
public SlideShow(){
slide.setSize(300,400);
slide.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
slide.setVisible(true);
slide.setLocationRelativeTo(null);
JPanel panel = new JPanel();
JLabel label = new JLabel(new ImageIcon("Images/picture1"));
panel.add(label);
slide.add(panel);
}
public static void main(String[] args){
SlideShow slide = new SlideShow();
}
}
I have to create a simple Java GUI that displays some cards. First, I just wanted to test it by displaying one card. For some reason I can't seem to figure out why nothing is being displayed.
You haven't actually used a proper file name "Images/picture1". Should be something like "Images/picture1.png" with the file format
Also image files, generally should be read from the class path, if you plan on having them embedded to the program. To do so, you will first need to put the file in the class path. With most IDE build configurations it's as simple as placing the image in the src. So
ProjectRoot
src
images
picture1.png
Then you would read it like
new ImageIcon(getClass().getResource("/images/picture1.png"));
A better approach would be to use ImageIO.read(). If the file path is incorrect, it will throw an exception, so you know where you're going wrong
Image image = ImageIO.read(getClass().getResource("/images/picture1.png"));
ImageIcon icon = new ImageIcon(image);
You will need to put it in the try/catch block
Also do what codeNinja said about the setVisible() after adding component. Also preferably pack() the frame, instead of setSize()
You need to set the Frame visible after you add all necessary components to it. Move slide.setVisible(true); Down to the bottom of the constructor like this:
...
slide.add(panel);
slide.setVisible(true);
Alternatively you can add slide.revalidate(); at the bottom of your constructor.

Force Horizontal Tab Text on Left Aligned JTabbedPane

I am trying to make a JTabbedPane in Java 7 on OSX that has tabs positioned to the left with their text horizontal (instead of vertical). However, with the code:
import javax.swing.*;
import java.awt.Dimension;
class Probs extends JDialog {
JTabbedPane options = new JTabbedPane();
Probs(JFrame owner) {
//main constructor
super(owner, "User Preferences", true);
//set the tabs to be left aligned
options.setTabPlacement(JTabbedPane.LEFT);
//construct the authorization panel
JPanel authorization = new JPanel();
authorization.add(new JLabel("test"));
options.addTab("test", authorization);
add(options);
setSize(new Dimension(300,300)); //should use pack here
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
JFrame test = new JFrame();
new Probs(test);
test.dispose();
}
}
I get a dialog box which looks like: this image
I would like the tab text to be horizontal (the 'test' title on the tab be oriented horizontally instead of vertically).
I searched around on Google for a while and have only run into occurrences wherein people wanted to achieve vertical text on their tabs, I could not manage to locate any in which people wanted to have horizontal text (what I am trying to achieve).
In particular, I am trying to achieve something which exactly looks like the image mentioned in the first post of this question. It is basically the exact opposite of that question because the person in that tab started with what I am trying to achieve (I believe). Basically, I am trying to determine how to create the image displayed in the first post of that question.
Can someone please tell me how to have left-oriented tabs while preserving horizontal tab titles (as opposed to vertical)?
Thank you for your time and assistance.
Again, since I can't replicate the problem, Try this suggestion:
JPanel authorization = new JPanel();
authorization.add(new JLabel("test"));
options.addTab("", authorization);
JLabel labTab2 = new JLabel("test"); // create a label
options.setTabComponentAt(0, labTab2); // set it to the component
The alignment is determined by your operating system. If you want to change the alignment of the tab text, you have to change the look and feel of your swing application. This worked for me. See here.
The system look and feel at MacOSX didn't support what you want in JTabbedPane. You must create a customized JComponent to do this or to set the look and feel of your application to cross platform (java metal) as stated before by #MonkeySupersonic.
I suggest the readings:
Apple Java Development Guide (section: User Interface Toolkits for Java) - https://developer.apple.com/library/content/documentation/Java/Conceptual/Java14Development/04-JavaUIToolkits/JavaUIToolkits.html
mac User Interface Guidelines - https://developer.apple.com/macos/human-interface-guidelines

Add JInternalFrame to JDesktopPane using a button inside other JInternalFrame

This snippet code I got from https://stackoverflow.com/a/6868039/2240900
how to add the internal2 to desktoppane1 using a button placed somewhere in internal1.
In the ActionListener added to your button you can use code like the following to get a reference to the desktop pane:
Container container = SwingUtilities.getAncestorOfClass(JDesktopPane.class, (Component)event.getSource());
if (container != null)
{
JDesktopPane desktop = (JDesktopPane)container;
JInternalFrame frame = new JInternalFrame(...);
desktop.add( frame );
}
My question is how to add another JInternalFrame if the button reside in another JInternalFrame? ex: add internalX to desktoppane1 using a button placed somewhere in internal2/internal3/internalX, where each internal was created using a button inside internalX not using a menubar.
Any help will be appreciated. Thanks.
I accidentally find out that we can use a method of JInternalFrame that is getDesktopPane().
As mention in javadoc:
getDesktopPane
public JDesktopPane getDesktopPane()
Convenience method that searches the ancestor hierarchy for a JDesktop instance. If JInternalFrame finds none, the desktopIcon tree is searched.
Returns:
the JDesktopPane this internal frame belongs to, or null if none is found
So we can just use a command like:
JDesktopPane desktopPane = internalFrame.getDesktopPane();
desktopPane.add(internalX);
or if the class extends JInternalFrame simply use
JDesktopPane desktopPane = this.getDesktopPane();
desktoppane.add(internalX);
to get the JDesktopPane to add another JInternalFrame in a nested JInternalFrame.
Externalize the listener into it's own class, with proper parameters if needed. Then, you can instantiate this listener every time you create a new frame and apply it to its button.

How to create internal frames (child frames) in an applet

I am trying to create a child frame to exist inside my applet and it should be bound to a JPanel. I found this and that on the internet but nothing that worked. I think something went wrong during the process and the darn thing is hidden or something. Can someone please give me some help on this issue.
My source code follows...
public class EnableFrame {
public void init() {
EnableFrame theframe = new EnableFrame();
theframe.setSize(550, 300);
theframe.setVisible(true);
}
public EnableFrame() {
JPanel containall = new JPanel();
JInternalFrame iframe = new JInternalFrame("New Frame",true,true);
iframe.setBounds(10,10,150,150);
iframe.getContentPane().add(containall);
iframe.show(true);
}
}
Thanks in advance
-Roland
A JInternal is normally associated with a JDesktopPane.
I order for the internal frame to appear on the screen, you must have added the frame to an appropriate container, such as a JDesktopPane
You may find How to Use Internal Frames of some use.
my view only the comment
even is possible there could be caused with some side_effect for mouse and focus event betweens heavyweight (J)Applet and lightweight JInternalFrames that complicated this idea, and heavyweight (J)Applet can jumping toFront()
you'd don't do that and to use JDesktopPane from JFrame rather than for (J)Applet

Categories