Firefox 'Open new tab' on JTabbedPane - swing

I want to add a button to JTabbedPane's title bar (similar to the 'open new tab' ('+') button in Firefox)
I have tried to add to the glass pane of JTabbedPane's container. but since my tabbedpane contains within a JPanel seems it doesn't work for me.
Any suggestion will be a great help for me.
Thank you.

Instead of adding a button I have tried it in a different way and worked for me... I have added a JLabel (with '+') as a hidden tab and when user tries to select that tab i'll be adding a new tab.
public class AddTabButtonDemo extends JFrame{
private JTabbedPane tabbedPane = new JTabbedPane();
public AddTabButtonDemo() {
JLabel tab1Label = new JLabel("tab1");
JPanel tab1 = new JPanel();
tab1.add(tab1Label);
tabbedPane.addTab("tab1", tab1);
tabbedPane.addTab("+", new JLabel());
tabbedPane.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
if (tabbedPane.getSelectedComponent() instanceof JLabel) {
int count = tabbedPane.getTabCount();
JLabel newTabLabel = new JLabel("tab" + count);
JPanel newTab = new JPanel();
newTab.add(newTabLabel);
tabbedPane.add(newTab, count - 1);
tabbedPane.setTitleAt(count - 1, "tab" + count);
tabbedPane.setSelectedComponent(newTab);
}
}
});
this.add(tabbedPane, BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setMinimumSize(new Dimension(300, 300));
this.setVisible(true);
}
public static void main(String[] args) {
new AddTabButtonDemo();
}
}

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"));

repaint() not invoking paintComponent()?

