JInternalFrame Opening and Focus - java

I've got a couple JInternalFrames inside a DesktopPane, and I've noticed something with the first time they open.
When I first open one, and then open a second one, the first JInternalFrame will be on top of the second. I want them to open on top of each other, but this only happens the first time. I realize now that it is because of the order that they are added. But my program will be retrieving information right at the desktoppane load so I need the JIF variables to be added before the windows are actually opened. So how can I make sure the window that is opened comes out on top regardless of the order they are added?
I would prefer not to use a dialogbox.

Try this code...
I've create a sample application for you to understand...
public class NewJFrame extends javax.swing.JFrame {
private javax.swing.JButton jButton1;
private javax.swing.JDesktopPane jDesktopPane1;
//This is a Internal Frame I have created
private PaymentInternalFrame payIF;
public NewJFrame() {
initComponents();
}
private void initComponents() {
jButton1 = new javax.swing.JButton();
jDesktopPane1 = new javax.swing.JDesktopPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Click");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout jDesktopPane1Layout = new javax.swing.GroupLayout(jDesktopPane1);
jDesktopPane1.setLayout(jDesktopPane1Layout);
jDesktopPane1Layout.setHorizontalGroup(
jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jDesktopPane1Layout.setVerticalGroup(
jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 412, 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(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 529, Short.MAX_VALUE)
.addComponent(jDesktopPane1)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jDesktopPane1))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
if(payIF==null||payIF.isClosed()){
payIF = new PaymentInternalFrame();
mainDesktop.add(payIF);
payIF.setVisible(true);
payIF.setMaximum(true);
payIF.isSelected();
}else{
payIF.setSelected(true);
}
} catch (PropertyVetoException e) {
NotificationMessage.errorMessage(this, e);
}
}
//Main Method...
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
}

Related

How to read Text Field value from another file through a Dialog?

