Adding a JPopupMenu to an array of JCheckBoxes - java

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?

Related

How to build an expanding vaadin window with scrollbars as needed

I am builing an expanding Window/layout that shall expand/shrink on supplied data, possibly expanding to fill the browser window.
Edit: The scroll bars shall be on the work-area, not the window.
So far i have only managed to get it working vertically with a simple list of labels (see simple_list.png).
Vertical scrollbar appear when browser window filled vertically Horizontal scrollbar always present and width of window/layout seems directed by length of panel title and window icons at top-right.
Edit: Scroll bars OK on Panel. No horizontal expansion.
It fails completeley for a GridLayout (see gridlayout.png).
Edit: I.E no scrollbars on work-area (Panel)
The final layout is supposed to have a central work area and header, footer, left/right areas. The work area to expand according to data.
Barked up the wrong tree? CSSLayout? Not for Vaadin?
Any help/guidance appreciated.
A simplified test case below (Vaadin 7, subclassing Window).
public TestWindow() {
super.setSizeUndefined();
VerticalLayout vL = new VerticalLayout();
vL.setSizeFull();
super.setContent(vL);
//vL.addComponent(buildHeader());
Panel workArea = new Panel();
workArea.setSizeFull();
workArea.setCaption("View");
vL.addComponent(workArea);
vL.setExpandRatio(workArea, 1f);
//vL.addComponent(buildFooter());
//-----------------------------
VerticalLayout view = new VerticalLayout();
view.setSizeUndefined();
workArea.setContent(view);
for (int i=0; i < 100; i++) {
String s = "view line "+i;
for (int j=0; j < 10; j++) {
s += "____________________"+j;
}
Label line = new Label(s);
line.setSizeUndefined();
view.addComponent(line);
}
//=============================
/*
VerticalLayout view = new VerticalLayout();
view.setSizeUndefined();
workArea.setContent(view);
GridLayout gl = new GridLayout();
gl.setSizeUndefined();
view.addComponent(gl);
view.setExpandRatio(gl, 1);
gl.setColumns(2);
for (int i=0; i < 100; i++) {
Label line = new Label("view line "+i);
line.setSizeUndefined();
gl.addComponent(line);
}
*/
//------------------------------
center();
setModal(true);
setDraggable(true);
UI.getCurrent().addWindow(this);
}
simple_list.png
gridlayout.png
Vaadin documentation says:
If a sub-window has a fixed or percentual size and its content becomes too big to fit in the content area, a scroll bar will appear for the particular direction. On the other hand, if the sub-window has undefined size in the direction, it will fit the size of the content and never get a scroll bar.
Source: https://vaadin.com/docs/v7/framework/layout/layout-sub-window.html

How to make a Jpanel show after clicking on picture

I'm Writing a game where there are a set of islands and there are 5 workers on each island when i left click on a worker a cursor appears on it and when i right click on somewhere in the island the selected worker moves there..i want a JPanel to show on the right side of the screen every time i left click on a worker.the panel will show the workers stats such as Health and carrying weight but the problem is i can't make the panel appear.this is the code where i left click on a worker:
if(SwingUtilities.isLeftMouseButton(e)){
if(canSelect){
for (int j = 0; j < countries.size(); j++) {
for (int i = 0; i < countries.elementAt(j).getMen().size(); i++) {
if(m==countries.elementAt(j).getMen().elementAt(i).getX() && n==countries.elementAt(j).getMen().elementAt(i).getY()) {
countries.elementAt(j).getMen().elementAt(i).setY(countries.elementAt(j).getMen().elementAt(i).getY()+1);
countries.elementAt(j).getMen().elementAt(i).setSelected(true);
getGraphics().drawImage(arrow.getImage(), (countries.elementAt(j).getMen().elementAt(i).getX()+mapX)*20+5,(countries.elementAt(j).getMen().elementAt(i).getY()+mapY)*20-12,null);
canSelect = false;
}
}
}
}
}
I tried the below code on a jbutton click within JFrame and it didn't worked as expected. (that is to dynamically creating a panel with 2 labels and show in a JFrame)
JPanel p = new JPanel();
p.setLayout(new GridLayout(2, 1));
p.add(new JLabel("Health : 90%"));
p.add(new JLabel("Carrying weight : 4 Kg"));
this.add(p); //JFrame
p.setSize(100, 50);
p.setLocation(100, 100);
p.setVisible(true);
But adding the code this.validate(); at the end of the above code, it worked fine.
if, is this your problem, then it may help you.
Reference Link
May be your JPanel is created by below your existing JPanel so it can't be seen. Place your new JPanel on your existing JPanel . Like this
JPanel nPanel = new JPanel();
nPanel.setBounds(x,y,w,h); //your desired location and size
mainPanel.add(nPanel);
nPanel.setVisible(false);
Your can make it visible again with your event

