How can I pass an ArrayList between two separate JTabbedPanes - java

I have a program utilizing JTabbedPane. On one pane I have a button that updates an arrayList of objects based on input from the same pane.
What I would like to happen is have the second pane update itself with the object information based on the arrayList in the first pane.
However, I am not sure how to pass the list between the panes. Is there some way to push the array to pane #2 when the update button on the first pane is pressed?
Here is the main file. Instantiating the two tabs
public class Assignment6 extends JApplet
{
private int APPLET_WIDTH = 650, APPLET_HEIGHT = 350;
private JTabbedPane tPane;
private StorePanel storePanel;
private PurchasePanel purchasePanel;
private ArrayList computerList;
public void init()
{
computerList = new ArrayList();
storePanel = new StorePanel(computerList, purchasePanel);
purchasePanel = new PurchasePanel(computerList);
tPane = new JTabbedPane();
tPane.addTab("Store Inventory", storePanel);
tPane.addTab("Customer Purchase", purchasePanel);
getContentPane().add(tPane);
setSize (APPLET_WIDTH, APPLET_HEIGHT); //set Applet size
}
}
The first panel instantiates a button listener that applies all of the logic to the array list "compList"
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//Add Computer to list
Computer comp = new Computer();
comp.setBrandName(brandField.getText());
comp.setPrice(Double.parseDouble(priceField.getText()));
comp.setMemory(Integer.parseInt(memoryField.getText()));
comp.setCPU(typeField.getText(), Integer.parseInt(speedField.getText()));
compList.add(comp);
}
stringField.setText(listString);
alertLabel.setText("Computer Added");
}
}
Here is the other pane. The for loop at the end is what I need to push the arrayList to. After it receives the list, it populates a box with a checkbox for each object in the list
public PurchasePanel(ArrayList compList)
{
west = new JPanel();
east = new JPanel();
totalField = new JTextField();
this.compList = compList;
setLayout(new GridLayout(0,2));
add(west);
add(east);
east.setLayout(new BorderLayout());
east.add(currentTotalLabel, BorderLayout.NORTH);
east.add(totalField, BorderLayout.CENTER);
west.setLayout( new BoxLayout(west, BoxLayout.Y_AXIS));
for(Object c : compList){
System.out.println("Made it");
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String str = ("BrandName:" + (((Computer) c).getBrandName() +"CPU:" + (((Computer) c).getCPU() +"Memory:" + ((Computer) c).getMemory() + "M" +"Price:" + fmt.format(((Computer) c).getPrice()))));
JCheckBox chk = new JCheckBox(str);
west.add(chk);
}
}
}

You can use the same listener used to update the first ArrayList, to update the second pane. Something like:
jButton1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// Update the first ArrayList
// Update the second pane
}

Related

JComboBox actionListener doesn't function

I have a class that has 4 private attributes, and through a JComboBox selection, I want to modify them through calling a procedure. However, it seems like even though the JComboBox appears with the selection, the attributes that are shown don't change.
public class PanneauVehicule extends JPanel {
private String[] vehicules;
private int majCarburant;
private int majPassager;
public class PanneauVehicule extends JPanel {
//Main constructor
public PanneauVehicule(){
//Creates a JPanel
super();
//Sets layout as BorderLayout
setLayout(new BorderLayout());
initListeVehicule();
initLabels();
}
public void initListeVehicule(){
vehicules = new String[] {Constantes.CS100 , Constantes.CS300 ,
Constantes.GREYHOUND102D3 , Constantes.GREYHOUNDG4500 ,
Constantes.TGVATLANTIQUE , Constantes.TGVDUPLEX};
final JComboBox<String> vehiculesList = new JComboBox<>(vehicules);
//Keep in mind the comboBox does appear with the right selections
add(vehiculesList,BorderLayout.NORTH);
//Here's where it doesnt work.
vehiculesList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
majInfo(2,4);
}
});
}
public void majInfo(int test1, int test2){
this.majCarburant = test2;
this.majPassager = test1;
}
public void initLabels(){
JPanel panneauBas = new JPanel();
panneauBas.setLayout(new GridLayout(2,1,5,5));
JLabel labelCarburant = new JLabel();
labelCarburant.setText("Type de caburant: " + this.majCarburant);
JLabel labelPassagers = new JLabel();
labelPassagers.setText("Nb de passagers: " + this.majPassager);
panneauBas.add(labelPassagers);
panneauBas.add(labelCarburant);
add(panneauBas, BorderLayout.SOUTH);
panneauBas.setBackground(Color.WHITE);
}
After that, I use another procedure that will make majCarburant and majPassager appear on screen. However their values are shown as default (0). I can make their values change manually without using an ActionListener, but the task at hand requires me to use one.
I've been trying ways to just simply change the values through actionListener directly,
You don't invoke an ActionListener directly. Once an ActionListener has been added to the combo box, you can invoke:
setSelectedItem(...) or
setSelectedIndex(...)
on the combo box and the combo box will invoke the ActionListener.
I found the solution after a few more hours of meddling. I just integrated the procedure that creates labels into initListeVehicule, and from there the ActionListener can access the labels to modify their texts.
public void initListeVehiculeInfos(){
vehicules = new String[] {Constantes.CS100 , Constantes.CS300 ,
Constantes.GREYHOUND102D3 , Constantes.GREYHOUNDG4500 ,
Constantes.TGVATLANTIQUE , Constantes.TGVDUPLEX};
final JComboBox<String> vehiculesList = new JComboBox<>(vehicules);
add(vehiculesList,BorderLayout.NORTH);
JPanel panneauBas = panelGenerator();
//these setTexts serve as default values before doing your first selection
final JLabel carb = labelGenerator();
carb.setText("Carburant: Kérosène");
final JLabel passager = labelGenerator();
passager.setText("Nb Passager: 110");
vehiculesList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
InterfaceVehicules info = FabriqueVehicule.obtenirVehicule(vehiculesList.getSelectedIndex());
carb.setText("Carburant: " + info.tabNomTypeCarburant[info.getTypeCarburant()] );
passager.setText("Nb Passagers: " + info.getNbPassagersMax());
}
});
panneauBas.add(carb);
panneauBas.add(passager);
add(panneauBas, BorderLayout.SOUTH);
}

