How to manage a JInternalFrame calling another JInternalFrame? - java

I have a JDesktopPane with this code.
public class Menu extends JFrame implements ActionListener{
/**
* Creates new form Portada
*/
public static JDesktopPane desktop;
public JDesktopPane getDesktop() {
return desktop;
}
public Menu() {
desktop = new JDesktopPane();
setContentPane(desktop);
desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
initComponents();
}
}
then i add the new components like this
desktop.add(orden);
and when i want to call them i use
if(e.getSource()==jMenuItem1_1){
orden.setVisible(true);
desktop.setSelectedFrame(orden);
desktop.moveToFront(orden);
try {
orden.setSelected(true);
} catch (PropertyVetoException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
The problem i get is that when "orden" wants to pop out another JInternalFrame i use the next code.
searchSupplier.setVisible(true);
Main.getInstance().getPortada().getDesktop().add(searchSupplier);
Main.getInstance().getPortada().getDesktop()
.moveToFront(searchSupplier);
try {
searchSupplier.setSelected(true);
} catch (PropertyVetoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
When I execute the event more than 2 times i get the next error:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: illegal component position
Where should i add the new JInternalFrame to the DesktopPane? or to Orden?, or What can i do to fix this error?

If the searchSupplier frame is already on the desktop, it is unlikely that you will able to add it again. Try using getParent to determine if the frame needs to be added
if (searchSupplier.getParent() == null) {
Main.getInstance().getPortada().getDesktop().add(searchSupplier);
}
searchSupplier.setVisible(true);
Main.getInstance().getPortada().getDesktop().moveToFront(searchSupplier);
try {
searchSupplier.setSelected(true);
} catch (PropertyVetoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Related

Vaadin clicklistener / buttonclick throws exception without click

I'm using Vaadin and it seems that the Clicklistener of my Button recognizes a click where no click is.
When I load the page in my browser, I get an error message, TreeTaggerException is fired, because the List "selectedWords" is empty.
buildDic = new Button();
buildDic.addClickListener(new ClickListener() {
public void buttonClick(ClickEvent event) {
Dictionary dic1 = new Dictionary();
try {
dic1.generateDictionary("D:/....txt", selectedWords, 5, 5, 6);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TreeTaggerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Where does the event come from, or where is my mistake?

HyperLinkListener not triggering - java swing

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)

Code tells me I need to implement ActionListener when I already have?

So I am making this code to write to a file based on user clicks. The only problem I have, is that I get an error on "public class prog". The prog name is where I get the error: It says: The type prog must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent). When I do the quickfix of adding the uninherited methods, it adds the action listener method to the end of my code but with nothing in it. If I already have action listeners in the program, why does it tell me I need to implement them? And why when I add it at the end, does it work fine even though nothing is in it?
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.*;
public class prog extends JFrame implements ActionListener {
//create newLine
final String newLine = System.getProperty("line.separator");
//create buttons
JPanel row1 = new JPanel();
JButton oneLeft = new JButton("oneLeft");
JButton oneRight = new JButton("oneRight");
JPanel row2 = new JPanel();
JButton twoLeft = new JButton("twoLeft");
JButton twoRight = new JButton("twoRight");
JPanel row3 = new JPanel();
JButton threeLeft = new JButton("threeLeft");
JButton threeRight = new JButton("threeRight");
public prog() {
super("Prog");
setLookAndFeel();
setSize(400, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout layout = new GridLayout(3, 2);
setLayout(layout);
//create outStream for writing to file
try {
final File numClicks = new File("numClicks.properties");
final FileOutputStream outStream = new FileOutputStream(numClicks);
//add Listeners
oneLeft.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
write(outStream, "oneLeft has been clicked.");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
oneRight.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
write(outStream, "oneRight has been clicked.");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
twoLeft.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
write(outStream, "twoLeft has been clicked.");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
twoRight.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
write(outStream, "twoRight has been clicked.");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
threeLeft.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
write(outStream, "threeLeft has been clicked.");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
threeRight.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
write(outStream, "threeRight has been clicked.");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
} catch (IOException ioe) {
System.out.println("The file could not be written.");
}
row1.add(oneLeft);
row1.add(oneRight);
row2.add(twoLeft);
row2.add(twoRight);
row3.add(threeLeft);
row3.add(threeRight);
add(row1);
add(row2);
add(row3);
setVisible(true);
}
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
//ignore error
}
}
void write(FileOutputStream stream, String output) throws IOException {
output = output + newLine;
byte[] data = output.getBytes();
stream.write(data, 0, data.length);
}
public static void main(String[] args) {
prog progApp = new prog();
}
}
Your class shouldn't implement ActionListener. Instead of writing a top-level class that implements the interface, you're writing a bunch of little inline classes (called anonymous inner classes) that do this work for you when you say new ActionListener().
You implement ActionListener it, but you don't actually implement the required methods (i.e., actionPerformed()). Therefore your class is invalid to the compiler.
You need a method like:
#Override
public void actionPerformed(ActionEvent e) {
// ...
}
The way an interface works is that it defines what the classes that implements it have to... well... implement. That way any other process can treat it as an ActionListener and know that certain methods have been defined.
Just another way Java tries to make polymorphism your friend.
To address something from the comment below, it's actually not that uncommon to see a class implement an interface (like KeyListener) and define the method without even using it.
For example, KeyListener requires you to implement three different methods:
public void keyPressed(KeyEvent e);
public void keyReleased(KeyEvent e);
public void keyTyped(KeyEvent e);
Let's say I only really care about keyPressed. Then my class might look something like this:
public class MyKeyListener implements KeyListener {
#Override
public void keyPressed(KeyEvent e) {
// do stuff
}
#Override
public void keyReleased(KeyEvent e){}
#Override
public void keyTyped(KeyEvent e){}
}

Image is not displaying in JFrame

My image was displaying properly before I had a JButton on top of it. Now that I have added a JButton to my code, my image does not display. In the ActionPerformed method I am telling the button to setVisbible(false). When I click the button, it disapears and all that is behind it is the background.
public class Main extends JFrame implements ActionListener {
public static void main(String[] args) {
Main main = new Main();
}
ImageIcon GIF = new ImageIcon("src/Zombie Steve gif.gif");
JButton button = new JButton("Click me!");
JLabel Label = new JLabel(GIF);
public Main() {
button.addActionListener(this);
Label.setHorizontalAlignment(0);
JFrame Frame = new JFrame("zombieSteveGIF");
Frame.setSize(650, 650);
Frame.setVisible(true);
Frame.add(Label);
Frame.add(button);
Frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
while (true) {
Frame.getContentPane().setBackground(Color.BLUE);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Frame.getContentPane().setBackground(Color.GREEN);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Frame.getContentPane().setBackground(Color.RED);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent e) {
button.setVisible(false);
}
}
Your problem is that you have a BorderLayout (the default for JFrames), and you are adding two components in the same position. The default is BorderLayout.CENTER, and by adding two components with just the default constraints, the first one is removed and the second put in its place.
As for fixing your problem, what do you want to achieve? If you want the components to show on top of one another, you can use the OverlayLayout. If you don't want this, try some other layout manager.

Unable to use Sea Glass Look and Feel together with JFreeChart

When i used Sea-glass look and feel with JFreechart, the chart appreared normally but Sea-Glass didn't change the look and feel at all, although when sea-glass was used without Jfreechart, it worked.
So, what could be the reason?
public static void main(String[] args) throws Exception{
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel("com.seaglasslookandfeel.SeaGlassLookAndFeel");
JFreeChart chart = chart_producer(url) //my own static method to make the chart//
JFrame frame = new JFrame();
JPanel panel = new JPanel();
ChartPanel chartPanel = new ChartPanel(chart);
//chartPanel.setPopupMenu(null);
//chartPanel.setMouseZoomable(false);
panel.add(chartPanel);
frame.add(panel);
frame.pack();
frame.setVisible(true);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (UnsupportedLookAndFeelException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}
);
}
Note that JFreeChart is not a JComponent. It does not have a UI delegate to change. Alternatively, applying a ChartTheme may be effective.
Addendum: Although a chart is not a component, it may be added to a ChartPanel as part of an arbitrary L&F/layout, as shown here, here and here.

Categories