How do I fix aligning issues when using JFXPanel? - swing

My problem is, that i have a normal JavaFX fxml file, which automatically realigns its elements when i start it normally and resize it. But i when i start it with the JFXPanel suddenly nothing realigns anymore. And i have no idea what the reason could be. (I have to use this method to start my gui, there is no other way)
My code:
final JFXPanel panel = new JFXPanel();
panel.addComponentListener(new ComponentAdapter() {
#Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
// Maybe here some code?
}
});
JFrame frame = new JFrame();
Platform.runLater(() -> {
try {
Group root = new Group();
Scene scene = new Scene(root);
FXMLLoader loader = new FXMLLoader(getClass().getResource("myGui.fxml"));
Node node = loader.load();
root.getChildren().add(node);
panel.setScene(scene);
} catch (IOException e) {
e.printStackTrace();
}
});
frame.add(panel);
frame.pack();
frame.setVisible(true);
I researched for hours and don't really find a solution to my problem. Maybe i searched wrong? i would be really grateful if someone could help me.
Im a bit suspicious with the Group object "root" but i have no clue what it does. (I have this code from a website, because im inexperienced with JFXPanels so im not entirely sure what every line does)
My whole class:
public class Test {
public static void main(String[] args) {
Test test = new Test();
test.startGui();
}
public void startGui() {
final JFXPanel panel = new JFXPanel();
panel.setPreferredSize(new Dimension(600, 400));
panel.addComponentListener(new ComponentAdapter() {
#Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
// Maybe here some code?
}
});
JFrame frame = new JFrame();
Platform.runLater(() -> {
try {
Group root = new Group();
Scene scene = new Scene(root);
FXMLLoader loader = new FXMLLoader(getClass().getResource("myGui.fxml"));
Node node = loader.load();
root.getChildren().add(node);
panel.setScene(scene);
} catch (IOException e) {
e.printStackTrace();
}
});
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
And my fxml file
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<StackPane prefHeight="150.0" prefWidth="200.0" HBox.hgrow="ALWAYS">
<children>
<Label text="left" />
</children>
</StackPane>
<StackPane prefHeight="150.0" prefWidth="200.0" HBox.hgrow="ALWAYS">
<children>
<Label text="right" />
</children>
</StackPane>
</children>
</HBox>
When you start the gui and resize it the left and right label should go further away. They should have the same distance to the borders of the gui.

You are adding the JFXPanel to a JFrame which uses BorderLayout by default.
To change the resizing and positioning of the JFXPanel set other layout manager.
For example:
frame.setLayout(new GridBagLayout());

Related

JavaFX ChoiceBox ContextMenu shown again when it is displayed and clicked again

I have JavaFX panel with ChoiceBox in Swing application. Standard behaviour of ChoiceBox is that when you click it for the first time the popup menu with items is shown and when you click ChoiceBox for the second time, the popup menu is hidden. But when you put it to Swing application the second click causes popup to hide and to be shown immediately again. How can I prevent this behaviour?
public class ComboTest {
private static void initAndShowGUI() {
JFrame frame = new JFrame("FX");
final JFXPanel fxPanel = new JFXPanel();
fxPanel.setPreferredSize(new Dimension(100, 100));
frame.add(fxPanel);
frame.pack();
frame.setVisible(true);
Platform.runLater(new Runnable() {
#Override
public void run() {
initFX(fxPanel);
}
});
}
private static void initFX(JFXPanel fxPanel) {
// This method is invoked on JavaFX thread
Scene scene = createScene();
fxPanel.setScene(scene);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
initAndShowGUI();
}
});
}
private static Scene createScene() {
ChoiceBox choiceBox = new ChoiceBox(FXCollections.observableArrayList("item 1", "item 2"));
VBox vbox = new VBox(choiceBox);
return new Scene(vbox);
}
}
My suspicion is that when I click the choicebox for the second time the popup loses focus which causes it to hide and the choicebox then handles mouse click and shows the popup again.
I believe that this problem caused by the existing ChoiceBox bug in javafx.
The simplest fix is just to use ComboBox instead:
ComboBox<String> choiceBox = new ComboBox<>(FXCollections.observableArrayList("item 1", "item 2"));

Thumb.DragStarted event not working in visual studio 2013

