Customizing javax.swing.JFileChooser to include an additional JTextField - java

I want to include an additional (optional) JTextField in the FileChooser, allowing the user to fill it in while choosing the file rather than giving them an additional prompt after they make their choice. Has anybody attempted something similar and found a working solution?
My target result would look something like this:

The documented way to add controls to a JFileChooser is via the setAccessory(JComponent) method.
JTextField field = new JTextField("Hello, World");
JPanel accessory = new JPanel();
accessory.setLayout(new FlowLayout());
accessory.add(field);
JFileChooser chooser = new JFileChooser();
chooser.setAccessory(accessory);
int ret = chooser.showOpenDialog(frame);
However, this will layout the new control on the right of the dialog (exact positioning is probably locale-dependent).
To locate the component to the position you want it, you'll probably have to walk the component graph and manipulate it. This would be a very fragile approach and you may be better off just building your own dialog.
This could incorporate a file chooser:
JFileChooser chooser = new JFileChooser();
chooser.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// TODO - wire into something
System.out.println(e);
}
});
JTextField field = new JTextField("Hello, World");
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(chooser, BorderLayout.CENTER);
panel.add(field, BorderLayout.SOUTH);

Related

Reusing a Button in Java Makes Nothing Display

I am really new to GUI programming in Java so please forgive me if this code is really basic. In short, I want to have 2 panels that are the same design. After I press the "A" button on the panel 1, I want to make panel 2 appear with the same design. Making the GUI efficient or pretty doesn't currently matter to me. I just want it to work. I have parts of the code listed below.
JButton buttonA = new JButton("a");
JButton buttonB = new JButton("b");
JButton buttonC = new JButton("c");
JButton buttonD = new JButton("d");
JPanel pan1 = new JPanel();
JPanel pan2 = new JPanel();
setTitle ("Test");
setSize (640, 640);
setResizable(false);
GridLayout grid1 = new GridLayout();
setLayout (grid1);
FlowLayout flow1 = new FlowLayout();
pan1.setLayout (flow1);
pan1.add(buttonA);
pan1.add(buttonB);
pan1.add(buttonC);
pan1.add(buttonD);
buttonA.addActionListener(this);
buttonB.addActionListener(this);
buttonC.addActionListener(this);
buttonD.addActionListener(this);
FlowLayout flow2 = new FlowLayout();
pan2.setLayout (flow2);
pan2.add(buttonA);
pan2.add(buttonB);
pan2.add(buttonC);
pan2.add(buttonD);
add(pan1);
add(pan2);
pan1.setVisible(true);
pan2.setVisible(false);
setVisible(true);
public void actionPerformed(ActionEvent event) {
if (command.equals("a")){//i want to show the panel 2 after button a is pressed
System.out.println("HelloA");
pan1.setVisible(false);
pan2.setVisible(true);
}
Currently, it just shows nothing in the window. Any help guys?
Short answer is, you can't.
Long answer is, a component can only reside on a single parent. Adding a component to a second container will automatically remove it from the first container before its added to the new one.
Instead, you will need to create individual buttons for both containers.
Also, understand that BorderLayout can't support what you're trying to do, it will only manage one component at a time (in each of the 5 available positions)
A better solution would be to make use of the CardLayout which is designed to facilitate the action you are trying to achieve

Issue with JScrollPane + JTree display after having it initialized

I have a simple swing App, that can manage a specific type of project with multiple JButton and that print the project tree on the bottom, see the screenshoot bellow when a project is opened in the App :
App screenshoot with project opened
The thing is when no project is opened I get something like this :
App screenshoot without project opened
The HMI is simple and looks like this :
public class Desktop extends JFrame implements ActionListener {
public Desktop() {
JButton newProject, generate, quit, bAddToClassPath, openProject, saveProject;
JPanel mainPanel;
JScrollPane jscrollpane;
super("MainWindow");
setLookAndFeel();
setSize(330, 440);
ParamMainPanel();
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Desktop();
}
});
}
public static void ParamMainPanel() {
mainPanel = new JPanel(new FlowLayout());
// BUTTONS PARAM
newProject = new JButton("Nouveau projet");
generate = new JButton("Générer...");
quit = new JButton("Quitter");
openProject = new JButton ("Ouvrir projet");
saveProject = new JButton ("Sauvegarder");
bAddToClassPath = UIUtil.iconButton();
bAddToClassPath.setActionCommand("setCP");
bAddToClassPath.addActionListener(this);
mainPanel.add(bAddToClassPath);
newProject.addActionListener(this);
newProject.setActionCommand("newP");
generate.addActionListener(this);
generate.setActionCommand("gen");
quit.addActionListener(this);
quit.setActionCommand("qui");
openProject.addActionListener(this);
openProject.setActionCommand("openP");
saveProject.addActionListener(this);
saveProject.setActionCommand("save");
mainPanel.add(newProject);
mainPanel.add(generate);
mainPanel.add(openProject);
mainPanel.add(saveProject);
mainPanel.add(quit);
// PROJECT TREE
jscrollpane = new JScrollPane(new JTree());
jscrollpane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jscrollpane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
jscrollpane.setMinimumSize(new Dimension(50, 50));
jscrollpane.setLocation(4,61);
jscrollpane.setSize(306,322);
mainPanel.add(jscrollpane);
}
}
So what I want is at the App launch, instead of having the bad looking display JTree (into the jscrollpane) without project opened, having the same display with a project opened (white bloc) but without the project tree inside.
I can't figure how to do it, any ideas ?
Here the answer I found to resolve this display problem :
The FlowLayout used on my main panel somehow was preventing me from resizing my jscrollpane directly using setSize()
So I decided to have a secondary panel on my MainFrame secondMainPanel without specific layout using new JPanel(null);
I did add jscrollpaneon it, then I could resize it without problems to have a correct display.
I think there might be better way to fix it, but this one works.

