Multi Slide Representation - java

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().

Related

Individually adding Buttons

I plan to make a text Adventure for a school project, for this I need to add individual buttons which all have actions
Example:
You stand before a troll, a button shows "Attack" and it actually lets
you attack
you stand before a door, a button on the exact same spot shows "Open"
and it opens the door
Every tutorial I've found only shows how to make buttons and add them but I need to individually generate buttons with full functions.
You can create all of your buttons and place them where you want to (same position as you mentioned). After that you only have to change the visibility of the buttons with the setVisible() method.
Like this:
if(/*stand before a troll condition*/)
{
attackButton.setVisible(true);
openButton.setVisible(false);
}
else if(/*stand before a door condition*/)
{
attackButton.setVisible(false);
openButton.setVisible(true);
}
else // standing before nothing
{
attackButton.setVisible(false);
openButton.setVisible(false);
}

Info card prioritizing -java flash card applet

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.

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.

Trouble with Java GUI

I am creating a GUI for my project. I am completely stuck at one point. I was able to create a GUI which will make some simple queries like the release number, command name, and few other boolean questions.
Now I want the user to press CONTINUE button to move to the next page of the GUI form. I have created the continue button, but now I need to register an event to it. This is where I am stuck. I have no idea what event can I register to it which will move the GUI to next page. (By moving to the next page I mean, the different GUI pages we see when we are installing a software for our computer.)
For instance, if I am installing iTunes, I'd first select the radio button for "I accept the terms and conditions" and then I'd press the CONTINUE or the NEXT button to move ahead. If I need to come back, I'd press the BACK button.
One logical answer would be to create another GUI form and then link it to the one I created first.
EDIT:: this is the first time I am working in Java, so I might have ignored some obvious facts.
Look into using a CardLayout, adding several JPanels to the CardLayout-using container, and then to swap to the next view (the next JPanel), call the layout's next(...) method in the JButton's ActionListener. You could also randomly access components held by the CardLayout using its show(...) method.
To learn more about this including sample code, please have a look at the CardLayout Tutorial, and the API.
Well, register an ActionListener or an Action with the button. To do that wizard style, have a look at the CardLayout layout manager to switch cards or use a tabbed pane, hide the tabs and switch them inside the action or action listener.
if i understood, i think you should use the CardLayout
If you are using Swing, you can using several instances of JPanel over a one instance of JFrame.
You can have something like that structure:
JFrame
+------JPanel:root
|
+---JPanel:current // This panel change by other instance
|
+---JPanel:controlPanel // This panel contains you button
In you button need add a ActionListener with the method addActionListener
The panel control may need changes your elements, may the text of button or remove the listener and change by other.
I hope that can help

How to use setVisible in JFrames?

In my program I have two JFrame instances. When I click next button I want to show next frame and hide current frame. So I use this.setVisible(false) and new Next().setVisible(true). But in Next window if I click back button I want to set previous frame to be visible again and next frame must be ended (which means it must be exited).
Is there any special method(s) to do this? How can I do it?
Consider using CardLayout instead of hunting for how many JFrames there are. Then..
only one JFrame would be needed
any of Next/Back Actions will be only switching between cards
There are lots of examples in this forum - e.g. as shown here.
That is an odd & quirky GUI. I suggest instead to run a JFrame for the main GUI, and when the user wants to search, pop a JOptionPane (or modal JDialog) to accept the details to search for. This will not have the effect described above, but will follow the 'path of least surprise' for the end user.
If you want to destroy a JFrame releasing all associated resources you shold call dispose() method on it.
You may place your JFrames on a list data structure and keep a reference to current position according to the window you are displaying. In that way it will be easy to move to next and previous. But note that each frame added to the list will use memory and will have its state as you placed it in to the list.
If you are trying to create a wizard like UI, you should look up Sun(oracle)tutorial here.
create the instance of your main window in next() window.. and use same method which you chosed befoe to hide your main window, for example if your main window is named as gui then what we have to do is.
gui obj = new gui();
and if you click on back button now than do these also
this.setVisibility(false);
obj.setVisibility(true);
that's all you need.
good luck.

Categories

Resources