So I'm trying to make a simple dialog where the user can input some information... My problem is that I'm trying to make the whole background white; I got MOST of it, but there's a gray line behind the buttons that I don't know how to fix (make white as well). How can I fix it? :(
What it looks like:
What I want:
Code:
JPanel all = new JPanel();
all.setLayout(new BorderLayout());
all.add(names, BorderLayout.NORTH);
all.add(academic, BorderLayout.CENTER);
all.setBackground(Color.WHITE);
all.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); //int top, int left, int bottom, int right
Object [] options = {"SAVE", "EXIT"};
JOptionPane jop = new JOptionPane(all, JOptionPane.PLAIN_MESSAGE , JOptionPane.YES_NO_OPTION, null, options, null);
final JDialog dialog = jop.createDialog(null, "Username Information");
jop.setBackground(Color.WHITE);
dialog.setBackground(Color.WHITE);
dialog.setLocation(585, 300);
dialog.setVisible(true);
String choice = (String) jop.getValue();
Related
I am creating a messaging program that has multiple chats. On the side of the chat window there is a JPanel containing a List Array of all the buttons to switch chats.I am having trouble getting all the buttons in the side panel to be the same width, no matter what they contain. But whatever I try doesn't seem to work and I am looking for some help. Please can you explain what the code does and how it can be used so I can learn it for next time. Sorry if the comments aren't the best it's a work in progress as I wait till code works before adding detailed comments otherwise I am constantly changing them. :(
This is what it looks like without any chats:
This is with multiple chats and you can see the width variation
Here is the code that is run when a new user is added:
public void newUser() {
JPanel dialogue = new JPanel();
dialogue.setLayout(new BoxLayout(dialogue, BoxLayout.Y_AXIS));
//Creating/adding dialogue components
JLabel Enter_ip = new JLabel("Enter the ip address");
JTextField Get_ip = new JTextField("");
dialogue.add(Enter_ip);
dialogue.add(Get_ip);
dialogue.add(Box.createHorizontalStrut(15));
JLabel Enter_name = new JLabel("Enter the user's name");
JTextField Get_name = new JTextField("");
dialogue.add(Enter_name);
dialogue.add(Get_name);
//Creating the dialogue box
JOptionPane.showConfirmDialog(null, dialogue, "New User", JOptionPane.OK_CANCEL_OPTION);
//Getting data from dialogue box
String ip = Get_ip.getText();
String name = Get_name.getText();
//Try connecting to other user here
//Adding user message data
int size = Users_Messages_Data.size();
Users_Messages_Data.add(new ArrayList());//New user
Users_Messages_Data.get(size).add(new ArrayList());//Messages
Users_Messages_Data.get(size).add(new ArrayList());//Details
Users_Messages_Data.get(size).get(1).add(name);
Users_Messages_Data.get(size).get(1).add(ip);
Users_Messages_Data.get(size).get(1).add("port number");
//adds new UserButton
int temp = users.size();
users.add(new JButton(Users_Messages_Data.get(size).get(1).get(0)));
users.get(temp).addActionListener(this);
users.get(temp).setSize(new Dimension(500, 500));
SelectUser.add(users.get(temp), gbc);
Messaging.revalidate();
pack();
}
and here is the initialisation method:
public void MessagingGUI() {
//Creates JFrame and pane
Messaging = new JFrame();
Container pane = getContentPane();
JLabel info = new JLabel("29/07/2016 15:36");
//Creates user chats panel
SelectUser = new JPanel(new GridBagLayout());
gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
SelectUser.setLayout(new BoxLayout(SelectUser, BoxLayout.Y_AXIS));
SelectUser.setSize(new Dimension(500, 500));
//Adds different chats
users = new ArrayList<JButton>();
int x;
for (x = 0; x < Users_Messages_Data.size(); x++) {
users.add(new JButton(Users_Messages_Data.get(x).get(1).get(0)));
users.get(x).addActionListener(this);
SelectUser.add(users.get(x), gbc);
}
JButton newUser = new JButton("+");
newUser.addActionListener(this);
SelectUser.add(newUser);
JScrollPane UserScroll = new JScrollPane(SelectUser,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//Creates messages feed
JPanel Messages = new JPanel();
Messages.setLayout(new BoxLayout(Messages, BoxLayout.Y_AXIS));
AllMessages = new JTextArea(10, 30);
AllMessages.setBackground(Color.WHITE);
AllMessages.setEditable(false);
AllMessages.setBorder(BorderFactory.createLineBorder(Color.BLUE, 1));
JScrollPane MessageScroll = new JScrollPane(AllMessages,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//Creates user text entry box
UserText = new JTextArea(5, 30);
//UserText.setLineWrap(true);
//UserText.setWrapStyleWord(true);
UserText.setBorder(BorderFactory.createLineBorder(Color.CYAN, 1));
UserText.setText("Enter Message. Press enter to send");
UserText.setFocusable(true);
UserText.addKeyListener(this);
UserText.setPreferredSize(new Dimension(5, 20));
//Adds all components to pane
Messages.add(info);
Messages.add(MessageScroll);
Messages.add(UserText);
pane.add(UserScroll, BorderLayout.WEST);
pane.add(Messages, BorderLayout.CENTER);
//JFrame setup
Messaging.setTitle("Messaging");
Messaging.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Messaging.setContentPane(pane);
Messaging.setVisible(true);
Messaging.setSize(400, 350);
Load_User(current_user);
}
First of all, variable names should NOT start with an upper case character. Some of your variable names are correct, other are not. Be consistent and follow Java conventions!!!
.I am having trouble getting all the buttons in the side panel to be the same width,
Don't use a BoxLayout. It does not automatically resize on the opposite axis of the layout.
Instead you can use:
a GridLayout to make all the buttons the same size. The GridLayout will also fill the area vertically which is not what you want so you will need to nest panels. So create a parent panel using a BorderLayout. Add your panel using the GridLayout with the buttons to the BorderLayout.PAGE_START of this parent panel. Then add the BorderLayout panel to the scroll pane.
a GridBagLayout. You will need to use the "fill" contstraint to have the component fill the width of the cell.
Read the Swing tutorial on Layout Managers for more information and examples.
Or maybe a different approach is to use a JList to display the users.
I wrote a little code to see how the scroll Pane functions but my code never worked.
here's the code,
public Fenetre(){
this.setTitle("Data Simulator");
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
String hello = "hello";
int number = 69;
JPanel content = new JPanel();
content.setBackground(Color.LIGHT_GRAY);
//Box imad = Box.createHorizontalBox();
JTextArea textArea = new JTextArea(10, 10);
JLabel imad = new JLabel();
imad.setText(hello + " your favorite number is " + number + "\nRight?");
JScrollPane scrollPane = new JScrollPane();
setPreferredSize(new Dimension(450, 110));
scrollPane.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setEnabled(true);
scrollPane.setWheelScrollingEnabled(true);
scrollPane.setViewportView(textArea);
scrollPane.setViewportView(imad);
add(scrollPane, BorderLayout.CENTER);
//---------------------------------------------
//On ajoute le conteneur
scrollPane.add(textArea);
scrollPane.add(imad);
content.add(textArea);
content.add(imad);
content.add(scrollPane);
this.setContentPane(content);
this.setVisible(true);
this.setResizable(false);
}
When I run it, I get a little window with the textArea and next to the text area a very little white square, which is the scrollpane i suppose because when I remove it from the code, this square disappears. When I write in the text area and exceed the window's dimension, I can't scroll vertically using the mouse wheel, and not horizontally at all. I saw many examples on internet and I can't understand why my code doesn't work??
Any help explaining how scrollpane works?
scrollPane.setViewportView(textArea);
scrollPane.setViewportView(imad);
Only one component can be added to the viewport of the scroll pane, so the label replaces the text area.
content.add(textArea);
content.add(imad);
A component can only have a single parent. The above code removes the label from the scrollpane, so nothing is now in the scrollpane.
Try something like:
JScrollPane = new JScrollPane( textArea );
JPanel content = new JPanel( new BorderLayout() );
content.add(scrollPane, BorderLayout.CENTER);
content.add(imad, BorderLayout.PAGE_END);
setContentPane( content );
For a better solution, start with the working example found in the Swing tutorial on How to Use Text Areas and then modify the code. This way you will start with a better structured program that follows Swing standards.
I created an OptionDialog without any buttons and put a JPanel in it that uses MigLayout for its layout. That JPanel has another JPanel inside of it.
Both of these panels seem to have a margin on the outside of it. Maybe it is padding on the container. Either way I would like a way to get rid of them.
How can I get rid of these margins? In the picture they are the grey and the dark orange borders around the JPanels.
Here is the panel code:
setBackground(new Color(239,209,59));
setLayout(new MigLayout("wrap 1"));
JLabel title = new JLabel("Enroll Today!", JLabel.CENTER);
Font f = title.getFont().deriveFont((float)36);
title.setFont(f);
add(title);
JPanel docsPanel = new JPanel();
docsPanel.setBorder(BorderFactory.createEmptyBorder());
docsPanel.setLayout(new MigLayout("wrap 1", "", "[grow,fill]"));
docsPanel.setBackground(new Color(255,235,115));
for (final Document d : docs){
JButton doc = new JButton("* "+d.getName());
doc.setFont(f.deriveFont((float)24));
doc.setBorder(null);
doc.setContentAreaFilled(false);
docsPanel.add(doc);
}
add(docsPanel);
Here is the OptionDialog code:
DocumentPanel panel = new DocumentPanel(controller.getDocuments());
JOptionPane.showOptionDialog(null, panel, "Enroll now!", JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE, null, new Object[] {}, null);
Blindly, try "ins 0, wrap 1" in the MigLayout constructor.
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How can I put my Java program in the system tray?
I am making a notification system in java, I want the program to show up in the system tray, instead of on the task bar, I have tried:
notification.setExtendedState(JFrame.ICONIFIED);
Not only does this not work, but it lags the heck otta my computer
Current Code:
public static void notify(String line1, String line2, String imagepath, int style){
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int width = gd.getDisplayMode().getWidth();
int swidth = width - 320;
JFrame notification = new JFrame();
JPanel main = new JPanel(new FlowLayout(FlowLayout.RIGHT));
main.setLayout( new GridLayout( 2 , 1 ) );
JLabel notifyline1 = new JLabel();
notifyline1.setText(line1);
notifyline1.setFont(new Font("Minecraft",1 , 16));
notifyline1.setForeground(new Color(242, 238, 17));
main.add(notifyline1);
JLabel notifyline2 = new JLabel();
notifyline2.setText(line2);
notifyline1.setFont(new Font("Minecraft",1 , 12));
notifyline1.setForeground(Color.black);
main.add(notifyline1);
notification.add(main);
notification.setExtendedState(JFrame.ICONIFIED);
notification.setSize(new Dimension(320,64));
notification.setLocation(swidth, 0);
notification.setUndecorated(true);
notification.setVisible(true);
}
ALSO, to kill 2 birds with one stone
is there a way to color a jlabel, tried
label1.setForegroundColor(new Color(100, 100, 100));
Java has TrayIcon class which can be used to minimize application to SystemTray.You can see the working example here.
All I want to do is have a JOptionPane inputDialog with a JTextArea instead of a JTextField.
I tried putting the JTextArea inside of the Message parameter like so
Object[] inputText = new Object[]{new JLabel("Enter Graph Information"),
newJTextArea("",20,10)};
graphInfo=(String)JOptionPane.showInputDialog(null,
inputText,
"Create Graph",
JOptionPane.PLAIN_MESSAGE,
null,
null,
"");
But it still has the text field at the bottom and I cannot get the text from the JTextArea.
Is there any way to either remove the original text field and get the text from the jtextarea or replace the text field with the text area completely? I'm trying to avoid having to make a custom dialog if possible and this "seems" like something that should be easy to do?
You're on the right lines; you just need to use showConfirmDialog instead of showMessageDialog, which allows you to pass any Component as your "message" and have it displayed within the JDialog. You can then capture the contents of the JTextArea if the user clicks OK; e.g.
int okCxl = JOptionPane.showConfirmDialog(SwingUtilities.getWindowAncestor(this),
textArea,
"Enter Data",
JOptionPane.OK_CANCEL_OPTION)
if (okCxl == JOptionPane.OK_OPTION) {
String text = textArea.getText();
// Process text.
}
If you want to show a JLabel in conjunction with your JTextArea you can create and pass in a JPanel containing both Components; e.g.
JTextArea textArea = ...
JPanel pnl = new JPanel(new BorderLayout());
pnl.add(new JLabel("Please enter some data:"), BorderLayout.NORTH);
pnl.add(textArea, BorderLayout.CENTER);
JOptionPane.show...