Displaying JLabel based on combobox option - java

How do I display a JLabel based on a combobox option. When I run the code all the JLabels are displayed straight away.
combo1.addActionListener(this);
panel.add(combo1);
panel.add(img1);
panel.add(img2);
panel.add(img3);
frame.add(panel);
img1.setText("<html> Image : Image1/</html>");
img2.setText("<html> Image : Image2/</html>");
img3.setText("<html> Image : Image3/</html>");
img1.setCursor(new Cursor(Cursor.HAND_CURSOR));
img2.setCursor(new Cursor(Cursor.HAND_CURSOR));
img3.setCursor(new Cursor(Cursor.HAND_CURSOR));
I'm using mouseListener to check which label is passed into the goWebsite() function, and then according to that it'll add a hyperlink to the label.
private void goWebsite(final JLabel website) {
website.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
if(website == img1){
try {
Desktop.getDesktop().browse(new URI("http://2.bp.blogspot.com/_2dxp9ORKKAM/TBZpViy7O1I/AAAAAAAABGY/zitq3ZLA8K4/s1600/red.png"));
} catch (URISyntaxException | IOException ex) {
//It looks like there's a problem
}
}
if(website == img2){
try {
Desktop.getDesktop().browse(new URI("http://www.pratikbagaria.com/wp-content/uploads/2011/04/BlueGreenPink.jpg"));
} catch (URISyntaxException | IOException ex) {
//It looks like there's a problem
}
}
if(website == img3){
try {
Desktop.getDesktop().browse(new URI("http://i.imgur.com/9OPnZNk.png"));
} catch (URISyntaxException | IOException ex) {
//It looks like there's a problem
}
}
if(website == img4){
try {
Desktop.getDesktop().browse(new URI("http://www.solidbackgrounds.com/images/three/2048x2048/2048x2048-fluorescent-orange-fluorescent-pink-fluorescent-yellow-three-color-background.jpg"));
} catch (URISyntaxException | IOException ex) {
//It looks like there's a problem
}
}
The actionPerformed checks which option from the combobox the user has selected and then passes the right img JLabel into the goWebsite() function.
#Override
public void actionPerformed(ActionEvent e)
{
String color1 = (String)combo1.getSelectedItem();
// Possibly check if either color is 'null' here
if (color1.equals("red") )
{
goWebsite(img1);
}
if (color1.equals("blue") )
{
goWebsite(img2);
}
if (color1.equals("green") )
{
goWebsite(img3);
}
}

Try adding a ItemListener to the JComboBox, when itemStateChanged is called, check that the ItemEvent#getStateChanged equals ItemEvent.SELECTED.
Check what is selected in the combo box (or use ItemEvent#ItemSelected) and either update a single JLabel with the information you want OR make the current label associated with the selected item visible by using JLabel#setVisible(true), but also make sure you hide the others
For example...
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class ComboBoxUpdate {
public static void main(String[] args) {
new ComboBoxUpdate();
}
public ComboBoxUpdate() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JComboBox comboBox;
private JLabel option1;
private JLabel option2;
private JLabel option3;
public TestPane() {
comboBox = new JComboBox(new String[]{"Choice 1", "Choice 2", "Choice 3"});
option1 = new JLabel("Bananas");
option2 = new JLabel("Appels");
option3 = new JLabel("Grapes");
option1.setVisible(false);
option2.setVisible(false);
option3.setVisible(false);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(comboBox, gbc);
add(option1, gbc);
add(option2, gbc);
add(option3, gbc);
comboBox.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent e) {
switch (e.getStateChange()) {
case ItemEvent.SELECTED:
Object value = comboBox.getSelectedItem();
option1.setVisible(false);
option2.setVisible(false);
option3.setVisible(false);
if ("Choice 1".equals(value)) {
option1.setVisible(true);
} else if ("Choice 2".equals(value)) {
option2.setVisible(true);
} else if ("Choice 3".equals(value)) {
option3.setVisible(true);
}
break;
}
}
});
comboBox.setSelectedItem(null);
}
}
}

Related

Why can't I retrieve my buttons when I run my program?

