Repaint Isn't doing anything - java

I am trying to make a rectangle change color with GUI sliders. I know this can be done by changing the background but I'm trying to use repaint.
I know that repaint holds the requests and executes at will almost. I'm trying to find a workaround because I'm stuck.
I've read up on repaint(); and repaintManager and tried manipulating my code but I'm still unable to get my desired output.
Any help would be appreciated.
import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
*
*
*/
public class MyColorChooser2 extends JPanel {
private JFrame frame;
private JLabel redLabel, greenLabel, blueLabel;
private JSlider redSlider, greenSlider, blueSlider;
private JTextField redTextField, greenTextField, blueTextField;
private JPanel redPanel, greenPanel, bluePanel, colorPanel, paintPanel;
private int holdNbr1, holdNbr2, holdNbr3;
DrawPanel rect = new DrawPanel();
public MyColorChooser2() {
JFrame frame = new JFrame();
frame.setTitle("Color Chooser");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set up labels
redLabel = new JLabel("Red:");
greenLabel = new JLabel("Green:");
blueLabel = new JLabel("Blue:");
// set up sliders and register their event handler:
redSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 50);
redSlider.setMajorTickSpacing(10); // create tick every 10
redSlider.setPaintTicks(true); // paint ticks on slider
greenSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 100);
greenSlider.setMajorTickSpacing(10); // create tick every 10
greenSlider.setPaintTicks(true); // paint ticks on slider
blueSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 200);
blueSlider.setMajorTickSpacing(10); // create tick every 10
blueSlider.setPaintTicks(true); // paint ticks on slider
//slider event handling:
SliderHandler sliderHandler = new SliderHandler();
redSlider.addChangeListener(sliderHandler);
greenSlider.addChangeListener(sliderHandler);
blueSlider.addChangeListener(sliderHandler);
//set up textFields and register their event handler:
redTextField = new JTextField(3);
redTextField.setText("50"); //initialize
redTextField.setEditable(false);
redTextField.setText("" + redSlider.getValue());
greenTextField = new JTextField(3);
greenTextField.setText("100"); //initialize
greenTextField.setEditable(false);
greenTextField.setText("" + greenSlider.getValue());
blueTextField = new JTextField(3);
blueTextField.setText("200"); //initialize
blueTextField.setEditable(false);
blueTextField.setText("" + blueSlider.getValue());
// build colorPanel:
// build redPanel:
redPanel = new JPanel();
redPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
redPanel.add(redLabel);
redPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
redPanel.add(redSlider);
redPanel.add(redTextField);
// build greenPanel:
greenPanel = new JPanel();
greenPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
greenPanel.add(greenLabel);
greenPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
greenPanel.add(greenSlider);
greenPanel.add(greenTextField);
// build bluePanel:
bluePanel = new JPanel();
bluePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
bluePanel.add(blueLabel);
bluePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
bluePanel.add(blueSlider);
bluePanel.add(blueTextField);
colorPanel = new JPanel();
colorPanel.add(redPanel);
colorPanel.add(greenPanel);
colorPanel.add(bluePanel);
frame.setLayout(new BorderLayout());
frame.add(rect, BorderLayout.CENTER);
frame.add(colorPanel, BorderLayout.SOUTH);
frame.setPreferredSize(new Dimension(900, 300));
frame.setVisible(true);
frame.pack();
}
public class DrawPanel extends JPanel {
private Color color;
private int red = 50, blue = 100, green = 200;
private Graphics g;
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
changeColor();
g.setColor(color);
g.fillRect(10, 10, 880, 200);
g.dispose();
}
public void DrawPanel(Color c) {
color = c;
red = color.getRed();
green = color.getGreen();
blue = color.getBlue();
}
void changeColor() {
color = new Color(red, green, blue);
this.color = color;
}
public void setRed(int r) {
red = r;
changeColor();
}
public void setBlue(int b) {
blue = b;
changeColor();
}
public void setGreen(int g) {
green = g;
changeColor();
}
}
private class SliderHandler implements ChangeListener {
#Override
public void stateChanged(ChangeEvent e) {
JSlider change = (JSlider) e.getSource();
DrawPanel draw = new DrawPanel();
int value;
if (change == redSlider) {
value = change.getValue();
redTextField.setText(String.valueOf(value));
draw.setRed(value);
} else if (change == greenSlider) {
value = change.getValue();
greenTextField.setText(String.valueOf(value));
draw.setGreen(value);
} else if (change == blueSlider) {
value = change.getValue();
blueTextField.setText(String.valueOf(value));
draw.setBlue(value);
}
draw.changeColor();
draw.repaint();
}
}
public static void main(String args[]) {
MyColorChooser2 color = new MyColorChooser2();
}
}

