So I have this FormatedTextField
JFormattedTextField myFtf = new JFormattedTextField();
which has the following mask, placed in my application constructor
try {
myFtf.setFormatterFactory(
new DefaultFormatterFactory(
new MaskFormatter("###.###.###-##")));
} catch (java.text.ParseException ex) {
ex.printStackTrace();
}
Then, I have a two radio buttons, which should be changing the mask formatter in myFtf.
I have tried the following:
private radioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
myFtf.setFormatterFactory(
new DefaultFormatterFactory(
new MaskFormatter("###.###.###-##")));
} catch (Exception e) {
e.printStackTrace();
}
}
private void radioButton2ActionPerformed(java.awt.event.ActionEvent evt) {
try {
myFtf.setFormatterFactory(
new DefaultFormatterFactory(
new MaskFormatter("##.###.###/####-##")));
} catch (Exception e) {
e.printStackTrace();
}
}
Which works fine, until I try to change their masks when there is input within the text field. In case there is, it doesn't change the mask anymore. Here are a couple of prints:
OK scenario:
img a:
switching radio buttons gives me this:
img b:
Buggy scenario:
img c:
switching radio buttons gives me this:
img d:
I was expecting img d to be exactly like img a
How can I dynamically change its mask correctly?
Change your action listeners to this:
private radioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
myFtf.setFormatterFactory(
new DefaultFormatterFactory(
new MaskFormatter("###.###.###-##")));
myFtf.setText("");
} catch (Exception e) {
e.printStackTrace();
}
}
private void radioButton2ActionPerformed(java.awt.event.ActionEvent evt) {
try {
myFtf.setFormatterFactory(
new DefaultFormatterFactory(
new MaskFormatter("##.###.###/####-##")));
myFtf.setText("");
} catch (Exception e) {
e.printStackTrace();
}
}
That should clear the text fields.
Good luck!
I got it working correctly! All I needed to do was adding a
myFtf.setValue(null);
after setting the new formatter factory. myFtf.setText("") wasn't working as expected, but it was a close shot! :-)
Related
I am having a problem with loading images, as I am trying to load a background for my launcher. It works when I run it in Eclipse, but when I export it to a jar file, it doesn't. I have paged through the already asked questions, No luck.
Here is my code:
public void initializeJFrame(){
ImageIcon bg = new ImageIcon("src/background.png");
jb.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (jta.getText().length() < 3 || jta.getText().length() > 16) {
System.exit(-1);
} else {
try {
Runtime.getRuntime().exec(cmd, null, dir);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.exit(0);
}
}
});
jf.setSize(WIDTH,HEIGHT);
jf.setTitle("Cracked Launcher");
jf.setLayout(null);
jf.setContentPane(new JLabel(bg));
jf.setResizable(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.start();
My image is in the src folder, nothing else. I cannot find what the problem is.
Thanks for your support.
ImageIcon bg = new ImageIcon("src/background.png");
This constructor takes a filename; it will not load resources from a jar. You want to pass a URL generated by a classloader, like so:
ImageIcon bg = new ImageIcon(getClass().retResource("background.png"));
So i have this code here, and it quite literally doesn't work and i just don't know why. It doesn't click, doesn't print out anything. My goal is to make the buttons function as switches between two methods of clicking. Right mouse button clicks, and Left mouse button clicks. Can anyone tell me why this doesn't function?
if (rbRightClickRadioButton.isSelected()) {
System.out.println("RMB Clicker");
Robot clicker = null;
try {
clicker = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
clicker.mousePress(InputEvent.BUTTON2_DOWN_MASK);
clicker.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
Thread.sleep(delay);
try {
clicker = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
clicker.mousePress(InputEvent.BUTTON2_DOWN_MASK);
clicker.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
} else if (rbRightClickRadioButton.isSelected()) {
System.out.println("LMB Clicker");
Robot clicker = null;
try {
clicker = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
clicker.mousePress(InputEvent.BUTTON2_DOWN_MASK);
clicker.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
Thread.sleep(delay);
try {
clicker = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
clicker.mousePress(InputEvent.BUTTON2_DOWN_MASK);
clicker.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
If you want a button to work as a switch between two options, I would rather use checkbox than radiobutton. The main problem your code has right now is the simple fact that you check the same expression in your if statements.
You should either check if the other radiobutton is selected in your else if statement or just assume that if the rbRightClickRadioButton is not selected, then the other one is, and you don't use else if just a simple else.
I'm trying to implement a JEditorPane with hyperlinks. I'm using a HyperLinkListener but it seems to never trigger.
Code:
JEditorPane editorPane = new JEditorPane("text/html", programInfo);
editorPane.addHyperlinkListener(e -> {
System.out.println("CLICK");
if (e.getEventType().equals(HyperlinkEvent.EventType.ENTERED))
try {
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().browse(e.getURL().toURI());
}
} catch (IOException e1) {
e1.printStackTrace();
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
});
JOptionPane.showMessageDialog(contentPane, editorPane);
Sample HTML:
<body>
<p><b>Author:</b> James - sample</p>
</body>
This leads to this:
But when I click on the links nothing happens.
Additional Info:
I'm testing this on Ubuntu 14.04.
I have set Look and Feel to system.
EDIT: thanks to #AndrewThompson for finding the real issue.
The reason why it does not trigger events is because the editor pane will only fire events when it is not editable. So, to make your code work you should add this line after the construction of the editorPane:
editorPane.setEditable(false);
Below you can find a self contained example:
public class TestFrame extends JFrame {
public static void main(String[] args) {
JEditorPane editorPane = new JEditorPane("text/html", "test link to example.com");
editorPane.addHyperlinkListener(new HyperlinkListener() {
#Override
public void hyperlinkUpdate(HyperlinkEvent e) {
System.out.println("CLICK");
if (e.getEventType().equals(HyperlinkEvent.EventType.ENTERED)) try {
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().browse(e.getURL().toURI());
}
}
catch (IOException e1) {
e1.printStackTrace();
}
catch (URISyntaxException e1) {
e1.printStackTrace();
}
}
});
editorPane.setEditable(false); // otherwise ignores hyperlink events!
JFrame frame = new JFrame("EditorPane Example");
frame.add(editorPane);
frame.setSize(300,200);
frame.setVisible(true);
} }
(sorry, I removed the lambda because I don't have a jdk8 on this PC)
The print button below displays the printer selection window but it prints nothing ...but the JTable contains data
print_button.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
try {
boolean complete = table2.print();
if (complete) {
JOptionPane.showMessageDialog(null, "Done printing");
} else {
JOptionPane.showMessageDialog(null, "printing.....");
}
} catch (PrinterException pe) {
}
}});
You need to give a size to your JTable in order to get printed:
table2.setSize(table2.getPreferredSize());
It's correct that it has data, but it needs to have a size for the priniting to work.
i got the answer
print_button.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
MessageFormat header = new MessageFormat("report printing");
MessageFormat footer = new MessageFormat("page{0,number,integer}");
try {
table1.print(JTable.PrintMode.NORMAL,header,footer);
} catch (PrinterException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}});
I have a set of JButtons, each of which opens a separate YouTube video webpage. When first running the program, I can click on any ONE button and get the video page. When I try to get another video page with a button click, it doesn't work - in fact, all the buttons are deactivated. This is the case whether or not I close the video webpage.
How can I keep all the buttons activated? Thanks in advance.
Here's the code for reference. The button links and tags are populated from a text file.
//import statements
public class VideoRecord extends JFrame {
private File videoRecordFile;
public VideoRecord() throws FileNotFoundException {
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridLayout(2,2));
setSize(new Dimension(500, 500));
videoRecordFile = new File("videorecord.txt");
getButtons();
pack();
}
public void getButtons() throws FileNotFoundException {
Scanner input = new Scanner(videoRecordFile);
while (input.hasNextLine()) {
Scanner lineInput = new Scanner(input.nextLine());
while (lineInput.hasNext()) {
final String urlString = lineInput.next();
String buttonText = lineInput.next();
JButton btn = new JButton(buttonText);
add(btn);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
URL videoURL = new URL(urlString);
URLConnection videoConnection = videoURL.openConnection();
videoConnection.connect();
openWebpage(videoURL);
}
catch (MalformedURLException mue) {}
catch (IOException ioe) {}
setEnabled(false);
}
});
}
}
}
public static void openWebpage(URI uri) {
Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
try {
desktop.browse(uri);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void openWebpage(URL url) {
try {
openWebpage(url.toURI());
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws FileNotFoundException {
VideoRecord vr = new VideoRecord();
}
}
Take a second to look at you code...
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
URL videoURL = new URL(urlString);
URLConnection videoConnection = videoURL.openConnection();
videoConnection.connect();
openWebpage(videoURL);
}
catch (MalformedURLException mue) {}
catch (IOException ioe) {}
setEnabled(false);
}
});
When you click a button you call setEnabled(false);...
This has actually disable the frame, not the button that was clicked...
Try using ((JButton)e.getSource()).setEnabled(false) instead
Don't throw away you Exceptions blindly, they provide important and useful information that can help solve problems