Create a Method to add components in Selected JTabbedPane

I am a newbie, and I have already passed some days to get the idea, but could not solve it, if anyone can help me.
I have main two JPanel in one JFrame. I have some buttons in the first panel. I have One method to add a panel to theJTabbedPane by calling another method to create that JPanels then add it to the Tab.
I want a method which will add any buttons/components to the Selected tab which I want whenever I click the button from the first panel.
How can I do this. I can post those three method's code here if anyone wish to help me. Thanks in advance.
The first code add new tabs to the JTabbedPane,
private class TabPlus implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
JPanel panel = CreateSlice();
String title = "Slice " + String.valueOf(pane.getTabCount());
pane.insertTab(title, null, panel, null, pane.getTabCount() - 1);
}
}
The second code is for selecting the tabs,
private class TabSelect implements ChangeListener {
#Override
public void stateChanged(ChangeEvent e) {
JTabbedPane source = (JTabbedPane) e.getSource();
if (source.getSelectedComponent() != null) {
int index = source.getSelectedIndex();
System.out.print(index);
}
}
}
This creates the JPanel to be added to the JTabbedPane,
public JPanel CreateSlice() {
JPanel Slice = new JPanel();
Slice.setPreferredSize(new Dimension(550, 600));
return Slice;
}
The buttons are in another Panel with their own action.
GUI Image
You can add a panel with butttons this way:
public JPanel CreateSlice() {
JPanel Slice = new JPanel();
Slice.setPreferredSize(new Dimension(550, 600));
JPanel buttonsPanel = new JPanel();
buttonsPanel.add(new JButton("dumpButton"));
buttonsPanel.setVisible(false);
Slice.add(buttonsPanel);
return Slice;
}
And whenever a selection occurs set the visibility of buttonsPanel to true.
I think, I found one way to do this, My index variable int index = source.getSelectedIndex(); must be public variable, then in Action event of the buttons from another tab will add component to this selected JTabbedPane.
In my case I introduced and ArrayList of panel (network_slices), and every time I am adding a panel to the JTabbedPane, it is also adding to the ArrayList. then I called the panel from the ArrayList by using the index of selected JTabbedPane. Here is the part of the code.
private class TabSelect implements ChangeListener {
#Override
public void stateChanged(ChangeEvent e) {
JTabbedPane source = (JTabbedPane) e.getSource();
if (source.getSelectedComponent() != null) {
index = source.getSelectedIndex();
System.out.print(index);
}
}
}
private class AddTab implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
Slice panel = new Slice();
panel.setPreferredSize(new Dimension(550, 600));
String title = "Slice " + String.valueOf(pane.getTabCount());
pane.insertTab(title, null, panel, null, pane.getTabCount() - 1);
network_slices.add(panel);
}
}
private class AddNetworkFuncitons implements ActionListener {
public void actionPerformed(ActionEvent e) {
String buttonselected = (String) e.getActionCommand();
JButton button = new JButton(buttonselected);
network_slices.get(index).add(button).setVisible(true);
System.out.print(buttonselected);
}
}

Change JTextArea texts from JButton's actionPerformed which is in another class

