Hi this is a stange one for me so I hope you can help :)
I have a method..
public void WMPEGUI(String info)
it loads a JFrame...
final JFrame mainFrame = new JFrame("JFrame");
the method is set up to recieve strings into it to later be wrote out to a text area also created within the method. When I run the program everytime the methods recieves a string it opens a new JFrame I have tried to solve it using...
mainFrame.setAlwaysOnTop(true);
mainFrame.setLocationByPlatform(true);
But this hasn't solved it, hence asking you kind people. If anyone knows why as I can't find anything on it :/
Many thansk in advance
Well I think your problem is that according to you, your method loads a JFrame. You need to create the JFrame outside that method, somewhere else within the same class, and then refer to it (or to one of it's containers where you will display that string).
Something like this:
public class YourClass {
//Class variables
...
JFrame mainFrame = new JFrame("JFrame");
...
public void WMPEGUI(String info) {
...
mainFrame.someMethod(...) //or a get for one of its containers
...
}
}
Related
I've created a JDialog, and passed my JFrame in with it.
for (int i = 0; i < digiProdRadioBtns.length; i++) {
if (digiProdCheck[i].isSelected()) {
ProdDialog a = new ProdDialog(digiPopup[i], frame, digiProductList.getProduct(counter), digiProductList);
}
I've then tried to access the methods of the JFrame from within the JDialog, but cannot.
public class ProdDialog extends JDialog {
cdDialog = new JDialog(jFrame, true);
this.jframe = jFrame;
jframe.newEmployee();
I've read that what I'm trying to do is possible, any reason as to why it's not working for me?
JFrame and JDialog are top-level containers typically used as view components. In general, they don't communicate except to position a dialog relative to its parent frame. Instead, arrange for your views to communicate using a PropertyChangeEvent, as shown in this example. Having a separate model that contains a notional List<Product> will let you employ the pattern discussed here.
I'm missing a lot of context here, what is it that is not working for you?
I assume you are seeing an error on the jframe.newEmployee(); command saying that the method is undefined, if so then that is reasonable because the JFrame class does not have that method, if your class is class ProdJFrame and it extends JFrame and has that method, then you need to do ((ProdJFrame)jframe).newEmployee();
I read about JDialogs and JOptionPane messages but I still can't get it to work. I have a GUI class that extends JFrame. All I want to do is have a popup at the beginning of my program which informs the user about a couple of things. In my main I create the following gui:
GUI g = new GUI();
Right after that I was to display the window. I have tried the following in the main method:
JOptionPane.showMessageDialog(g, "work?");
JOptionPane.showMessageDialog(frame, "work?"); //(frame was used in documentation example so I tried it)
I also tried to add the pop up into the GUI class with the following
JOptionPane.showMessageDialog(this, "work?"); //(I'm not exactly sure what the Frame Owner parameter is supposed to be, unless I'm confusing this with JDialog.)
In any case, how would I make this window appear? Every single one of the methods I tried compiled, and nothing happened.
public class GUI extends JFrame implements ActionListener{
private Container background;
private static buttons etc...
private static JLabel disp,edisp;
private static JTextArea info;
//setting up the GUI for my program, adding action listeners, I can post more if necessary
}
And then I have the main where I want to call the pop up window
public static void main(String[] args){
GUI g = new GUI();
JOptionPane.showMessageDialog(g,"Work?");
}
Make sure that these are called near the beginning, be it in the main method or not.
Also, try just setting the first parameter as null.
So it reads:
JOptionPane.showMessageDialog(null,"Work?");
Also, remember to import it!
I have a Java program with icons, tooltips...
What I need is that every time a tooltip is showed, the containing text should be copied to the windows clipboard.
Any solution should be a general single solution for all the tooltips. I cannot change the properties of each one of them (there are thousands...)
Thank you!
The way I see it, if you are indeed using Swing, you might need to subclass your components and override createToolTip():
class MyJButton extends JButton {
#Override
public JToolTip createToolTip() {
return new MyJTooltip();
}
}
And:
class MyJTooltip extends JToolTip {
#Override
public void setVisible(boolean aFlag) {
super.setVisible(aFlag);
//copy your text to clipboard here
}
}
Not very practical, not very pretty, but I can't think of another way right now: AFAIK, there is no way to subclass TooltipManager and make it be used as the default.
If you are using JavaFX, things get much easier:
new Button().setTooltip(new MyJavaFxTooltip());
I'm using netbeans and want to edit the text in a label. I want to edit this label from another class in the main driver class. I have about 7 or 8 JDialog pages and let's say the label is on one of those pages.
When I try to call the method from one of those JDialogs so that I can edit it, it keeps asking for a java.awt.Frame. Where would I find that Frame name? Or, is there an easier way of editing labels from another class?
Netbeans makes labels private by default so I looked online and people have said making a setter method would be easiest.
QuickScreen is a .java file for instance...
public static void resetNumbers(){
QuickScreen qs = new QuickScreen(some frame);
qs.editLabel("Hello");
}
Please refer to my last comment on bmoran's solution.
If you change the label you want to set to either default or protected level access, then you can set it from your dialog as long as both classes are in the same package (for default) or your dialog box extends the class that the label is in (protected).
public class FrameClass extends JFrame {
JLabel label1;// package access
MyDialog dialog;
//constructor *** Netbeans may have an init() method ***
public FrameClass(){
label1=new JLabel("Hello!");
...
}
...
}
public class MyDialog{
public void changeLabel(){
FrameClass.label1.setText("Good Bye!");
}
}
I have a running java GUI application right now in a single class file, in this application I have a button that when clicked it is used to instantiate and display a separate form from a different class file in the same project. I am confused with how I actually access this other .java file in order to instantiate and display the form from it. Hope you can help.
Thanks, Beef
How do you access any class file? JFrame, JPanel, JTextField are all examples of Java source code contained in separate files. You would use:
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JTextField textField = new JTextField();
So to access your custom form your would use:
CustomForm form = new CustomForm();
As long as the class file is found on your classpath it should not be a problem.
If you are having compile or run time problems then you need to display the message so we can give further help.
I am somewhat new to Java but can't you just create another class that contains a GUI and then when you click on a JButton component you can just create an instance of that class.
if (clicked == myButton) then {
myGUIClass = new myGUIClass(); //if the GUI is in the constructors this will create
//the frame.
}
Then when you are finished with the JFrame or class then you should have a dispose() method that tidies up all of your files and exits the JFrame.
Best Regards,
Doug Deines Hauf
I think that creating a new form in Java is a bit easier than C# or Visual Basic. Basically you can just create another class and build your form in that class. Once your GUI is built then you can just create an instance of that method in another class to show the gui.
Such as:
if (ButtonClick == true) {
MyGui m = new myGui(...);
m.show
} else
//no GUI shown here
}
Or you can just create an anonymous class to instantiate the GUI.
new myGui(...);
The above would create an anonymous class but I think it is better coding practice to create an actual variable of the class and then to call some method parameter that will show the GUI.