How can I output my data to JPanel from my JFrame - java

I have a JFrame named MainGUI. Inside of MainGUI I have passed three LinkedList ll1, ll2, ll3.
These LinkedList are full of data and I'm trying to just print one of them on the screen into my JPanel. I'm use to just doing a for loop and using System.out.println to print things out onto the screen.
So right now I have MainGUI which hosts three buttons.
New Tasks In Progress Tasks Completed Tasks
Each button has a different LinkedList ll1, ll2, ll3 etc.
I want to be able to click the button and have the data elements listed below in the JPanelI created which rests under the buttons.
Any help is deeply appreciated.

Since you provided no code, I assume you are having trouble understanding how LinkedList can interact in programs that have a GUI.
First off, when using buttons you always need to instruct them to do something when they are clicked by adding an ActionListener, as explained in this answer.
Secondly, if you want to add the list data to the JPanel, there are a few ways you can do it. A JList, or if you'd like the user to be able to copy and paste the data (I find it to be very handy), a JTextArea, ... Just make sure to call setEditable(false) in order to stop the user from fiddling with the data you provide. Considering a JTextArea, here's what that would look like, if ll1 contained Strings:
Adding somewhere that our JPanel contains a JTextArea:
JTextArea txtArea = new JTextArea();
txtArea.setEditable(false);
panel.add(txtArea);
Now, we order the button to do something when clicked:
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txtArea.setText(null); //clear out old text
for(String str: ll1) {
txtArea.append(str+"\n");
}
panel.revalidate(); //repaint JPanel
}
});
This way, you can click the button as many times as you want. Note that if you add more content to ll1 after it is displayed it won't get visually updated by itself, you will always need to click the button again or look further into Listeners.

You can try adding a JTextArea or whichever JComponent that suits what you want to display to the JPanel that you want to display the data from. Write the data from your linked list to that JComponent using its method e.g. append() if you're using JTextArea.

Related

Java: MouseListener of JPanel behind JTextAreas

I've created a component that basically is a JPanel fully covered with (non-editable) JTextAreas. I want a MouseListener to be fired everytime the JPanel area is being clicked on. I do want to add the Listener once to the JPanel instead of n times to the JTextAreas.
Is there a way to send the JTextAreas to background, so the JPanel is clicked "through" the JTextArea?
Note: With JLabels this works without anything special, the JPanels Listener is always fired, but I prefer JTextAreas, because of the linebreak.
With JLabels this works without anything special, the JPanels Listener is always fired
This is because by default a JLabel does not contain a MouseListener so the MouseEvent is passed up the parent tree until a component the does use a MouseListener is found.
In the case of a JTextArea a MouseListener is added to the text area so you can position the caret and select text etc. If you don't need all this functionality you can remove the MouseListener from each text area with code something like:
JTextArea textArea = new JTextArea(...);
MouseListener[] ml = (MouseListener[])textArea.getListeners(MouseListener.class);
for (int i = 0; i < ml.length; i++)
textArea.removeMouseListener( ml[i] );
However, since you have to do that for every text area, I would suggest it is easier to just add the MouseListener to each text area. You can share the same MouseListener with every text area.
There is a solution proposed here, but it might not fully work for your needs.
I don't know if there is a way around adding the listener n times, but if there is not, you could integrate the process cleanly in your code.
For example, with a dedicated method to add the JTextAreas:
public void addJTextArea(JTextArea tArea){
this.add(tArea, ...);
tArea.addMouseListener(this.listener);
}
Or even more transparently with an extended JTextArea:
public class ClickableTextArea extends JTextArea {
public ClickableTextArea(MouseListener listener){
super();
addMouseListener(listener);
}
}

Switch between multiple JFrames

I'm relatively new to Java and I'm trying to make some kind of quiz. I created 3 JFrames, all in the same package. On my main frame, there are two buttons (one for the english version and the other one for the german version). I want to switch JFrames after pressing these buttons (so that I can, by pressing "English", see and interact with my english quiz frame). Looking it up didn't help me the slightest, because I'm not really experienced with it. Is it even possible to do it like this? If not, how could I do it?
The standard approach is to use the Card Layout, which allows you to use the same JFrame as you populate it with different things at different points in your application. So initially, your JFrame would show the loading screen, then the user presses a button and you load a new set of components without discarding the current JFrame you have. In some cases, you might also need to make some size adjustments.
It is difficult to say without seeing any code, but usually, what is done is that you do something like so:
new Frame(args);
this.dispose();
The code above assumes that the constructor of Frame takes care of launching and making the components visible. The this.dispose(); disposes of the current JFrame (assuming your class extends JFrame).
You have two buttons in your frame 1 right? So first, double click the button which says "English". Lets say the variable name for that button is jButton1. Inside that button type this.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
EnglishFrame eng = new EnglishFrame();
eng.setVisible(true);
}
Then double click the other button which says "German" (jButton2). Inside that type this.
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
GermanFrame german = new GermanFrame();
german.setVisible(true);
}
this.dispose() - This will cause the jFrame window to close
Then you create an object of the other two forms. (In your case the form for English and Germany)
.setVisible(true) - This will show you the form.
Create a single Jframe window. After that create JPanels with all the compenents such as buttons, textfields and labels you want. Make sure the panel is the same size as your Jframe. Panel's work about the same as JFrame's, code wise.
This code will stitch everything together for you:
panel.setSize(Jframe.getSize()) //That wont
panel.add(button); //Just remember you need to add more code to position the buttons correctly.
//If you using netbeans builder:
//You just have to use this one line in the constructor/intialiser method
frame.add(panel); //This will add the panel to the Jframe/Window
//No need to add extra code for positioning.
If you want to swap between the panels. In the button press method, use this
frame.setContentPane(panel); //panel = panel you want to change too.
frame.repaint(); //Ensures that the frame swaps to the next panel and doesn't get stuck.
frame.revalidate(); //Ensures that the frame swaps to the next panel and doesn't get stuck.
When you first start the java application you have to set the content pane or else it will appear as a blank window.
frame.setContentPane(panel); //Starting Panel
frame.setVisible(true); //Make the frame visible
Sorry if the explanation is bad, I don't have enough time to explain it fully.

