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.
Related
I am working on an application that has to show a list of elements belonging to text documents (Notes).
If from the SWING editor of NetBeans itself the elements are duplicated, everything works correctly. The problem comes when I do it from code by manually adding the elements with the function (add ()).
I need to make a scrollable list which is composed of panels that include their own elements.
package notas.forms;
public class MainWindow extends javax.swing.JFrame
{
public MainWindow() {
initComponents();
this.setResizable(false);
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel3 = new javax.swing.JPanel();
jPanel1 = new javax.swing.JPanel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jPanel2 = new javax.swing.JPanel();
jPanel4 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(255, 255, 255));
jPanel3.setBackground(new java.awt.Color(255, 255, 255));
jPanel1.setBackground(new java.awt.Color(255, 255, 255));
jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)));
jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/grap.png"))); // NOI18N
jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/add.png"))); // NOI18N
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(jLabel5)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap(149, Short.MAX_VALUE)
.addComponent(jLabel4)
.addGap(108, 108, 108))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(197, 197, 197)
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 167, Short.MAX_VALUE)
.addComponent(jLabel5)
.addContainerGap())
);
jScrollPane1.setBackground(new java.awt.Color(255, 255, 255));
jScrollPane1.setName("listaNotas"); // NOI18N
jPanel2.setBackground(new java.awt.Color(255, 255, 255));
jPanel4.setBackground(new java.awt.Color(255, 255, 255));
jPanel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)));
jPanel4.setEnabled(false);
jLabel2.setText("TITULO DE LA PUTA NOTA TENSANTE Y ASQUEROSA");
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/trash.png"))); // NOI18N
jLabel3.setForeground(new java.awt.Color(153, 153, 153));
jLabel3.setText("3/12/2017");
javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
jPanel4.setLayout(jPanel4Layout);
jPanel4Layout.setHorizontalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(jPanel4Layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1)))
.addContainerGap())
);
jPanel4Layout.setVerticalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jLabel1))
.addGroup(jPanel4Layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
.addComponent(jLabel3)))
.addContainerGap())
);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(95, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(1343, Short.MAX_VALUE))
);
jScrollPane1.setViewportView(jPanel2);
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 384, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, 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(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
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(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainWindow.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 MainWindow().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
}
If from the SWING editor of NetBeans itself the elements are duplicated, everything works correctly. The problem comes when I do it from code by manually adding the elements with the function (add ()).
The problem is NetBeans generates layout code using the GroupLayout. GroupLayout is complicated and requires multiple constraints to be supplied when you add components to the panel.
So when you use the add(...) method you need to provide the same constraints that NetBeans does.
However, the simpler solution is to not use NetBeans to generate your GUI code. Instead you can take control of the layout management and make your code simpler and easier to maintain.
For example you can do something like:
Box children = Box.createVerticalBox();
JPanel wrapper = new JPanel( new BorderLayout() );
wrapper.add(children, BorderLayout.Page_Start);
JScrollPane scrollPane = new JScrollPane( wrapper );
frame.add(scrollPane, BorderLayout.CENTER)'
Now whenever you want to add a new panel to the scroll pane you simply do:
children.add( a new child panel );
children.revalidate();
The above code replaces all the GroupLayout code generated by the IDE.
Another option might be to use a GridBagLayout. It allows you to add components to cells on the panel.
Take the time to learn how to use layout managers. Start by reading the Swing tutorial on Layout Managers.
i'm currently working on a java desktop project , oddly enough whenever i hit the run button , the Jpanel doesn't show up. I declared this as main but still it doesn't works. Any ideas ?
Here is the code for my Jpanel form.
Thanks in advance!.
package consumidor;
public class ventana extends javax.swing.JPanel {
public ventana() {
initComponents();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ventana().setVisible(true);
}
});
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jInternalFrame1 = new javax.swing.JInternalFrame();
jPanel2 = new javax.swing.JPanel();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
buscarProductoTxt = new javax.swing.JTextField();
btnBuscarProducto = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
buscarProductoArea = new javax.swing.JTextArea();
jInternalFrame1.setVisible(true);
javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
jInternalFrame1Layout.setHorizontalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jInternalFrame1Layout.setVerticalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel1.setText("Buscar producto por rut");
jLabel2.setText("Ingresar rut del proveedor");
buscarProductoTxt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buscarProductoTxtActionPerformed(evt);
}
});
btnBuscarProducto.setText("Consultar");
btnBuscarProducto.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnBuscarProductoActionPerformed(evt);
}
});
buscarProductoArea.setColumns(20);
buscarProductoArea.setRows(5);
jScrollPane1.setViewportView(buscarProductoArea);
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, false)
.addComponent(jLabel1)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(buscarProductoTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(btnBuscarProducto))
.addComponent(jScrollPane1))
.addContainerGap(24, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(buscarProductoTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnBuscarProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 56, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 324, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
}// </editor-fold>
private void buscarProductoTxtActionPerformed(java.awt.event.ActionEvent evt) {
}
private void btnBuscarProductoActionPerformed(java.awt.event.ActionEvent evt) {
String producto=buscarProductoTxt.getText();
buscarProductoRut(producto);
buscarProductoArea.setText("Rut proveedor: "+mostrarRut()+", Descripcion"
+ ": "+mostrarDecripcion());
}
// Variables declaration - do not modify
private javax.swing.JButton btnBuscarProducto;
private javax.swing.JTextArea buscarProductoArea;
private javax.swing.JTextField buscarProductoTxt;
private javax.swing.JInternalFrame jInternalFrame1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
private static boolean buscarProductoRut(java.lang.String arg0) {
webservice.ProcesoProductos_Service service = new webservice.ProcesoProductos_Service();
webservice.ProcesoProductos port = service.getProcesoProductosPort();
return port.buscarProductoRut(arg0);
}
private static String mostrarRut() {
webservice.ProcesoProductos_Service service = new webservice.ProcesoProductos_Service();
webservice.ProcesoProductos port = service.getProcesoProductosPort();
return port.mostrarRut();
}
private static String mostrarDecripcion() {
webservice.ProcesoProductos_Service service = new webservice.ProcesoProductos_Service();
webservice.ProcesoProductos port = service.getProcesoProductosPort();
return port.mostrarDecripcion();
}
}
Nothing "odd" about this. Your ventana class is a JPanel, and yes, you create it and set it visible, but a JPanel by itself can not display, and to in fact have it show, it needs to be part of a visible "top-level window" such as a JFrame or JDialog. So if you want to show this JPanel in its own separate dialog window, put it into a JDialog, pack() the dialog, and then set the dialog visible. Or the quick and dirty way to do this is to put the ventana JPanel into a JOptionPane. e.g.,
JOptionPane.showMessageDialog(null, new ventana());
A JOptionPane will then create and display a modal JDialog that will display your ventana object.
Note that the ventana class name should begin with an upper-case letter, like all Java class names should.
I'm using a JPanel on a JFrame and I have a TitledBorder on the JPanel. I create the JPanel with the titled border in NetBeans and I can see my title and the etched frame. When I run, I only see the title but not the etched frame. Any idea why I can't see the etched frame? I've tried setting the look and feel but can't get the frame to show up and it looks odd having the title with no border.
Running Java 1.7 on Windows 7. I don't recall this happening when we were using java 1.6.
Here's sample code:
package javaapplication1;
public class NewJFrame extends javax.swing.JFrame {
/** Creates new form NewJFrame */
public NewJFrame() {
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() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder()));
jLabel1.setText("This is just an etched frame");
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(jLabel1)
.addContainerGap(234, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addContainerGap(80, Short.MAX_VALUE))
);
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("My Titled Border"));
jTextField1.setText("Not on the titled border");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jLabel2.setText("Where is the frame:");
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(61, 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(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, 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(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
This is not an answer yet
This is what I get in 1.7:
Is it not what you want?
Edit:
Try running this:
public class NewJFrame extends JFrame {
public NewJFrame() {
JPanel p = new JPanel();
p.add(new JLabel("AAAAA"));
p.setBorder(BorderFactory.createTitledBorder("Title"));
add(p);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String args[]) {
new NewJFrame();
}
}
It's very minimal and should narrow the options. This is what I get:
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.
I'm doing a simple Swing layout (see image) but for some reason no matter what I try I cannot change the colors of the 'borders' that split the three sections of the UI. Am I missing something?
The layout is done using GroupLayout.
EDIT:
The green portions are each individual JPanels contained within a GroupLayout.
I have tried background, and foreground colors, on any objects where it would make sense. I have tried applying EmptyBorder to each of the jpanels and still no luck. I think it might have to do with the layout but I'm not even sure about that.
Here is the relevent layout code. Note that this was all auto-generated by Netbeans:
javax.swing.GroupLayout pnlTopLayout = new javax.swing.GroupLayout(pnlTop);
pnlTop.setLayout(pnlTopLayout);
pnlTopLayout.setHorizontalGroup(
pnlTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 532, Short.MAX_VALUE)
);
pnlTopLayout.setVerticalGroup(
pnlTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 59, Short.MAX_VALUE)
);
javax.swing.GroupLayout pnlSideLayout = new javax.swing.GroupLayout(pnlSide);
pnlSide.setLayout(pnlSideLayout);
pnlSideLayout.setHorizontalGroup(
pnlSideLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 200, Short.MAX_VALUE)
);
pnlSideLayout.setVerticalGroup(
pnlSideLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 214, Short.MAX_VALUE)
);
javax.swing.GroupLayout pnlMainLayout = new javax.swing.GroupLayout(pnlMain);
pnlMain.setLayout(pnlMainLayout);
pnlMainLayout.setHorizontalGroup(
pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 326, Short.MAX_VALUE)
);
pnlMainLayout.setVerticalGroup(
pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 214, 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(pnlSide, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(pnlMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(pnlTop, javax.swing.GroupLayout.DEFAULT_SIZE, 532, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(pnlTop, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(pnlMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(pnlSide, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
);
You should be setting the background color of either the container's content pane, or the JComponent that overlays it (e.g. JPanel).
public final class Demo {
private static BufferedImage bi;
public static void main(String[] args){
try {
loadImage();
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run() {
createAndShowGUI();
}
});
} catch (IOException e) {
// handle exception
}
}
private static void loadImage() throws IOException{
bi = ImageIO.read(Demo.class.getResource("../resources/whitefro1.jpg"));
}
private static void createAndShowGUI(){
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel = new JPanel();
panel.setBackground(Color.BLUE.darker()); // here
final JLabel labelOne = new JLabel();
labelOne.setIcon(new ImageIcon(bi));
final JLabel labelTwo = new JLabel();
labelTwo.setIcon(new ImageIcon(bi));
panel.add(labelOne);
panel.add(labelTwo);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
As you can see, the area between is colored a darker shade of blue.