drag and drop of jcomponents in java in rum time - java

I can do this:
I have a JFrame with 2 JPanel objects. In the panel on the left, I want to put the components of Java Swing useful for the construction of an user interface. The panel on the right is empty.
On run-time, my user should to be able to copy with drag and drop drag from one panel to another the selected component. But the copied component on the right panel, can be moved or deleted.
I have created the panel, but I don't know if is better inserire the component as image, label or true component.. and how to make this possible..
This is my panel for the customization.. I have to insert the component in the JTabbedPane in the PanelSx..
public class Customize extends JFrame {
private JPanel panelSx, panelCx, panelMobile;
private JButton buttonSave;
private TabbedPaneComponents tpc;
public Customize(){
Container c = getContentPane();
c.setLayout(new BorderLayout());
setResizable(true);
setTitle("Design Preview");
setSize(800, 650);
setLocation(250,50);
panelSx = new JPanel();
panelSx.setPreferredSize(new Dimension(300, 200));
panelSx.setOpaque(true);
panelSx.setBackground(Color.RED.darker());
panelCx = new JPanel();
panelCx.setPreferredSize(new Dimension(200, 200));
panelCx.setOpaque(true);
panelCx.setBackground(Color.BLUE);
// display panel
panelMobile = new JPanel();
panelMobile.setPreferredSize(new Dimension(300,500));
panelMobile.setOpaque(true);
panelMobile.setBackground(Color.PINK.darker());
panelMobile.setFocusable(false);
buttonSave = new JButton("Save");
panelCx.add(panelMobile);
c.add(panelSx, BorderLayout.WEST);
c.add(panelCx, BorderLayout.CENTER);
tpc = new TabbedPaneComponents();
panelSx.add(tpc);
}
}
public class TabbedPaneComponents extends JTabbedPane{
private JPanel panel1,panel2,panel3;
public TabbedPaneComponents(){
panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(200,300));
addTab("Form Widgets", panel1);
panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(200,300));
addTab("Text Field", panel2);
panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(200,300));
addTab("Other", panel3);
}
}

I would have to suggest using a JToolBar because of it's built in drag'n'drop features. I would put the component inside of the toolbar.

Related

Contents of JScrollPane are not visible

I have a main class:
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Hex");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent inputs = new InputPanel();
JComponent hexGrid = new HexGridPanel(10,10,30);
JComponent outputs = new OutputPanel();
JComponent toolbar = new ToolbarPanel(); // This one is having problems
Container pane = frame.getContentPane();
pane.add(inputs, BorderLayout.LINE_START);
pane.add(hexGrid, BorderLayout.CENTER);
pane.add(outputs, BorderLayout.LINE_END);
pane.add(toolbar, BorderLayout.PAGE_END); // This one is having problems
frame.pack();
frame.setVisible(true);
}
}
And all of my other panels work except for ToolbarPanel that for some reason does not show its content:
public ToolbarPanel(){
JPanel content = new JPanel();
content.setLayout(new BoxLayout(content, BoxLayout.X_AXIS));
ButtonGroup buttonGroup = new ButtonGroup();
JRadioButton button = new JRadioButton("Test");
buttonGroup.add(button );
content.add(button );
JScrollPane scroll = new JScrollPane(content);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
content.setBorder(new LineBorder(Color.RED));
scroll.setBorder(new LineBorder(Color.GREEN));
this.add(scroll);
this.setPreferredSize(new Dimension(900, 200));
this.setBorder(new LineBorder(Color.BLACK)); // Only this is showing up in the UI
}
The ToolbarPanel itself shows up, but not the scroll pane or the radio buttons. It should show up inside of the black rectangle at the bottom of this image:
Well, you didn't include a MRE and I don't see the declaration of your class but I'm guessing you are using:
public class ToolbarPanel extends JComponent
The problem is that by default a JComponent doesn't have a layout manager so you won't see your components.
If you use:
public class ToolbarPanel extends JPanel
It will be a little better, but all the components will be displayed in a small square.
So you will also want to add
setLayout( new BorderLayout() );
to your constructor.
Note:
This is why a minimal reproducible example should be included with every question. We should not have to spend time guessing what you may or may not be doing.