Hello I am trying to solve the following problem: Write a program that prompts the user to enter the x- and y-positions of a center point and a radius, using text fields. When the user clicks a "Draw" button, draw a circle with that center and radius in a component. I do not see what is wrong in my code but something is because it doesnt seem like repaint() is invoking paintComponent() as message will change to TESTING 1 but not TESTING 2 and no drawing is made.
My Code:
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.applet.*;
import java.awt.geom.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class q3{
public static class cgPanel extends JPanel{
private static double x;
private static double y;
private static double r;
private static JTextField xField;
private static JTextField yField;
private static JTextField rField;
private static JButton draw;
private static JLabel message;
//This is all just Layout work.
public cgPanel(){
setLayout(new BorderLayout());
JPanel drawPanel = new JPanel();
drawPanel.setBackground(Color.WHITE);
add(drawPanel, BorderLayout.CENTER);
message = new JLabel("");
JPanel sub1ForSub1 = new JPanel();
sub1ForSub1.add(message);
JLabel coordinates = new JLabel("Coordinates:");
JPanel sub2ForSub1 = new JPanel();
sub2ForSub1.add(coordinates);
JPanel subPanel1 = new JPanel();
subPanel1.setLayout(new GridLayout(2, 1));
subPanel1.add(sub1ForSub1);
subPanel1.add(sub2ForSub1);
JLabel xLabel = new JLabel("x:");
xField = new JTextField(4);
JLabel yLabel = new JLabel(" y:");
yField = new JTextField(4);
JLabel rLabel = new JLabel(" Radius:");
rField = new JTextField(4);
JPanel subPanel2 = new JPanel();
subPanel2.add(xLabel);
subPanel2.add(xField);
subPanel2.add(yLabel);
subPanel2.add(yField);
subPanel2.add(rLabel);
subPanel2.add(rField);
draw = new JButton("Draw");
ActionListener bL = new ButtonListener();
draw.addActionListener(bL);
JPanel subPanel3 = new JPanel();
subPanel3.add(draw);
JPanel Panel = new JPanel();
Panel.setLayout(new BorderLayout());
Panel.add(subPanel1, BorderLayout.NORTH);
Panel.add(subPanel2, BorderLayout.CENTER);
Panel.add(subPanel3, BorderLayout.SOUTH);
add(Panel, BorderLayout.SOUTH);
setVisible(true);
}
static class ButtonListener extends JComponent implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
String xString = xField.getText();
String yString = yField.getText();
String rString = rField.getText();
message.setText("TESTING 1");
x = Double.parseDouble(xString);
y = Double.parseDouble(yString);
r = Double.parseDouble(rString);
repaint();
}
catch (NumberFormatException exception){
message.setText("Please enter a number.");
}
}
//This is where I cant seem to get the code in paintComponent to run when the Draw button is pressed.
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
Ellipse2D.Double circle = new Ellipse2D.Double(x - r, y - r, r*2, r*2);
g2.draw(circle);
message.setText("TESTING 2");
}
}
}
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setSize(800, 800);
frame.setTitle("Circle Generator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cgPanel panel = new cgPanel();
frame.add(panel);
frame.setVisible(true);
}
}
So, a couple of things.
Your problem stems from that fact that your ButtonListener is extending a JComponent, so the repaint() method is calling the one for the ButtonListener (which really isn't a JComponent).
And the paintComponent method is also for the the ButtonListener.
Instead, you want your button listener to have access to your cgPanel, so it can tell IT to repaint. And your paintComponent needs to be moved to the cgPanel, but even then you probably don't want it there since you have a bunch of other components on cgPanel.
It's not clear from your code where you really want the circle to be drawn.
You should probably create a CirclePanel that extend JPanel, and overrides paintComponent to draw your circles, and then add that to your cgPanel. Then make your ButtonListener tell the CirclPanel instance to repaint.

Reloading component Frame

I'm Working on time schedule booking application , when I run the project it shows component(total frame) , however i want that when the button is pressed to reload ,component should be spitted.(two frames with one below other by spilt) ???
Based on your description I think you need to either add splitpane in frame on click of a button or you already have a splitpane in frame and want to add panel in it on click of button.
For first option you can do something like this:
final JFrame frame = new JFrame("Split test");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel jPanel2 = new JPanel();
JLabel jLabel = new JLabel("I am added by click on button");
jPanel2.add(jLabel);
final JPanel jPanel = new JPanel();
JButton button = new JButton("Click me to add pane in split");
jPanel.add(button);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
JSplitPane pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
pane.add(jPanel);
pane.add(jPanel2);
pane.setDividerLocation(frame.getHeight()/2); // set Divider location.
frame.remove(jPanel);
frame.add(pane);
frame.validate();
}
});
frame.add(jPanel);
frame.setVisible(true);
If you are stuck in later one then try this:
final JFrame frame = new JFrame("Split test");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JSplitPane pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
frame.add(pane);
pane.setEnabled(false); // stop user from clicking on divider of split pane.
final JPanel jPanel2 = new JPanel();
JLabel jLabel = new JLabel("I am added by click on button");
jPanel2.add(jLabel);
final JPanel jPanel = new JPanel();
JButton button = new JButton("Click me to add pane in split");
jPanel.add(button);
pane.add(jPanel);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
pane.add(jPanel2);
pane.setDividerLocation(frame.getHeight()/2); // set Divider location.
pane.setEnabled(true); // let user change divider location.
}
});
frame.setVisible(true);

Refreshing picture in drawingPanel extends JPanel