I have two files in my package: GUI.java and Dialog.java
The GUI has two Text Fields and a Button. When I press that Button, the Dialog becomes visible. The Dialog has another button which saves the Text Fields' values to an .ini file. And here comes the problem, it doesn't save the updated values, but the ones that were there when the program first started.
After some searches on Google I found out that I need to use something called "Action Listener", but I just can't seem to get it right at all.
GUI.java:
import java.io.File;
import java.io.IOException;
import org.ini4j.Wini;
public class GUI extends javax.swing.JFrame {
public GUI(){
initComponents();
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
textField1 = new javax.swing.JTextField();
textField2 = new javax.swing.JTextField();
openDialog = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
textField1.setText("1");
textField2.setText("2");
openDialog.setText("Open Dialog");
openDialog.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
openDialogActionPerformed(evt);
}
});
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)
.addComponent(openDialog, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(textField2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(textField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(openDialog)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void openDialogActionPerformed(java.awt.event.ActionEvent evt) {
Dialog dialog = new Dialog(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
#Override
public void windowClosing(java.awt.event.WindowEvent e) {
dialog.setVisible(false);
}
});
dialog.setVisible(true);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new GUI().setVisible(true);
}
});
}
void save(int i){
try {
Wini ini = new Wini(new File("C:/Users/Paul/Desktop/Configs.ini"));
int firstValue = Integer.parseInt(textField1.getText());
int secondValue = Integer.parseInt(textField2.getText());
ini.put("Configuration " + i, "First Value", firstValue);
ini.put("Configuration " + i, "Second Value", secondValue);
ini.store();
}
catch (IOException ex) {}
}
private javax.swing.JButton openDialog;
private javax.swing.JTextField textField1;
private javax.swing.JTextField textField2;
}
Dialog.java:
public class Dialog extends javax.swing.JDialog {
GUI GUI = new GUI();
public Dialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
save = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
save.setText("Save");
save.addActionListener(new java.awt.event.ActionListener() {
#Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveActionPerformed(evt);
}
});
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()
.addComponent(save)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(save)
.addContainerGap())
);
pack();
}// </editor-fold>
private void saveActionPerformed(java.awt.event.ActionEvent evt) {
GUI.save(1);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
Dialog dialog = new Dialog(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
#Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
private javax.swing.JButton save;
}
Dialog should not be creating a new instance of GUI, there is no relationship between the instance which is been displayed on the screen and that which Dialog is using.
Instead, GUI should pass a reference of itself to Dialog
public class Dialog extends javax.swing.JDialog {
private GUI gui;
public Dialog(GUI parent) {
super(parent, true);
this.gui = gui;
initComponents();
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
save = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
save.setText("Save");
save.addActionListener(new java.awt.event.ActionListener() {
#Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveActionPerformed(evt);
}
});
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()
.addComponent(save)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(save)
.addContainerGap())
);
pack();
}// </editor-fold>
private void saveActionPerformed(java.awt.event.ActionEvent evt) {
gui.save(1);
}
private javax.swing.JButton save;
}
This is pretty basic Java and something you should be familiar with before embarking on something as complicated as GUI development. See Passing Information to a Method or a Constructor for more details
And your GUI class might look something like...
public class GUI extends javax.swing.JFrame {
public GUI() {
initComponents();
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
textField1 = new javax.swing.JTextField();
textField2 = new javax.swing.JTextField();
openDialog = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
textField1.setText("1");
textField2.setText("2");
openDialog.setText("Open Dialog");
openDialog.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
openDialogActionPerformed(evt);
}
});
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)
.addComponent(openDialog, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(textField2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(textField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(openDialog)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void openDialogActionPerformed(java.awt.event.ActionEvent evt) {
Dialog dialog = new Dialog(this);
dialog.setVisible(true);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new GUI().setVisible(true);
}
});
}
void save(int i) {
// Code I can't execute
// Also, don't ignore the exception, you should probably
// rethrow it...
}
private javax.swing.JButton openDialog;
private javax.swing.JTextField textField1;
private javax.swing.JTextField textField2;
}
Although, I'd argue that, if you want GUI to save the data, it should be inspecting the result from the dialog, rather the exposing itself to possible mis-treatment (removeAll anyone?). In that case, I'd consider using a JOptionPane instead
See How to Make Dialogs for more details.
Also, the sooner you ditch the form editor, the better you will become
I will look forward and see if I can manage to make it work through JOptionPane
private void openDialogActionPerformed(java.awt.event.ActionEvent evt) {
int response = JOptionPane.showOptionDialog(this, "Do you wish to save", "Save", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
if (response == JOptionPane.YES_OPTION) {
save(1);
}
}

Swing JButton is not rendering properly on Solaris with Java 8

I've connected to Solaris 11 from my windows machine. I've set DISPLAY to my machine. And I'm using Java 8.
Note: This worked fine when using Java 6.
When I'm launching dialog then its button and other swing components are not getting rendered.
Observed that it works on
o Windows 7 Enterprise
o Windows Server 2012 Enterprise
I tried changing the L&F but that didn't work. When I used "GTKLookAndFeel" then buttons appeared but without text/label.
Any help is greatly appreciated. Kindly let me know if any elaboration needed. Thanks.
Code for my dialog is as follows
package com.ui;
import javax.swing.SwingUtilities;
public class SimpleDialog extends java.awt.Dialog {
private static final long serialVersionUID = -8298472889742780386L;
public SimpleDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
btnSkip = new javax.swing.JButton();
btnRetry = new javax.swing.JButton();
btnAbort = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
lblMessage = new javax.swing.JTextArea();
btnViewLog = new javax.swing.JButton();
setLocationRelativeTo(null);
setMinimumSize(new java.awt.Dimension(600, 200));
setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
setName("Form"); // NOI18N
setResizable(false);
setTitle("Simple Dialog");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
closeDialog(evt);
}
});
btnSkip.setText("Skip this Step"); // NOI18N
btnSkip.setName("btnSkip"); // NOI18N
btnSkip.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSkip_Click(evt);
}
});
btnRetry.setText("Retry"); // NOI18N
btnRetry.setName("btnRetry"); // NOI18N
btnRetry.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnRetry_Click(evt);
}
});
btnAbort.setText("Abort"); // NOI18N
btnAbort.setName("btnAbort"); // NOI18N
btnAbort.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAbort_Click(evt);
}
});
jScrollPane1.setName("jScrollPane1"); // NOI18N
lblMessage.setColumns(20);
lblMessage.setEditable(false); // NOI18N
lblMessage.setLineWrap(true);
lblMessage.setRows(5);
lblMessage.setName("lblMessage"); // NOI18N
jScrollPane1.setViewportView(lblMessage);
btnViewLog.setText("View log"); // NOI18N
btnViewLog.setName("btnViewLog"); // NOI18N
btnViewLog.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
// open some log file
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.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(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 580, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(btnSkip)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnRetry)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 87, Short.MAX_VALUE)
.addComponent(btnViewLog)
.addGap(79, 79, 79)
.addComponent(btnAbort)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnSkip)
.addComponent(btnAbort)
.addComponent(btnRetry)
.addComponent(btnViewLog))
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
/** Closes the dialog */
private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
setVisible(false);
dispose();
}//GEN-LAST:event_closeDialog
private void btnAbort_Click(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAbort_Click
this.setVisible(false);
}//GEN-LAST:event_btnAbort_Click
private void btnRetry_Click(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRetry_Click
this.setVisible(false);
}//GEN-LAST:event_btnRetry_Click
private void btnSkip_Click(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSkip_Click
this.setVisible(false);
}//GEN-LAST:event_btnSkip_Click
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
SimpleDialog dialog = new SimpleDialog(new java.awt.Frame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btnAbort;
private javax.swing.JButton btnRetry;
private javax.swing.JButton btnSkip;
private javax.swing.JButton btnViewLog;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea lblMessage;
// End of variables declaration//GEN-END:variables
}
To apply look and feel you need to provide two commands:
UIManager.setLookAndFeel(lnf_name);
SwingUtilities.updateComponentTreeUI(root_obj_or_specific_one);
I had a similar problem and 2nd line solved the problem.
Try this
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Let me know if it works or not. :)