I am trying to make a small music player for windows phone. I have added a slider functionality in the player. The slider works fine as the music plays. But I want to change the media according to how much i drag the slider, but cannot find any relevant event for it. I have tried value changed but it does not help. Also I tried Thumb.Dragstarted event but my visual studio gives an error.. this is the code so far:
XAML:
<Slider AllowDrop="True" x:Name="sld1" Thumb.DragStarted="sld1_DragStarted" HorizontalAlignment="Left" Margin="58,213,0,0" VerticalAlignment="Top" Width="351" ValueChanged="sld1_ValueChanged"/>
<MediaElement x:Name="bleep" Source="abcd.wav" AutoPlay="False" Visibility="Collapsed" MediaEnded="bleep_MediaEnded"/>
C#:
public Page1()
{
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += timer_Tick;
timer.Start();
}
private bool userIsDraggingSlider = false;
private void timer_Tick(object sender, EventArgs e)
{
if ((bleep.Source != null) && (bleep.NaturalDuration.HasTimeSpan) && (!userIsDraggingSlider))
{
sld1.Minimum = 0;
sld1.Maximum = bleep.NaturalDuration.TimeSpan.TotalSeconds;
sld1.Value = bleep.Position.TotalSeconds;
}
}
private void sld1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
tm_passed.Text = TimeSpan.FromSeconds(sld1.Value).ToString(#"mm\:ss");
}
private void sld1_DragStarted(object sender, DragStartedEventArgs e)
{
userIsDraggingSlider = true;
}
private void sld1_DragCompleted(object sender, DragCompletedEventArgs e)
{
userIsDraggingSlider = false;
bleep.Position = TimeSpan.FromSeconds(sld1.Value);
}
But since the DragCompleted and DragStarted events are not working I cannot provide the drag functionality to the slider.
What I identified from the Thumb class is that, you can't simply add Thumb.DragStarted="sld1_DragStarted within your Slider. You'll be able to add that kind of event only for Thumb control. Refer the bottom of the article for sample code.

Swing UI Delay adding and removing elements

I have a form stored in both the inputWidget and the outputWidget. The buttons addInput and addOutput will show two different forms in the secondaryInOutPanel.
However there is a significant delay when moving between the form by clicking the buttons. In fact it changes when I attempting to click on the form. And there is still some visible drawings from the pervious form.
I tried using SwingUtilities but that caused the delay to be worst.
secondaryInOutPanel = new JPanel(new BorderLayout());
secondaryInOutPanel.setMinimumSize(new Dimension(200,400));
JPanel btnPanel = new JPanel();
outinPanel.add(btnPanel, BorderLayout.NORTH);
JButton addInput = new JButton("Add Input");
btnPanel.add(addInput);
addInput.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
secondaryInOutPanel.removeAll();
secondaryInOutPanel.add(inputWidget, BorderLayout.NORTH);
JButton addBtn = new JButton("Save Input");
secondaryInOutPanel.add(addBtn, BorderLayout.SOUTH);
addBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
}
});
JButton addOutput = new JButton("Add Output");
btnPanel.add(addOutput);
addOutput.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
secondaryInOutPanel.removeAll();
secondaryInOutPanel.add(outputWidget, BorderLayout.NORTH);
JButton addBtn = new JButton("Save Output");
secondaryInOutPanel.add(addBtn, BorderLayout.SOUTH);
addBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
}
});
A better design is to use a Card Layout to hold your input and output panels. Then you can just swap panels as required. The CardLayout will then manage the revalidating and repainting of the panel for you.
You need to make a call to revalidate() and or repaint() on the secondaryInOutPanel after you make changes.

Why JScrollPane does not react to mouse wheel events?

