Reputation: 55
I have in the little bit of code below a JWindow
with a JTextPane
in it. When I run the full code in Netbeans it creates a nice window, but the JTextPane
won't respond to mouse or keyboard events. When I mouse over it, the text mouse icon appears, but when I click on it, it selects the Netbeans window behind it. I've tried to call the setFocusable
method of the JTextPane
with true
and called grabFocus
as well.. How can I get the JTextPane
to respond to mouse and keyboard events?
I have two files. One is called MainClass.java
and just creates a new MainWindow
object. MainWindow.java
is where the problem is. It has a artificial title bar, two sides, and the centered JTextPanel
that is giving me problems.
Please note that using a JFrame
is not an option.
Thanks!
package Window;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Rectangle;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.JWindow;
import javax.swing.SpringLayout;
public class MainWindow extends JWindow{
private Color TitleColor = new Color(18, 15, 65);
private Color BGColor = new Color(50, 50, 50);
public MainWindow(){
GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
Rectangle MaxBounds = env.getMaximumWindowBounds();
SpringLayout Layout = new SpringLayout();
Container ContentPane = this.getContentPane();
this.setLayout(Layout);
JPanel TitleBar = new JPanel();
TitleBar.setBackground(TitleColor);
TitleBar.setPreferredSize(new Dimension(0, 30));
this.add(TitleBar);
JPanel LeftPanel = new JPanel();
LeftPanel.setBackground(BGColor);
this.add(LeftPanel);
JPanel RightPanel = new JPanel();
RightPanel.setBackground(BGColor);
this.add(RightPanel);
JTextPane TextPane = new JTextPane();
TextPane.setBackground(Color.WHITE);
TextPane.setForeground(Color.BLACK);
TextPane.setPreferredSize(new Dimension(700, 0));
TextPane.setFocusable(true);
TextPane.setMargin(new Insets(20,20,20,20));
TextPane.grabFocus();
this.add(TextPane);
Layout.putConstraint(SpringLayout.EAST, TitleBar, 0, SpringLayout.EAST, ContentPane);
Layout.putConstraint(SpringLayout.WEST, TitleBar, 0, SpringLayout.WEST, ContentPane);
Layout.putConstraint(SpringLayout.NORTH, TitleBar, 0, SpringLayout.NORTH, ContentPane);
Layout.putConstraint(SpringLayout.NORTH, TextPane, 0, SpringLayout.SOUTH, TitleBar);
Layout.putConstraint(SpringLayout.SOUTH, TextPane, 0, SpringLayout.SOUTH, ContentPane);
Layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, TextPane, 0, SpringLayout.HORIZONTAL_CENTER, ContentPane);
Layout.putConstraint(SpringLayout.NORTH, LeftPanel, 0, SpringLayout.SOUTH, TitleBar);
Layout.putConstraint(SpringLayout.SOUTH, LeftPanel, 0, SpringLayout.SOUTH, ContentPane);
Layout.putConstraint(SpringLayout.EAST, LeftPanel, 0, SpringLayout.WEST, TextPane);
Layout.putConstraint(SpringLayout.WEST, LeftPanel, 0, SpringLayout.WEST, ContentPane);
Layout.putConstraint(SpringLayout.NORTH, RightPanel, 0, SpringLayout.SOUTH, TitleBar);
Layout.putConstraint(SpringLayout.SOUTH, RightPanel, 0, SpringLayout.SOUTH, ContentPane);
Layout.putConstraint(SpringLayout.EAST, RightPanel, 0, SpringLayout.EAST, ContentPane);
Layout.putConstraint(SpringLayout.WEST, RightPanel, 0, SpringLayout.EAST, TextPane);
this.setBounds(MaxBounds);
this.setVisible(true);
}
}
package Window;
import javax.swing.SwingUtilities;
public class MainClass {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run(){
new MainWindow();
}
});
}
}
Upvotes: 3
Views: 125
Reputation: 109813
JTextComponents
placed in JWindow
without parent isn't editable etc.
you would need to set JFrame (never need to be visible)
as parent, then everything works
Upvotes: 1