Changing the size of a jFrame on button click

I'm creating a "Presentation" on a jFrame as part of a project and need some help.
I have it starting as a 200, 200 size frame with a button in the middle that says "Let's Start"
then when you click on it, the size of the frame changes. When I click on the button you have to drag the window to make it the already defined size seen as the window stays the original 200 by 200 it doesn't open the window to 1335 by 675.
Here's my code.
package randomGUIs;
import java.awt.Color;
import java.awt.Dimension;
public class Presentation extends javax.swing.JFrame {
public Presentation() {
initComponents();
startPanel.setBackground(Color.blue);
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
startPanel = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Let's Start");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout startPanelLayout = new javax.swing.GroupLayout(startPanel);
startPanel.setLayout(startPanelLayout);
startPanelLayout.setHorizontalGroup(
startPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(startPanelLayout.createSequentialGroup()
.addGap(50, 50, 50)
.addComponent(jButton1)
.addContainerGap(49, Short.MAX_VALUE))
);
startPanelLayout.setVerticalGroup(
startPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(startPanelLayout.createSequentialGroup()
.addGap(78, 78, 78)
.addComponent(jButton1)
.addContainerGap(77, 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()
.addComponent(startPanel, 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(startPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
startPanel.setSize(new Dimension(1335, 675));
startPanel.setMaximumSize(new Dimension(1335, 675));
startPanel.setMinimumSize(new Dimension(1335, 675));
jButton1.setVisible(false);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Presentation().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JPanel startPanel;
}
I've tried using startPanel.repaint(); with no luck.
Simply set the frame size with this function.
In order to use this function you must include the relevant package.
import java.awt.Dimension;
...
setSize(new Dimension(1320,220));
Your JFrame won't automagically resize itself unless you tell it to. Something like this:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
startPanel.setSize(new Dimension(1335, 675));
startPanel.setMaximumSize(new Dimension(1335, 675));
startPanel.setMinimumSize(new Dimension(1335, 675));
frame.setSize(1335, 675);
jButton1.setVisible(false);
}
You could also use the pack() function of the JFrame class, depending on what you wanted to do.

Close an Opened JInternalFrame that is been opened within another JInternalFrame while changing the tab of a JTabbedPane

I've created and application in which there are two tabs titled Tabbedpane 1 and Tabbedpane 2. In one tab body Tabbedpane 1 contains a JInternalFrame in which there is a search button. On clicking the button another JInternalFrame opens within the main JInternalFrame.
Can anyone please tell me how to close the said opened JInternalFrame ie the search JInternalFrame while clicking on to the second tab titled Tabbedpane 2
I've tried .setClosed(true) and .setVisible(true) on property change still it does'nt works for me.
Use a ChangeListener which will get notified when JTabbedPane state changes (i.e tabs switched):
final JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
//tab has been changed
}
});
The problem is we will need a way to monitor the previous tab i.e the tab we were on before it was changed this can be done via:
tabbedPane.addChangeListener(new ChangeListener() {
int prev_index = 0;
int curr_index = 0;
public void stateChanged(ChangeEvent e) {
prev_index = curr_index;
curr_index = tabbedPane.getSelectedIndex();
System.out.println("Tab (Current): " + curr_index);
System.out.println("Tab (Previous): " + prev_index);
}
});
UPDATE 1:
To close JInternalFrame I'd suggest calling dispose() on its instance
UPDATE 2:
Here is your fixed code, basically added a getter for Search class, thus JIFrame1 has a getSearch() method which allows us to gaim access to the Search classes current instance created in JIFrame1, in changedState(..) I call jiFrame1.getSearch().dispose() which will make sure we dispose of the instance that has already been created:
import java.awt.Container;
import java.awt.event.MouseListener;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Sample extends javax.swing.JFrame {
JIFrame1 jiframe1 = new JIFrame1();
public Sample() {
initComponents();
Container jiframe1cont = tab1;
for (MouseListener listener : ((javax.swing.plaf.basic.BasicInternalFrameUI) jiframe1.getUI()).getNorthPane().getMouseListeners()) {
((javax.swing.plaf.basic.BasicInternalFrameUI) jiframe1.getUI()).getNorthPane().removeMouseListener(listener);
}
jiframe1.setLocation(10, 10);
jiframe1cont.add(jiframe1);
jiframe1.setVisible(true);
JIFrame2 jiframe2 = new JIFrame2();
Container jiframe2cont = tab2;
for (MouseListener listener : ((javax.swing.plaf.basic.BasicInternalFrameUI) jiframe2.getUI()).getNorthPane().getMouseListeners()) {
((javax.swing.plaf.basic.BasicInternalFrameUI) jiframe2.getUI()).getNorthPane().removeMouseListener(listener);
}
jiframe2.setLocation(10, 10);
jiframe2cont.add(jiframe2);
jiframe2.setVisible(true);
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
tab1 = new javax.swing.JDesktopPane();
tab2 = new javax.swing.JDesktopPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
tabbedPane.setTabPlacement(javax.swing.JTabbedPane.LEFT);
tabbedPane.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
tabbedPaneStateChanged(evt);
}
});
tab1.setBackground(new java.awt.Color(240, 240, 240));
tabbedPane.addTab("Tabbedpane 1", tab1);
tab2.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.background"));
tabbedPane.addTab("Tabbedpane 2", tab2);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 731, Short.MAX_VALUE));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 503, Short.MAX_VALUE));
pack();
}// </editor-fold>
private void tabbedPaneStateChanged(javax.swing.event.ChangeEvent evt) {
try {
jiframe1.getSearch().dispose();
} catch (Exception ex) {
Logger.getLogger(Sample.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Sample().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JDesktopPane tab1;
private javax.swing.JDesktopPane tab2;
private final javax.swing.JTabbedPane tabbedPane = new javax.swing.JTabbedPane();
// End of variables declaration
}
class Search extends javax.swing.JInternalFrame {
public Search() {
initComponents();
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jButton1 = new javax.swing.JButton();
jTextField1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object[][]{
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String[]{
"Title 1", "Title 2", "Title 3", "Title 4"
}));
jScrollPane1.setViewportView(jTable1);
jButton1.setText("CLOSE");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
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)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(205, Short.MAX_VALUE))))
.addGroup(layout.createSequentialGroup()
.addGap(158, 158, 158)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(172, Short.MAX_VALUE)));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(22, 22, 22)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
class JIFrame1 extends javax.swing.JInternalFrame {
public JIFrame1() {
initComponents();
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
pane = new javax.swing.JDesktopPane();
jButton1 = new javax.swing.JButton();
pane.setBackground(new java.awt.Color(240, 240, 240));
jButton1.setText("SEARCH");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton1.setBounds(170, 60, 90, 23);
pane.add(jButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(pane, javax.swing.GroupLayout.DEFAULT_SIZE, 567, Short.MAX_VALUE));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(pane, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE));
pack();
}// </editor-fold>
private Search search;
public Search getSearch() {
return search;
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
search = new Search();
Container searchcont = pane;
for (MouseListener listener : ((javax.swing.plaf.basic.BasicInternalFrameUI) search.getUI()).getNorthPane().getMouseListeners()) {
((javax.swing.plaf.basic.BasicInternalFrameUI) search.getUI()).getNorthPane().removeMouseListener(listener);
}
search.setLocation(10, 10);
searchcont.add(search);
search.setVisible(true);
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JDesktopPane pane;
// End of variables declaration
}
class JIFrame2 extends javax.swing.JInternalFrame {
public JIFrame2() {
initComponents();
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel1.setText("JIFrame2..............");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(100, 100, 100)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(137, Short.MAX_VALUE)));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(86, 86, 86)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(145, Short.MAX_VALUE)));
pack();
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
UPDATE 3:
The reason for NPE is here:
private void tabbedPaneStateChanged(javax.swing.event.ChangeEvent evt) {
try {
jiframe1.getSearch().dispose();
} catch (Exception ex) {
Logger.getLogger(Sample.class.getName()).log(Level.SEVERE, null, ex);
}
}
change to this:
private void tabbedPaneStateChanged(javax.swing.event.ChangeEvent evt) {
try {
Search s=jiframe1.getSearch();
if(s!=null)
s.dispose();
} catch (Exception ex) {
Logger.getLogger(Sample.class.getName()).log(Level.SEVERE, null, ex);
}
}

