I am trying to move all the selected checkboxes from panel1 into panel2 and also removing the checkboxes from panel1 after the transfer. I made two arraylist to hold the checkboxes
ArrayList<JCheckBox> todo_box = new ArrayList<JCheckBox>();
ArrayList<JCheckBox> inprogress_box = new ArrayList<JCheckBox>();
and when the user clicks on the button I have this,
for(JCheckBox cb : todo_box)
{
if(cb.isSelected() )
{
inprogress_box.add(cb);
jPanel2.add(cb);
jPanel2.revalidate();
cb.setVisible(false);
todo_box.remove(cb);
jPanel1.remove(cb);
jPanel1.revalidate();
}
}
Not sure if I'm on the right track since I'm getting an exception error inside netbean.
Related
I have three JRadioButtons added to a JPanel. However, when I select one, I can select another one and the previous one selected stays selected. How can I make sure that only one is selected at a time?
My JPanel class, which is added by my main JFrame:
public class MainPanel extends JPanel {
private static JRadioButton addHouse, addRoad, calculateDist;
static {
addHouse = new JRadioButton("Add House");
addRoad = new JRadioButton("Add Road");
calculateDist = new JRadioButton("Calculate Distance");
}
MainPanel() {
setBackground(Color.WHITE);
addHouse.setBounds(50, 50, 100, 50);
addRoad.setBounds(50, 150, 100, 50);
addHouse.setBounds(50, 250, 100, 50);
addHouse.setBackground(Color.WHITE);
addRoad.setBackground(Color.WHITE);
calculateDist.setBackground(Color.WHITE);
add(addHouse);
add(addRoad);
add(calculateDist);
}
}
As you didn't share the complete code i can't tell how to do it in your case but in general
It can be done like that
ButtonGroup buttonGroup = new ButtonGroup() // create a button group , buttons in a button group knows how to behave together
JRadioButton radioButton1 = new JRadioButton("R1"); // create your buttons
JRadioButton radioButton2 = new JRadioButton("R2"); // as many you want
buttonGroup.add(radioButton1); // make them part of group
buttonGroup.add(radioButton2);
myJFrame.setLayout(new FlowLayout()); // or any layout you want
myJFrame.add(radioButton1); // add buttons to jframe
myJFrame.add(radioButton1);
when buttons were added to JFrame (or any other container) they were part of a group , so group handles the communications between them, now you can check only one at a time,
try commenting buttonGroup.add(); you will loose that behaveiour .
Same thing can be achieved manually , it that case you will track all other radio buttons
at every selection to check if others are already check and then uncheck them which is tedious so better use ButtonGroup class of swing
You can add the JRadioButtons in a ButtonGroup instance first. Here is a simple example:
// configure buttons bounds, background etc.
ButtonGroup buttonGroup= new ButtonGroup();
buttonGroup.add(addHouse);
buttonGroup.add(addRoad);
buttonGroup.add(calculateDist);
// add the buttons to the panel too.
I have an array that fills in by the user. Then each element of this array will be a CheckBox. For example if the array has 6 elements, it must create 6 checkboxes.
This is how I tried to loop through the array and create the checkbox, but it only overwrite on one checkbox.
public static void main(String[] args) {
JFrame frame = new JFrame("Options");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
ArrayList<String> myArrayList = new ArrayList<String>();
myArrayList.add("checkbox 1");
myArrayList.add("checkbox 2");
myArrayList.add("checkbox 3");
myArrayList.add("checkbox 4");
myArrayList.add("checkbox 5");
for(String element : myArrayList){
JCheckBox box = new JCheckBox(element);
frame.add(box);
}
frame.setVisible(true);
}
It is important that I have the access to each single checkbox later, so I can specify for example if checkbox2 is selected, do this.
So is there any way to make these checkboxes dynamically according to the ArrayList's size?
Every time you add something new to the JFrame, it removes the thing that was previously in it.
You'll need to create a JPanel or some other container to hold the JCheckBoxes, and then put that inside the JFrame.
Also, you can keep track of the checkboxes in a List.
For instance:
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(BoxLayout.Y_AXIS, panel));
List<JCheckBox> checkboxes = new ArrayList<>();
for(String element : myArrayList) {
JCheckBox box = new JCheckBox(element);
checkboxes.add(box);
panel.add(box);
}
frame.add(panel);
The main problem is, you're adding all the check boxes to the same location on the frame.
A JFrame uses a BorderLayout by default. A BorderLayout allows a single component to be managed in each of its five available slots. Basically a BorderLayout will ignore all but the last component added to any of the slots
Instead, try changing the LayoutManager to something more useful, like FlowLayout or GridBagLayout depending on your needs
Take a look at Laying Out Components Within a Container for more details.
Depending on your needs, I might be tempered to fill the ArrayList with the JCheckBoxes instead of String or even a Map of some kind, to make it easier to link the text with the JCheckBox
I am trying to add JPopupMenu (on right click) to an array of check boxes and am doing it in below way:
JPanel Pane = new JPanel();
Pane.setLayout(new BoxLayout(Pane, BoxLayout.PAGE_AXIS));
m_popMenu = new JPopupMenu();
JMenuItem item = new JMenuItem("Setup");
item.addActionListener(this);
m_popMenu.add(item);
for (int k = 0; k < 5; k++) {
checkBoxes[k] = new JCheckBox(List[k]); //in List i have names for CheckBoxes
checkBoxes[k].addActionListener(this);
//For popupmenu item
checkBoxes[k].addMouseListener(this);
Pane.add(checkBoxes[k]);
checkBoxes[k].add(m_popMenu);
}
This code does what I need exactly but GUI has some problems. If only two check boxes are there, check boxes gets aligned with more space and if I right click to open popup menu the alignment changes properly.
Whenever I open popup by right click (for the first time) the checkboxes reduce space between them.
Why does this happen?
I have 1 JFrame while 3 JPanel inside it. In JPanel 1, it contains some JTextArea (Linkedlist of TextArea). JPanel 2 contains JTree. JPanel 3 contains JLabel.
Each time i press enter key in JTextArea, JPanel 1 will add a new JTextArea. Each new Text Area add, JPanel 2 (JTree) will add new node. Every TextArea have unique name. If we focus to any text area, JPanel 3 will display name of that text area with JLabel.
My question is, how can i change other componenet outside of JPanel 1? Most of my code inside in JPanel 1. I've already done with adding new text area. I'm using JPanel just only for make my work easier to look.
I've try to search other post and other resource. But, i still dont get what i want.
I'm using AbstractAction method. Here my state now:
Action enter = new AbstractAction() {
//add new text area
};
I want be like this:
Action enter = new AbstractAction() {
//add new text area
//add node in JTree in JPanel 2
//change text of JLabel in JPanel 3
};
I hope this is clear for all of you
I am trying to iterate over a JList where each item contains:
JPanel - JLabel
Currently what i have is:
System.out.println("Reading all list items:");
System.out.println("-----------------------");
for (int i = 0; i < menuList.getModel().getSize(); i++) {
Object item = menuList.getModel().getElementAt(i);;
System.out.println("Item = " + item);
}
The output i get is:
Item =
javax.swing.JPanel[,0,0,0x0,invalid,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]
Instead i want to access the text that is inside the JPanel.
How could this be done?
Edit:
This is how i add my JPanel to the JList
menuList = new JList(v);
v = new Vector <String> ();
menuList.setListData(v);
.....
// get our images
Icon pingImage = new javax.swing.ImageIcon(getClass().getResource("/resources/icnNew.png"));
// add the images to jlabels with text
JLabel pingLabel = new JLabel("Hi there", pingImage, JLabel.LEFT);
// create the corresponding panels
JPanel pingPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
// add the labels onto the panels
pingPanel.add(pingLabel);
v.add(pingPanel);
So the text i want to find is "Hi there"
Then what you need is to check for the elements inside that JPanel. I mean, a panel is a container of UI elements, that said, you need to check for the elements, once you checked for that you need to compare whether it is a label or not, if it is a label then you will be able to get the text of that label.
Can't you show us the code, probably could be easier if you provide the snippet.