GridLayout in panel not showing buttons - java

I created a class that extends JPanel called GamePane which on creation sets the layout to a GridLayout and then adds buttons one at a time:
public class GamePane extends JPanel {
private JButton[][] buttons;
private final int sizeX, sizeY;
private Partida partida;
public GamePane(int sizeX, int sizeY, Partida p) {
super();
this.sizeX = sizeX;
this.sizeY = sizeY;
this.partida = p;
this.setLayout(new GridLayout(sizeX + 1, sizeY + 1));
buttons = new JButton[sizeX + 1][sizeY + 1];
for (int y = 0; y < sizeY + 1; y++) {
for (int x = 0; x < sizeX + 1; x++) {
JButton button = new JButton();
button.addActionListener(new ListenerGameButton(x, y, this));
this.add(button);
button.setBackground(Color.yellow);
button.setVisible(true);
buttons[x][y] = button;
}
}
The panel initialization is done on the parent JDialog:
gamePane = new GamePane(sizeX, sizeY, parentPartida);
And here's how it looks in the NetBeans editor: (The selected frame being an instance of GamePane)
Then when testing it out it doesn't show the buttons:
This is the class of the parent JDialog where the child GamePane is located, most of the code is autogenerated by Netbeans:
public class gameDialog extends javax.swing.JDialog {
private final int sizeX, sizeY;
private Partida parentPartida;
public gameDialog(java.awt.Frame parent, boolean modal, int sizeX, int sizeY, Partida parentPartida)
{
super(parent, modal);
this.parentPartida = parentPartida;
this.sizeX = sizeX;
this.sizeY = sizeY;
initComponents();
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
logoPane = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
gamePane = new GamePane(sizeX, sizeY, parentPartida);
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Partida en curso");
setModal(true);
setResizable(false);
logoPane.setBackground(new java.awt.Color(95, 143, 191));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/logo.png"))); // NOI18N
jLabel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(0, 0, 0), new java.awt.Color(0, 0, 0), new java.awt.Color(0, 0, 0), new java.awt.Color(0, 0, 0)));
jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
javax.swing.GroupLayout logoPaneLayout = new javax.swing.GroupLayout(logoPane);
logoPane.setLayout(logoPaneLayout);
logoPaneLayout.setHorizontalGroup(
logoPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
logoPaneLayout.setVerticalGroup(
logoPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
);
javax.swing.GroupLayout gamePaneLayout = new javax.swing.GroupLayout(gamePane);
gamePane.setLayout(gamePaneLayout);
gamePaneLayout.setHorizontalGroup(
gamePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 437, Short.MAX_VALUE)
);
gamePaneLayout.setVerticalGroup(
gamePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 353, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(gamePane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(logoPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(logoPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(gamePane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>
// Variables declaration - do not modify
private GUI.GamePane gamePane;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel logoPane;
// End of variables declaration
}
The initComponents() method is generated by NetBeans and that's where the GamePane is created and later on the frame is packed at the end of initComponents() by calling pack()
So, what am I doing wrong? Thanks in advance!

There is no problem with your code. It is perfectly working fine.
JDialog dialog = new JDialog();
dialog.getContentPane().add(new GamePane(10, 10));
dialog.pack();
dialog.setVisible(true);
And i got the below.

Related

How to calculate dimensions of a HTML formatted JComponent?

I am designing a Swing form using the GUI builder of Netbeans, and I'm using HTML to word wrap a JRadioButton. That works, but now the encapsulating JFrame does not resize the JFrame to fit the contents. I'm using the default GUI builder's LayoutManager, GroupLayout.
I tried to calculate the height of the component after it is word wrapped by HTML. That worked (see code below), but after setting the size and preferred size of the JRadioButton, the window does not get resized.
How can I fix it?
private void recalculateSize(JComponent component, int width) {
View v = (View) component.getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);
float preferredSpan = v.getPreferredSpan(View.Y_AXIS);
int height = component.getHeight();
float margin = height - preferredSpan;
Dimension actualSize = getActualSize(component, width);
int actualHeight = (int) (actualSize.height + margin);
Dimension d = new Dimension(component.getPreferredSize().width, actualHeight);
component.setPreferredSize(d);
component.setSize(d);
// This height is the real height. I tested it setting the background
// color to red.
System.out.println("Actual height: " + actualHeight);
}
private static Dimension getActualSize(JComponent component, int width) {
View view = (View) component.getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);
view.setSize(width, 0);
float w = view.getPreferredSpan(View.X_AXIS);
float h = view.getPreferredSpan(View.Y_AXIS);
return new java.awt.Dimension((int) Math.ceil(w), (int) Math.ceil(h));
}
Below is the whole code. Since it is generated by the GUI builder, it it hard to remove the unnecessary code.
import java.awt.Dimension;
import javax.swing.JComponent;
import javax.swing.text.View;
public class Test extends javax.swing.JFrame {
Test() {
initComponents();
initMoreComponents();
}
private void initComponents() {
one = new javax.swing.ButtonGroup();
two = new javax.swing.ButtonGroup();
three = new javax.swing.JTextField();
four = new javax.swing.JLabel();
five = new javax.swing.JLabel();
six = new javax.swing.JTextField();
seven = new javax.swing.JRadioButton();
eight = new javax.swing.JRadioButton();
nine = new javax.swing.JLabel();
ten = new javax.swing.JLabel();
eleven = new javax.swing.JCheckBox();
twelve = new javax.swing.JCheckBox();
thirteen = new javax.swing.JCheckBox();
fourteen = new javax.swing.JCheckBox();
fifteen = new javax.swing.JCheckBox();
sixteen = new javax.swing.JCheckBox();
seventeen = new javax.swing.JButton();
eightteen = new javax.swing.JButton();
nineteen = new javax.swing.JButton();
twenty = new javax.swing.JRadioButton();
twentyone = new javax.swing.JRadioButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(new java.awt.Dimension(300, 420));
setResizable(false);
setSize(new java.awt.Dimension(0, 0));
four.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
four.setText("Four");
five.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
five.setText("Five");
one.add(seven);
seven.setSelected(true);
seven.setText("<html>A very long sentence, which might take up more than one line.</html>");
one.add(eight);
eight.setText("<html>Another multiline sentence.</html>");
nine.setText("Nine");
ten.setText("Ten");
eleven.setText("Eleven");
twelve.setText("Twelve");
thirteen.setText("Thirteen");
fourteen.setText("Fourteen");
fifteen.setText("Fifteen");
sixteen.setText("Sixteen");
seventeen.setText("Seventeen");
eightteen.setText("Eightteen");
nineteen.setText("Nineteen");
two.add(twenty);
twenty.setText("<html>Twenty with another line of text</html>");
two.add(twentyone);
twentyone.setText("Twenty one");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(three)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(seventeen))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(six)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(eightteen))
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(nineteen))
.addComponent(eight, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(nine)
.addComponent(four)
.addComponent(five)
.addComponent(ten)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(fourteen)
.addComponent(fifteen))
.addGap(87, 87, 87)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(twelve)
.addComponent(thirteen))))
.addGroup(layout.createSequentialGroup()
.addComponent(eleven)
.addGap(67, 67, 67)
.addComponent(sixteen)))
.addGap(0, 101, Short.MAX_VALUE))
.addComponent(seven, javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(twenty)
.addComponent(twentyone, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(nine)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(seven, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(twenty, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(twentyone)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(eight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(29, 29, 29)
.addComponent(four)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(three, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(seventeen))
.addGap(18, 18, 18)
.addComponent(five)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(six, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(eightteen))
.addGap(18, 18, 18)
.addComponent(ten)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(eleven)
.addComponent(sixteen))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(fourteen)
.addComponent(thirteen))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(fifteen)
.addComponent(twelve))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(nineteen)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}
private void initMoreComponents() {
recalculateSize(this.seven, 280);
}
private void recalculateSize(JComponent component, int width) {
View v = (View) component.getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);
float preferredSpan = v.getPreferredSpan(View.Y_AXIS);
int height = component.getHeight();
float margin = height - preferredSpan;
Dimension actualSize = getActualSize(component, width);
int actualHeight = (int) (actualSize.height + margin);
Dimension d = new Dimension(component.getPreferredSize().width, actualHeight);
component.setPreferredSize(d);
component.setSize(d);
component.revalidate();
System.out.println("Actual height: " + actualHeight);
System.out.println("Dimension preferred and set size: " + d);
System.out.println("Dimension after revalidate: " + component.getPreferredSize());
pack();
}
private static Dimension getActualSize(JComponent component, int width) {
View view = (View) component.getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);
view.setSize(width, 0);
float w = view.getPreferredSpan(View.X_AXIS);
float h = view.getPreferredSpan(View.Y_AXIS);
return new java.awt.Dimension((int) Math.ceil(w), (int) Math.ceil(h));
}
public static void main(String args[]) {
new Test().setVisible(true);
}
private javax.swing.JCheckBox eleven;
private javax.swing.JCheckBox fourteen;
private javax.swing.JButton eightteen;
private javax.swing.JButton seventeen;
javax.swing.JTextField six;
private javax.swing.JLabel five;
private javax.swing.JRadioButton twentyone;
private javax.swing.JCheckBox fifteen;
private javax.swing.JButton nineteen;
private javax.swing.JRadioButton seven;
private javax.swing.ButtonGroup one;
private javax.swing.JLabel nine;
private javax.swing.JRadioButton twenty;
private javax.swing.ButtonGroup two;
private javax.swing.JRadioButton eight;
javax.swing.JTextField three;
private javax.swing.JLabel four;
private javax.swing.JCheckBox sixteen;
private javax.swing.JCheckBox thirteen;
private javax.swing.JCheckBox twelve;
private javax.swing.JLabel ten;
}