So, my program is a user adding buttons during runtime. When he/she clicks on the 'save' button the program is saved to a file. But when I run it again, the buttons are gone. I tried to serialize my buttons using XMLEncoder and XMLDecoder, but when I ran my program, it didn't save anything, it started all over again. How would I serialize this correctly so that when I start my program, the buttons are there? Any help would be appreciated.
Here is a snippet of my code:
public class saveButton
{
//JFrame and JPanels have been declared earlier
class ClickListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
str = JOptionPane.showInputDialog("What is the name of the new button?");
JButton b = new JButton(str);
frame.add(b);
try
{
XMLEncoder encdr = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("file.ser")));
encdr.writeObject(new JButton(str));
encdr.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
ActionListener addButtonClicked = new ClickListener();
b.addActionListener(addButtonClicked);
class ClickListenerTwo implements ActionListener
{
public void actionPerformed(ActionEvent f)
{
try
{
XMLDecoder d = new XMLDecoder(new BufferedInputStream(new FileInputStream("file.ser")));
Object result = d.readObject();
d.close();
}
catch (IOException decoder)
{
decoder.printStackTrace();
}
}
}
Once you decode the object, you need to cast the object appropriately and then add the component to the container.
This is pretty basic example which generates a random number of buttons on a panel each time you click the Random button. When you click Save, the panel is saved to disk and when you click Load, it loads the panel from disk and reapplies it the container
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private RandomButtonPane pane;
public TestPane() {
setLayout(new BorderLayout());
JPanel actions = new JPanel();
JButton random = new JButton("Random");
JButton save = new JButton("Save");
JButton load = new JButton("Load");
actions.add(random);
actions.add(save);
actions.add(load);
add(actions, BorderLayout.SOUTH);
random.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (pane != null) {
remove(pane);
}
pane = new RandomButtonPane();
pane.randomise();
add(pane);
Window window = SwingUtilities.windowForComponent(TestPane.this);
window.pack();
window.setLocationRelativeTo(null);
}
});
save.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (pane != null) {
try (OutputStream os = new FileOutputStream(new File("Save.dat"))) {
try (XMLEncoder encoder = new XMLEncoder(os)) {
encoder.writeObject(pane);
remove(pane);
pane = null;
}
} catch (IOException exp) {
exp.printStackTrace();
}
}
}
});
load.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (pane != null) {
remove(pane);
pane = null;
}
try (InputStream is = new FileInputStream(new File("Save.dat"))) {
try (XMLDecoder decoder = new XMLDecoder(is)) {
Object value = decoder.readObject();
if (value instanceof RandomButtonPane) {
pane = (RandomButtonPane)value;
pane.revalidate();
add(pane);
}
}
} catch (IOException exp) {
exp.printStackTrace();
}
Window window = SwingUtilities.windowForComponent(TestPane.this);
window.pack();
window.setLocationRelativeTo(null);
}
});
}
}
public static class RandomButtonPane extends JPanel {
public RandomButtonPane() {
setLayout(new GridBagLayout());
}
public void randomise() {
int count = ((int) (Math.random() * 100)) + 1;
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
for (int index = 0; index < count; index++) {
if (index % 10 == 0) {
gbc.gridx = 0;
gbc.gridy++;
}
add(new JButton(Integer.toString(index)), gbc);
gbc.gridx++;
}
}
}
}

Using swing how do I dim an unfocused text field?

A user is allowed to choose only one option from a few. Each option is represented by a text field (user is required to put some text there) so I though it should be nice to dim a field which is unfocused to let user understand he may choose one option.
Can anyone please advise me with an example / link to one for something similar?
So, the basic idea would be to simple use setEnabled to change the enabled state of the field which you don't want modified. The difficult part is knowing when to to disable/enable the fields
Fortunately, the JTextField's Document can generate events when it's updated. See Listening for Changes on a Document for more details
So, with this in hand, we can do something like this...
public class ManagedDocumentHandler implements DocumentListener {
private JTextField toBe;
private JTextField orNotToBe;
public ManagedDocumentHandler(JTextField toBe, JTextField orNotToBe) {
this.toBe = toBe;
this.orNotToBe = orNotToBe;
}
#Override
public void insertUpdate(DocumentEvent e) {
updateState();
}
#Override
public void removeUpdate(DocumentEvent e) {
updateState();
}
#Override
public void changedUpdate(DocumentEvent e) {
updateState();
}
protected void updateState() {
toBe.setEnabled(true);
orNotToBe.setEnabled(toBe.getText().trim().length() == 0);
}
}
Knowing that we have two fields, one will be enabled when the contents is changed, the other will (likely) be disabled when the other is changed (except if the field is empty, then they are both enabled)
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class MakeItSo {
public static void main(String[] args) {
new MakeItSo();
}
public MakeItSo() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.insets = new Insets(4, 4, 4, 4);
add(new JLabel("Choose this:"), gbc);
gbc.gridx++;
add(new JLabel("Or this:"), gbc);
JTextField thisOne = new JTextField(10);
JTextField orThisOne = new JTextField(10);
thisOne.getDocument().addDocumentListener(new ManagedDocumentHandler(thisOne, orThisOne));
orThisOne.getDocument().addDocumentListener(new ManagedDocumentHandler(orThisOne, thisOne));
gbc.gridx = 0;
gbc.gridy++;
add(thisOne, gbc);
gbc.gridx++;
add(orThisOne, gbc);
}
public class ManagedDocumentHandler implements DocumentListener {
private JTextField toBe;
private JTextField orNotToBe;
public ManagedDocumentHandler(JTextField toBe, JTextField orNotToBe) {
this.toBe = toBe;
this.orNotToBe = orNotToBe;
}
#Override
public void insertUpdate(DocumentEvent e) {
updateState();
}
#Override
public void removeUpdate(DocumentEvent e) {
updateState();
}
#Override
public void changedUpdate(DocumentEvent e) {
updateState();
}
protected void updateState() {
toBe.setEnabled(true);
orNotToBe.setEnabled(toBe.getText().trim().length() == 0);
}
}
}
}