Text not displayed when mouse is rolled over JLabel

With NetBeans (Java), I am having problems in JLabel. I have assigned an image as the icon of that JLabel.
Problem - 1st:
I want to display some text (e.g - logout) below that icon (image). How to do this?
Problem - 2nd:
I want to display some text when mouse is rolled over that JLabel. What should I do?
So , please guys tell me how to these things by writing code.
I recommend reading the basic Oracle tutorials which describe in detail how to accomplish this. You can use a MouseMotionListener to determine when the mouse is rolled over the JLabel, and you can position the JLabel text underneath the Icon of the JLabel by setting its vertical text position as described in the JLabel Tutorial. This should have all been found with a simple internet search of your questions, something that your question suggests was not done (and should have been) before asking
1.
Create a JPanel that contains two JLabels. This way you can control the layout of the internal components.
I used BoxLayout with the parameter BoxLayout.Y_AXIS to get the label below the icon.
2.
Add a MouseListener using the method component.addMouseListener(new MouseAdapter() { ... });, you'll need to create a MouseAdapter and implement any methods you need (click here).
Here is a working example for you buddy... Adapt this however you need to.
Note: You'll need to change the file-path of the ImageIcon()
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel container = new JPanel();
JPanel iconLabelPanel = new JPanel();
String TEXT_FIELD_TEXT = "Hover over the logout label.";
JLabel icon = new JLabel(new ImageIcon("C:\\Users\\Gary\\Google Drive\\Pictures\\puush\\ss (2015-02-19 at 06.00.00).png"));
JLabel label = new JLabel("Logout!");
JTextField textField = new JTextField(TEXT_FIELD_TEXT);
//Add a mouse motion listener for the JLabel
label.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
//Set text of another component
textField.setText("You're over Logout!");
}
#Override
public void mouseExited(MouseEvent e) {
//Set text of another component
textField.setText(TEXT_FIELD_TEXT);
}
});
//Add components and set parameters for iconLabelPanel
iconLabelPanel.setLayout(new BoxLayout(iconLabelPanel, BoxLayout.PAGE_AXIS));
iconLabelPanel.add(icon);
iconLabelPanel.add(label);
//Add components and set parameters for container
container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));
container.add(iconLabelPanel);
container.add(textField);
//Set parameters for frame
frame.add(container);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setSize(400, 400);
frame.setVisible(true);
}

Changing only one Panel in JFrame

