Info card prioritizing -java flash card applet - java

I am wanting to make an applet for practice, and my goal is to make a program that displays seven rectangles with info inside of each one. I would also like the cards to display in a random order.
After the cards are displayed, the user should be able to click on the card, and then the card should be removed from the options and be displayed beneath them, in the order that you click them. This may sound confusing, but I basically want the user to be able to prioritize or sort the info cards.
For example, if the cards had dates on them, the user could sort them in order from past to present.
My first idea was to draw rectangles on the screen and get the mouse click x and y to see if the user clicked that card, but I'm sure that there is another way that doesn't have to be that complicated.
I'm sorry that I don't have decent code to post, I would rather not post my messy version. I can update this with code later.
I'm wondering what the best solution would be, because I would like to learn as much as I can from this project.

You can use panels, and register for action events. Action events don't care the coordinates where your mouse was clicked, but rather if the component was clicked or not. You can use setActionCommand() to identify each panel (card) or use some other attribute of your panel that you can read after capturing the event (the event.getSource() method returns the component that was clicked).
panel.setActionCommand("card1");
panel.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent event) {
if (event.getActionCommand().equals("card1") {
// do something
}
}
}
You can also use an existing LayoutManager or adapt or write one to display your panels any way you like.

Related

Java and working with different instances

I have a program where I can click on a button and it calls in a new panel, making the current one false through action listener. This panel uses up the whole size of the JFrame, giving me a form to fill out and buttons to press.
Is this the correct method to do this? Or is there a better way as I am running into bugs where the newly called JPanel appears on other JPanels. I could use setVisible(false) but then certain elements such as buttons do not appear.
According to what you want, it appears to me you would probably want to look at CardLayout where you can swtich 2 or more panels in the frame.
Conceptually, each component that a CardLayout manages is like a playing card or trading card in a stack, where only the top card is visible at any time. You can choose the card that is showing in any of the following ways:
By asking for either the first or last card, in the order it was added to the container
By flipping through the deck backwards or forwards
By specifying a card with a specific name

Adding components in GUI upon repaint method

I'm not understanding Java GUI's as well as I thought. In my paint method for a frame, I'd like to wipe all of the current buttons, and add new ones. (The overall goal is to have an interface where the user can see characters and click on the buttons to download documents related to the character. Since every character is different, when the user selects a new user from my list, a new set of documents and buttons will be available to them.)
This is a test frame that I just wrote that shows where things go sideways. It has the similar paradigms that I use in my actual program, without too much clutter:
public class GUITest extends JFrame
{
/**
* #param args
*/
public static void main(String[] args)
{
Container gui_test = new GUITest();
}
private JComponent content = null;
public GUITest()
{
super();
setVisible(true);
}
public void paint(Graphics g)
{
this.removeAll();
content = new JPanel();
JComponent test_button = new JButton("New Button 1");
JComponent button = new JButton("New Button 2");
content.add(button);
content.add(test_button);
this.add(content);
super.paint(g);
}
}
Without the call to removeAll(), buttons will continue to be thrown on top of the JPanel, but with the call, nothing shows up. I don't know why this is, as I'm adding the components appropriately, right?
Edit
Got it, let me give you a more detailed breakdown. A client is navigating my program by looking at a list of characters in a game on a west panel. They can select a row from the list which will show char details on the east panel. The details are an image and description. Recently, I added relevant documents for that particular char, which will show on the bottom of the east panel. I created key listener's, so the client can quickly view the document by pressing a num key, but I also want to give them the ability to click on the button to launch a pdf view and see the contents of the document.
Since every char has different related docs and different number of docs, I repainted the buttons every time, to reflect the amount of related docs and the appropriate titles for the docs. This is where the repaint is acting strange. You gave me a good explanation of what's going wrong, but I don't know how to give the client access to the docs now, aside from painting a description of the doc along with the hot key needed to launch it. Does that make sense?
Never add components to your GUI or remove components in the paint or paintComponent methods. Just don't do it. Ever. Period.
These methods are for drawing only, and need to be as fast as possible, else your program will appear unresponsive. Not only that, you do not have full control over when or even if these methods will be called, so program logic and structure should not go into these methods.
Instead react to user events with event listeners such as ActionListeners, ListSelectionListeners, or with key bindings.
Edit
Regarding
Got it, let me give you a more detailed breakdown. A client is navigating my program by looking at a list of characters in a game on a west panel. They can select a row from the list which will show char details on the east panel. The details are an image and description. Recently, I added relevant documents for that particular char, which will show on the bottom of the east panel. I created key listener's, so the client can quickly view the document by pressing a num key, but I also want to give them the ability to click on the button to launch a pdf view and see the contents of the document.
I'd use a JList to hold the list of selectable information on the left, and would react to it with a ListSelectionListener. In the listener, I'd change the related displayed information. I also avoid using KeyListeners with Swing but instead gravitate towards Key Bindings as they're more flexible and less rigid regarding focus.
Regarding
Since every char has different related docs and different number of docs, I repainted the buttons every time, to reflect the amount of related docs and the appropriate titles for the docs. This is where the repaint is acting strange. You gave me a good explanation of what's going wrong, but I don't know how to give the client access to the docs now, aside from painting a description of the doc along with the hot key needed to launch it. Does that make sense?
I'm not sure what you're doing here or what you're trying to do.
Since every char has different related docs and different number of docs, I repainted the buttons every time, to reflect the amount of related docs and the appropriate titles for the docs. This is where the repaint is acting strange. You gave me a good explanation of what's going wrong, but I don't know how to give the client access to the docs now, aside from painting a description of the doc along with the hot key needed to launch it. Does that make sense?
So rather then "painting" the buttons, why not just change there text (setText(...)).
When a user selects a "char". You are going to need to rebuild portions of your screen. Change the list model (as suggest above) and remove/add any buttons you need on the document container.

