Passing text field value into a constructor - java

Ok, I have a program I am doing for class. I have to take the input form the text field, ad throw it into an overloaded constructor that takes a string as an argument. There are a few other things I have to do with constructors as well, so in my code you'll see some stuff that is outside of this question. also, I am not posting my methods because it's a big part of the homework, and those all work fine, (prior to my attempt at passing info to the constructor). What I would like to know is i there is a way to pass the text field info into the constructor, because I have been going at this for a couple hours now, but it's becoming plainly obvious that I don't understand this. If you just want to direct me to somewhere that I can learn how to do this I would appreciate it.
Code:
package lab4;
import java.awt.*;
import java.awt.List;
import javax.swing.*;
import java.awt.event.*;
import java.lang.Iterable.*;
import java.util.*;
public class Lab4 extends JFrame{
private JTextField jTextField1;
private JButton doSome;
private JLabel ansTxt;
private JLabel asciSum;
private JLabel jLabel1;
private JLabel jLabel3;
private JLabel jLabel4;
private JLabel jLabel5;
private JLabel jLabel6;
private JLabel lowCnt;
private JLabel numDigit;
private JLabel upCnt;
private JLabel vowelCnt;
private String userInput = jTextField1.getText();
public Lab4()
{
initComponents();
}
public Lab4(String x)
{
this.userInput = x;
}
public Lab4(char[] x)
{
}
public Lab4(byte[] x)
{
}
private void initComponents() {
jTextField1 = new JTextField();
jLabel1 = new JLabel();
doSome = new JButton();
ansTxt = new JLabel();
jLabel3 = new JLabel();
jLabel4 = new JLabel();
jLabel5 = new JLabel();
jLabel6 = new JLabel();
asciSum = new JLabel();
vowelCnt = new JLabel();
numDigit = new JLabel();
upCnt = new JLabel();
lowCnt = new JLabel();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setAutoRequestFocus(false);
setFont(new java.awt.Font("Comic Sans MS", 1, 12)); // NOI18N
jLabel1.setText("Enter a string Here and I'll show you some info about it!");
doSome.setToolTipText("This Will Do Something");
doSome.setInheritsPopupMenu(true);
doSome.setLabel("Click Me When Your Done!");
doSome.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
doSomeActionPerformed(evt);
}
});
ansTxt.setText("ASCII SUM:");
jLabel3.setText("Number of Vowels:");
jLabel4.setText("Number of Digits:");
jLabel5.setText("Number Of Uppercase:");
jLabel6.setText("Number Of Lowercase:");
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
.addComponent(ansTxt)
.addComponent(jLabel5, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel4, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel6, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(84, 84, 84)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(lowCnt)
.addComponent(vowelCnt)
.addComponent(asciSum)
.addComponent(numDigit)
.addComponent(upCnt)))
.addGroup(layout.createSequentialGroup()
.addGap(95, 95, 95)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField1)
.addComponent(jLabel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(doSome, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap(239, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(34, 34, 34)
.addComponent(jLabel1)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(doSome)
.addGap(34, 34, 34)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(ansTxt)
.addComponent(asciSum))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(vowelCnt))
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(numDigit))
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(upCnt))
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(lowCnt))
.addContainerGap(157, Short.MAX_VALUE))
);
pack();
}
private void doSomeActionPerformed(ActionEvent evt)
{
String takeTfIn = this.jTextField1.getText();
this.asciSum.setText(getAsciiSum(takeTfIn));
this.vowelCnt.setText(getNumVowels(takeTfIn));
this.numDigit.setText(getNumDigits(takeTfIn));
this.upCnt.setText(getNumUpperCase(takeTfIn));
this.lowCnt.setText(getNumLowerCase(takeTfIn));
}
// Excluded methods go here...
public static void main(String[] args)
{
Lab4 test = new Lab4();
test.setVisible(true);
}
}

You'll need to create new instance of lab4 after you fill tex field value and click on submit. on click of submit you can do like this
this.setVisible(false);//Hide current instance
Lab4 test = new Lab4(this.jTextField1.getText()); // create new instance
test.setVisible(true); // set new instance visible
Don't forget to add following constructor:
public Lab4 (String value)
{
this.jTextField1.setText(value); // if you like to set value back to textbox
}

Related

JFrame freezes without reason outside of IDE