I have to load a small icon on the botton of my software. just to have a loading/ok/error icon. as sudgested on "http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter06/images.html" i create a dwowing panel extending JPanel.
class drawingPanel extends JPanel
{
Image img;
drawingPanel (Image img){
this.img = img;
}
public void paintComponent (Graphics g) {
super.paintComponent (g);
// Use the image width & height to find the starting point
int imgX = getSize ().width/2 - img.getWidth (this);
int imgY = getSize ().height/2 - img.getHeight (this);
//Draw image centered in the middle of the panel
g.drawImage (img, imgX, imgY, this);
} // paintComponent
}
I initialize the component in the following way:
// Grab the image.
Image img = new ImageIcon(iconPath+"ok.png").getImage();
// Create an instance of DrawingPanel
iconPanel = new drawingPanel(img);
all works well but at runtime i want to be able to change the icon within the pannel. i tryed all the fo;llowing but none managed to view the new picture:
Image img = new ImageIcon(iconPath+"loading.gif").getImage();
// Create a new instance of DrawingPanel
this.iconPanel = new drawingPanel(img);
this.iconPanel.repaint();
this.iconPanel.revalidate();
this.iconPanel.repaint();
this.repaint();
this.revalidate();
(i tryed this because the class in whic i am writing the code is another extension of JPanel that contains IconPanel. Any idea about why i do not manage to change the picture?
Thanks,
Stefano
First thing don't start class name with small name. Rename drawingPanel to DrawingPanel.
I have tried making a simple demo based on your description and it works fine. The image in panel is changing perfectly.
public class Demo {
public Demo() {
JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
// Grab the image.
Image img = new ImageIcon("1.png").getImage();
// Create an instance of DrawingPanel
final DrawingPanel iconPanel = new DrawingPanel(img);
frame.add(iconPanel, BorderLayout.CENTER);
JButton button = new JButton("Change image..");
frame.add(button, BorderLayout.NORTH);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
iconPanel.setImg(new ImageIcon("2.png").getImage());
iconPanel.repaint();
}
});
frame.setVisible(true);
}
public static void main(String[] args){
new Demo();
}
}
class DrawingPanel extends JPanel {
Image img;
DrawingPanel(Image img) {
this.img = img;
}
public void setImg(Image img) {
this.img = img;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Use the image width & height to find the starting point
int imgX = getSize().width / 2 - img.getWidth(this);
int imgY = getSize().height / 2 - img.getHeight(this);
// Draw image centered in the middle of the panel
g.drawImage(img, imgX, imgY, this);
} // paintComponent
}
The changes I have made is add a setter method of img in DrawingPanel class. So instead of creating new DrawingPanel you just have to call setImg() with new Image and then call reapint to paint the new image.

Why do the JButton in my dialog look weird