I have one JFrame with two Buttons in East and West,Label in North,Label and TextField in South. and one Image in the Center. I want to call another Image after 30 seconds in the center. But every time I call the 2nd image the components in North,South,East and West are disappearing.
This is my code.
//The Components in JFrame.
firstPicblur = new JPanel(new BorderLayout());
pictureblur01 = new ImageIcon("C:\\java pics\\papsi2.jpg");
pictureblurA = new JLabel(pictureblur01);
firstPicblur.add(pictureblurA,BorderLayout.CENTER);
//FirstPicture blurred B
firstPicblurB = new JPanel(new BorderLayout());
pictureblur02 = new ImageIcon("C:\\java pics\\papsi1.jpg");
pictureblurB = new JLabel(pictureblur02);
firstPicblurB.add(pictureblurB,BorderLayout.CENTER);
//Next0Prev buttons
Next0Prev = new JPanel(new BorderLayout());
Next = new JButton("NEXT");
Prev = new JButton("PREV");
//Next0Prev labels is constant at SOUTH
firstPicblurA = new JPanel(new BorderLayout());
clueNo1 = new JLabel("Picture Number 01");
TypeHere = new JTextField("Guess Who");
firstPicblurA.add(TypeHere, BorderLayout.CENTER);
firstPicblurA.add(clueNo1, BorderLayout.SOUTH);
Next0Prev.add(firstPicblur,BorderLayout.CENTER);
Next0Prev.add(Next,BorderLayout.EAST);
Next0Prev.add(Prev,BorderLayout.WEST);
Next0Prev.add(firstPicblurA,BorderLayout.SOUTH);
//other components
and this is the actionEvent
if(e.getSource() == continueButton){
add(Next0Prev);
Next0Prev.setVisible(true);
Next0Prev.repaint();
Next0Prev.revalidate();
loadingEffectBtn.setVisible(false);
Timer ta = new Timer(10000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
add(firstPicblurB);
firstPicblurB.setVisible(true);
Next0Prev.setVisible(true);
}
});
ta.start();
ta.setRepeats(true);
}
but when i do this. The 2nd picture is appearing in center but the N,E,W,S components are disappearing.
This is a bit hard to tell, but what appears to be happening is...
You're creating Next0Prev and adding all your components to it...
Next0Prev = new JPanel(new BorderLayout());
//....
Next0Prev.add(firstPicblur,BorderLayout.CENTER);
Next0Prev.add(Next,BorderLayout.EAST);
Next0Prev.add(Prev,BorderLayout.WEST);
Next0Prev.add(firstPicblurA,BorderLayout.SOUTH);
Which I assume you are adding to the frame...
Then in your Timer's actionPerformed, you're adding firstPicblurB to the main container...
public void actionPerformed(ActionEvent e) {
add(firstPicblurB);
firstPicblurB.setVisible(true);
Next0Prev.setVisible(true);
}
Now, assuming that the main container is either a JFrame or is using a BorderLayout, this will effectively hide Next0Prev as BorderLayout can only have a single component in any of it's 5 positions.
Instead, you should be adding firstPicblurB to Next0Prev...
A simpler solution would be (as has already being pointed out) to use a single JLabel and simple change it's icon
I would also highly recommend that you take the time to read through and apply Code Conventions for the Java Programming Language

Trying to build a GUI that would enable users to add more TextFields to it

I need to let users add more text fields to my JFrame so once the size of the containing frame being JPanel has exceeded its original value a scroll pane would step in.
In order to be able to do this, I came up with an idea to put one JButton and upon hitting it a new TextField would show up (this was my original idea which doesn't necessarily mean I am right). The problem is, once I call the ActionListener class to add more TextFields and eventually stretch its containing panel, the program asks me to make the JPanel final which in turns doesn't allow for stretching of the panel. In other words, it appears to me that I'm just beating around the bush, please help me out put this together, below is a piece of my code:
public class Button {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setLayout(new BorderLayout());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel p = new JPanel(new GridLayout(0, 5));
JScrollPane jsp = new JScrollPane(p);
jsp.setPreferredSize(new Dimension(300,300));
jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
JButton but = new JButton("Add");
f.add(but);
but.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
int height=0;
JTextField jtx = new JTextField();
jtx.setSize(new Dimension(70,20));
jtx.setPreferredSize(new Dimension(70,20));
p.add(jtf);
height+=20;
p.setSize(new Dimension(300, height));
p.setPreferredSize(new Dimension(300, height));
}
});
f.add(jsp, BorderLayout.CENTER);
f.setLocation(300, 300);
f.setVisible(true);
f.pack();
}
}
I will give you an idea : you can add button and inside the button instruction you would add an instruction set that create text field . if you want more than once then you must handle position pointer that tell you the last place the user add text field then by updating the pointer the user can see the text in different place , if you want the user to control the position of the text then he must enter the location .

Categories