Change image in JLabel based on which button the cursor is hovering over?

I have created a Swing interface where I want an icon to appear in a JLabel when the cursor is hovered over a button. The icon will change based in which button the cursor hovers over.
Here is the coding I am using at the moment:
public void iconchange()
{
if(btnPlay.isRolloverEnabled() == true)
{
lblIcon.setIcon(new ImageIcon("Images/Play-icon.png"));
}
}
I know the coding above is wrong, so what can I do to achieve the feature I mentioned above?
Use a ChangeListener on the JButton's ButtonModel to monitor changes to the models state, update your UI based on the state of the model, for example...
public class ChangeHandler implements ChangeListener {
private JLabel label;
private Icon armedIcon;
public ChangeHandler(JLabel label, Icon armedIcon) {
this.armedIcon = armedIcon;
this.label = label;
}
#Override
public void stateChanged(ChangeEvent e) {
ButtonModel model = (ButtonModel) e.getSource();
if (model.isRollover()) {
label.setIcon(armedIcon);
} else {
label.setIcon(null);
}
}
}
This will update the supplied JLabel, changing it's icon to the specified icon when the ButtonModel it's attached to is in the rollOver state
This could be used something like this...
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class RollOver {
public static void main(String[] args) {
new RollOver();
}
public RollOver() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel label;
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
// I'm doing this, because I don't have blank icon of 128x128 and my
// icons are both 128x128
label = new JLabel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(128, 128);
}
};
add(label, gbc);
try {
JButton btn = new JButton("Bunnies");
btn.getModel().addChangeListener(new ChangeHandler(label, new ImageIcon(ImageIO.read(getClass().getResource("/Bunny.png")))));
add(btn);
btn = new JButton("Zomnies");
btn.getModel().addChangeListener(new ChangeHandler(label, new ImageIcon(ImageIO.read(getClass().getResource("/Zombi.png")))));
add(btn);
} catch (IOException exp) {
exp.printStackTrace();
}
}
public class ChangeHandler implements ChangeListener {
private JLabel label;
private Icon armedIcon;
public ChangeHandler(JLabel label, Icon armedIcon) {
this.armedIcon = armedIcon;
this.label = label;
}
#Override
public void stateChanged(ChangeEvent e) {
ButtonModel model = (ButtonModel) e.getSource();
if (model.isRollover()) {
label.setIcon(armedIcon);
} else {
label.setIcon(null);
}
}
}
}
}
Take a closer look at How to Use Buttons, Check Boxes, and Radio Buttons for more details
Add an MouseListener to btnPlay, and use the mouse exit and enter methods
btnPlay.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
lblIcon.setIcon(new ImageIcon("Images/Play-icon.png"));
}
public void mouseExited(java.awt.event.MouseEvent evt) {
//I assume you want to remove or change the icon afterward
}
});

Change JButton Color in a Thread