I'm fairly new to Java Swing/AWT et al and hence the question. I have a simple dialog with some text and a couple JButton. I am using the GridbagLayout for this dialog.
But when I look at the dialog, the JButtons are kind of having a weird shape and I cannot seem to right justify the text either.
Is this a limitation of the Layout I am using. Is there much simpler and elegant solution for my problem with either the buttons or the textlayout?
A screenshot of the dialog is attached below.
The code for my dialog Class is:
public class UpgradePopupWindow extends JPanel implements ActionListener {
static final long serialVersionUID = 0;
final String upgrade = " Continue Upgrade ";
final String restore = "Restore Previous Version";
JPanel panels;
JButton upgradeButton;
JButton restoreButton;
JTextArea Message;
JFrame newFrame;
FlasherThread flash;
protected JTextArea addText(String text, boolean visible, int fontStyle) {
JTextArea textArea = new JTextArea(text);
textArea.setFont(new Font("SansSerif", fontStyle, 12)); //$NON-NLS-1$
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
textArea.setBackground(Color.DARK_GRAY);
textArea.setForeground(Color.WHITE);
textArea.setOpaque(false);
textArea.setVisible(visible);
textArea.setAlignmentX(Component.CENTER_ALIGNMENT);
add(textArea);
return textArea;
}
protected JTextArea addMultiLineLabel(String text, int fontStyle, int fontSize, Object constraints) {
JTextArea textArea = new JTextArea(text);
textArea.setFont(new Font("SansSerif", fontStyle, fontSize));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
textArea.setBackground(new Color(0, 0, 0, 0)); // Zero alpha = transparent background
textArea.setOpaque(false);
textArea.setBorder(new TitledBorder(""));
textArea.setAlignmentX(Component.CENTER_ALIGNMENT);
add(textArea, constraints);
return textArea;
}
private UpgradePopupWindow(JFrame frame, Object ft) {
super(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
flash = (FlasherThread)ft;
String text = "An error occurred during the attempt to update your device's software. We recommend the following: (1) Restore your device to its previous version, (2) back up important data, and then (3) try updating your device again. If you continue with the current update, only your previously backed-up data will be available.";
//addFiller(5);
//addLabel(text, Font.PLAIN, 12);
gbc.gridy = 0;
gbc.gridx = 0;
gbc.gridwidth = 2;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
addMultiLineLabel(text, Font.PLAIN, 12, gbc);
//addText(text, true, Font.PLAIN);
addFiller(20);
newFrame = frame;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.NONE;
upgradeButton = new JButton(upgrade);
upgradeButton.setActionCommand("upgrade");
upgradeButton.addActionListener(this);
upgradeButton.setEnabled(true);
upgradeButton.setBackground(Color.WHITE);
upgradeButton.setForeground(Color.GRAY);
add(upgradeButton,gbc);
++ gbc.gridx;
restoreButton = new JButton(restore);
restoreButton.setActionCommand("restore");
restoreButton.addActionListener(this);
restoreButton.setEnabled(true);
//restoreButton.setForeground(Color.DARK_GRAY);
restoreButton.setBackground(Color.DARK_GRAY);
add(restoreButton,gbc);
setOpaque(true);
newFrame.setContentPane(this);
//newFrame.getContentPane().setBackground(Color.LIGHT_GRAY);
}
protected void addFiller(int size) {
Dimension diminsion = new Dimension(size, size);
Filler filler = new Filler(diminsion, diminsion, diminsion);
filler.setAlignmentX(Component.CENTER_ALIGNMENT);
add(filler);
}
public static void createGUI(Object obj) {
//Create and set up the frame.
JFrame frame = new JFrame("PopUp Dialog");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(440, 180));
//create and setup the content pane
UpgradePopupWindow popUpContentPane = new UpgradePopupWindow(frame, obj);
popUpContentPane.setOpaque(true);
frame.setContentPane(popUpContentPane);
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if("restore".equals(e.getActionCommand())) {
System.out.println("restore button selected");
flash.setUpgradeRestoreChoice("restore");
newFrame.dispose();
} else if ("upgrade".equals(e.getActionCommand())) {
System.out.println("upgrade button selected");
flash.setUpgradeRestoreChoice("upgrade");
newFrame.dispose();
}
}
}
![alt text][1]
As #Javaguru said - use MigLayout. For the text DO NOT use JTextArea. This component is mostly for text editing. JLabel is enough for your purpose - you can even use HTML inside of it.
You don't have to create this kind of dialog from scratch. There are many options. One of them is my Swing TaskDialog framework ;)
But if you still insist on implementing it yourself here a simple implementation using MigLayout ( even with MigLayout it can be done using several differnt ways):
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import net.miginfocom.swing.MigLayout;
public class TestDialog extends JDialog {
private static final long serialVersionUID = 1L;
private final JLabel label = new JLabel("Your text here");
private final JButton restoreButton = new JButton("Restore Previous Version");
private final JButton ugradeButton = new JButton("Continue Upgrade");
public TestDialog( String title ) {
super( (JDialog)null, title );
setContentPane(createContent());
}
private JPanel createContent() {
JPanel content = new JPanel();
content.setPreferredSize( new Dimension(400, 100));
content.setBackground(Color.WHITE);
content.setLayout(new MigLayout("", "[400px,grow][pref!]", "[grow][pref!]"));
this.label.setVerticalAlignment(SwingConstants.TOP);
content.add(this.label, "cell 0 0 2 1,grow");
content.add(this.restoreButton, "cell 1 1,alignx left,aligny top");
content.add(this.ugradeButton, "cell 0 1,alignx right,aligny top");
return content;
}
// very simplified test
public static void main(String[] args) {
TestDialog dlg = new TestDialog("Popup Dialog");
dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dlg.pack();
dlg.setLocationRelativeTo(null);
dlg.setVisible(true);
}
}
There are a lot of much more elegant solutions out there .. ;)
You should really never use GridbagLayout if not absolutely required (or only with a gui editor)! It's simply a pain in the ass!
Rather I suggest using one of the following LayoutManagers:
MigLayout: http://www.miglayout.com/
TableLayout: https://tablelayout.dev.java.net/
Creating a pleasing layout is much simpler and intuitive than with the GridbagLayout and its configuration overhead.