I have a fully functional console-based database which I need to add GUIs to. I have created a tab page (currently only one tab) with a button "Display All Student" which when triggered will display a list of students inside a JTextArea which of course is in its own class and not inside the button's action listener class. Problem is, the JTextArea is not recognised inside button's action listener. If I add parameter into the action listener, more errors arise. Help?
I have searched Stack Overflow for similar problems but when I tried it in my code, doesn't really do the trick? Or maybe I just need a nudge in the head. Anyways.
Here is my code so far:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class StudDatabase extends JFrame
{
private JTabbedPane tabbedPane;
private JPanel studentPanel;
private static Scanner input = new Scanner(System.in);
static int studentCount = 0;
static Student studentArray[] = new Student[500];
public StudDatabase()
{
setTitle("Student Database");
setSize(650, 500);
setBackground(Color.gray);
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
// Create the tab pages
createStudentPage();
// more tabs later...
// Create a tab pane
tabbedPane = new JTabbedPane();
tabbedPane.addTab( "Student Admin", studentPanel );
topPanel.add( tabbedPane, BorderLayout.CENTER );
}
public void createStudentPage()
{
studentPanel = new JPanel();
studentPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
JButton listButton = new JButton("List All Student(s)");
listButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if(studentCount > 0)
{
for(int i=0; i<studentCount; i++)
{
// print out the details into JTextArea
// ERROR! textDisplay not recognised!!!
textDisplay.append("Student " + i);
}
System.out.printf("\n");
}
else // no record? display warning to user
{
System.out.printf("No data to display!\n\n");
}
}
});
studentPanel.add(listButton);
JTextArea textDisplay = new JTextArea(10,48);
textDisplay.setEditable(true); // set textArea non-editable
JScrollPane scroll = new JScrollPane(textDisplay);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
studentPanel.add(scroll);
}
public static void main(String[] args)
{
StudDatabase mainFrame = new StudDatabase();
mainFrame.setVisible(true);
}
Your code isn't working for the same reason this wouldn't work:
int j = i+5;
int i = 4;
You have to declare variables before using them in Java.
Secondly, in order to use a variable (local or instance) from inside an inner class - which is what your ActionListener is - you need to make it final.
So, the below code will compile and run:
final JTextArea textDisplay = new JTextArea(10,48);
...
listButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
...
textDisplay.append("Student " + i);

Deleting selected checkboxes using java swing

I am new to java swing. I have a code that generates checkboxes. I want to have a button somewhere in my frame, which on clicking, should delete the selected checkbox entries. Here is what I have so far.
public class Scroll extends JPanel {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame frame = new JFrame("JFrame with ScrollBar");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new ResultButtonBar();
newContentPane.setOpaque(true);
JScrollPane scrollPane = new JScrollPane(newContentPane);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
frame.getContentPane().add(scrollPane);
frame.setSize(800, 800);
frame.setVisible(true);
JButton startButton = new JButton("Start");
frame.add(startButton, BorderLayout.SOUTH);
startButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "basdsadad");
}
});
}
}
and the new ResultButtonBar().java
public class ResultButtonBar extends JPanel {
private HashMap<JCheckBox, ArrayList<Integer>> map = new HashMap<>();
private JLabel _label;
private static final int MAX_CHECKS = 1000;
public ResultButtonBar() {
super();
JButton btn = new JButton();
btn.setVisible(true);
JCheckBox checkBox;
Random r = new Random();
JPanel checkPanel = new JPanel(new GridLayout(0, 1));
_label = new JLabel("You selected nothing");
checkPanel.add(_label);
for (int i = 0; i < MAX_CHECKS; i++) {
StringBuilder sb = new StringBuilder();
ArrayList<Integer> a = new ArrayList<>();
for (int j = 0; j < 2; j++) {
Integer temp = (r.nextInt()) % 100;
a.add(temp);
sb.append(temp).append(" ");
}
checkBox = new JCheckBox(sb.toString().trim());
checkBox.setName("CheckBox" + i);
map.put(checkBox, a);
checkPanel.add(checkBox);
}
add(checkPanel);
}
}
First of all, keep all your check boxes in an ArrayList so you will have a reference to them when you need it.
Then, add a JButton wherever you need. Then iterate over this ArrayList and call invalidate() on the component which contains your check boxes. Next statement would be to call the remove() method on the container; the checkPanel.
Alternatively, you may call removeAll() if all the components in the container are check boxes and you want to remove them.
The alternative pointed by StanislavL is also a good one if you have a lot of different components along with check boxes
I can think of two approaches:
if You are maintaining one JPanel instance which contains only the instances of JCheckBox, then you can first get all the checkbox's using panel.getComponents() method, check their selection state and depending on the state remove it by calling panel.remove(component). For example:
Component checkBox[] = checkBoxPanel.getComponents();
for(Component c:checkBox)
if(((JCheckBox)c).isSelected())
checkBoxPanel.remove(c);
checkBoxPanel.revalidate();
checkBoxPanel.repaint();
The last call revalidate() and repaint() on the checkBoxPanel is important for reflecting changes on the layout and graphics rendering of the components.
You can use ItemListener with the instances of JCheckBox to do things on selection state change. Use an instance of ArrayList<JCheckBox> to add the selected checkBox to the list. However you should use an implemented ItemListener: MyItemListener implements ItemListener and create one instance and add this instances to all the checkboxes to react on state change. You can use event source e.getSource() to get the JCheckBox instance on which the ItemEvent is performed.
Tutorial resource:
How to Write an Item Listener

