I am developing the Exam Registration System with MVC in java.I can add comboBox into big JPanel.However,when I push the JComboBox button , there is no element in the combobox.I can not solve this. ComboBox addition to my view is here ;
JPanel panel = new JPanel();
panel.setBounds(425, 494, 117, 39);
adminPanel.add(panel);
panel.setLayout(null);
panel.setVisible(true);
comboBox = new JComboBox();
comboBox.setBounds(0, 0, 117, 39);
panel.add(comboBox);
and ı try to add exams in here ;
admin_view.getComboBox().setVisible(true);
String s = "Exam1";
admin_view.getComboBox().addItem(s);
How can I show the elements into JComboBox. No action , no other things I just want to show elements.
admin_view.getComboBox().setVisible(true);
String s = "Exam1";
admin_view.getComboBox().addItem(s);
The problem is; you try to add an element to an already visible object. So if you add an element, you have to refresh the component/panel to see updated version of that component/panel. And if the elements don't have to be added dynamically, just add those elements before you make that combobox visible.
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'm creating a list of bank movements with the following code. pane is a JPanel and array is an ArrayList that contains respectively amount and description data. Setting is a little icon that allow you to modify each movement.
MouseClass is a class that extends MouseAdapter that I've created to add "k" index to mouseClicked method. I'm new with java gui programming. I'd like to know if there is a quick method to add a scroll to my panel
JLabel[] movement = new JLabel[array.size()];
JLabel[] description = new JLabel[array.size()];
JLabel[] data = new JLabel[array.size()];
JLabel[] setting = new JLabel[array.size()];
System.out.println(array.size());
int i = 0;
for(int k=0; k<array.size(); k++){
movement[k] = new JLabel("");
movement[k].setForeground(SystemColor.text);
movement[k].setFont(new Font("Segoe UI", Font.PLAIN, 20));
movement[k].setBounds(17, i, 145, 30);
movement[k].setText(array.get(array.size() - k - 1).getAmount() + "€");
panel.add(movement[k]);
description[k] = new JLabel("");
description[k].setForeground(SystemColor.text);
description[k].setFont(new Font("Segoe UI", Font.PLAIN, 20));
description[k].setBounds(187, i, 274, 30);
description[k].setText(array.get(array.size() - k - 1).getDescription());
panel.add(description[k]);
data[k] = new JLabel("");
data[k].setForeground(SystemColor.text);
data[k].setFont(new Font("Segoe UI", Font.PLAIN, 20));
data[k].setBounds(478, i, 145, 30);
data[k].setText(array.get(array.size() - k - 1).getDate());
panel.add(data[k]);
setting[k] = new JLabel();
setting[k].setIcon(new ImageIcon(List.class.getResource("/it/andreavaiuso/financemanager/images/edit.png")));
setting[k].setForeground(SystemColor.text);
setting[k].setFont(new Font("Segoe UI", Font.PLAIN, 20));
setting[k].addMouseListener(new MouseClass(array.size() - k - 1) {
#Override
public void mouseClicked(MouseEvent arg0) {
Modify mdf = new Modify(this.index);
mdf.setVisible(true);
dispose();
}
});
setting[k].setBounds(640, i, 82, 30);
panel.add(setting[k]);
i += 40;
}
But I don't know how to scroll it. I've tried woth JScrollPane but don't work!
I'm sure there is a simplest way to add these items to my panel...
I've tried woth JScrollPane but don't work!
Well I see lots of code with setBounds(...) which implies you are using a null layout.
Don't use a null layout. Swing was designed to be used with layout managers. In fact the scroll pane will only work when used with a layout manager because the scroll pane needs to know the preferred size of the panel so it can determine when you use scrollbars.
I would also suggest you should also be using a JTable for something like this. It is more efficient because you don't need to create individual components for each row of data. Read the section from the Swing tutorial on How to Use Tables for more information and examples.
For a JScrollPane you need, 1 JScrollPane, 1 JList and one DefaultListModel.
First you add your items to DefaultListModel, then you add the model to the JList and then you make a JSCrollPane with passing as argument your JList
Example:
DefaultListModel model = new DefaultListModel<String>();
JList list = new JList<String>();
model.addElemenet("1");
model.addElemenet("3");
model.addElemenet("2");
list.setModel(model);
JScrollPane scrollPane = new JScrollPane(list);
I have an issue in my Java application where putting a JList inside a JScrollPane seems to disrupt the first drawing of the list items.
I have a list being populated with items
public void populateFormulaList(Iterator<String> iter)
{
list.setModel(new DefaultListModel<String>());
DefaultListModel<String> listModel = (DefaultListModel<String>) list.getModel();
while (iter.hasNext())
{
listModel.addElement(iter.next());
}
}
And the component initialization looks like this
list = new JList<String>();
list.setFont(new Font("Tahoma", Font.PLAIN, 14));
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listPanel = new JScrollPane(list);
listPanel.setBounds(0, 40, 375, 550);
add(listPanel);
My problem is that when the application first runs, the list is populated with items, which I've debugged within eclipse and know the items exist within the JLists model, but if the JList is in the JScrollPane the items do not show the first time around. If I force the items to be repopulated the list shows all the items as expected.
I've tried using repaint() and revalidate() on the scrollpane, JList and their containing frame.
Any help would be greatly appreciated.
I used windowbuilder to create a GUI that should be a contact list. There are two lists: one of contacts, the other one of numbers. when you select a contact his number will appear.
But, when I add a lot of contacts, the scroll bar does not automatically appear, as it does to my friend, so I tried adding a JScrollPane. Still does not work.
Here is what it was:
DefaultListModel contact = new DefaultListModel();
JList contacts = new JList(contact);
contacts.setBounds(22, 64, 186, 135);
contentPane.add(contacts);
here is what I tried:
DefaultListModel contact = new DefaultListModel();
JList contacts = new JList(contact);
contacts.setBounds(22, 64, 186, 135);
JScrollPane scrollPane1 = new JScrollPane(contacts);
contentPane.add(contacts);
contentPane.add(scrollPane1, BorderLayout.WEST);
then I tried addind Bounds to the scrollPanel as well:
DefaultListModel contact = new DefaultListModel();
JList contacts = new JList(contact);
contacts.setBounds(22, 64, 186, 135);
JScrollPane scrollPane1 = new JScrollPane(contacts);
scrollPane1.setBounds(22, 64, 186, 135);
contentPane.add(contacts);
contentPane.add(scrollPane1, BorderLayout.WEST);
when I erased 'contacts.setBounds(22, 64, 186, 135);', the list stopped working.
I also tried not having the last line, I tried only having the last line and not having the second last line, I tried adding "scrollPane1.setViewportView(contacts);", I tried taking away the "BorderLayout.WEST" argument for all the other tries, so I basically tried every combination of "solutions" I could find on the internet
I have no idea what is going on and I have no idea how to fix it. All I want is to be able to see all my contacts by scrolling.
EDIT, SOLVED:
The problem was my panel was declared as contentPane.setLayout(null); therefore "BorderLayout.WEST" was unnecessary. In addition, the bounds should be applied to the scrollpane, not the list. Last, I should not have used add(contacts) to the panel. the solution was:
DefaultListModel contact = new DefaultListModel();
JList contacts = new JList(contact);
JScrollPane scrollPaneContact = new JScrollPane(contacts);
scrollPaneContact.setBounds(22, 64, 186, 135);
contentPane.add(scrollPaneContact);
I'm updating in case anyone have a similar problem.
You need to set the policies for JScrollPane using proper constructor.
JScrollPane(Component view, int vsbPolicy, int hsbPolicy)
Creates a JScrollPane that displays the view component in a viewport whose view position can be controlled with a pair of scrollbars.
For instance:
new JScrollPane(COMPONENT, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
EDIT:
DefaultListModel model = new DefaultListModel();
JList contactsList = new JList(model);
JScrollPane scrollPane1 = new JScrollPane(contactsList, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
JScrollPane scrollPane1 = new JScrollPane(contacts);
//contentPane.add(contacts); // get rid of this
contentPane.add(scrollPane1, BorderLayout.WEST);
First you add the contacts JList to the viewport of the scroll pane which is correct.
But then you add the contacts to the frame directly, which is wrong. A component can only have a single parent. By adding the contact JList to the frame you remove it from the scroll pane.
Also, don't use a null layout and don't use setBounds(...). Swing was designed to be used with Layout Managers.
If you need more help then post a proper SSCCE that demonstrates the problem. A SSCCE should be included with every question.
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.