It's not that it isn't doing anything. The problem is that you're stuck, because every time you make a change you're trying to update a different component. You're recreating multiple panels in your state changed.
I've added comments about the changes I've done to make it work properly. If you need more help, just ask. Cheers!
Would you care trying this code:
public class MyColorChooser2 extends JPanel {
private JFrame frame;
private DrawPanel drawPanel;
private JLabel redLabel, greenLabel, blueLabel;
private JSlider redSlider, greenSlider, blueSlider;
private JTextField redTextField, greenTextField, blueTextField;
private JPanel redPanel, greenPanel, bluePanel, colorPanel, paintPanel;
private int holdNbr1, holdNbr2, holdNbr3;
private Color initialColor = Color.RED;
public MyColorChooser2() {
JFrame frame = new JFrame();
frame.setTitle("Color Chooser");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Let DrawPanel choose the initial color. I don't care.
drawPanel = new DrawPanel();
// This way I control what is the initial color
//drawPanel = new DrawPanel(initialColor);
// set up labels
redLabel = new JLabel("Red:");
greenLabel = new JLabel("Green:");
blueLabel = new JLabel("Blue:");
// set up sliders and register their event handler:
redSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 255, initialColor.getRed());
redSlider.setMajorTickSpacing(10); // create tick every 10
redSlider.setPaintTicks(true); // paint ticks on slider
greenSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 255, initialColor.getGreen());
greenSlider.setMajorTickSpacing(10); // create tick every 10
greenSlider.setPaintTicks(true); // paint ticks on slider
blueSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 255, initialColor.getBlue());
blueSlider.setMajorTickSpacing(10); // create tick every 10
blueSlider.setPaintTicks(true); // paint ticks on slider
// slider event handling:
SliderHandler sliderHandler = new SliderHandler();
redSlider.addChangeListener(sliderHandler);
greenSlider.addChangeListener(sliderHandler);
blueSlider.addChangeListener(sliderHandler);
// set up textFields and register their event handler:
redTextField = new JTextField(3);
redTextField.setText("50"); // initialize
redTextField.setEditable(false);
redTextField.setText("" + redSlider.getValue());
greenTextField = new JTextField(3);
greenTextField.setText("100"); // initialize
greenTextField.setEditable(false);
greenTextField.setText("" + greenSlider.getValue());
blueTextField = new JTextField(3);
blueTextField.setText("200"); // initialize
blueTextField.setEditable(false);
blueTextField.setText("" + blueSlider.getValue());
// build colorPanel:
// build redPanel:
redPanel = new JPanel();
redPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
redPanel.add(redLabel);
redPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
redPanel.add(redSlider);
redPanel.add(redTextField);
// build greenPanel:
greenPanel = new JPanel();
greenPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
greenPanel.add(greenLabel);
greenPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
greenPanel.add(greenSlider);
greenPanel.add(greenTextField);
// build bluePanel:
bluePanel = new JPanel();
bluePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
bluePanel.add(blueLabel);
bluePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
bluePanel.add(blueSlider);
bluePanel.add(blueTextField);
colorPanel = new JPanel();
colorPanel.add(redPanel);
colorPanel.add(greenPanel);
colorPanel.add(bluePanel);
frame.setLayout(new BorderLayout());
frame.add(drawPanel, BorderLayout.CENTER);
frame.add(colorPanel, BorderLayout.SOUTH);
frame.setPreferredSize(new Dimension(900, 300));
frame.setVisible(true);
frame.pack();
}
private class DrawPanel extends JPanel {
private Color color;
private int red = 255, blue = 0, green = 0;
#Override
protected void paintComponent(Graphics g) {
// I've removed the call to changeColor because it was creating
// an infinite loop of revalidates and repaints.
// Now, the paintComponent just finishes the job it was required to.
super.paintComponent(g);
g.setColor(color);
g.fillRect(10, 10, 880, 200);
}
public DrawPanel() {
this(Color.BLUE);
}
public DrawPanel(Color c) {
color = c;
red = color.getRed();
green = color.getGreen();
blue = color.getBlue();
// I'm calling this first repaint here just to make
// sure the panel is initiated the way we want with
// the given Color
repaint();
}
void changeColor() {
color = new Color(red, green, blue);
// We just need to change the color and call this repaint here
// so that the paintComponent can do its job
repaint();
}
public void setRed(int r) {
red = r;
changeColor();
}
public void setBlue(int b) {
blue = b;
changeColor();
}
public void setGreen(int g) {
green = g;
changeColor();
}
}
private class SliderHandler implements ChangeListener {
#Override
public void stateChanged(ChangeEvent e) {
JSlider change = (JSlider) e.getSource();
int value;
// I've removed this line of code because you were
// recreating the drawingPanel. That's not what you want.
// You want to reuse the same panel.
// DrawPanel draw = new DrawPanel();
if (change == redSlider) {
value = change.getValue();
redTextField.setText(String.valueOf(value));
drawPanel.setRed(value);
} else if (change == greenSlider) {
value = change.getValue();
greenTextField.setText(String.valueOf(value));
drawPanel.setGreen(value);
} else if (change == blueSlider) {
value = change.getValue();
blueTextField.setText(String.valueOf(value));
drawPanel.setBlue(value);
}
// You don't need to call those methods, because the
// changeColor will be called by the settings
// of Red, Green and Blue
// draw.changeColor();
// draw.repaint();
}
}
public static void main(String args[]) {
MyColorChooser2 color = new MyColorChooser2();
}
}

Related

How to change background color using JComboBox

