How to use another classes' functions in Java - 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.

Related

Access Panel from Main Form from a Panel Form

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.

Should be really simple, how do I make a jbutton show a jframe i have already created via the window builder in eclipse?

Not sure if I'm just using a visual basic approach, but I was under the impression i could use the window builder in eclipse to create my Jframes, then simply invoke them when the button/actionlistener is selected...
Like:
JButton btn_register = new JButton("Register");
btn_register.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frm_register.setvisible(true);
}
frm_register is the class name for the register form...
YourFrame frame=new YourFrame();
frame.setVisible(true);
frm_register is the class name for the register form...
I hope not. Class names should start with upper case character. For example: RegistrationForm.
Then you need code like:
RegistrationForm register = new RegistrationForm();
register.setVisible( true );
This of course assumes that the constructor for the RegistrationForm adds components to the form and does a pack on the form.
i could use the window builder in eclipse to create my Jframes,
Also, you should not be using a JFrame. An application generally has a single JFrame. A child window would typically be a JDialog. See: The Use of Multiple JFrames: Good or Bad Practice?

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 modularize panels in GWT

Suppose I have the following layout for my page:
vertical panel mainbody = new VerticalPanel;
//beginning of 1st sub-vertical panel
VerticalPanel first_panel = new verticalPanel();
first_panel.add(...);
//other such stuff, end of first_panel
mainbody.add(first_panel);
//beginning of 2nd sub-vertical panel
VerticalPanel second_panel = new verticalPanel();
second_panel.add(...);
//other such stuff, end of first_panel
mainbody.add(second_panel);
//beginning of 3rd sub-vertical panel
VerticalPanel third_panel = new verticalPanel();
third_panel.add(...);
//other such stuff, end of first_panel
mainbody.add(third_panel);
//end of mainbody
RootPanel.get().add(mainbody);
I want to modularize it, such that each sub panel (first_panel, second_panel and third_panel) belong to individual files, so that after some import, I can just code this in the main page to do the trick:
vertical panel mainbody = new VerticalPanel;
mainbody.add(first_panel);
mainbody.add(second_panel);
mainbody.add(third_panel);
//end of mainbody
RootPanel.get().add(mainbody);
The sub-panels themselves may use other sub-panels, for which the modularization should cascade. Now what do I need to do to enable this? The Google documentation is not quite clear to me. Specifically,
How do I define those panels in individual files (packages or folders) so that they can be imported into the main file?
How do I import them (somewhat like include in PHP)?
What changes do I need to make to the xml file (if any is needed)?
How to define the same set of rules for the imported files also, so that the imports cascade? That is, if a imports b, and b imports c and d to construct itself, both c and d are imported into the main file dutring import?
Will the CSS rules in the main CSS file be enough for all imported panels?
If someone can explain this from the viewpoint of the default StockWatcher app which comes packaged with GWT (so that we can have some common ground for understanding the directory structure), it will be easy for me to understand.
You need to make a Widget out of the panels.
package com.example.widgets // Make this package whatever you want
import com.google.gwt.user.client.ui.Composite
public class FirstPanel extends Composite{
private VerticalPanel verticalPanel;
public FirstPanel(){
verticalPanel = new VerticalPanel();
// All composites must call initWidget() in their constructors.
initWidget(verticalPanel);
// For your CSS
setStyleName("example-SomeStyle");
// Continue constructing object ...
}
}
The initWidget() needs to be called exactly once every time the constructor is used. It sets the widget to be wrapped by the composite. Check the documentation here.
Then you'll be able to use it like so
VerticalPanel mainPanel = new VerticalPanel();
FirstPanel firstPanel = new FirstPanel();
mainPanel.add(firstPanel);
The above should answer point 1
If your Widget isn't in the same package as the code above you'll need to import it. For the example above: import com.example.widget.FirstPanel
No changes need to be made in the XML file
Yes, the imports will cascade as you described.
The CSS Rule in the main CSS should be enough as long as you declare the style names in the widgets. You can do that by calling setStyleName("example-SomeStyle"); as you can see in the example above.
What you are asking about is called custom widgets. You can create a custom widget to represent first_panel, another one to represent second_panel, etc. Then you can make widgets that consist of other widgets, if necessary. Each custom widget is a separate class - it will be represented by a separate file (or two files if you use Ui:Binder for your custom widget).
You can read more about custom widgets here:
https://developers.google.com/web-toolkit/doc/latest/DevGuideUiCustomWidgets

Categories