how to make a pop-up window using java - java

I wanted a button to show a pop-up window, I tried using the
JOptionPane.showMessageDialog(null,"");
But I can't put my desired object such as the Table and List. Is it possible?

This is really poorly documented in the Java Docs, because all that tells you is that the "message" parameter is an Object, which can be anything - but does not go into specifics about what happens with different types of objects that may warrant special case handling.
As far as I have gathered from experimenting with it, the "message" can be a subtype of Component - then it will just place the component in the message area of the dialog box as-is, like:
JOptionPane.showMessageDialog(null, new JCheckBox("I'm a checkbox!"));
Otherwise, it will just call the toString method on the object, converting it into a string, which it then will just wrap in a label and place that in the dialog as the message.
But you can also pass in an Array of Objects, in which case it will just place each element in a separate row in the message area:
JOptionPane.showMessageDialog(null, new Object[] {
new JCheckBox("check"),
new JRadioButton("radio"),
"plain text"});
There might be other special cases, but I haven't found them yet.
That said, if what you want to display is a subclass of Component (or JComponent), just passing it in as the message parameter should work. If it doesn't, you might want to edit your question to describe whatever problems you are encountering in more detail, perhaps also providing some sample code.

Start by taking a closer look at the JOptionPane JavaDocs, showMessageDialog clearly states that it accepts a Object as the message parameter
One of the nice features of this, is if the Object is Component, it will be added to the dialog.
For example: JOptionPane displaying HTML problems in Java and How do i make the output come in different columns?

Related

Java Swing How can I create a Color Swatch in my custom ColorChooserPanel?

I'm working on my custom ColorChooserPanel class, but I don't know how to create a ColorSwatch.
I found a class named ColorSwatch or something like that but its package private and I can't access it.
How can I ensure that I have a proper color chooser?
First, you need to think about the way this should work, to formulate your goals into action points that you can always check and see whether and how they are fulfilled:
it should be displayed
it should handle click events
it should be able to return a color (the last chosen color, or a default in lack of that)
Now, a very common way of supporting color choosers is to have something like in this image:
However, this is way too complex for a first implementation. You should first have something which "works", that is, you are able to choose colors to taste success and then work out the subsequent details. Unless you say otherwise, I'm assuming that the Basic colors section is good for now. In that case, you can create a class, which contains a JPanel, containing clickable elements. Your class needs to have a Color member, which one can get calling a getter and which is set when a clickable item inside your JPanel is clicked. The clickable items can be JPanels on their own, for example.
If you prefer something ready-made, then kindly read these:
https://docs.oracle.com/javase/tutorial/uiswing/components/colorchooser.html
https://coderanch.com/t/332515/java/Color-Palette

Change information in a dialog box from another dialog box in WindowsBuilder?

In windowsbuilder, I would like to change information from a seperate window, more specifically a table, from my main window. Is it possible to use my information from one seperate window to change it in another box?
Basically, take information from here:
And put it into this table:
Is it possible? If so, how? Thank you in advance.
Yes, it is possible - but not in Windows Builder.
Create an object which will hold the information from the first window and pass the information from the window to the object. Next, pass the object to the second window (how you do this will depend on how you've configured your window objects). Finally, populate the table with the information from the object.
As far as I know, there is no direct way to do this in Windows Builder, although the above method will work fine provided you can pass objects from the first window to the second.

JOptionPane List Actions

I want change the icon property of an open JOptionPane upon clicking a list item. (They will correspond with each other)
Is there an equivalent to ActionListener for this? I know how to code for the buttons, unfortunately these actions are not easy?
JOptionPane.showInputDialog(null,
"SELECT A PRIMARY WEAPON FROM THE LIST",
"ARMOURY",
JOptionPane.OK_CANCEL_OPTION,
get.getIcon("shield_and_swords.png"),
character.weaponList(),
"Absolix Polearm")
This is not possible. The JOptionPane.showInputDialog documentation states:
It is up to the UI to decide how best to represent the selectionValues, but usually a JComboBox, JList, or JTextField will be used.
This means the actual GUI element used is implementation-defined and so there couldn't really be a general way to access it, or a consistent action interface to it (it might not even be a clickable list box, for example). Basically, you call showInputDialog() and you get a result back, but beyond that it's a black box and the actual GUI could be anything -- no guarantees are made.
Perhaps you could come up with some really dirty object tree hacks to find the GUI element in most cases, but... consider perhaps creating your own custom dialog with a predictable GUI that you can control instead.

Adding text input to a blackberrycanvas

The BlackBerryCanvas documentation says:
This class extends the functionality of the Canvas class to include full touch support and featured text input support.
I have extended BlackBerryCanvas, but am having trouble adding any text input.
It's not that I know what to do but cannot get it work -- I simply do not know how to add a text input box or field.
EDIT: Or have I misunderstood and this is not possible? From reading around, it seems as though it is, but I'm starting to wonder why it's so hard to find anything on it.
EDIT2: I'm think maybe it's something to do with the BlackBerryTextBox?
EDIT3:
Applications using this class can call the #setInputHelper method to get the text input support.
might also be something. It hasn't given me quite enough clues to be able to do it myself though I'm afraid.
Thanks.
If you don't particularly need to use the Canvas hierarchy, I suggest you use the more commonly used Field hierarchy.
That means you should create an application and start with a Screen. An easy concrete implementation of Screen is the FullScreen. To get the text input you are seeking, add an AutoTextEditField to the screen.

Reset/remove a border in Swing

Here's a very specific coding question:
I've recently been asked to maintain some old-ish Java Swing GUI code at work and ran into this problem:
I've attached my own subclass of InputVerifier called MyFilenameVerifier to a JTextField (but it may as well be any JComponent for these purposes). I've overridden the verify() method such that it calls super.verify(input) (where input is the JComponent parameter to verify()). If super.verify(input) comes back false, I do:
input.setBorder(BorderFactory.createLineBorder(Color.RED));
This is a convention used throughout the UI of this application that started long before me, so I don't have a lot of choice as far as using other ways to get the users attention (wish I did). This is just the way it works.
Problem is, once the user goes back and types something valid into the text field, I need a way to set it back to default border (instead of just saying set it to Color.GRAY or whatever, which is a different color from its original border). I need a way to say, "remove the extra decoration and go back to normal" or just set the border to its default, in other words.
Couldn't you just call input.getBorder() and cache it somewhere before setting the border to red?
Or without caching anything, you could tell the JComponent to update its UI back to the look and feel's defaults via component.updateUI. That should make the component reset its colors, borders, fonts, etc to match the original settings.
input.getBorder()
Wouldn't it be awesome if no one ever saw this and I got away free without the ass-beating I deserve for asking this question?
Not sure how your system is build, but I think you can store the original border before changing it. So you can change it back later
// assuming the Border was not null before
if (!super.verify(input)) {
original = input.getBorder();
input.setBorder(...);
} else {
if (original != null) {
input.setBorder(original);
original = null; // not needed
}
}
You need to preserve the existing border when you change it.
One way to do this is to use the methods putClientProperty() and getClientProperty(), which you'll find documented in the API.
Another possibility, if there are only a few input widgets you need this for is to subclass, e.g. JTextField, add setBorderOverride() and modify getBorder() to return "overriddingBorder" if it is not null.
Then you just use setBorderOverride(redBorder) to make it red and setBorderOverride(null) to clear it.
This of course depends on the painting to use getBorder(), which it may or may not do, and which may be implementation specific.
Incidentally, you only need a single static reference to the border-- it's the selfsame border instance used by all JTextFields.

Categories