Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Editable issues (http://www.velocityreviews.com/forums/t957037-editable-issues.html)

K 01-29-2013 05:19 AM

Editable issues
 
I made a java program and can't figure out why my uneditable text can still be edited. I have 2 classes in this program here's the secondary class:
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

public class GUI_two extends JFrame{

private JTextField item2;
private JTextField item3;
private JTextField item4;
private JPasswordField itempass;
ImageIcon image = new ImageIcon("star.png");
public GUI_two(){
super("Kale");
setLayout(new FlowLayout());
item2 = new JTextField(10);
add(item2);
item3 = new JTextField("Enter text here");
item3.setEditable(false);
add(item3);
item4 = new JTextField("Can't edit", 20);
item3.setEditable(false);
add(item4);
itempass = new JPasswordField ("Password");
add(itempass);
reg = new JButton("Button");
add(reg);
Icon b = new ImageIcon(getClass().getResource("Button 2.png"));
Icon a = new ImageIcon(getClass().getResource("Button.png"));
custom = new JButton("Custom", a);
custom.setRolloverIcon(b);
add(custom);

thehandler handler = new thehandler();
item2.addActionListener(handler);
item3.addActionListener(handler);
item4.addActionListener(handler);
itempass.addActionListener(handler);
reg.addActionListener(handler);
custom.addActionListener(handler);

}

private JButton reg;
private JButton custom;

private class thehandler implements ActionListener{
public void actionPerformed(ActionEvent event){
String string = "";
if(event.getSource()==item2){
string=String.format("field 2: %s", event.getActionCommand());}
else if (event.getSource()==item3)
string=String.format("field 3: %s", event.getActionCommand());
else if (event.getSource()==item4)
string=String.format("field 4: %s", event.getActionCommand());
else if (event.getSource()==itempass)
string=String.format("itempass is : %s", event.getActionCommand());


//JOptionPane.showMessageDialog(null, String.format("%s, event.getActionCommand()"));
JOptionPane.showMessageDialog(null, string);
}
}


}

here's the main class:
import javax.swing.JOptionPane;
import javax.swing.JFrame;

public class Kale_GUI {


public static void main(String[] args) {
String fn = JOptionPane.showInputDialog("Enter first number");
String sn = JOptionPane.showInputDialog("Enter second number");
int num1 = Integer.parseInt(fn) ;
int num2 = Integer.parseInt(sn) ;
int sum = num1 + num2;
JOptionPane.showMessageDialog(null, "The answer is "+sum, "The title", JOptionPane.PLAIN_MESSAGE);

GUI_two kale = new GUI_two();

kale.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
kale.setSize(500,500);
kale.setVisible(true);
}

}



Mikhail Vladimirov 01-29-2013 06:21 AM

Re: Editable issues
 
> item3 = new JTextField("Enter text here");
> item3.setEditable(false);
> add(item3);
> item4 = new JTextField("Can't edit", 20);
> item3.setEditable(false);
> add(item4);

Probably because you did setEditable (false) on item3 twice, and didn't do it on item4?

Arved Sandstrom 01-29-2013 10:27 AM

Re: Editable issues
 
On 01/29/2013 03:17 AM, Peter Duniho wrote:
> On Mon, 28 Jan 2013 22:21:25 -0800 (PST), Mikhail Vladimirov wrote:
>
>>> item3 = new JTextField("Enter text here");
>>> item3.setEditable(false);
>>> add(item3);
>>> item4 = new JTextField("Can't edit", 20);
>>> item3.setEditable(false);
>>> add(item4);

>> Probably because you did setEditable (false) on item3 twice, and didn't do it on item4?

>
> Copy/paste rears its ugly head again. A helper method would have prevented
> the mistake:

[ SNIP ]

Helper method probably best. I'd also name variables better, and
declare/define and do everything with them in one spot demarcated by WS,
if possible, e.g.:

private JTextField nameTextFld;
nameTextFld = new JTextField("Enter name");
nameTextFld.setEditable(false);
add(nameTextFld);
MyActionListener aLsnr = new MyActionListener();
nameTextFld.addActionListener(aLsnr);

We've had this discussion before, blocks also ensure that erroneous copy
& paste (for situations like this) also fails.

I barely ever use Swing, so I won't comment on the ActionListener
implementation. I do know it's fairly wrong-looking and just plain ugly.
I'm myself not going to have a spaghetti event handler that looks to see
who fired it out of many possibilities.

Main problem here is that in 2013 we still have people hand-cranking
Swing code. It's ugly, tedious, unedifying, error-prone code. It's worse
than JSF boilerplate and that's saying quite a lot.

..NET tooling has Java beat in this regard.

AHS

Roedy Green 01-29-2013 10:38 AM

Re: Editable issues
 
On Mon, 28 Jan 2013 21:19:03 -0800 (PST), K <kalezwe@gmail.com> wrote,
quoted or indirectly quoted someone who said :

>I made a java program and can't figure out why my uneditable text can still be edited


see sample code at http://mindprod.com/jgloss/jpasswordfield.html

it works fine with setEditable( false )

You made an error that would not likely have happened with more
meaningful names for your variables:

item3 = new JTextField("Enter text here");
item3.setEditable(false); <-- you meant true
item4 = new JTextField("Can't edit", 20);
item3.setEditable(false); <-- you meant item4

let me rename the vars:

canEnter= new JTextField("Enter text here");
canEnter.setEditable(false); <-- you meant true
cantEnter= new JTextField("Can't edit", 20);
canEnter.setEditable(false); <-- you meant cantEnter
--
Roedy Green Canadian Mind Products http://mindprod.com
The first 90% of the code accounts for the first 90% of the development time.
The remaining 10% of the code accounts for the other 90% of the development
time.
~ Tom Cargill Ninety-ninety Law

Arne Vajh°j 01-30-2013 03:02 AM

Re: Editable issues
 
On 1/29/2013 5:27 AM, Arved Sandstrom wrote:
> Main problem here is that in 2013 we still have people hand-cranking
> Swing code. It's ugly, tedious, unedifying, error-prone code. It's worse
> than JSF boilerplate and that's saying quite a lot.
>
> .NET tooling has Java beat in this regard.


I strongly suspect that it is not:

lack of GUI builder tools => desire to hand write Swing code

but instead:

desire to hand write Swing code => lack of GUI builder tools

Arne




Arved Sandstrom 01-30-2013 11:38 AM

Re: Editable issues
 
On 01/29/2013 11:02 PM, Arne Vajh°j wrote:
> On 1/29/2013 5:27 AM, Arved Sandstrom wrote:
>> Main problem here is that in 2013 we still have people hand-cranking
>> Swing code. It's ugly, tedious, unedifying, error-prone code. It's worse
>> than JSF boilerplate and that's saying quite a lot.
>>
>> .NET tooling has Java beat in this regard.

>
> I strongly suspect that it is not:
>
> lack of GUI builder tools => desire to hand write Swing code
>
> but instead:
>
> desire to hand write Swing code => lack of GUI builder tools
>
> Arne
>

If I parse your English correctly, Arne, you're suggesting that folks
*prefer* to code Swing directly rather than make use of GUI builder
interfaces?

Man, I'm not sure I buy that. I've suffered through writing Swing code
from scratch a few times, I don't see how it's any more useful to do
that than hand-coding JSF Facelets .xhtml and backing bean boilerplate.
Which is to say, not useful at all. You gain nothing over using GUI
builder tools, and you lose time better spent on real logic.

AHS


Lew 01-30-2013 04:32 PM

Re: Editable issues
 
Arved Sandstrom wrote:
> Arne Vajh´┐Żj wrote:
> > I strongly suspect that it is not:
> > lack of GUI builder tools => desire to hand write Swing code
> > but instead:
> > desire to hand write Swing code => lack of GUI builder tools

>
> If I parse your English correctly, Arne, you're suggesting that folks
> *prefer* to code Swing directly rather than make use of GUI builder
> interfaces?


No, he's suggesting that in this particular case the person might be deliberately
doing it by hand. I guess programmers do that when they want to learn what they're
actually doing.

> Man, I'm not sure I buy that. I've suffered through writing Swing code
> from scratch a few times, I don't see how it's any more useful to do
> that than hand-coding JSF Facelets .xhtml and backing bean boilerplate.
> Which is to say, not useful at all. You gain nothing over using GUI
> builder tools, and you lose time better spent on real logic.


Yes, even you have written Swing by hand. What's so odd that others might also?

Didn't the experience teach you about Swing? That wasn't useful?

--
Lew


Arved Sandstrom 01-30-2013 11:34 PM

Re: Editable issues
 
On 01/30/2013 12:32 PM, Lew wrote:
> Arved Sandstrom wrote:
>> Arne Vajh´┐Żj wrote:
>>> I strongly suspect that it is not:
>>> lack of GUI builder tools => desire to hand write Swing code
>>> but instead:
>>> desire to hand write Swing code => lack of GUI builder tools

>>
>> If I parse your English correctly, Arne, you're suggesting that folks
>> *prefer* to code Swing directly rather than make use of GUI builder
>> interfaces?

>
> No, he's suggesting that in this particular case the person might be deliberately
> doing it by hand. I guess programmers do that when they want to learn what they're
> actually doing.
>
>> Man, I'm not sure I buy that. I've suffered through writing Swing code
>> from scratch a few times, I don't see how it's any more useful to do
>> that than hand-coding JSF Facelets .xhtml and backing bean boilerplate.
>> Which is to say, not useful at all. You gain nothing over using GUI
>> builder tools, and you lose time better spent on real logic.

>
> Yes, even you have written Swing by hand. What's so odd that others might also?
>
> Didn't the experience teach you about Swing? That wasn't useful?
>

During the learning phase, sure. Makes sense.

But in routine work, once I've gotten the technology, no, I don't want
to write all the tedious boilerplate. Swing, JSF, JPA etc - I generate
as much as I can if I can, or I wish I could if it's not possible. All I
really want to spend my time on is modifying the generated code.

AHS

Arne Vajh°j 01-31-2013 01:03 AM

Re: Editable issues
 
On 1/30/2013 6:38 AM, Arved Sandstrom wrote:
> On 01/29/2013 11:02 PM, Arne Vajh°j wrote:
>> On 1/29/2013 5:27 AM, Arved Sandstrom wrote:
>>> Main problem here is that in 2013 we still have people hand-cranking
>>> Swing code. It's ugly, tedious, unedifying, error-prone code. It's worse
>>> than JSF boilerplate and that's saying quite a lot.
>>>
>>> .NET tooling has Java beat in this regard.

>>
>> I strongly suspect that it is not:
>>
>> lack of GUI builder tools => desire to hand write Swing code
>>
>> but instead:
>>
>> desire to hand write Swing code => lack of GUI builder tools
>>

> If I parse your English correctly, Arne, you're suggesting that folks
> *prefer* to code Swing directly rather than make use of GUI builder
> interfaces?


Correct.

> Man, I'm not sure I buy that. I've suffered through writing Swing code
> from scratch a few times, I don't see how it's any more useful to do
> that than hand-coding JSF Facelets .xhtml and backing bean boilerplate.
> Which is to say, not useful at all. You gain nothing over using GUI
> builder tools, and you lose time better spent on real logic.


A GUI builder is very fast to create something that from
simple to medium complexity. But if you need to create
something of high complexity you often end up having to
work around the GUI builder.

The generated code is usually much harder to read when
debugging.

And if you change IDE you risk to loose the capability
on existing code.

Anyway - there has been several GUI builders for Java.
JBuilder had one. NetBeans has tried twice I believe.
Eclipse has tried twice. No success. No widespread
usage.

And note that it is not really Java that is different
from the rest. It is really MS that is different from
the rest. GUI builders are not common in PHP, various X
etc..

Arne



Arne Vajh°j 01-31-2013 01:04 AM

Re: Editable issues
 
On 1/30/2013 6:34 PM, Arved Sandstrom wrote:
> On 01/30/2013 12:32 PM, Lew wrote:
>> Arved Sandstrom wrote:
>>> Arne Vajh´┐Żj wrote:
>>>> I strongly suspect that it is not:
>>>> lack of GUI builder tools => desire to hand write Swing code
>>>> but instead:
>>>> desire to hand write Swing code => lack of GUI builder tools
>>>
>>> If I parse your English correctly, Arne, you're suggesting that folks
>>> *prefer* to code Swing directly rather than make use of GUI builder
>>> interfaces?

>>
>> No, he's suggesting that in this particular case the person might be
>> deliberately
>> doing it by hand. I guess programmers do that when they want to learn
>> what they're
>> actually doing.
>>
>>> Man, I'm not sure I buy that. I've suffered through writing Swing code
>>> from scratch a few times, I don't see how it's any more useful to do
>>> that than hand-coding JSF Facelets .xhtml and backing bean boilerplate.
>>> Which is to say, not useful at all. You gain nothing over using GUI
>>> builder tools, and you lose time better spent on real logic.

>>
>> Yes, even you have written Swing by hand. What's so odd that others
>> might also?
>>
>> Didn't the experience teach you about Swing? That wasn't useful?
>>

> During the learning phase, sure. Makes sense.
>
> But in routine work, once I've gotten the technology, no, I don't want
> to write all the tedious boilerplate. Swing, JSF, JPA etc - I generate
> as much as I can if I can, or I wish I could if it's not possible. All I
> really want to spend my time on is modifying the generated code.


That requires the generated code to be as good as handwritten code
to work.

That is rarely the case.

Arne




All times are GMT. The time now is 11:17 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.