How to create image slider in swing using grouplayout?

Hi i am trying to create to create desktop application in using i am using Group Layout for creating mu Gui i tried to create image slider but i am not able to achieve how can i achieve this
when i run my progrram it throws exception ArrayIndexOutofBound pleas solve my problem
public class MyGui3 extends JFrame {
private ImageIcon myImage1 = new ImageIcon ("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\yellow.png");
private ImageIcon myImage2 = new ImageIcon ("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\c.jpg");
private ImageIcon myImage3 = new ImageIcon ("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\yellow.png");
private ImageIcon myImage4 = new ImageIcon ("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\yellow.png");
private ImageIcon[] myImages = new ImageIcon[4];
private int curImageIndex=0;
private JButton jButton1;
private JButton jButton2;
private JPanel jPanel1;
private JLabel jLabel1;
public MyGui3() {
jPanel1 = new JPanel();
jLabel1 = new JLabel(myImage1);
myImages[0]=myImage1;
myImages[1]=myImage2;
myImages[2]=myImage3;
myImages[3]=myImage4;
jButton1 = new JButton();
jButton2 = new JButton();
//ImageGallery.add(new JLabel (myImage1));
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(new java.awt.Dimension(1386, 768));
jPanel1.setBackground(new java.awt.Color(153, 153, 255));
jLabel1.setText("jLabel1");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(197, 197, 197)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(141, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(54, 54, 54)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(130, Short.MAX_VALUE))
);
jButton1.setText("jButton1");
jButton2.setText("jButton2");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(455, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING))
.addGap(124, 124, 124)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(137, 137, 137))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(157, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(143, 143, 143))
.addGroup(layout.createSequentialGroup()
.addGap(325, 325, 325)
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jButton2)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
//OptionPane.showMessageDialog(frame,"Welcome to allhabad High Court");
if(curImageIndex>0 && curImageIndex <= 3)
{ jPanel1.remove(0);
curImageIndex=curImageIndex-1;
ImageIcon TheImage= myImages[curImageIndex];
jLabel1 = new JLabel(TheImage);
jPanel1.validate();
jPanel1.repaint();
}
else
{
jPanel1.remove(0);
jLabel1 = new JLabel(myImage1);
curImageIndex=0;
jPanel1.validate();
jPanel1.repaint();
}
}
});
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
//JOptionPane.showMessageDialog(frame,"Welcome to allhabad High Court");
if(curImageIndex>=0 && curImageIndex < 3)
{ jPanel1.remove(0);
curImageIndex = curImageIndex + 1;
ImageIcon TheImage= myImages[curImageIndex];
jLabel1 = new JLabel(TheImage);
jPanel1.validate();
jPanel1.repaint();
}
else
{
jPanel1.remove(0);
jLabel1 = new JLabel(myImage4);
curImageIndex=3;
jPanel1.validate();
jPanel1.repaint();
}
}
});
setTitle("Find");
pack();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String args[]) {
MyGui3 g1=new MyGui3();
g1.setVisible(true);
g1.setExtendedState(JFrame.MAXIMIZED_BOTH);
g1.setBackground(Color.yellow);
}
}
Thanks in advance
The group layout is intended for UI designing software, rather than humans. Avoid using it.
Instead, you have quite a selection of built-in layout managers, and it's not very difficult to implement a custom one in case you need special behavior that cannot (or should not) be achieved by nesting layouts (by nesting components with different layout).
I'm not sure what's the layout you're trying to achieve, but if you're referring to a list of images that you can scroll down in a JScrollPane or something like that, you should consider FlowLayout or even BoxLayout. There are other layouts you can wield to this task, but these two would be the simplest to use.