So, basically, the main idea for this project is that I wanted to change the background color by using the jcombobox method. and the background will change with the option chosen when the button(proceed) is clicked. The background color chosen will then be implemented to the next page when the button is clicked. The background that im talking about is the background for all the JPanel(including the other page's JPanel). If (getsource() == f2.btenter2) , it will go to f2(2nd file) btenter2(button)'s when clicked.
public class MainPage extends JFrame implements ActionListener {
JLabel lbTitle, lbWelcome, lbSelect;
JComboBox cbColor;
public Color color[] = {RED, GREEN, BLUE, YELLOW, GRAY};
public String clr[] ={"RED","GREEN","BLUE","YELLOW","GRAY"};
public int index;
JButton btProceed;
public static void main(String[] args) {
MainPage f1 = new MainPage();
f1.setSize(800, 500);
f1.setTitle("Java Assignment");
f1.setVisible(true);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public MainPage() {
lbTitle = new JLabel("Rock-Paper-Scissors-Lizard-Spock");
lbWelcome = new JLabel("Welcome!");
lbSelect = new JLabel("Please select the background color before you proceed:");
lbWelcome.setFont(new Font("Courier", Font.PLAIN, 18));lbSelect.setFont(new Font("Courier", Font.PLAIN, 15)); cbColor = new JComboBox(color);btProceed = new JButton("Proceed");
//1st panel on the north side to set the title
JPanel p1 = new JPanel();
p1.add(lbTitle);
p1.setBackground(new Color(255, 106, 0));
//2nd panel on the center position
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(14, 1));JPanel a1 = new JPanel();
JPanel a2 = new JPanel();
JPanel a3 = new JPanel();
JPanel e1 = new JPanel();
a1.add(lbWelcome);
a2.add(lbSelect);
a3.add(cbColor);
p2.add(e1);
p2.add(a1);
p2.add(a2);
p2.add(a3);
JPanel p3 = new JPanel();
p3.add(btProceed);
setLayout(new BorderLayout());
add(p1, BorderLayout.NORTH);
add(p2, BorderLayout.CENTER);
add(p3, BorderLayout.SOUTH);
btProceed.addActionListener(this);
cbColor.addActionListener(this);
color().addActionListener(this);
}
#Override
public void actionPerformed(ActionEvent e) {
index = cbColor.getSelectedIndex(); //to get the index
if (e.getSource()==cbColor) {
index = cbColor.getSelectedIndex();
p2.setBackground(color[index]);
a1.setBackground(color[index]);
a2.setBackground(color[index]);
a3.setBackground(color[index]);
e1.setBackground(color[index]);
e1.setBackground(color[index]);
p3.setBackground(color[index]);
}
Team f2 = new Team();
GamePage f3 = new GamePage();
GamePage2 f4 = new GamePage2();
Result f5 = new Result();
if (e.getSource()==btProceed) {
this.setVisible(false);
f2.setVisible(true);
f2.setSize(800, 500);
f2.p2.setBackground(color[index]);
f2.p3.setBackground(color[index]);
f2.p4.setBackground(color[index]);
// btenter2 is located in the second file under the same package
if (e.getSource()==f2.btenter2){
this.setVisible(false);
f3.setVisible(true);
f3.setSize(800,500);
f3.p2.setBackground(color[index]);
f3.p3.setBackground(color[index]);
f3.p4.setBackground(color[index]);
f3.p5.setBackground(color[index]);}
if (e.getSource()==f3.btNext){this.setVisible(false);
f4.setVisible(true);
f4.setSize(800,500);
f4.p2.setBackground(color[index]);
f4.p3.setBackground(color[index]);
f4.p4.setBackground(color[index]);
f4.p5.setBackground(color[index]); }
if (e.getSource() == f4.btResult)
{
this.setVisible(false);
f5.setVisible(true);
f5.setSize(800,500);
f5.p2.setBackground(color[index]);
f5.p3.setBackground(color[index]);
f5.p4.setBackground(color[index]);
f5.p5.setBackground(color[index]);}}
Introduction
I started working on this before I saw the OP's latest edit.
Here's the GUI I came up with.
Here's the GUI when you pick red.
Here's the GUI when you pick yellow.
The red GUI uses white text for better contrast. Some background colors work better with black text.
I didn't create an ActionListener for the button. I wanted to demonstrate two things.
A JComboBox can hold any class, not just the String class.
An ActionListener that changes the background and foreground color of a JPanel.
Explanation
When creating a Swing GUI, or almost any Java application, I use the model / view / controller pattern. This pattern allows me to focus on one part of the application at a time and helps me separate my concerns.
So, the first thing I did was create a ColorPair class. The ColorPair class holds a background color, a foreground color, and a String name of the background color. When passing class instances to a JComboBox, the class toString method is used to display the combo box options.
Next, I created a Pallete class. The Pallete class holds a List of ColorPair instances.
Once I created the model, I started on the view. I reorganized the OP's view code so it was easier to read. Generally, I keep all the methods for each Swing component together, and create the Swing components in the order they appear on the JPanel.
I separated the JFrame and JPanel creation into separate methods.
After I was satisfied with the view, I wrote the ActionListener for the JComboBox. I didn't need to pass all the components to the ActionListener class since I could get the children components of the JPanel.
The propagateColors method of the ActionListener is recursive. This is one of those rare instances where a recursive method makes sense in the real world since you can have JPanels as children of a JPanel.
Here's the complete runnable code. I hope it's helpful.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class BackgroundColorGUI implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new BackgroundColorGUI());
}
private ColorPair colorPair;
private JPanel mainPanel;
private JPanel buttonPanel;
private Pallete pallete;
public BackgroundColorGUI() {
this.pallete = new Pallete();
}
#Override
public void run() {
JFrame frame = new JFrame("Java Assignment");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createTitlePanel(), BorderLayout.BEFORE_FIRST_LINE);
this.mainPanel = createMainPanel();
frame.add(mainPanel, BorderLayout.CENTER);
this.buttonPanel = createButtonPanel();
frame.add(buttonPanel, BorderLayout.AFTER_LAST_LINE);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createTitlePanel() {
//1st panel on the north side to set the title
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JLabel lbTitle = new JLabel("Rock-Paper-Scissors-Lizard-Spock");
lbTitle.setFont(panel.getFont().deriveFont(Font.BOLD, 24f));
panel.add(lbTitle);
panel.setBackground(new Color(255, 106, 0));
return panel;
}
private JPanel createMainPanel() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
panel.setLayout(new GridLayout(0, 1, 5, 5));
JPanel e1 = new JPanel();
JLabel dummy = new JLabel(" ");
e1.add(dummy);
panel.add(e1);
JPanel a1 = new JPanel();
JLabel lbWelcome = new JLabel("Welcome!");
lbWelcome.setFont(new Font("Courier", Font.PLAIN, 18));
a1.add(lbWelcome);
panel.add(a1);
JPanel a2 = new JPanel();
JLabel lbSelect = new JLabel("Please select the background "
+ "color before you proceed:");
lbSelect.setFont(new Font("Courier", Font.PLAIN, 15));
a2.add(lbSelect);
panel.add(a2);
JPanel a3 = new JPanel();
JComboBox<ColorPair> cbColor = new JComboBox<ColorPair>();
List<ColorPair> colors = pallete.getColors();
for (ColorPair colorPair : colors) {
cbColor.addItem(colorPair);
}
cbColor.addActionListener(new ColorListener(this));
a3.add(cbColor);
panel.add(a3);
return panel;
}
private JPanel createButtonPanel() {
JPanel panel = new JPanel();
JButton btProceed = new JButton("Proceed");
panel.add(btProceed);
return panel;
}
public JPanel getMainPanel() {
return mainPanel;
}
public JPanel getButtonPanel() {
return buttonPanel;
}
public class ColorListener implements ActionListener {
private BackgroundColorGUI frame;
public ColorListener(BackgroundColorGUI frame) {
this.frame = frame;
}
#Override
public void actionPerformed(ActionEvent event) {
JComboBox<?> comboBox = (JComboBox<?>) event.getSource();
colorPair = (ColorPair) comboBox.getSelectedItem();
propagateColors(frame.getMainPanel(), colorPair);
propagateColors(frame.getButtonPanel(), colorPair);
}
private void propagateColors(JPanel panel, ColorPair colorPair) {
panel.setBackground(colorPair.getBackgroundColor());
panel.setForeground(colorPair.getForegroundColor());
Component[] components = panel.getComponents();
for (Component component : components) {
if (component instanceof JPanel) {
JPanel childPanel = (JPanel) component;
propagateColors(childPanel, colorPair);
}
component.setBackground(colorPair.getBackgroundColor());
component.setForeground(colorPair.getForegroundColor());
}
}
}
public class Pallete {
private final List<ColorPair> colors;
public Pallete() {
this.colors = generatePalleteFactory();
}
private List<ColorPair> generatePalleteFactory() {
List<ColorPair> colors = new ArrayList<>();
colors.add(new ColorPair("Light Grey",
new Color(238, 239, 238), Color.BLACK));
colors.add(new ColorPair("Red", Color.RED, Color.WHITE));
colors.add(new ColorPair("Green", Color.GREEN, Color.BLACK));
colors.add(new ColorPair("Blue", Color.BLUE, Color.WHITE));
colors.add(new ColorPair("Yellow", Color.YELLOW, Color.BLACK));
colors.add(new ColorPair("Grey",
new Color(153, 153, 153), Color.WHITE));
return colors;
}
public List<ColorPair> getColors() {
return colors;
}
}
public class ColorPair {
private final Color backgroundColor;
private final Color foregroundColor;
private final String backgroundColorName;
public ColorPair(String backgroundColorName,
Color backgroundColor, Color foregroundColor) {
this.backgroundColorName = backgroundColorName;
this.backgroundColor = backgroundColor;
this.foregroundColor = foregroundColor;
}
public Color getBackgroundColor() {
return backgroundColor;
}
public Color getForegroundColor() {
return foregroundColor;
}
public String getBackgroundColorName() {
return backgroundColorName;
}
#Override
public String toString() {
return getBackgroundColorName();
}
}
}
The background assigned by the renderer is overriden by the selection background color of the JList that is used in the popup for the combo box.
JComboBox cbColor = new JComboBox(...);
Object child = cbColor.getAccessibleContext().getAccessibleChild(0);
BasicComboPopup popup = (BasicComboPopup)child;
JList list = popup.getList();
list.setSelectionBackground(Color.RED);
Credits to this answer

