Scrollbar in JEditorPane in a Rectangle - java

I have a JEditorPane that is inside a rectangle in a window so that it doesnt fill the whole window. However when the HTML page gets too big, it cuts it off instead of putting in a scrollbar, I know I need a JScrollpane or something, but I dont want the scrollbar on the whole window, only inside the rectangle.
Here is a snippet of my code (rweb is already defined as a Rectangle):
JEditorPane web = new JEditorPane();
web.setEditable(false);
try {
web.setPage("http://www.google.com");
}catch (IOException e) {
web.setContentType("text/html");
web.setText("<html>Could not load webpage</html>");
}
web.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
if(Desktop.isDesktopSupported()) {
try {
Desktop.getDesktop().browse(e.getURL().toURI());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
});
rweb = new Rectangle(0, 0, width, 600);
web.setBorder(null);
web.setBounds(rweb);
window.add(web);

You never add the JEditorPane to a JScrollPane. Text components don't have scroll support by themselves.
Try using window.add(new JScrollPane(web)); instead
Take a look at How to use scroll panes for more details
ps- web.setBounds(rweb); will only work if you are using a null layout and if you are, you'd be better of using a EmptyBorder or a layout manager that provided the ability to specify insets, like GridBagLayout

Related

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)

How to add Image as header in JEditorPane

I want to print Header with some text and a specific logo and footer with some text and page no.
How to add image on header?
public class JEditorPaneTest {
public static void main(String args[]) {
JEditorPane pane = new JEditorPane();
JScrollPane js = new JScrollPane(pane);
try {
URL url = new URL("file:C:/temp/html/12.html");
// File f=new File("C:/temp/html/12.html");
pane.setPage(url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pane.setContentType("text/html");
JFrame frmae = new JFrame();
frmae.setSize(200, 300);
try {
MessageFormat header = new MessageFormat("Order Details History");
MessageFormat footer = new MessageFormat(" Page #{0,number,integer}");
pane.print(header, footer);
} catch (PrinterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// frmae.add(pane);
frmae.add(js);
frmae.setVisible(true);
}
}
http://java-sl.com/JEditorPanePrinter.html this one is editor kit independent printing
http://java-sl.com/Pagination_In_JEditorPane_HF.html this one if you need WYSIWYG editor.
Or you can jus override paintComponent() method and after calling super draw your images over the content.

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.

How to manage a JInternalFrame calling another JInternalFrame?

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();
}

jpanel dont draw a buffered image but the buffered image can be created as file

Hi i have a jpanel to draw on. from this jpanel i make bufferedimages and safe them into a linkedlist. on a button press i want to animate this bufferedimages(play one after one)
the problem is that the jpanel dont show the buffered images but when i use ImageIO.write to safe the bufferedimages on disk i get all the pictures i want to animate. pls help me.
here is my code:
public void run(){
for(int i=0;i`<`cm.animationListe.size();i++){
b= cm.animationListe.get(i);
try {
ImageIO.write( b, "png", new File( "c:/java/circle"+i+".png" ) );
} catch (IOException e1) {
e1.printStackTrace();
}
try {
Thread.sleep(1000);
repaint();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
animation = false;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if(animation){
g.drawImage(b,0, 0,null);
}
}
The Thread.sleep() causes the GUI to freeze so it can't repaint itself.
To do animation you need to use a Swing Timer.

Categories