How can I pass variables from a jtextfield to another one in another JFrame/class?

I've got a problem with JTextField. I don't know how to pass a variable from a JTextField (situated in JFrame A) to another JTextField (situated in JFrame B).
I tried to do that, but it doesn't do anything, i.e. it doesn't get any runtime/compilation error nor it receives the text.
I tried to do this in ClassB:
ClassA a = new ClassA();
String text = a.jtextfield1.getText();
but it doesn't work!!
Could you help me, may with a simple example? What did I do wrong?
p.s.: i'm using netbeans
[edit --] That's the code of ClassA:
public class ClassA extends javax.swing.JFrame {
public ClassA() {
initComponents();
}
public void initComponents() {
jTextField1 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextField1.setText("Some text blah blah");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(124, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(47, 47, 47)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(86, Short.MAX_VALUE))
);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ClassA().setVisible(true);
}
});
}
public javax.swing.JTextField jTextField1;
}
Here's the code of ClassB:
public class ClassB extends javax.swing.JFrame {
public ClassB() {
initComponents();
}
public ClassA a = new ClassA();
public void initComponents() {
jTextField1 = new javax.swing.JTextField();
getText = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getText.setText("GetText");
getText.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
getTextActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(31, 31, 31)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(50, 50, 50)
.addComponent(getText)))
.addContainerGap(143, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(50, 50, 50)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(26, 26, 26)
.addComponent(getText)
.addContainerGap(49, Short.MAX_VALUE))
);
pack();
}
public void getTextActionPerformed(java.awt.event.ActionEvent evt) {
a.jTextField1.getText(); //this doesn't work. How can I it makes work?
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ClassB().setVisible(true);
}
});
}
public javax.swing.JButton getText;
public javax.swing.JTextField jTextField1;
}
Thanks in advance.
If you instantiate a new ClassA instance (and, BTW, the syntax is new ClassA()), then you will have... a new instance, with another jtextfield1 than the one in the existing ClassA instance. You need to pass a reference to the existing ClassA instance to the ClassB instance:
When ClassB is constructed:
ClassB theClassB = new ClassB(theClassA);
You don't seem to master the basic OO concepts of classes and objects, and not even the syntax of Java. I would advise not using Swing now, and learning the basics first. Swing is hard, much too hard for a developer which doesn't understand these concepts.
Read the basics and OO lessons of the Java tutorial
You ask for a simple example, you will get one. I leave it up to you how to match this example on your code, which shouldn't be too difficult.
public class PanelWithTextField extends JPanel{
private JTextField textField = new JTextField();
public JTextField getTextField(){ return textField; };
}
public static void main( String[] args ){
EventQueue.invokeLater(){ new Runnable(){
public void run(){
//create a first panel
PanelWithTextField panel = new PanelWithTextField();
panel.getTextField().setText( "Some text" );
//create a second panel
PanelWithTextField anotherPanel = new PanelWithTextField();
//copy the text from the first panel's textfield to the second panel's textfield
anotherPanel.getTextField().setText( panel.getTextField().getText() );
}
}
}
I hope I did not make too much typo's in it, as I did not try to run it nor did I used my IDE to write this code.
Basically you will need a reference to your first panel in your second panel (or in any part of the code where you which to access that textfield).
And as already suggested by others. You should start by making sure you understand the basic OO concepts and basic Java syntax before you start messing around with Swing and UI's

Categories

Resources