I have a Java application program that when I run it with my IDE (Netbeans) works fine. The application is a simple JFrame with a JScrollPane, JTextArea (as logger) and JButton to perform the main function. The problem is, when I "clean and build" the project and execute it outside from IDE, It freezes itself without reason when I click on the JButton. The JScrollPane change to indertemine mode when the programs runs, but it freezes in the middle!. I tried to execute with the console to see some Exception, but nothing happens.
TestSwingThread.java
package testswingthread;
public class TestSwingThread
{
public static void main(String[] args)
{
DisplayWindow dw = new DisplayWindow();
//FRAME PROPERTIES
dw.setTitle("Test");
dw.setResizable(false);
dw.setLocationRelativeTo(null);
dw.setVisible(true);
}
}
DisplayWindow.java
package testswingthread;
public class DisplayWindow extends javax.swing.JFrame {
public DisplayWindow()
{
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();
mainActionButton = new javax.swing.JButton();
statusBar = new javax.swing.JProgressBar();
jScrollPane2 = new javax.swing.JScrollPane();
logger = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos"));
mainActionButton.setText("MainAction");
mainActionButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mainActionButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(mainActionButton)
.addGap(0, 0, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(mainActionButton)
.addGap(0, 54, Short.MAX_VALUE))
);
logger.setEditable(false);
logger.setBackground(new java.awt.Color(225, 224, 224));
logger.setColumns(20);
logger.setFont(new java.awt.Font("Courier New", 0, 15)); // NOI18N
logger.setRows(5);
logger.setBorder(javax.swing.BorderFactory.createTitledBorder("Logger"));
logger.setCaretColor(new java.awt.Color(255, 255, 255));
jScrollPane2.setViewportView(logger);
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(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 484, Short.MAX_VALUE)
.addComponent(statusBar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, 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.RELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 380, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(statusBar, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
.addGap(6, 6, 6))
);
pack();
}// </editor-fold>
private void mainActionButtonActionPerformed(java.awt.event.ActionEvent evt) {
Runnable e = new MainActionButtonActions(this);
Thread process_e = new Thread(e);
process_e.start();
}
public void blockButtons()
{
mainActionButton.setEnabled(false);
}
public void unblockButtons()
{
mainActionButton.setEnabled(true);
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane2;
public javax.swing.JTextArea logger;
private javax.swing.JButton mainActionButton;
public javax.swing.JProgressBar statusBar;
// End of variables declaration
}
MainActionButtonActions.java
package testswingthread;
import javax.swing.JTextArea;
public class MainActionButtonActions implements Runnable
{
private final JTextArea logger;
private final DisplayWindow window;
public MainActionButtonActions(DisplayWindow p_window)
{
window = p_window;
logger = window.logger;
logger.setText(null); //CLEAR THE LOG
}
public void run()
{
exec();
}
public boolean exec()
{
boolean result = false;
window.blockButtons();
window.statusBar.setIndeterminate(true);
appendToLogger("TRY ONE..");
appendToLogger("TRY TWO..");
appendToLogger("TRY THREE..");
window.unblockButtons();
window.statusBar.setIndeterminate(false);
return result;
}
private void appendToLogger(String text)
{
logger.append("\n" + text);
logger.setCaretPosition(logger.getDocument().getLength());
}
}
Regards!

JFrames load blank

