Reputation: 884
I have a Jpanel class representing 3 panels each for its own tab. It decides what to add on the panel and what not. Then I have a class where I store my data that the program uses. but when I make 3 instances, one for each tab I have to make sure that the data class is the same for each instance and that even when modified will still remain the same.
Here I instanciate the tabs and I pass the number of the tab and the data (Mensola)
Mensola m = new Mensola(20);
JTabbedPane tabbedPane = new JTabbedPane();
JComponent panel1 = new MyPanel1(1,m);
tabbedPane.addTab("Aggiungi",null ,panel1,"Aggiungi");
JComponent panel2 = new MyPanel1(2,m);
tabbedPane.addTab("Cerca",null, panel2,"Cerca");
JComponent panel3 = new MyPanel1(3,m);
tabbedPane.addTab("Togli",null, panel3,"Togli");
add(tabbedPane);
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
Panel class:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class MyPanel1 extends JPanel implements ActionListener, ListSelectionListener{
private Libro temp;
ActionEvent Event;
Mensola m = new Mensola(20);
private String sTemp = "Non conosiuto";
private JLabel title;
private JLabel author;
private JLabel editor;
private JLabel pages;
private JLabel price;
private JLabel type;
private JLabel inProduzione;
private JLabel message;
private JLabel nummeroLibri;
private JRadioButton si;
private JRadioButton no;
private ButtonGroup group;
private JList list;
private String[] generi = {"Horror", "Commedia", "Thriller", "Comic"};
private JTextField titleField;
private JTextField authorField;
private JTextField editorField;
private JTextField pagesField;
private JTextField priceField;
private JButton aggiungi;
private JButton cerca;
private JButton togli;
public MyPanel1(int sheet,Mensola x){
m = x;
this.setLayout(new FlowLayout());
m = new Mensola(20);
title = new JLabel("Titolo: ");
author = new JLabel("autore: ");
editor = new JLabel("editore: ");
pages = new JLabel("pagine: ");
price = new JLabel("prezzo: ");
type = new JLabel("genere: ");
type.setPreferredSize(new Dimension(280,20));
inProduzione = new JLabel("In porduzione: ");
message = new JLabel("");
message.setPreferredSize(new Dimension(280,170));
nummeroLibri = new JLabel("Nummero di liberi: 0");
list = new JList(generi);
list.addListSelectionListener(this);
list.setPreferredSize(new Dimension(280,70));
titleField = new JTextField("",20);
authorField = new JTextField("",20);
editorField = new JTextField("",20);
pagesField = new JTextField("",20);
priceField = new JTextField("",20);
aggiungi = new JButton("Aggiungi Libro");
aggiungi.addActionListener(this);
aggiungi.setPreferredSize(new Dimension(280,30));
cerca = new JButton("Cerca Libro In Base Al Titolo");
cerca.addActionListener(this);
cerca.setPreferredSize(new Dimension(280,30));
togli = new JButton("Togli Libro In Base Al Titolo");
togli.addActionListener(this);
togli.setPreferredSize(new Dimension(280,30));
si = new JRadioButton("Si");
si.addActionListener(this);
no = new JRadioButton("No");
no.addActionListener(this);
group = new ButtonGroup();
group.add(si);
group.add(no);
if(sheet == 1){
this.add(title);
this.add(titleField);
this.add(author);
this.add(authorField);
this.add(editor);
this.add(editorField);
this.add(pages);
this.add(pagesField);
this.add(price);
this.add(priceField);
this.add(inProduzione);
this.add(si);
this.add(no);
this.add(type);
this.add(list);
this.add(aggiungi);
this.add(nummeroLibri);
this.add(message);
}else if (sheet == 2){
this.add(title);
this.add(titleField);
this.add(cerca);
this.add(nummeroLibri);
this.add(message);
}else {
this.add(title);
this.add(titleField);
this.add(togli);
this.add(nummeroLibri);
this.add(message);
}
}
}
Upvotes: 0
Views: 66
Reputation: 1123
If I understand your problem correctly, you might want to use the Singleton Design Pattern.
Thus, instead of creating new objects every time, you create a method getInstance()
, which either creates a new Object (if there is no existing one yet) or returns the current instance. Therefore, it is ensured you are always working with the same instance of an Object.
Another method would be using a constructor variable each time, but you must not create a new Object otherwise you will use different instances of course.
Edit: to clarify my answer:
Add the method getInstance()
to your Mensola class. Simplified:
private static Mensola mensolaInstance;
[...]
public static Mensola getInstance() {
if (mensolaInstance == null)
mensolaInstance = new Mensola(20); // or whatever
return mensolaInstance;
}
Any time and any place you want to access your Mensola object, you simply use Mensola.getInstance()
. You don't need to pass it as a parameter anymore.
So class A needs Mensola you can call getInstance()
from there. Later, class B needs to work with Mensola, again you simply call getInstance()
.
Upvotes: 1