Drawing Circles on random JPanels by Pressing a JButton

I got a grid with JPanels, by pressing the Button on top I want a random generator to draw circles on 3 random Panels.
In theory I think I have to overwrite the PaintComponent of every JPanel with a circle, put the flag on false and when I press the button an action listener puts the flag of 3 random JPanels on true.
But I really have no idea how to do this. Is it possible to do it this way ? if yes, could you show me how, if no, could you tell me what else i have to do ? Thanks a lot. Here's my code so far:
package feld;
import javax.swing.*;
import java.awt.*;
public class Spielplan {
public static void main(String[] args) {
JFrame f1 = new JFrame();
f1.setLayout(new BorderLayout());
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setPreferredSize(new Dimension(800, 800));
JButton tokens = new JButton("Spielsteine setzen");
f1.add(tokens, BorderLayout.NORTH);
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(3,3));
f1.add(p1, BorderLayout.CENTER);
JPanel g1 = new JPanel();
g1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
g1.setPreferredSize(new Dimension(100, 100));
g1.setVisible(true);
p1.add(g1);
JPanel g2 = new JPanel();
g2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
g2.setPreferredSize(new Dimension(100, 100));
g2.setVisible(true);
p1.add(g2);
JPanel g3 = new JPanel();
g3.setBorder(BorderFactory.createLineBorder(Color.BLACK));
g3.setPreferredSize(new Dimension(100, 100));
g3.setVisible(true);
p1.add(g3);
JPanel g4 = new JPanel();
g4.setBorder(BorderFactory.createLineBorder(Color.BLACK));
g4.setPreferredSize(new Dimension(100, 100));
g4.setVisible(true);
p1.add(g4);
JPanel g5 = new JPanel();
g5.setBorder(BorderFactory.createLineBorder(Color.BLACK));
g5.setPreferredSize(new Dimension(100, 100));
g5.setVisible(true);
p1.add(g5);
JPanel g6 = new JPanel();
g6.setBorder(BorderFactory.createLineBorder(Color.BLACK));
g6.setPreferredSize(new Dimension(100, 100));
g6.setVisible(true);
p1.add(g6);
JPanel g7 = new JPanel();
g7.setBorder(BorderFactory.createLineBorder(Color.BLACK));
g7.setPreferredSize(new Dimension(100, 100));
g7.setVisible(true);
p1.add(g7);
JPanel g8 = new JPanel();
g8.setBorder(BorderFactory.createLineBorder(Color.BLACK));
g8.setPreferredSize(new Dimension(100, 100));
g8.setVisible(true);
p1.add(g8);
JPanel g9 = new JPanel();
g9.setBorder(BorderFactory.createLineBorder(Color.BLACK));
g9.setPreferredSize(new Dimension(100, 100));
g9.setVisible(true);
p1.add(g9);
f1.pack();
f1.setVisible(true);
}
}
The simplest way I could think of is to create s subclass of JPanel with a boolean to determine whether the circle should be drawn. I named it CirclePanel:
public class CirclePanel extends JPanel{
public static final Color circleColor = Color.BLACK;
private boolean drawCircle;
public CirclePanel() {
drawCircle=false;
setBorder(BorderFactory.createLineBorder(Color.BLACK));
setBackground(Color.WHITE);
}
public void setDrawCircle(boolean drawCircle) {
this.drawCircle = drawCircle;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if(drawCircle) {
Graphics2D g2d = (Graphics2D) g;
Color tmp = g2d.getColor();
g2d.setColor(circleColor);
g2d.fillOval(0, 0, getWidth(), getHeight());
g2d.setColor(tmp);
}
}
}
Then I created a subclass of JFrame for the frame but you could also do it in a main method. I placed the circle panels in an array to avoid repeating code and placed them in a grid. When the button is clicked, a List of indices are created and three are removed at random. The list is then used to set the boolean variable of the panels. see below:
public class CircleGrid extends JFrame implements ActionListener{
private CirclePanel[] panels;
private JButton button;
public CircleGrid() {
super("Circle test");
setLayout(new BorderLayout());
panels = new CirclePanel[9];
JPanel center = new JPanel();
center.setLayout(new GridLayout(3, 3));
for (int i = 0; i < panels.length; i++) {
panels[i] = new CirclePanel();
center.add(panels[i]);
}
button = new JButton("Color in");
button.addActionListener(this);
this.add(button, BorderLayout.NORTH);
this.add(center, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(new Dimension(800, 800));
setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(button)) {
// select three random circle indices -
// https://stackoverflow.com/a/42353488/7015661
ArrayList<Integer> indices = new ArrayList<Integer>();
int numRandom = 3; // three indices
for (int i = 0; i < panels.length; i++) {
indices.add(i);
}
Random r = new Random();
for (int i = 0; i < numRandom; i++) {
int rndPos1 = r.nextInt(indices.size());
indices.remove(rndPos1); // remove three indices from the list
}
// change panel boolean
for (int i = 0; i < panels.length; i++) {
CirclePanel pi = panels[i];
if(indices.contains(i)) {
// no circle
pi.setDrawCircle(false);
}else {
//draw circle
pi.setDrawCircle(true);
}
}
repaint(); // redraw panels
}
}
public static void main(String[] args) {
new CircleGrid();
}
}
I end up with the following:
What you would do is essentially before your draw on your jPanel white out the entire panel then draw your circle apply the code below hopes in helps.
int name=(int) Math.random() * 100;
int name1=(int) Math.random() * 100;
Then you would draw the circle on the JPanels using the graphics method and the appropriate code. Below is the code that draws the circle in a random place:
g.draw/fillOval (name,name1,100,100);