I have a JScrollPane containing a panel with a BoxLayout (PAGE AXIS).
My problem is that the JScrollPane does not react to mouse wheel events. To make it scroll using the mouse wheel i need to be on the JScrollBar.
I found this thread and i have no MouseMotionListener or MouseWheelListener, only a MouseListener. I think my problem come from the fact that my JScrollPane act on a JPanel that contains other panels itself. So when the mouse is on a panel within the JScrollPane it seems that the event is consumed by this panel i never seen by the scroll pane.
Is there a correct way to make the events caught by the children of the scroll pane visible to this scroll pane?
SSCCE:
Here a simple test case trying to show when i try to do in my Swing application.
The frame:
public class NewJFrame extends javax.swing.JFrame {
public NewJFrame() {
initComponents();
for (int i = 0; i < 50; i++) {
jPanel1.add(new TestPanel());
}
}
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jPanel1 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.PAGE_AXIS));
jScrollPane1.setViewportView(jPanel1);
getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new NewJFrame().setVisible(true);
}
});
}
}
And the TestPanel definition:
public class TestPanel extends javax.swing.JPanel {
public TestPanel() {
initComponents();
}
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jLabel1.setText("jLabel1");
setBackground(new java.awt.Color(255, 51, 51));
setLayout(new java.awt.BorderLayout());
jLabel2.setText("TEST LABEL");
jLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
add(jLabel2, java.awt.BorderLayout.PAGE_START);
jTextArea1.setEditable(false);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jTextArea1.setFocusable(false);
jScrollPane1.setViewportView(jTextArea1);
add(jScrollPane1, java.awt.BorderLayout.CENTER);
}
}
The JTextArea seems to consume the event since when the mouse cursor is inside it, the scrolling using wheel does not work. I have to put the mouse cursor outside the text area to make it works again.
Walter beat me to analysing the issue :-)
Adding a bit of detail:
It's correct that a JScrollPane supports mouseWheelHandling. According to the rules of mouseEvent dispatching, the top-most (in z-order) component gets the event, and that's the scrollPane around the textArea. So if wheeling the textarea is not required, a simple solution might be to disable the wheel-support in its scrollPane. And JScrollPane even has api for doing it:
scrollPane.setWheelScrollingEnabled(false);
Unfortunately, that doesn't work. Reason it's not working is that this property has no effect in the event dispatch chain which ultimately calls into eventTypeEnabled:
case MouseEvent.MOUSE_WHEEL:
if ((eventMask & AWTEvent.MOUSE_WHEEL_EVENT_MASK) != 0 ||
mouseWheelListener != null) {
return true;
}
This returns true if a mouseWheelListener is installed - which is done unconditionally by BasicScrollPaneUI, and not removed when the wheelEnabled property is changed (the ui doesn't even listen to that property ...) Plus the listener simply does nothing if the property is false. At least one of those facts is a bug, the ui should
either remove/add the listener depending on wheelEnabled
or: implement the listener such that it dispatches the event up the chain (as Walter does in his example)
The first option can be handled by application code:
scrollPane = new JScrollPane();
scrollPane.removeMouseWheelListener(scrollPane.getMouseWheelListeners()[0]);
it's a bit of a hack (as bug-workarounds always are :-), production code would have to listen to the wheelEnable to re-install if needed plus listen to LAF changes to update/re-remove the listeners installed by the ui.
Implementing the second option in slight modification (as to Walter's dispatching) by subclassing the JScrollPane and dispatch the event to parent if the wheelEnabled is false:
scrollPane = new JScrollPane() {
#Override
protected void processMouseWheelEvent(MouseWheelEvent e) {
if (!isWheelScrollingEnabled()) {
if (getParent() != null)
getParent().dispatchEvent(
SwingUtilities.convertMouseEvent(this, e, getParent()));
return;
}
super.processMouseWheelEvent(e);
}
};
scrollPane.setWheelScrollingEnabled(false);
The mouse wheel event gets consumed by the scroll pane around the text area. You can try to manually pass the event to the parent scroll pane like this:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestScrollPane2 {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
// might want to use a http://tips4java.wordpress.com/2009/12/20/scrollable-panel/
JPanel panel = new JPanel(new GridLayout(0, 1));
for (int i = 0; i < 10; i++) {
panel.add(new JScrollPane(new JTextArea(3, 40)) {
#Override
protected void processMouseWheelEvent(MouseWheelEvent e) {
Point oldPosition = getViewport().getViewPosition();
super.processMouseWheelEvent(e);
if(getViewport().getViewPosition().y == oldPosition.y) {
delegateToParent(e);
}
}
private void delegateToParent(MouseWheelEvent e) {
// even with scroll bar set to never the event doesn't reach the parent scroll frame
JScrollPane ancestor = (JScrollPane) SwingUtilities.getAncestorOfClass(
JScrollPane.class, this);
if (ancestor != null) {
MouseWheelEvent converted = null;
for (MouseWheelListener listener : ancestor
.getMouseWheelListeners()) {
listener.mouseWheelMoved(converted != null ? converted
: (converted = (MouseWheelEvent) SwingUtilities
.convertMouseEvent(this, e, ancestor)));
}
}
}
});
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(new JScrollPane(panel));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}

Not showing images in JToolBar

JButton btnCalendar = new JButton("Chart",new ImageIcon("new_chart.jpg"));
btnCalendar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), "Chart clicked");
}
});
jToolBar1.add(btnCalendar);
jToolBar1.addSeparator();
I'm developing a Swing based application in which I want to add JToolBar with images in buttons.But images are not visible in the buttons
I face same problem and I resolved by
use of following code :
ImageIcon newImage= new ImageIcon(getClass().getResource("com/some/package/newItemImage.jpg"));
JButton newButton= new JButton(newImage);
this.add(newButton);