How to have a JButton that moves as I scroll on the frame?

I have a Panel which I have made scrollable in my frame.
What I need is to add a button that stays fixed in the lower right corner even when I scroll.
I'm new to Java Swing so would appreciate all and any help that I can get.
mainPanel = new SimulationPanel(); //class SimulationPanel extends JPanel
//making mainPanel scrollable
mainPanel.setPreferredSize(new Dimension(((int)(WIDTH*1.2)), HEIGHT));
JScrollPane scrollPane = new JScrollPane(mainPanel);
scrollPane.setViewportView(mainPanel);
// Settings for JFrame
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame = new JFrame("Warehouse Simulator");
frame.setContentPane(scrollPane);
frame.setSize(screenSize.width, screenSize.height);
frame.setResizable(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
I would use nested panels with the outer one be with BorderLayout. Then one with FlowLayout and align FlowLayout.RIGHT and the button inside it.
public class Example extends JFrame {
public Example() {
super("");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JTextArea textArea = new JTextArea(10000, 0);
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane, BorderLayout.CENTER);
JButton button = new JButton("button");
JPanel panelWithButton = new JPanel(new FlowLayout(FlowLayout.RIGHT));
panelWithButton.add(button);
add(panelWithButton, BorderLayout.PAGE_END);
setLocationByPlatform(true);
pack();
setSize(600, 600);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new Example().setVisible(true);
});
}
}
Result:
I would go for a BoxLayout. Add another panel (metaPanel) in which your first put your scrollingPanel, and then you add a button. Instead of usgin scrollingPanel as contentPane, you use metaPanel. Example (the example works, but you need to modify it to make the interface look nice):
JPanel mainPanel = new JPanel();
JScrollPane scrollPane = new JScrollPane(mainPanel);
scrollPane.setViewportView(mainPanel);
JPanel metaPanel = new JPanel();
BoxLayout boxlayout = new BoxLayout(metaPanel, BoxLayout.Y_AXIS);
metaPanel.setLayout(boxlayout);
metaPanel.add(scrollPane);
metaPanel.add(new JButton("button"));
// Settings for JFrame
frame = new JFrame("Warehouse Simulator");
frame.setContentPane(metaPanel); // Put metaPanel here
frame.setSize(500, 300);
frame.setResizable(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);

BoxLayout only showing last component added