Add (multiple) forms to ScrollPane after clicking button in java

So I have a program that at the start only contains an 'add movie' button at the bottom of the frame.
Above it I inserted a scrollpane.
I also made a seperate JPanel form which contains labels and textfields where you have to input the data of the movie.
Every time I click the 'add'-button I want a form to appear inside the scrollpane (next to previously made forms).
So I figured I just needed to do this:
private void AddMovieButtonActionPerformed(java.awt.event.ActionEvent evt) {
MovieForm movie = new MovieForm();
MovieScrollPane.add(movie);
}
But nothing new appears.
I tried validate() and repaint(), but so far these don't seem to work.
I made the interface in Eclipse btw.
Anyone who can help me?
Thanks anyway!
MovieScrollPane.add(movie);
Don't add components directly to the scrollpane. Normally a JPanel is added the the viewport of the scrollpane.
Then, whenever you add a component to a visible GUI the basic code is:
panel.add(...);
panel.revalidate();
panel.repaint();
This makes sure the layout manager is invoked to the preferred size can be recalculated.
Also, follow Java naming conventions. Variable names should NOT start with an upper case characters.

Delete element from GridBagLayout

how can I delete element from GridBagElement ?
http://i.stack.imgur.com/1BsDW.jpg
I want to remove the selected item by clicking on the button.
How can I send the name of the selected item as a parameter to the action button?
First, you don't delete from GridBagLayout. With swing you add Components to a Container, and you remove them from a Container.
http://docs.oracle.com/javase/6/docs/api/java/awt/Container.html#remove%28java.awt.Component%29
To select the Green Panel you can Register an ActionListener or MouseListener for this Panel.
The MouseListener delivers the MouseEvent.
The MouseEvent can get you the Source with getSource, which is the green Panel.
And that panel can be removed.
A simple example snippet:
final JPanel mainPanel = new JPanel();
JPanel greenPanel = new JPanel();
greenPanel.addActionListener(new ActionListener(){
#Override
public void ActionPerformed(ActionEvent e){
int answer = JOptionPane.showConfirmDialog(null, "Delete?");
if(answer == JOptionPane.YES_OPTION){
mainPanel.remove(e.getSource());
}
}
});
mainPanel.add(greenPanel);
You will want to establish some kind of contract between things like buttons and other controls and your view.
Things that want to modify the view should not be able to do anything you don't want them to (like change the layout for instance).
I'd recommend creating a simple interface which provided access to the operations you want external controls to have access to (like adding, editing, removing). This you would pass to your controls, there by restricting what the controls can actually do and not exposing unnecessary functionality to them (you wouldn't one of them to remove you main panel ;)).
When you want to remove a selected task, you click the appropriate button, it calls the appropriate "remove" method on your model.
You implementation would then find the selected item (which I assume you either maintain a reference to or have some means to find) and simply remove it from it's parent container.

Image not getting displayed on a JPanel

class Deal implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
dl.setDeck();
dl.shuffle();
dl.firstDraw(pl);
for(Card c:pl.showHand())
panelplay.add(new JLabel(c.getImageIcon()));
panelplay.validate();
}
}
This is an event handler for a Jbutton. The method pl.showHand() returns a ArrayList of a user defined class 'Card'. Inserting a println() inside the loop shows the print, so the code is being executed but the Panel panelplay isnt showing card Images.
What about the existing labels on the panel? You don't remove them. I'm guessing you are using a FlowLayout and the labels just get added to the end of the panel so you don't see them.
So one solution is to use panel.removeAll() before adding the labels back to the panel. I then use:
panel.revalidate();
panel.repaint();
Or the better option as suggested earlier is to not replace the labels but just replace the Icons using the setIcon() method.
Do as Gilbert says, look at the Swing Tutorial part that concerns Labels.
JLabel has the following methods...
void setIcon(Icon)
Icon getIcon()
Also look at the SplitPaneDemo It does exactly what you want, you can even run it with JNLP to see.
You don't want to add the JLabel in the ActionListener.
You want to use an already added JLabel setText() method in the ActionListener.
You define all the Swing components once, when you create the GUI.

Categories

Resources