Sliding any JPanel

Im trying to make a universal class for sliding two any-samesized JPanels but when I use it, the one to get slid just disappears. What I also noticed is that the JPanel "innerPanel"'s width is being reset (when transisting a 200,200 panel) from 400,200 that the size gets reset to 200,200 after the Thread is started
package org.chimeras1684.ui.panels;
import java.awt.Point;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JViewport;
/**
*
* #author Arhowk
*/
public final class Transistor extends javax.swing.JFrame {
/**
* Creates new form test
*/
public enum TDirection{
MOVING_TO_LEFT,
MOVING_TO_RIGHT,
MOVING_TO_TOP,
MOVING_TO_BOTTOM;
}
private static Transistor instance;
static{
instance = new Transistor();
}
public static Transistor getInstance(){
return instance;
}
private Transistor() {
initComponents();
setVisible(true);
slide(TDirection.MOVING_TO_TOP, this, jPanel2, jPanel1);
}
public void slide(final TDirection dir, final JFrame root, final JPanel in, final JPanel out){
if(in.getSize().height != out.getSize().height || out.getSize().width != in.getSize().width ){
throw new IllegalArgumentException("Dimension in is not equal to dimension of out");
}
panelTL.removeAll();
panelBL.removeAll();
panelBR.removeAll();
panelTR.validate();
panelTR.repaint();
panelTL.validate();
panelTL.repaint();
panelBR.validate();
panelBR.repaint();
panelBL.validate();
panelBL.repaint();
final double width = in.getSize().width;
final double height = in.getSize().height;
int x = out.getLocation().x;
int y = out.getLocation().y;
final double speedWidthModifier = (dir == TDirection.MOVING_TO_LEFT) ? -1 : (dir == TDirection.MOVING_TO_RIGHT) ? 1 : 0;
final double speedHeightModifier = (dir == TDirection.MOVING_TO_BOTTOM) ? -1 : (dir == TDirection.MOVING_TO_TOP) ? 1 : 0;
final double xAtEnd = (dir == TDirection.MOVING_TO_RIGHT) ? width : 0;
final double yAtEnd = (dir == TDirection.MOVING_TO_BOTTOM) ? height : 0;
if(dir == TDirection.MOVING_TO_LEFT){
panelTR.setSize(200,200);
panelTL.setSize(200,200);
panelTL.add(in);
panelTR.add(out);
}
if(dir == TDirection.MOVING_TO_RIGHT){
panelTR.setSize(200,200);
panelTL.setSize(200,200);
panelTL.add(out);
panelTR.add(in);
}
if(dir == TDirection.MOVING_TO_BOTTOM){
panelBL.setSize(200,200);
panelTL.setSize(200,200);
panelBL.add(in);
panelTL.add(out);
}
if(dir == TDirection.MOVING_TO_TOP){
panelBL.setSize(200,200);
panelTL.setSize(200,200);
panelBL.add(out);
panelTL.add(in);
}
root.remove(out);
scrollPanel.setSize(in.getSize());
innerPanel.setSize(in.getSize().width * 2, in.getSize().height);
root.add(scrollPanel);
scrollPanel.setLocation(x, y);
if(speedWidthModifier == 0 && speedHeightModifier == 0){
throw new NullPointerException("TDirection dir");
}
System.out.println(new Date());
System.out.println("tlsize1: " + panelTL.getSize());
(new Timer()).schedule(new TimerTask(){
#Override
public void run() {
System.out.println("tlsize1: " + panelTL.getSize());
System.out.println(new Date());
Point viewport = scrollPanel.getViewport().getViewPosition();
JViewport newViewport = scrollPanel.getViewport();
double currentX = viewport.x;
double currentY = viewport.y;
double speed = 50;
double newX = currentX + (speed * speedWidthModifier);
double newY = currentY + (speed * speedHeightModifier);
newViewport.getViewPosition().x = (int)newX;
newViewport.getViewPosition().y = (int)newY;
if((xAtEnd != 0 && newX > xAtEnd) || (yAtEnd != 0 && newY > yAtEnd)){
this.cancel();
}
root.repaint();
System.out.println("tlsize2: " + panelTL.getSize());
}
}, 0,25);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
scrollPanel = new javax.swing.JScrollPane();
innerPanel = new javax.swing.JPanel();
panelTL = new javax.swing.JPanel();
panelTR = new javax.swing.JPanel();
panelBL = new javax.swing.JPanel();
panelBR = new javax.swing.JPanel();
jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
scrollPanel.setBorder(null);
scrollPanel.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPanel.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
innerPanel.setBackground(new java.awt.Color(255, 0, 153));
innerPanel.setLayout(new java.awt.GridBagLayout());
panelTL.setAutoscrolls(true);
panelTL.setPreferredSize(new java.awt.Dimension(0, 0));
javax.swing.GroupLayout panelTLLayout = new javax.swing.GroupLayout(panelTL);
panelTL.setLayout(panelTLLayout);
panelTLLayout.setHorizontalGroup(
panelTLLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
panelTLLayout.setVerticalGroup(
panelTLLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
innerPanel.add(panelTL, gridBagConstraints);
panelTR.setPreferredSize(new java.awt.Dimension(0, 0));
javax.swing.GroupLayout panelTRLayout = new javax.swing.GroupLayout(panelTR);
panelTR.setLayout(panelTRLayout);
panelTRLayout.setHorizontalGroup(
panelTRLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
panelTRLayout.setVerticalGroup(
panelTRLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
innerPanel.add(panelTR, gridBagConstraints);
panelBL.setPreferredSize(new java.awt.Dimension(0, 0));
javax.swing.GroupLayout panelBLLayout = new javax.swing.GroupLayout(panelBL);
panelBL.setLayout(panelBLLayout);
panelBLLayout.setHorizontalGroup(
panelBLLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
panelBLLayout.setVerticalGroup(
panelBLLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
innerPanel.add(panelBL, gridBagConstraints);
panelBR.setBackground(new java.awt.Color(204, 0, 51));
panelBR.setPreferredSize(new java.awt.Dimension(0, 0));
javax.swing.GroupLayout panelBRLayout = new javax.swing.GroupLayout(panelBR);
panelBR.setLayout(panelBRLayout);
panelBRLayout.setHorizontalGroup(
panelBRLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
panelBRLayout.setVerticalGroup(
panelBRLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
innerPanel.add(panelBR, gridBagConstraints);
scrollPanel.setViewportView(innerPanel);
jPanel1.setBackground(new java.awt.Color(204, 153, 0));
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 200, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 200, Short.MAX_VALUE)
);
jPanel2.setBackground(new java.awt.Color(255, 255, 0));
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 200, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 200, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(scrollPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(109, 109, 109)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 90, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(78, 78, 78))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(scrollPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(35, 35, 35))
);
pack();
}// </editor-fold>
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Transistor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Transistor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Transistor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Transistor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Transistor().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel innerPanel;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel panelBL;
private javax.swing.JPanel panelBR;
private javax.swing.JPanel panelTL;
private javax.swing.JPanel panelTR;
private javax.swing.JScrollPane scrollPanel;
// End of variables declaration
}
Yes, I know the class extends JFrame, I'm just using that for testing purposes. (I'm also not sure how to fix the indenting on this page)
Components in Swing are generally under the control of a layout manager. To achieve what you are trying to do, you would actually be better of creating an animated layout manager.
I suggest you take a look at...
The Universal Tween Engine
Sliding layout
You could also take a look at Animations when using Gridbag Layout. (shameless plug)

Adding JCheckBox Array to JPanel in Netbeans

I've been trying to add an array of JCheckBoxes to a JPanel in Netbeans that has been automatically generated by the Netbeans GUI, when I create an array of the JCheckBoxes and then try to add them to the JPanel using JPanel.add(jCheckboxArray[x]) , the program compiles ok but they don't appear.
I've created an array like so:
// custom private member variables
private javax.swing.JCheckBox[] jCheckboxArray;
//
int CheckBoxNumber = 5;
jCheckboxArray = new javax.swing.JCheckBox[CheckBoxNumber];
for(int x = 0; x < CheckBoxNumber ; x++) {
jCheckboxArray[x] = new javax.swing.JCheckBox();
jCheckboxArray[x].setText("CheckBox " + x);
}
I've tried setting viable but no result, and all the examples online appear to make a reference to adding the JPanel to a JFrame however no JFrame exists in the auto-generated code probably because the auto generated class extends javax.swing.JFrame.
Any help greatly appreciated.
Here is the entire code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package my.ArduinoGUI;
/**
*
* #author EEE
*/
public class ArduinoGUI extends javax.swing.JFrame {
/**
* Creates new form ArduinoGUI
*/
public ArduinoGUI() {
initCustomComponents();
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
jPanel1 = new javax.swing.JPanel();
jCheckBox1 = new javax.swing.JCheckBox();
jCheckBox2 = new javax.swing.JCheckBox();
jCheckBox3 = new javax.swing.JCheckBox();
jCheckBox4 = new javax.swing.JCheckBox();
jCheckBox5 = new javax.swing.JCheckBox();
jPanel2 = new javax.swing.JPanel();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jPanel3 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Outputs"));
jPanel1.setName("Outputs"); // NOI18N
jCheckBox1.setText("jCheckBox1");
jCheckBox2.setText("jCheckBox2");
jCheckBox3.setText("jCheckBox3");
jCheckBox4.setText("jCheckBox4");
jCheckBox5.setText("jCheckBox5");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jCheckBox1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jCheckBox5))
.addComponent(jCheckBox2)
.addComponent(jCheckBox3)
.addComponent(jCheckBox4))
.addContainerGap(39, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jCheckBox1)
.addComponent(jCheckBox5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jCheckBox2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jCheckBox3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jCheckBox4)
.addGap(0, 14, Short.MAX_VALUE))
);
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("PinState"));
buttonGroup1.add(jRadioButton1);
jRadioButton1.setText("Input");
jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton1ActionPerformed(evt);
}
});
buttonGroup1.add(jRadioButton2);
jRadioButton2.setText("Output");
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(8, 8, 8)
.addComponent(jRadioButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jRadioButton2)
.addContainerGap(67, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(16, 16, 16)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jRadioButton1)
.addComponent(jRadioButton2))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Generated Boxes"));
int CheckBoxNumber = 5;
jCheckboxArray = new javax.swing.JCheckBox[CheckBoxNumber];
for(int x = 0; x < CheckBoxNumber ; x++) {
jCheckboxArray[x] = new javax.swing.JCheckBox();
System.out.print(x);
System.out.print(jCheckboxArray[x].getText());
jCheckboxArray[x].setText("CheckBox " + x);
System.out.print(jCheckboxArray[x].getText());
jPanel3.add(jCheckboxArray[x]);
}
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 175, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
pack();
}// </editor-fold>
private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ArduinoGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ArduinoGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ArduinoGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ArduinoGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ArduinoGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JCheckBox jCheckBox1;
private javax.swing.JCheckBox jCheckBox2;
private javax.swing.JCheckBox jCheckBox3;
private javax.swing.JCheckBox jCheckBox4;
private javax.swing.JCheckBox jCheckBox5;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
// End of variables declaration
private javax.swing.JCheckBox[] jCheckboxArray;
private javax.swing.JPanel jPanel4;
private javax.swing.JCheckBox jCheckBox6;
private void initCustomComponents() {
//throw new UnsupportedOperationException("Not yet implemented");
// create checkbox array
}
}
I have given the example
public static void main(String args[]) {
String title = (args.length == 0 ? "CheckBox Sample" : args[0]);
JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(0, 1));
Border border = BorderFactory.createTitledBorder("Test");
panel.setBorder(border);
javax.swing.JCheckBox[] jCheckboxArray;
//
int CheckBoxNumber = 5;
jCheckboxArray = new javax.swing.JCheckBox[CheckBoxNumber];
for(int x = 0; x < CheckBoxNumber ; x++) {
jCheckboxArray[x] = new javax.swing.JCheckBox();
jCheckboxArray[x].setText("CheckBox " + x);
panel.add(jCheckboxArray[x]);
}
Container contentPane = frame.getContentPane();
contentPane.add(panel, BorderLayout.CENTER);
// contentPane.add(button, BorderLayout.SOUTH);
frame.setSize(300, 200);
frame.setVisible(true);
}