Opening contents of a JList in new window

I'm very new to Java.
I've created two JLists in which you can add and remove 'shopping cart' items.
Once the user has added all their items they can click a submit button to view their selected items in a new window.
My first list is itemList (populated with items from array), the second list is shoppinglist which gets populated with whatever the user selects with a JButton.
Additional arrays are created to handle the actions of the buttons moving the items to and from the JLists. I've tried a few things, but haven't been successful in showing the items that get selected and shown in shopinglist to appear in a new window once submit is hit.
Any help is much appreciated.
//Create itemList
itemList = new JList(shopping);
contentPane.add(itemList);
itemList.setVisibleRowCount(10);
itemList.setFixedCellHeight(20);
itemList.setFixedCellWidth(140);
itemList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
//Add JScrollPane to maintain size
JScrollPane list1 = new JScrollPane(itemList);
//contentPane.add(list1);
//Create shoppingList
shoppingList = new JList(items);
contentPane.add(shoppingList);
shoppingList.setVisibleRowCount(10);
shoppingList.setFixedCellHeight(20);
shoppingList.setFixedCellWidth(140);
shoppingList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
//Add JScrollPane to maintain size
JScrollPane list2 = new JScrollPane(shoppingList);
JPanel buttonPanel = new JPanel();
//contentPane.add(list2);
Buttonin = new JButton(">>");
//Buttonin.setBounds(144, 46, 60, 23);
Buttonin.addActionListener(this);
buttonPanel.add(Buttonin);
ButtonOut = new JButton("<<");
//ButtonOut.setBounds(144, 80, 60, 23);
ButtonOut.addActionListener(this);
buttonPanel.add(ButtonOut);
JPanel submitPanel = new JPanel();
submitButton = new JButton("Submit");
submitPanel.add(submitButton);
submitButton.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent az) {
JFrame frame = new JFrame ("Go Shopping!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MyPanel2());
frame.pack();
frame.setVisible(true);
}
});
contentPane.add(list1);
contentPane.add(buttonPanel);
contentPane.add(list2);
contentPane.setOpaque(true);
contentPane.add(submitPanel);
return contentPane;
}
public void actionPerformed(ActionEvent e)
{
int i = 0;
//When in buttonin is pressed index and value of selected item is output to array
if (e.getSource() == Buttonin)
{
int[] fromindex = itemList.getSelectedIndices();
Object[] from = itemList.getSelectedValues();
//add items to the shoppingList
for (i = 0; i < from.length; i++)
{
items.addElement(from[i]);
}
//Must remove items that are selected from the itemList
for (i = (fromindex.length-1); i >= 0; i--)
{
shopping.remove(fromindex[i]);
}
}
//When out button is pressed index and value of selected item is output to new array
else if (e.getSource() == ButtonOut)
{
Object[] to = shoppingList.getSelectedValues();
int [] toindex = shoppingList.getSelectedIndices();
//add items to previous list
for(i = 0; i < to.length; i++)
{
shopping.addElement(to[i]);
}
//Must remove what's deselected
for (i = (toindex.length-1); i >= 0; i--)
{
items.remove(toindex[i]);
}
}
Ok, bear with me (very very new to java) is this how I would set up the constructor to reference to ProfileFrame objects? And if so how do I change my main to reflect the new constructor?
public class GoShopping extends JPanel {
private JList shopList;
public GoShopping(ProfileFrame slist) {
//construct components
shopList = new JList(slist.getListModel());
shopList.setBounds(6, 6, 123, 166);//don't worry I'm changing the layout
add(shopList);
}
public static void main (String[] args) {
JFrame frame = new JFrame ("MyPanel");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
//I need new GoShopping to reflect the new constructor, but not sure how to make it work
frame.getContentPane().add (new GoShopping());
frame.pack();
frame.setVisible (true);
}
}
You're creating a new ProfileFrame object in your MyPanel2 constructor, and since this is not the same instance as the visualized ProfileFrame object, then its JList's model will be null. A bad solution is to use static variables -- just don't do this please. A better solution is to pass a reference to the true visualized ProfileFrame object into your MyPanel2 constructor and call the public methods off of this instance.
And again, don't use null layout and setBounds(...), but instead use the layout managers unless you like making things more difficult than they need to be.

Categories

Resources