JTextField horizontal scrolling java swing

I have a JTextField inside a JPanel . The user can type text as big as she wants in the textfield , however horizontal scrolling should appear when the text goes beyond the screen of the textfield. At this point if the user backspace the text and shrink the text within the screen of the textfield , the scrolling should disappear. So basically scrolling should appear only when its needed. How to do it ? Help please.
If you don't like the behavior of the JScrollPane + JTextField, you need to use JScrollBar#setModel(BoundedRangeModel) + JTextField#getHorizontalVisibility():
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.basic.BasicScrollBarUI;
public class TextFieldScrollBarTest {
private static final String TEXT = "javascript:(function(){var l=location,m=l.href.match('^(https?://)(.+)(api[^+]+|technotes[^+]+)');if(m)l.href=m[1]+'docs.oracle.com/javase/8/docs/'+decodeURIComponent(m[3]).replace(/\\+.*$/,'').replace(/\\[\\]/g,':A').replace(/, |\\(|\\)/g,'-');}());";
public JComponent makeUI() {
JScrollPane scroll = new JScrollPane(new JTextField(TEXT));
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
JTextField textField = new JTextField(TEXT);
JScrollBar scroller = new JScrollBar(Adjustable.HORIZONTAL) {
#Override public void updateUI() {
//super.updateUI();
setUI(new ArrowButtonlessScrollBarUI());
}
#Override public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
d.height = 5;
return d;
}
};
scroller.setModel(textField.getHorizontalVisibility());
Box box = Box.createVerticalBox();
box.add(scroll);
box.add(Box.createVerticalStrut(50));
box.add(textField);
box.add(Box.createVerticalStrut(2));
box.add(scroller);
box.add(Box.createVerticalGlue());
JPanel p = new JPanel(new BorderLayout());
p.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
p.add(box, BorderLayout.NORTH);
return p;
}
public static void main(String... args) {
EventQueue.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new TextFieldScrollBarTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}
class ZeroSizeButton extends JButton {
private static final Dimension ZERO_SIZE = new Dimension();
#Override public Dimension getPreferredSize() {
return ZERO_SIZE;
}
}
class ArrowButtonlessScrollBarUI extends BasicScrollBarUI {
private static final Color DEFAULT_COLOR = new Color(220, 100, 100);
private static final Color DRAGGING_COLOR = new Color(200, 100, 100);
private static final Color ROLLOVER_COLOR = new Color(255, 120, 100);
#Override protected JButton createDecreaseButton(int orientation) {
return new ZeroSizeButton();
}
#Override protected JButton createIncreaseButton(int orientation) {
return new ZeroSizeButton();
}
#Override protected void paintTrack(Graphics g, JComponent c, Rectangle r) {
//Graphics2D g2 = (Graphics2D) g.create();
//g2.setPaint(new Color(100, 100, 100));
//g2.fillRect(r.x, r.y, r.width - 1, r.height - 1);
//g2.dispose();
}
#Override protected void paintThumb(Graphics g, JComponent c, Rectangle r) {
JScrollBar sb = (JScrollBar) c;
if (!sb.isEnabled()) {
return;
}
BoundedRangeModel m = sb.getModel();
int iv = m.getMaximum() - m.getMinimum() - m.getExtent() - 1; // -1: bug?
if (iv > 0) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Color color;
if (isDragging) {
color = DRAGGING_COLOR;
} else if (isThumbRollover()) {
color = ROLLOVER_COLOR;
} else {
color = DEFAULT_COLOR;
}
g2.setPaint(color);
g2.fillRect(r.x, r.y, r.width - 1, r.height - 1);
g2.dispose();
}
}
}