I'm trying to make a program that uses Swing for the GUI, but the form that opens up when I run the code below is blank. I'm still able to get information regarding the objects that aren't seen, however (tested with getting btnLogin.getText()). Am I missing something, or am I doing something wrong?
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class loginForm extends JFrame implements ActionListener{
public static void main(String[] args){
new loginForm();
}
public loginForm() {
System.out.println("Starting...");
pack();
initComponents();
System.out.println("Opened");
this.setVisible(true);
}
private void initComponents() {
loginForm = new JFrame();
lblUserID = new JLabel();
existingUserIDTextField = new JTextField();
lblPassword = new JLabel();
existingUserPasswordField = new JPasswordField();
btnLogin = new JButton();
btnLogin.addActionListener(this);
lblWelcome = new JLabel();
//======== loginForm ========
{
loginForm.setResizable(false);
loginForm.setTitle("Library Reservation System");
Container loginFormContentPane = loginForm.getContentPane();
//---- lblUserID ----
lblUserID.setText("User ID:");
lblUserID.setFont(new Font("Tahoma", Font.PLAIN, 16));
//---- lblPassword ----
lblPassword.setText("Password:");
lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 16));
//---- btnLogin ----
btnLogin.setText("Login");
//---- lblWelcome ----
lblWelcome.setText("Welcome to our Library Reservation System");
lblWelcome.setFont(new Font("Tahoma", Font.PLAIN, 16));
GroupLayout loginFormContentPaneLayout = new GroupLayout(loginFormContentPane);
loginFormContentPane.setLayout(loginFormContentPaneLayout);
loginFormContentPaneLayout.setHorizontalGroup(
loginFormContentPaneLayout.createParallelGroup()
.addGroup(loginFormContentPaneLayout.createSequentialGroup()
.addGroup(loginFormContentPaneLayout.createParallelGroup()
.addGroup(loginFormContentPaneLayout.createSequentialGroup()
.addGap(199, 199, 199)
.addComponent(btnLogin, GroupLayout.PREFERRED_SIZE, 145, GroupLayout.PREFERRED_SIZE))
.addGroup(loginFormContentPaneLayout.createSequentialGroup()
.addGap(72, 72, 72)
.addGroup(loginFormContentPaneLayout.createParallelGroup()
.addComponent(lblUserID, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
.addComponent(lblPassword))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(loginFormContentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
.addComponent(existingUserIDTextField, GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE)
.addComponent(existingUserPasswordField, GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE)))
.addGroup(GroupLayout.Alignment.TRAILING, loginFormContentPaneLayout.createSequentialGroup()
.addContainerGap()
.addComponent(lblWelcome, GroupLayout.PREFERRED_SIZE, 318, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(118, Short.MAX_VALUE))
);
loginFormContentPaneLayout.setVerticalGroup(
loginFormContentPaneLayout.createParallelGroup()
.addGroup(loginFormContentPaneLayout.createSequentialGroup()
.addContainerGap()
.addComponent(lblWelcome, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
.addGap(11, 11, 11)
.addGroup(loginFormContentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(lblUserID)
.addComponent(existingUserIDTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(loginFormContentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(lblPassword)
.addComponent(existingUserPasswordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(btnLogin)
.addContainerGap(12, Short.MAX_VALUE))
);
loginForm.pack();
loginForm.setLocationRelativeTo(loginForm.getOwner());
}
}
private JFrame loginForm;
private JLabel lblUserID;
private JTextField existingUserIDTextField;
private JLabel lblPassword;
private JPasswordField existingUserPasswordField;
private JButton btnLogin;
private JLabel lblWelcome;
}
The frame containing the controls is never displayed.
loginForm.setVisible(true);
Instead of having an outer redundant frame class you could do
public class LoginFormApp extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
LoginFormApp loginFormApp = new LoginFormApp();
loginFormApp.initComponents();
}
});
.....
}
where initComponents makes the loginForm visible as previously stated.

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.

Java UI Generator causing massive lag?

I am awful at Java GUI and used a generator to make most of the GUI for me. I made it in Netbeans and then copied it over to Eclipse. It lags out my pretty powerful computer for about 5 seconds on start and although I've tried timing it and stuff, I can't figure out why. I don't like to blame the generator but it is 100% coming from that UI code.
I would really appreciate someone helping me figure out what is causing it, and just in general improving the ugly UI code. The other code is unfinished, and this is just the interface part.
note that you can remove all of my code, even the gridLayout, and it will still lag, and if i compile it to a .jar and compile it with a -classpath argument it has the same lag
package ...
imports ...
public class x extends javax.swing.JFrame {
public x() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Point centrePoint = new Point(screenSize.width / 2, screenSize.height / 2);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
schMon = new javax.swing.JLabel();
schTue = new javax.swing.JLabel();
schWed = new javax.swing.JLabel();
schThu = new javax.swing.JLabel();
schFri = new javax.swing.JLabel();
schSat = new javax.swing.JLabel();
schSun = new javax.swing.JLabel();
timeLabel = new javax.swing.JLabel();
schedulePanel = new javax.swing.JPanel();
holdingPanel = new javax.swing.JPanel();
isEnabled = new javax.swing.JCheckBox();
dropboxPath = new javax.swing.JTextField();
fileButton = new javax.swing.JButton();
saveButton = new javax.swing.JButton();
resetButton = new javax.swing.JButton();
sep2 = new javax.swing.JSeparator();
sep = new javax.swing.JSeparator();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
schMon.setText("Monday");
schTue.setText("Tuesday");
schWed.setText("Wednesday ");
schThu.setText("Thursday");
schFri.setText("Friday");
schSat.setText("Saturday");
schSun.setText("Sunday");
Font font = new Font("Calibri", Font.BOLD, 13);
timeLabel.setFont(font);
timeLabel.setText("");
schedulePanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
schedulePanel.setLayout(new GridLayout(7, 24, -1, -1));
schedulePanel.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent mouseEvent) {
//JPanel clickedPanel = (JPanel) getComponentAt(mouseEvent.getPoint());
// System.out.println(clickedPanel.getName().toString());
}
});
ArrayList<ArrayList<JPanel>> times = new ArrayList<ArrayList<JPanel>>();
for (int day = 0; day < 7; day++) {
times.add(new ArrayList<JPanel>());
for (int hour = 0; hour < 24; hour++) {
times.get(day).add(new JPanel());
JPanel current = times.get(day).get(hour);
current.setSize(42, 42);
current.setBorder(BorderFactory.createLineBorder(Color.black));
current.setMaximumSize(current.getSize());
current.setBackground(Color.LIGHT_GRAY);
schedulePanel.add(current);
}
}
holdingPanel.setPreferredSize(new java.awt.Dimension(2, 100));
isEnabled.setText("Enable Scheduler");
dropboxPath.setText("Path to your Dropbox.exe");
dropboxPath.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
fileButton.setText("Find");
fileButton.setPreferredSize(new java.awt.Dimension(60, 22));
saveButton.setText("Save");
resetButton.setText("Reset");
resetButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
sep2.setOrientation(javax.swing.SwingConstants.VERTICAL);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(
holdingPanel);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(isEnabled)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(dropboxPath, GroupLayout.PREFERRED_SIZE, 480, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(fileButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(sep2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(58)
.addComponent(saveButton)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(resetButton)
.addGap(2))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(Alignment.TRAILING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(Alignment.BASELINE)
.addComponent(isEnabled, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(sep2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(dropboxPath, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(fileButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(saveButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(resetButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
holdingPanel.setLayout(jPanel2Layout);
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(sep)
.addComponent(
holdingPanel,
javax.swing.GroupLayout.DEFAULT_SIZE,
630, Short.MAX_VALUE)
.addGroup(
layout.createSequentialGroup()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(
schSat)
.addComponent(
schSun)
.addComponent(
timeLabel)
.addComponent(
schMon)
.addComponent(
schTue)
.addComponent(
schWed)
.addComponent(
schThu)
.addComponent(
schFri))
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(
schedulePanel,
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()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addGap(10)
.addComponent(
schMon)
.addGap(30)
.addComponent(
schTue)
.addGap(30)
.addComponent(
schWed)
.addGap(30)
.addComponent(
schThu)
.addGap(30)
.addComponent(
schFri)
.addGap(30)
.addComponent(
schSat)
.addGap(30)
.addComponent(
schSun)
.addGap(12)
.addComponent(
timeLabel)
.addGap(0,
0,
Short.MAX_VALUE))
.addComponent(
schedulePanel,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
.addGap(10, 10, 10)
.addComponent(sep,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(1, 1, 1)
.addComponent(holdingPanel,
javax.swing.GroupLayout.PREFERRED_SIZE,
45,
javax.swing.GroupLayout.PREFERRED_SIZE)));
pack();
Point newLocation = new Point(centrePoint.x - (this.getWidth() / 2), centrePoint.y - (this.getHeight() / 2));
setLocation(newLocation);
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
}
public static void main(String args[]) {
try {
javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager
.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(x.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(x.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(x.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(x.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new x().setVisible(true);
}
});
}
private javax.swing.JButton fileButton;
private javax.swing.JButton saveButton;
private javax.swing.JButton resetButton;
private javax.swing.JCheckBox isEnabled;
private javax.swing.JLabel schMon;
private javax.swing.JLabel schTue;
private javax.swing.JLabel schWed;
private javax.swing.JLabel schThu;
private javax.swing.JLabel schFri;
private javax.swing.JLabel schSat;
private javax.swing.JLabel schSun;
private javax.swing.JLabel timeLabel;
private javax.swing.JPanel holdingPanel;
private javax.swing.JSeparator sep;
private javax.swing.JSeparator sep2;
private javax.swing.JTextField dropboxPath;
private javax.swing.JPanel schedulePanel;
}
If the lag is during WindowBuilder's opening of the design page (parsing), the lag is normal.

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