Program with split screen, how can I change the content [Java]

I had just a few class of java on college, but I want to do a thing that I don't know how.
Is basically a window with a SplitPane, on Left side I have a menu made with toggle buttons, and on the Right side I need to change the content based on each button.
Theres any way to design the ViewA and ViewB on separated JFrame Form and load then inside my Right Side when I click on menu items?
Another idea is, put the ViewA and ViewB put a JTabbedPane on the Right Side, and hide the Tabs. So there's any way to hide the tabs?
I have none experience developing in java, any problem about this concept (difficult, loading time, memory, maintenance), If you guy know a better way to to this, I just don't want a lot of windows popping up.
A really simply way would be to simply have a set of jPanels in the right side, with only one ever set to Visible.
Basically, for each toggle on the left side, you will have an Event Listener that does this:
private void toggle1ActionPerformed(java.awt.event.ActionEvent evt) {
jPanel1.setVisible(false);
jPanel2.setVisible(false);
jPanel3.setVisible(true);
}
Simply changing the true value depending on the individual toggle.
In Netbeans, if using the GUI editor, you can simply double click the toggle button to generate the listener and appropriate method, then fill in the code for it.

Multi Slide Representation

I have a project in which I am supposed to have a simple painting panel and I also have to make it possible for the user to have more than one drawing panel, like a multi slide representation.
So far, I finished the coding for 1 single panel which is an expansion of JPanel. Now, with two smiple JButtons on it(previous and next), I need to be able to open a new clean panel and I also need to be able to go back to the previous one which includes my last drawings.
I'm kinda stuck here and need some idea about how to make this work.
Use a CardLayout for the slides. It has next() / previous() methods.
You can use a LinkedList to represent your slides, each element of the linkedList could be the JPanel. To navigate I think it's easier to use a ListIterator (you can access it with the method LinkedList.listIterator()), so when your user press the forward button your could would look like:
void btnForwardPressed(){
if(!this.iter.hasNext()) System.out.println("No slides forward");
else this.currentSlide = this.iter.next();
}
And for the back button you would have something like this:
void btnBackPressed(){
if(!this.iter.hasPrevious()) System.out.println("No slides back");
else this.currentSlide = this.iter.previous();
}
You could also control the back and forward buttons state by tracking the return of the methods this.iter.hasPrevious() and this.iter.hasNext().

Rectangular Java Swing Radio buttons?

I'd like to create a set of buttons in a Java Swing application like you get in a typical tool palette in a paint program. That is, a set of small square buttons, each containing an icon, only one of which is pressed down, and when you press another button, the first is deselected. I've thought of a number of solutions and none of them seem very easy/elegant.
This sounds like a job for JRadioButton, but if you add an Icon to that, you still get the small circle, which is fairly space inefficient. I guess an option would be finding an alternative Look and Feel or painting code for JRadioButton.
Another alternative might be to add JButton to a ButtonGroup, maybe setting a JToggleButton.ToggleButtonModel as the model, but that doesn't have the desired effect, as the painting code for a standard JButton does not keep it depressed when selected. Possibly the JButton code could be modified to do this. Like making it painting "selected" the same way as "pressed".
A third alternative would be to use normal JButton's, and add a common mouse listener that keeps them pressed or not, and communicates the changes between the buttons.
Can anyone advise on the best way to achieve the aim please? A simple method I've missed would be best, but advice on which of these three alternatives would be best and pointers on how to get started would be useful too.
What about a plain JToggleButton in a ButtonGroup? It is not abstract, you can instantiate one with an Icon, and it stays depressed while selected.
See the SwingSet2 demo:
http://java.sun.com/products/plugin/1.4/demos/jfc/SwingSet2/SwingSet2.html
Click the second icon on the toolbar (the one twith the check box and radio button) then tab "Radio buttons". Then click on "Paint Border" on the right panel, under "Display Options".
Source code of the demo is under your JDK install dir, so for example on my PC it's under \jdk1.6.0_01\demo\jfc\SwingSet2\src

Categories

Resources