JPanel Inside Of A JPanel

I am trying to get a JPanel to appear inside of another JPanel. Currently, the JPanel is in an external JFrame and loads with my other JFrame. I want the JPanel to be inside the other JPanel so the program does not open two different windows.
Here is a picture:
The small JPanel with the text logs I want inside of the main game frame. I've tried adding the panel to the panel, panel.add(othePanel). I've tried adding it the JFrame, frame.add(otherPanel). It just overwrites everything else and gives it a black background.
How can I add the panel, resize, and move it?
Edits:
That is where I want the chatbox to be.
Class code:
Left out top of class.
public static JPanel panel;
public static JTextArea textArea = new JTextArea(5, 30);
public static JTextField userInputField = new JTextField(30);
public static void write(String message) {
Chatbox.textArea.append("[Game]: " + message + "\n");
Chatbox.textArea.setCaretPosition(Chatbox.textArea.getDocument()
.getLength());
Chatbox.userInputField.setText("");
}
public Chatbox() {
panel = new JPanel();
panel.setPreferredSize(new Dimension(220, 40));
panel.setBackground(Color.BLACK);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setPreferredSize(new Dimension(380, 100));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
scrollPane
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
userInputField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
String fromUser = userInputField.getText();
if (fromUser != null) {
textArea.append(Frame.username + ":" + fromUser + "\n");
textArea.setCaretPosition(textArea.getDocument()
.getLength());
userInputField.setText("");
}
}
});
panel.add(userInputField, SwingConstants.CENTER);
panel.add(scrollPane, SwingConstants.CENTER);
//JFrame frame = new JFrame();
//frame.add(panel);
//frame.setSize(400, 170);
//frame.setVisible(true);
}
Main frame class:
public Frame() {
frame.getContentPane().remove(loginPanel);
frame.repaint();
String capName = capitalizeString(Frame.username);
name = new JLabel(capName);
new EnemyHealth("enemyhealth10.png");
new Health("health10.png");
new LoadRedCharacter("goingdown.gif");
new Spellbook();
new LoadMobs();
new LoadItems();
new Background();
new Inventory();
new ChatboxInterface();
frame.setBackground(Color.black);
Frame.redHealthLabel.setFont(new Font("Serif", Font.PLAIN, 20));
ticks.setFont(new Font("Serif", Font.PLAIN, 20));
ticks.setForeground(Color.yellow);
Frame.redHealthLabel.setForeground(Color.black);
// Inventory slots
panel.add(slot1);
panel.add(name);
name.setFont(new Font("Serif", Font.PLAIN, 20));
name.setForeground(Color.white);
panel.add(enemyHealthLabel);
panel.add(redHealthLabel);
panel.add(fireSpellBookLabel);
panel.add(iceSpellBookLabel);
panel.add(spiderLabel);
panel.add(appleLabel);
panel.add(fireMagicLabel);
panel.add(swordLabel);
// Character
panel.add(redCharacterLabel);
// Interface
panel.add(inventoryLabel);
panel.add(chatboxLabel);
// Background
panel.add(backgroundLabel);
frame.setContentPane(panel);
frame.getContentPane().invalidate();
frame.getContentPane().validate();
frame.getContentPane().repaint();
//I WOULD LIKE THE LOADING OF THE PANEL SOMEWHERE IN THIS CONSTRUCTOR.
new ResetEntities();
frame.repaint();
panel.setLayout(null);
Run.loadKeyListener();
Player.px = Connect.x;
Player.py = Connect.y;
new Mouse();
TextualMenu.rect = new Rectangle(Frame.inventoryLabel.getX() + 80,
Frame.inventoryLabel.getY() + 100,
Frame.inventoryLabel.getWidth(),
Frame.inventoryLabel.getHeight());
Player.startMessage();
}
Don't use static variables.
Don't use a null layout.
Use appropriate layout managers. Maybe the main panel uses a BorderLayout. Then you add your main component to the CENTER and a second panel to the EAST. The second panel can also use a BorderLayout. You can then add the two components to the NORTH, CENTER or SOUTH as you require.
For example, using a custom Border:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.RadialGradientPaint;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.AbstractBorder;
#SuppressWarnings("serial")
public class FrameEg extends JPanel {
public static final String FRAME_URL_PATH = "http://th02.deviantart.net/"
+ "fs70/PRE/i/2010/199/1/0/Just_Frames_5_by_ScrapBee.png";
public static final int INSET_GAP = 120;
private BufferedImage frameImg;
private BufferedImage smlFrameImg;
public FrameEg() {
try {
URL frameUrl = new URL(FRAME_URL_PATH);
frameImg = ImageIO.read(frameUrl);
final int smlFrameWidth = frameImg.getWidth() / 2;
final int smlFrameHeight = frameImg.getHeight() / 2;
smlFrameImg = new BufferedImage(smlFrameWidth, smlFrameHeight,
BufferedImage.TYPE_INT_ARGB);
Graphics g = smlFrameImg.getGraphics();
g.drawImage(frameImg, 0, 0, smlFrameWidth, smlFrameHeight, null);
g.dispose();
int top = INSET_GAP;
int left = top;
int bottom = top;
int right = left;
Insets insets = new Insets(top, left, bottom, right);
MyBorder myBorder = new MyBorder(frameImg, insets);
JTextArea textArea = new JTextArea(50, 60);
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
for (int i = 0; i < 300; i++) {
textArea.append("Hello world! How is it going? ");
}
setLayout(new BorderLayout(1, 1));
setBackground(Color.black);
Dimension prefSize = new Dimension(frameImg.getWidth(),
frameImg.getHeight());
JPanel centerPanel = new MyPanel(prefSize);
centerPanel.setBorder(myBorder);
centerPanel.setLayout(new BorderLayout(1, 1));
centerPanel.add(new JScrollPane(textArea), BorderLayout.CENTER);
MyPanel rightUpperPanel = new MyPanel(new Dimension(smlFrameWidth,
smlFrameHeight));
MyPanel rightLowerPanel = new MyPanel(new Dimension(smlFrameWidth,
smlFrameHeight));
top = top / 2;
left = left / 2;
bottom = bottom / 2;
right = right / 2;
Insets smlInsets = new Insets(top, left, bottom, right);
rightUpperPanel.setBorder(new MyBorder(smlFrameImg, smlInsets));
rightUpperPanel.setLayout(new BorderLayout());
rightLowerPanel.setBorder(new MyBorder(smlFrameImg, smlInsets));
rightLowerPanel.setBackgroundImg(createBackgroundImg(rightLowerPanel
.getPreferredSize()));
JTextArea ruTextArea1 = new JTextArea(textArea.getDocument());
ruTextArea1.setWrapStyleWord(true);
ruTextArea1.setLineWrap(true);
rightUpperPanel.add(new JScrollPane(ruTextArea1), BorderLayout.CENTER);
JPanel rightPanel = new JPanel(new GridLayout(0, 1, 1, 1));
rightPanel.add(rightUpperPanel);
rightPanel.add(rightLowerPanel);
rightPanel.setOpaque(false);
add(centerPanel, BorderLayout.CENTER);
add(rightPanel, BorderLayout.EAST);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private BufferedImage createBackgroundImg(Dimension preferredSize) {
BufferedImage img = new BufferedImage(preferredSize.width,
preferredSize.height, BufferedImage.TYPE_INT_ARGB);
Point2D center = new Point2D.Float(img.getWidth()/2, img.getHeight()/2);
float radius = img.getWidth() / 2;
float[] dist = {0.0f, 1.0f};
Color centerColor = new Color(100, 100, 50);
Color outerColor = new Color(25, 25, 0);
Color[] colors = {centerColor , outerColor };
RadialGradientPaint paint = new RadialGradientPaint(center, radius, dist, colors);
Graphics2D g2 = img.createGraphics();
g2.setPaint(paint);
g2.fillRect(0, 0, img.getWidth(), img.getHeight());
g2.dispose();
return img;
}
private static void createAndShowGui() {
FrameEg mainPanel = new FrameEg();
JFrame frame = new JFrame("FrameEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.setResizable(false);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
#SuppressWarnings("serial")
class MyPanel extends JPanel {
private Dimension prefSize;
private BufferedImage backgroundImg;
public MyPanel(Dimension prefSize) {
this.prefSize = prefSize;
}
public void setBackgroundImg(BufferedImage background) {
this.backgroundImg = background;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (backgroundImg != null) {
g.drawImage(backgroundImg, 0, 0, this);
}
}
#Override
public Dimension getPreferredSize() {
return prefSize;
}
}
#SuppressWarnings("serial")
class MyBorder extends AbstractBorder {
private BufferedImage borderImg;
private Insets insets;
public MyBorder(BufferedImage borderImg, Insets insets) {
this.borderImg = borderImg;
this.insets = insets;
}
#Override
public void paintBorder(Component c, Graphics g, int x, int y, int width,
int height) {
g.drawImage(borderImg, 0, 0, c);
}
#Override
public Insets getBorderInsets(Component c) {
return insets;
}
}
Which would look like:

DnD - Why is dragged label displayed below other components?

I've written a basic DnD program that has four JLabels in a row. I've noticed
that when I drag a label to the left, it is displayed below the next label. However,
when I drag the label to the right, it is displayed above the next label.
The labels are added in order, left to right. So the dragged label is being
displayed below other labels that were added before the dragged label, and displayed
above other labels that were added after the dragged label.
Can anyone explain why this happens? Can anyone offer a fix so that the dragged label
is displayed above the other labels?
Thanks.
source code:
public class LabelDnd extends JPanel {
private JLabel[] labels;
private Color[] colors = { Color.BLUE, Color.BLACK, Color.RED, Color.MAGENTA };
public LabelDnd() {
super();
this.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
this.setBackground(Color.WHITE);
this.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
JPanel basePanel = new JPanel();
basePanel.setLayout(new GridLayout(1, 4, 4, 4));
basePanel.setBackground(Color.CYAN);
MouseAdapter listener = new MouseAdapter() {
Point p = null;
#Override
public void mousePressed(MouseEvent e) {
p = e.getLocationOnScreen();
}
#Override
public void mouseDragged(MouseEvent e) {
JComponent c = (JComponent) e.getSource();
Point l = c.getLocation();
Point here = e.getLocationOnScreen();
c.setLocation(l.x + here.x - p.x, l.y + here.y - p.y);
p = here;
}
};
this.labels = new JLabel[4];
for (int i = 0; i < this.labels.length; i++) {
this.labels[i] = new JLabel(String.valueOf(i), JLabel.CENTER);
this.labels[i].setOpaque(true);
this.labels[i].setPreferredSize(new Dimension(100, 100));
this.labels[i].setBackground(this.colors[i]);
this.labels[i].setForeground(Color.WHITE);
this.labels[i].addMouseListener(listener);
this.labels[i].addMouseMotionListener(listener);
basePanel.add(this.labels[i]);
}
this.add(basePanel);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("LabelDnd");
frame.getContentPane().setLayout(new BorderLayout());
LabelDnd panel = new LabelDnd();
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
You are not changing the order that the labels have been added or are being painted in your code. Consider elevating the JLabel to the glasspane when dragging (after removing it from the main container), and then re-adding it to the main container on mouse release.
For example:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.*;
import java.util.Comparator;
import java.util.PriorityQueue;
import javax.swing.*;
#SuppressWarnings("serial")
public class LabelDnd extends JPanel {
private static final String X_POS = "x position";
private JLabel[] labels;
private Color[] colors = { Color.BLUE, Color.BLACK, Color.RED, Color.MAGENTA };
public LabelDnd() {
super();
// this.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
this.setBackground(Color.WHITE);
this.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
JPanel basePanel = new JPanel();
basePanel.setLayout(new GridLayout(1, 4, 4, 4));
basePanel.setBackground(Color.CYAN);
MouseAdapter listener = new MouseAdapter() {
Point loc = null;
Container parentContainer = null;
Container glasspane = null;
JLabel placeHolder = new JLabel("");
private Point glassLocOnScn;
#Override
public void mousePressed(MouseEvent e) {
JComponent selectedLabel = (JComponent) e.getSource();
loc = e.getPoint();
Point currLocOnScn = e.getLocationOnScreen();
parentContainer = selectedLabel.getParent();
JRootPane rootPane = SwingUtilities.getRootPane(selectedLabel);
glasspane = (Container) rootPane.getGlassPane();
glasspane.setVisible(true);
glasspane.setLayout(null);
glassLocOnScn = glasspane.getLocationOnScreen();
Component[] comps = parentContainer.getComponents();
// remove all labels from parent
parentContainer.removeAll();
// add labels back except for selected one
for (Component comp : comps) {
if (comp != selectedLabel) {
parentContainer.add(comp);
} else {
// add placeholder in place of selected component
parentContainer.add(placeHolder);
}
}
selectedLabel.setLocation(currLocOnScn.x - loc.x - glassLocOnScn.x,
currLocOnScn.y - loc.y - glassLocOnScn.y);
glasspane.add(selectedLabel);
glasspane.setVisible(true);
parentContainer.revalidate();
parentContainer.repaint();
}
#Override
public void mouseDragged(MouseEvent e) {
JComponent selectedLabel = (JComponent) e.getSource();
glassLocOnScn = glasspane.getLocationOnScreen();
Point currLocOnScn = e.getLocationOnScreen();
selectedLabel.setLocation(currLocOnScn.x - loc.x - glassLocOnScn.x,
currLocOnScn.y - loc.y - glassLocOnScn.y);
glasspane.repaint();
}
#Override
public void mouseReleased(MouseEvent e) {
JComponent selectedLabel = (JComponent) e.getSource();
if (parentContainer == null || glasspane == null) {
return;
}
// sort the labels based on their x position on screen
PriorityQueue<JComponent> compQueue = new PriorityQueue<JComponent>(
4, new Comparator<JComponent>() {
#Override
public int compare(JComponent o1, JComponent o2) {
// sort of a kludge -- checking a client property that
// holds the x-position
Integer i1 = (Integer) o1.getClientProperty(X_POS);
Integer i2 = (Integer) o2.getClientProperty(X_POS);
return i1.compareTo(i2);
}
});
// sort of a kludge -- putting x position before removing component
// into a client property to associate it with the JLabel
selectedLabel.putClientProperty(X_POS, selectedLabel.getLocationOnScreen().x);
glasspane.remove(selectedLabel);
compQueue.add(selectedLabel);
Component[] comps = parentContainer.getComponents();
for (Component comp : comps) {
JLabel label = (JLabel) comp;
if (!label.getText().trim().isEmpty()) { // if placeholder!
label.putClientProperty(X_POS, label.getLocationOnScreen().x);
compQueue.add(label); // add to queue
}
}
parentContainer.removeAll();
// add back labels sorted by x-position on screen
while (compQueue.size() > 0) {
parentContainer.add(compQueue.remove());
}
parentContainer.revalidate();
parentContainer.repaint();
glasspane.repaint();
}
};
this.labels = new JLabel[4];
for (int i = 0; i < this.labels.length; i++) {
this.labels[i] = new JLabel(String.valueOf(i), JLabel.CENTER);
this.labels[i].setOpaque(true);
this.labels[i].setPreferredSize(new Dimension(100, 100));
this.labels[i].setBackground(this.colors[i]);
this.labels[i].setForeground(Color.WHITE);
this.labels[i].addMouseListener(listener);
this.labels[i].addMouseMotionListener(listener);
basePanel.add(this.labels[i]);
}
this.add(basePanel);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("LabelDnd");
frame.getContentPane().setLayout(new BorderLayout());
LabelDnd panel = new LabelDnd();
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Because of z-index of your Swing component. You should use setComponentZOrder to change component's z-index value after you drag.
Please check this link to get more details

Categories

Resources