Looked at some previous posts pertaining to my subject but too no avail.
Trying to align components using BoxLayout but I cannot get it to work. I have tinkered with it for some time now with different results but I can't figure it out. I have used the default FlowLayout with no problems, I am trying to learn and expand my knowledge and BoxLayout will be better for my program. I want everything to stay in alignment if the User resizes their application window. I've adjusted all the sizes this way after just trying to get it to work and failing.
package GUI;
import javax.swing.*;
import java.awt.*;
/**
* Created by Thunderfoot on 7/31/2016. Keep Growing!
* Graphical User Interface
* Needs 3 JPanels(Text area + scroll pane)(2 Buttons) (1 Button), a JTextArea, JScrollPane, and 3 JButtons
*/
public class PrimaryFrame extends JFrame {
//Class variables
private static JPanel panel1, panel2, panel3;
public static JTextArea output;
//Constructor
public PrimaryFrame() {
//Frame component attributes
final Dimension FRAME_SIZE = new Dimension(400, 400);
final Dimension PANEL1_SIZE = new Dimension(400, 250);
final Dimension PANEL2_SIZE = new Dimension(400, 40);
final Dimension PANEL3_SIZE = new Dimension(400, 40);
//JFrame is PrimaryFrame
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(FRAME_SIZE);
setMaximumSize(FRAME_SIZE);
setTitle("Fighting Game");
//JPanel for Text
panel1 = new JPanel();
panel1.setLayout(new BoxLayout(panel1, BoxLayout.PAGE_AXIS));
panel1.setMinimumSize(PANEL1_SIZE);
panel1.setPreferredSize(PANEL1_SIZE);
panel1.setMaximumSize(PANEL1_SIZE);
panel1.setBackground(Color.BLACK);
//JPanel for Attack and Kick Buttons
panel2 = new JPanel();
panel2.setLayout(new BoxLayout(panel2, BoxLayout.LINE_AXIS));
panel2.setMinimumSize(PANEL2_SIZE);
panel2.setPreferredSize(PANEL2_SIZE);
panel2.setMaximumSize(PANEL2_SIZE);
panel2.setBackground(Color.BLUE);
//JPanel for Power Attack Button
panel3 = new JPanel();
panel3.setLayout(new BoxLayout(panel3, BoxLayout.PAGE_AXIS));
panel3.setMinimumSize(PANEL3_SIZE);
panel3.setPreferredSize(PANEL3_SIZE);
panel3.setMaximumSize(PANEL3_SIZE);
panel3.setBackground(Color.ORANGE);
panel3.add(Box.createHorizontalGlue());
panel3.add(Box.createVerticalGlue());
//JTextArea & JScrollPane
output = new JTextArea();
output.setEditable(false);
JScrollPane outputScroller = new JScrollPane(output, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
outputScroller.setMaximumSize(new Dimension(375, 250));
outputScroller.setBorder(BorderFactory.createLineBorder(Color.RED));
panel1.add(outputScroller);
panel1.add(Box.createHorizontalGlue());
panel1.add(Box.createVerticalGlue());
//Attack Button
JButton attackButton = new JButton(" ATTACK ");
attackButton.setMaximumSize(new Dimension(75, 30));
attackButton.setBorderPainted(true);
//Kick Button
JButton kickButton = new JButton(" KICK ");
kickButton.setMaximumSize(new Dimension(75, 30));
kickButton.setBorderPainted(true);
//Add components
panel2.add(attackButton);
panel2.add(Box.createHorizontalGlue());
panel2.add(Box.createVerticalGlue());
panel2.add(kickButton);
panel2.add(Box.createHorizontalGlue());
panel2.add(Box.createVerticalGlue());
//Power Attack Button
JButton powAttButton = new JButton(" POWER ATTACK ");
powAttButton.setMaximumSize(new Dimension(150, 30));
powAttButton.setBorderPainted(true);
panel3.add(powAttButton);
panel3.add(Box.createHorizontalGlue());
}
public void buildGUI() {
//Add components and build GUI Frame
this.add(panel3);
this.add(panel2);
this.add(panel1);
//Set attributes
//Pack components together inside of frame
pack();
//Center of screen
setLocationRelativeTo(null);
//Make frame visible
setVisible(true);
}
}
You have to set the Layout of your PrimaryFrame.
I suggest you add an additional line to your buildGUI() method:
public void buildGUI() {
//defines the Layout for the main Frame
this.setLayout(new GridLayout(3,1)) //its up to you wich Layout you use
//Add components and build GUI Frame
this.add(panel3);
this.add(panel2);
this.add(panel1);
//Set attributes
//Pack components together inside of frame
pack();
//Center of screen
setLocationRelativeTo(null);
//Make frame visible
setVisible(true);
}
Notice GridLayout(3,1) will generate a layout with three rows and one column

Stop expansion of JScrollPane within a BoxLayout

The JScrollPane in the JPanel below expands to fill the Frame. How can I stop this expansion so that the size of the JScrollPane is the size of the JButton within it?
public class GUITest extends JFrame {
public GUITest() {
setPreferredSize(new Dimension(700, 500));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
add(mainPanel);
JButton button1 = new JButton();
JButton button2 = new JButton();
JButton button3 = new JButton();
JScrollPane pane1 = new JScrollPane();
mainPanel.add(button1);
mainPanel.add(pane1);
pane1.setViewportView(button2);
mainPanel.add(button3);
//Display the window.
super.pack();
super.setLocationRelativeTo(null);
super.setVisible(true);
}
}
I tried
pane1.setMaximumSize(button2.getPreferredSize());
but the scrollbars appear and I can't see the button. Also, in my real program, the components within the JScrollPane will be dynamically added during runtime so I will need the JScrollPane to expand for this.
Use
pane1.setPreferredSize(button2.getPreferredSize());
If you still have autoresizing problems fix the panel size with:
pane1.setMaximumSize(button2.getPreferredSize());
pane1.setMinimumSize(button2.getPreferredSize());

How can I make a layout like the attached image

I am trying to design a layout which contains a form and couple of items. but I found it too hard to put items in right places.
In the following image, the right frame is what I am aiming to design and the left on is what I could made.
And this is the code for the right frame:
public class GUI extends JFrame{
public GUI(){
JFrame frame = new JFrame("frame");
frame.setSize(600, 600);
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JLabel("Title"), BorderLayout.NORTH);
JPanel formPanel = new JPanel(new GridLayout(1,2));
panel.add(formPanel);
TitledBorder formPanelTitle = BorderFactory.createTitledBorder("GridLayout(1,2)");
formPanel.setBorder(formPanelTitle);
//LEFT PANEL
JPanel labelsPanel = new JPanel(new GridLayout(4,1));
TitledBorder labelsPanelTitle = BorderFactory.createTitledBorder("GridLayout(4,1)");
labelsPanel.setBorder(labelsPanelTitle);
labelsPanel.add(new JLabel("Label 1"));
labelsPanel.add(new JLabel("Label 2"));
labelsPanel.add(new JLabel("Label 3"));
labelsPanel.add(new JLabel("Label 4"));
formPanel.add(labelsPanel);
//RIGHT PANEL
JPanel fieldsPanel = new JPanel(new GridLayout(4,1));
TitledBorder fieldsPanelTitle = BorderFactory.createTitledBorder("GridLayout(4,1)");
fieldsPanel.setBorder(fieldsPanelTitle);
fieldsPanel.add(new JTextField("Label 1"));
fieldsPanel.add(new JTextField("Label 2"));
fieldsPanel.add(new JTextField("Label 3"));
fieldsPanel.add(new JTextField("Label 4"));
formPanel.add(fieldsPanel);
//BOTTOM PANEL
JPanel bottomPanel = new JPanel(new GridLayout(2,1));
TitledBorder BottomPanelTitle = BorderFactory.createTitledBorder("GridLayout(2,1)");
bottomPanel.setBorder(BottomPanelTitle);
panel.add(bottomPanel, BorderLayout.SOUTH);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(new JButton("Browse"));
buttonPanel.add(new JLabel("Label"));
TitledBorder buttonPanelTitle = BorderFactory.createTitledBorder("FlowLayout()");
buttonPanel.setBorder(buttonPanelTitle);
bottomPanel.add(buttonPanel);
JPanel secondButtonPanel = new JPanel(new GridLayout(1,2));
secondButtonPanel.add(new JButton("Back"));
secondButtonPanel.add(new JButton("Next"));
TitledBorder secondButtonPanelTitle = BorderFactory.createTitledBorder("GridLayout(1,2)");
secondButtonPanel.setBorder(secondButtonPanelTitle);
bottomPanel.add(secondButtonPanel);
frame.add(panel);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
// TODO code application logic here
new GUI();
}
}
I am not sure if the code is really optimal, since there are a lot of inner panels and made it too complicated. Also I could not put items in the places I wanted to. Is there any suggestion or idea to make this layout look better?
Create a JPanel, using GridBagLayout and add your labels/fields to it, this forms the "center" portion of your layout.
Create a JPanel and add the Browse button a JLabel to it. Using GridBagConstraints#gridwidth set to REMAINDER, add this to your first panel
Create a JPanel, using BorderLayout, add the first panel to the CENTER position. Add the title Label to the NORTH position, you may need to adjust it's horizontalAlignment property
Create a JPanel using FlowLayout, aligned to the RIGHT and add your "Back" and "Next" buttons to it. Add this to the SOUTH position of the previous panel.
Check out Laying Out Components Within a Container for more details

Categories

Resources