Move icon from jbutton to jbutton

I setup a gridlayout, with 16 buttons in the center. I placed an icon on the first button.
How would I loop through, and when the user select the next button on the grid, it moves the icon from old position to new position?
private ArrayList<JButton> grid = new ArrayList<JButton>();
JPanel gridBtnPanel = new JPanel();
gridBtnPanel.setLayout(new GridLayout(4, 4));
for(int i = 0; i <= 16; i++){
JButton innerButton = new JButton();
gridBtnPanel.add(innerButton);
grid.add(innerButton);
}
ImageIcon player= new ImageIcon("player.JPG");
//starting position
grid.get(0).setIcon(player);
//wanting to move to next button when I select the near by button
for(int i = 0; i < grid.lastIndexOf(theifPerson); i++){
grid.get(i).setIcon(null);
}
Any help would be great.
Thank you.
You could add actionlisteners to the buttons, and once a button is pressed it searches through all the buttons to find one with a non-null icon, and switches the pressed button's icon with the non-null icon
Presumably you have some kind of ActionListener attached to each JButton so you know when the user clicks on one, if you don't, take a look at How to Use Buttons, Check Boxes, and Radio Buttons and How to Write an Action Listener
When the user clicks on a button, the actionPerformed method is called. Here you want to determine which button was clicked, set the icon property of the last button to null and set the icon of the clicked button...
This will require you to know the last "active" button
private int activeButton;
private ImageIcon player;
//...
grid.get(0).setIcon(player);
activeButton = 0;
Then you simply want to update the current state...
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if (source instanceof JButton) {
JButton clicked = (JButton)source;
grid.get(activeButton).setIcon(null);
clicked.setIcon(player);
activeButton = grid.indexOf(clicked);
}
}
For example...

Java iterate through JList which contains JPanel - JLabel

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.

Java JFrame refuses to refresh

I am making a monopoly game in Java and for some reason I cant seem to get this JFrame popup to refresh. When I click the button, the popup will appear, but when I close it and click it again later on it the game, it continues to show the features of the 1st JFrame. I assume that a totally new JFrame is created every time you click the button, but I cant seem to refresh the content. How do I fix this?
final static JButton tradeButton = new JButton("Click to Trade Property");
tradeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
final JPanel tradePanelcombo = new JPanel();
for(int x = 1; x< Player.Props.size(); x++)
{
if(x != Player.CurrentPlayer)
{
ArrayList<Property> props = Player.Props.get(x);
ArrayList<String> propnames = new ArrayList<String>();
for(Property prop : props)
{
propnames.add(prop.getName());
}
String[] strings = new String[propnames.size()];
for (int y = 0; y<propnames.size(); y++)
{
strings[y] = propnames.get(y);
}
JLabel confirmLabel = new JLabel("Player :" + x);
JComboBox petList = new JComboBox(strings);
petList.setPreferredSize(new Dimension(100, 150));
tradeFrame.add(confirmLabel);
tradePanelcombo.add(petList);
}
}
tradeFrame.add(tradePanelcombo, BorderLayout.PAGE_END);
tradeFrame.setPreferredSize(new Dimension(500,500));
tradeFrame.setResizable(true);
tradeFrame.pack();
tradeFrame.setVisible(true);
}
});
It appears to me that a new frame and components are created every time.
So, if the frame doesn't update, then I guess it would be because your Properties are not updated so the data added to the components doesn't change.
Edit:
You need to create a new JFrame every time, or you need to remove all the old components from the frame before adding the new components.
When you add a component to a BorderLayout, the previous component doesn't get removed. So you actually have two components in the container. When Swing paints components it actually paints the components in the reverse order that they where added to the container (this is how Z-Ordering works).
If you remove the setResizable() method, and then resize the frame I believe you should see the new components start to show given that they should increase in size as the frame size increases.

Categories

Resources