Hello I've just started programming and now i'm making a university project where I must get a gwtcontainer from another class and let it see in a popup window, don't know exactly how I can call it
This is a part of my code:
import com.is.lap.client.gui.Login;
public class StartRU extends TabPanel {
private static class MyPopup extends PopupPanel {
public MyPopup() {
super(true);
// Is here where i have the trouble don't know exactly how to do it
setWidget( (Widget) new Login().getLayoutData());
center();
}
}
public StartRU() {
Button LoginButton =new Button("Login");
LoginButton.setWidth("100px");
LoginButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
// Instantiate the popup and show it.
new MyPopup().show();
}
});
If Login class extends Widget, you can simply
setWidget(new Login());
Related
I've tried to add a method inside MyUi.java class in vaadin, but all I get are errors. So, is it actually possible to do so? The reason why I ask is this: basically MyUi.java class has a button which when clicked opens up another window (the code for this other window sits in a different class). This button is removed (button.setVisible(false);) when clicked and I added a addCloseListener to the new window so that when that window is closed it fires an event and calls a function which will allow me to re-display the button. This function needs to sit inside the MyUi class as I can't figure out how to access the button which currently sits inside MyUi class from a different class.
Some code to make things a little clearer: MyUi.java contains the button
public class MyUI extends UI {
#Override
protected void init(VaadinRequest vaadinRequest) {
final VerticalLayout layout = new VerticalLayout();
final NewWindow newWindow = new NewWindow();
final UploaderComponent uploaderComponent = new UploaderComponent();
//final TextField name = new TextField();
// name.setCaption("Type your name here:");
final Button button = new Button("Click Me");
button.addClickListener(new Button.ClickListener()
{
#Override
public void buttonClick(ClickEvent event)
{
//button.setVisible(false);
// TODO Auto-generated method stub
getUI().addWindow(newWindow.getWindow());
newWindow.getWindow().setContent(uploaderComponent.formLayout);
}
});
layout.addComponents(button );
layout.setMargin(true);
layout.setSpacing(true);
setContent(layout);
public void testMethod(){//produces errors
}
}
}
And the window class:
public class NewWindow
{
//public static final Sizeable.Unit PIXELS;
Window window = new Window();
public NewWindow(){
window.setStyleName("wrappingWindow");
window.setWidth("620px");
window.center();
window.addCloseListener(new CloseListener()
{
#Override
public void windowClose(CloseEvent e)
{
System.out.println("window closed");
}
});
}
public Window getWindow(){
return window;
}
}
This is my code, some parts have been omitted (such as imports, psvm, etc.):
public class Proyect extends Application implements EventHandler{
private HBox menu1() {
Image food1 = new Image (getClass().getResourceAsStream("clipboard.png"));
Button btnR = new Button ("R", new ImageView(food1));
}
#Override
public void handle(Event event) {
if(event.getSource() == btnR) {
}
}
}
The problem is my IDE says I'm mistaken in the "btnR" thing inside my if statement (it's underlined in red).
Button btnR is defined within the method menu1(). Hence it won't be accessible within the method handle(). Why don't you define it within the class as a private data member?
Please use the logic as mentioned below:
public class Proyect extends Application implements EventHandler{
private Button btnR;
private HBox menu1() {
Image food1 = new Image (getClass().getResourceAsStream("clipboard.png"));
btnR = new Button ("R", new ImageView(food1));
}
#Override
public void handle(Event event) {
if(event.getSource() == btnR) {
}
}
}
I'm trying to do a simple calculator program by building my swing interface on netbeans.
I want to have 3 Classes:
GUI Class - which holds the codes for building the interface
Listener Class - holds all the listener in the GUI interface
Boot Class - this will start the application
For simplicity, I will post my code for a single button. My goal here is to change the Buttons visible text from "1" to "11" to test my design. After verifying that my design works I will continue on working on other buttons.
calculatorGUI.class
import javax.swing.JButton;
public class calculatorGUI extends javax.swing.JFrame {
public calculatorGUI() {
initComponents();
}
private void initComponents() {
oneBtn = new javax.swing.JButton();
oneBtn.setText("1");
}
private javax.swing.JButton oneBtn;
public JButton getOneBtn() {
return oneBtn;
}
public void setOneBtn(String name) {
oneBtn.setText(name);
}
}
Listener.class
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Listener {
class oneBtnListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent ev) {
calculatorGUI g = new calculatorGUI();
g.setOneBtn("11");
}
}
}
Boot.class
public class Boot {
public static void main(String[] args) {
calculatorGUI gui = new calculatorGUI();
Listener listen = new Listener();
Listener.oneBtnListener oneListen = listen.new oneBtnListener();
gui.getOneBtn().addActionListener(oneListen);
gui.setVisible(true);
}
}
The problem is, nothing happens when I click the button. It seems that the actionListener is not being registered to the button. Can I ask for your help guys on which angle I missed?
The issue I am seeing is how you are initializing calculatorGUI twice, once with the default value and another with the changed value. Take out the initialization of calculatorGUI within your Listener class and pass it from your Boot class and it should work fine.
Although if I were you, I would add the GUI implementations within the GUI class, having it within the listener class that is using within the main function is not something I have seen before and would probably not advise.
Modify your code accordingly,
class Listener {
class oneBtnListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent ev) {
if(ev.getActionCommand() == "1")
{
JButton btn = (JButton)ev.getSource();
btn.setText("11");
}
}
}
}
class calculatorGUI extends javax.swing.JFrame {
public calculatorGUI() {
initComponents();
}
private void initComponents() {
oneBtn = new javax.swing.JButton();
oneBtnListener btnListener = new Listener().new oneBtnListener();
oneBtn.setText("1");
oneBtn.setBounds(100,100,100,25);
oneBtn.addActionListener(btnListener);
add(oneBtn);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);
}
private javax.swing.JButton oneBtn;
public JButton getOneBtn() {
return oneBtn;
}
public void setOneBtn(String name) {
oneBtn.setText(name);
}
}
You can change now other part according to your requirement, I just
gave you "1" -> "11", but you can do more.
Best of Luck.
First off - sorry for the wall of code but it's not too horrendous, just a framework for what I'm trying to explain. It runs without errors.
The goal
I'm making a reuseable button class for my GUI and each button object needs to have a different handler when it's clicked. I want to to assign a ClickHandler object to each new button. Then, the button would call init() on the handler, and be on its way. Unfortunately, there's a typing problem, since each handler class would have a different name.
Current progress
Right now, the handler is typed as HandlerA, but I'd like to have it handle any name, like "SettingsHandler" or "GoToTheWahWah" etc.
I've tried messing about with generic types, but since I'm new to this, and from a webdev background, there seems to be a conceptual hurdle I keep knocking over. Is this the right way to approach the problem?
The code
ReuseableButton.java is a reuseable class, the only thing that changes is the action when it's clicked:
package gui;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class ReuseableButton extends JButton implements ActionListener {
private static final long serialVersionUID = 1L;
// I want a generic type here, not just HandlerA!
private HandlerA ClickHandler;
// Assemble generic button
public ReuseableButton(Container c, String s) {
super(s);
addActionListener(this);
c.add(this);
}
// Once again, generic type, not just HandlerA!
public void SetClickHandler(HandlerA ch) {
ClickHandler = ch;
}
// Call init() from whatever class has been defined as click handler.
public void actionPerformed(ActionEvent e) {
ClickHandler.init();
}
}
Controller.java fires the frame and assembles buttons as needed (right now, only one button).
package gui;
import javax.swing.*;
import java.awt.*;
public class Controller extends JFrame {
private static final long serialVersionUID = 1L;
public Controller() {
JFrame frame = new JFrame("Handler Test GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = frame.getContentPane();
pane.setLayout(new FlowLayout());
ReuseableButton b = new ReuseableButton(pane,"Reuseable Button A");
// THE QUESTION IS HERE: Pass a generic object?
b.SetClickHandler(new HandlerA());
frame.pack();
frame.setSize(200,200);
frame.setVisible(true);
}
public static void main(String args[]) {
new Controller();
}
}
HandlerA.java is a sample of a random handler for the button click. Later, there could be HandlerB, HandlerC, etc.
package gui;
// A random handler
public class HandlerA {
public void init() {
System.out.println("Button clicked.");
}
}
Thanks very much in advance!
All of you handlers should implement an interface like Clickable or something. That way the interface specifies the existence of the init function:
public interface Clickable
{
public void init();
}
Making the HandlerA definition:
public class HandlerA implements Clickable {
public void init() {
System.out.println("Button clicked.");
}
}
I recommend to work with inheritence in this case:
public abstract class AbstractHandler {
public abstract void init();
}
Then:
public class ConcreteHandlerA extends AbstractHandler {
#Override
public void init() {
// do stuff...
}
}
Controller
public class ReuseableButton extends JButton implements ActionListener {
// I want a generic type here, not just HandlerA!
private AbstractHandler ClickHandler;
public Controller() {
//...
ReuseableButton b = new ReuseableButton(pane,"Reuseable Button A");
AbstractHandler handlerA = new ConcreteHandlerA();
b.SetClickHandler(handlerA);
// ...
}
}
Not sure if this is what you're looking for...
BTW: You can define the AbstractHandler as an interface as well, but you may want to implement some common logic here as well - shared across handlers.
You should use an interface for the handler.
public interface ClickHandler() {
void init();
}
ReuseableButton b = new ReuseableButton(pane,"Reuseable Button A");
b.SetClickHandler(object which implements the ClickHandler interface);
This is the same concept as the normal JButton. There you have the ActionListener interface and the actionPerformed method in it.
P.S. If I don't understand your question, please correct me.
Sorry if this question will sound too chaotic, feel free to edit it.
I have an application made entirely in netbeans, which uses SingleFrameApplication and auto-generated the GUI code, named "MyApp", and FrameView, named "MyView". Now, the MyApp somehow has the main() function, but the MyView has all the graphic elements..
I don't entirely understand how that happens, so used it as black box (it somehow created the window, I didn't have to care why). But now, I need the window to be only a window, opened by another JFrame. I don't know, how to accomplish that.
MyApp, which is extending SingleFrameApplication, have these methods:
public class MyApp extends SingleFrameApplication {
#Override protected void startup() {
show(new MyView(this));
}
#Override protected void configureWindow(java.awt.Window root) {
}
public static MyApp getApplication() {
return Application.getInstance(MyApp.class);
}
public static void main(String[] args) {
launch(MyApp.class, args);
}
}
MyView has these methods:
public class MyView extends FrameView {
public MyView(SingleFrameApplication app) {
super(app);
initComponents();
}
private void initComponents() {
//all the GUI stuff is somehow defined here
}
}
Now, I have no clue how the two classes work, I just want this window, defined in MyView, to appear after another window, "ordinary" JFrame. How can I call this MyApp/MyView?
But now, I need the window to be only a window, opened by another JFrame. I don't know, how to accomplish that.
1.) It's not just a window - it's a
Swing Framework Application (Ah, the
perils of GUI builders...); and -
2.) You haven't specified how you want
it "opened by another JFrame";
but something like this should work if you're launching it via a JButton -
JButton launchMyApp = new JButton("launch");
launchMyApp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String[] args = {};
Application.launch(MyApp.class, args);
}
});