I am trying to make a game like simon: http://www.freegames.ws/games/kidsgames/simon/simon.htm#
I am making a smaller scale with only 2 buttons. I want the color to switch between 2 buttons, button1 and button2. This is in a thread because I need buttons to be clicked while this is happening. When I open the program the button color stays as-is.
Thanks for help in advance!
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.*;
public class TestFrame extends JFrame{
public JButton button1;
public JButton button2;
boolean isTrue = true;
boolean switchColor = true;
TestFrame(){
super("Simon");
initialize();
this.setSize(200, 400);
this.setVisible(true);
}
private void initialize() {
this.setLayout(new BorderLayout());
button1 = new JButton();
button1.setBackground(Color.green);
button1.setSize(200,200);
button2 = new JButton();
button2.setSize(200, 200);
button2.setBackground(Color.blue);
this.add(button1, BorderLayout.NORTH);
this.add(button2, BorderLayout.SOUTH);
Thread t = new Thread(r1);
t.start();
}
Runnable r1 = new Runnable() {
public void run() {
while(isTrue){
if(switchColor = true){
button1.setBackground(Color.blue);
button2.setBackground(Color.green);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
refresh();
switchColor = false;
} else {
button1.setBackground(Color.green);
button2.setBackground(Color.blue);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
refresh();
switchColor = true;
}
}
}
};
public void refresh(){
this.invalidate();
this.validate();
this.repaint();
}
}
A number of issues stand out (shazin has addressed one), the other that scares me is you are violating the single thread requirements of Swing. All changes to the UI must be made from within the context of the Event Dispatching Thread.
Instead of using a Thread, you should be using a javax.swing.Timer. This will save you the need to have to resync your updates back to the EDT.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class FlashyButtons {
public static void main(String[] args) {
new FlashyButtons();
}
public FlashyButtons() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JButton btn1;
private JButton btn2;
private int count = 0;
public TestPane() {
setLayout(new GridBagLayout());
btn1 = new FlashButton();
btn2 = new FlashButton();
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(btn1, gbc);
add(btn2, gbc);
btn1.setBackground(Color.GREEN);
btn2.setBackground(Color.BLUE);
Timer timer = new Timer(500, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
count++;
if (count % 2 == 0) {
btn1.setBackground(Color.BLUE);
btn2.setBackground(Color.GREEN);
} else {
btn1.setBackground(Color.GREEN);
btn2.setBackground(Color.BLUE);
}
}
});
timer.start();
}
}
public class FlashButton extends JButton {
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
Take a look at Concurrency in Swing for more details
if(switchColor = true){
button1.setBackground(Color.blue);
button2.setBackground(Color.green);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
refresh();
switchColor = false;
} else {
button1.setBackground(Color.green);
button2.setBackground(Color.blue);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
refresh();
switchColor = true;
}
}
In the above code change the following to
if(switchColor = true){
to
if(switchColor == true){
or just
if(switchColor){
And it is best to have your Runnable anonymous class inside
SwingUtilities.invokeLater(new Runnable() {
public void run() {
}
});
than using a new Thread object to create and start the thread as it will be changing the Swing UI properties.

JOptionPane.showInternalInputDialog not being editable

I'm writing a JDesktopPane application, and in a JInternalFrame, I've got a JEditorPane with a webpage open (yes, I'm aware of the crappy abilities of JEditorPane with the net, don't scold).
I have a way for the user to input a page they would like to visit, but when I call JOptionPane.showInternalInputDialog(this, "What page would you like to visit?") the text field is not editable. This problem has occurred for me both in Java 6 and Java 7.
EDIT:
Here's the constructor of my class
public Internet() {
super("Internet", true, true, true, true);
setSize(500, 400);
try {
pane = new JEditorPane(new URL("http://www.vetrustech.tk"));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
setContentPane(pane);
bar = new JMenuBar();
page = new JMenu("Page");
enterPage = new JMenuItem("Enter a page");
bar.add(page);
page.add(enterPage);
setJMenuBar(bar);
enterPage.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
loadPage();
}
});
and here's the method for loading the page
private void loadPage() {
String s = JOptionPane.showInternalInputDialog(this,
"What page are you visiting?");
if (s == null) {
return;
}
if (s.equals("")) {
return;
}
try {
URL u = new URL(s);
pane.setPage(u);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Here's the reason why a suited SSCCE is so important...
This works....
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Internet {
public static void main(String[] args) {
new Internet();
}
public Internet() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JDesktopPane dp = new JDesktopPane();
final JInternalFrame inf = new JInternalFrame("Help", true, true, true, true);
inf.setSize(200, 200);
inf.setVisible(true);
dp.add(inf);
JButton btn = new JButton("Click");
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showInternalInputDialog(inf, "Hit me");
}
});
inf.add(btn);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(dp);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
This suggests that you are doing something else in your code that we're not seeing.

Categories

Resources