Allow user handwriten marking on images - Java, Swing

I am developing an application using Java, Swing. It will be mainly used in order to study oscillograms against pentagrams for medical purposes. It is wished that the user can keep notes on top of the images (presumably something like the pencil used in Window's paint).
Please, keep in mind that every time the user loads an image, the following mouse motion listener gets attached to it (in order to make the image draggable for usability reasons):
public class DragMouseListener implements MouseMotionListener {
JLabel jl;
int imageX, imageY;
public DragMouseListener(JLabel jlabel) {
this.jl = jlabel;
jl.addMouseMotionListener(this);
}
#Override
public void mouseDragged(MouseEvent e) {
updateImagePosition(e);
}
#Override
public void mouseMoved(MouseEvent e) {
}
private void updateImagePosition(MouseEvent e) {
imageX = e.getX();
imageY = e.getY();
jl.setLocation(imageX, imageY);
}
}
Until now I am unable to even come close to a way of implementing it and I cannot find any references of the issue in the Internet. Any help (even rough ideas) would be much appreciated. Thanks in advance.
Nick
I tried what you asked for DragMouseListener but found it very uncomfortable so used JInternal Frame it works fine(but you can still drag images inside JInternal Frame,as I implemented what you asked for).Insted of hand free writing,I used JEditorPane(you can add hand written thing if you want,but it will be a mess) with accessible parent JScrollPane so that you can type how much you want.
You can drag JInternal Frame easily:
Note: I have used NetBeans GUI Builder for this and suggest you too to use any GUI builder as it makes
typing work easy.
Here is the code:
import java.awt.event.MouseEvent;
public class Move extends javax.swing.JFrame {
public Move() {
initComponents();
}
#SuppressWarnings("unchecked")
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jInternalFrame1 = new javax.swing.JInternalFrame();
jLabel1 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jEditorPane1 = new javax.swing.JEditorPane();
jInternalFrame2 = new javax.swing.JInternalFrame();
jLabel2 = new javax.swing.JLabel();
jScrollPane2 = new javax.swing.JScrollPane();
jEditorPane2 = new javax.swing.JEditorPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(0, 0, 0));
jInternalFrame1.setVisible(true);
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images.jpg"))); // NOI18N
jLabel1.setText("jLabel1");
jLabel1.addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentMoved(java.awt.event.ComponentEvent evt) {
jLabel1ComponentMoved(evt);
}
});
jLabel1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent evt) {
jLabel1MouseDragged(evt);
}
});
jScrollPane1.setViewportView(jEditorPane1);
javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
jInternalFrame1Layout.setHorizontalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1, 0, 0, Short.MAX_VALUE))
.addContainerGap())
);
jInternalFrame1Layout.setVerticalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
.addContainerGap())
);
jInternalFrame2.setVisible(true);
jLabel2.setIcon(new javax.swing.ImageIcon("E:\\untitled.png")); // NOI18N
jLabel2.setText("jLabel2");
jScrollPane2.setViewportView(jEditorPane2);
javax.swing.GroupLayout jInternalFrame2Layout = new javax.swing.GroupLayout(jInternalFrame2.getContentPane());
jInternalFrame2.getContentPane().setLayout(jInternalFrame2Layout);
jInternalFrame2Layout.setHorizontalGroup(
jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 222, Short.MAX_VALUE))
.addContainerGap())
);
jInternalFrame2Layout.setVerticalGroup(
jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel2)
.addContainerGap(40, Short.MAX_VALUE))
);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jInternalFrame1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(177, 177, 177)
.addComponent(jInternalFrame2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(71, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGap(49, 49, 49)
.addComponent(jInternalFrame1)
.addGap(209, 209, 209))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(31, 31, 31)
.addComponent(jInternalFrame2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(166, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}
private void jLabel1ComponentMoved(java.awt.event.ComponentEvent evt) {
}
private void jLabel1MouseDragged(java.awt.event.MouseEvent evt) {
updateImagePosition(evt);
}
private void updateImagePosition(MouseEvent evt) {
int imageX = evt.getX();
int imageY = evt.getY();
jLabel1.setLocation(imageX, imageY);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Move().setVisible(true);
}
});
}
private javax.swing.JEditorPane jEditorPane1;
private javax.swing.JEditorPane jEditorPane2;
private javax.swing.JInternalFrame jInternalFrame1;
private javax.swing.JInternalFrame jInternalFrame2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
}
UPDATE :
See This post answerd by #mKorbel and #trashgod to make JInternal frame minimize/maximize and resize.Great answers.

Categories

Resources